blob: ce42e26bc51e0be9243b1e2ebef5fc9187902f7f [file] [log] [blame]
Alexandre Lision227b59c2013-10-03 17:37:57 -04001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 * Alexandre Savard <alexandre.savard@savoirfairelinux.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
32
33package org.sflphone.client;
34
Alexandre Lision227b59c2013-10-03 17:37:57 -040035import java.util.ArrayList;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040036import java.util.Locale;
Alexandre Lision3cefec22013-11-14 17:26:35 -050037import java.util.Observable;
38import java.util.Observer;
Alexandre Lision227b59c2013-10-03 17:37:57 -040039
40import org.sflphone.R;
Alexandre Lision227b59c2013-10-03 17:37:57 -040041import org.sflphone.account.AccountDetailBasic;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050042import org.sflphone.fragments.AdvancedAccountFragment;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040043import org.sflphone.fragments.AudioManagementFragment;
Alexandre Lision8907c1c2013-11-12 11:40:14 -050044import org.sflphone.fragments.GeneralAccountFragment;
Alexandre Lisiona3650992013-11-13 14:19:35 -050045import org.sflphone.fragments.NestedSettingsFragment;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050046import org.sflphone.fragments.SecurityAccountFragment;
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040047import org.sflphone.model.Account;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040048import org.sflphone.service.ISipService;
49import org.sflphone.service.SipService;
Alexandre Lision8907c1c2013-11-12 11:40:14 -050050import org.sflphone.views.PagerSlidingTabStrip;
Alexandre Lision227b59c2013-10-03 17:37:57 -040051
52import android.app.Activity;
53import android.app.AlertDialog;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040054import android.app.Fragment;
55import android.app.FragmentManager;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040056import android.content.ComponentName;
57import android.content.Context;
Alexandre Lision227b59c2013-10-03 17:37:57 -040058import android.content.DialogInterface;
59import android.content.Intent;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040060import android.content.ServiceConnection;
Alexandre Lision227b59c2013-10-03 17:37:57 -040061import android.os.Bundle;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040062import android.os.IBinder;
63import android.os.RemoteException;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040064import android.support.v13.app.FragmentStatePagerAdapter;
65import android.support.v4.view.ViewPager;
Alexandre Lision227b59c2013-10-03 17:37:57 -040066import android.util.Log;
67import android.view.Menu;
68import android.view.MenuInflater;
69import android.view.MenuItem;
70
Alexandre Lision3f1aa682013-11-18 10:49:28 -050071public class AccountEditionActivity extends Activity implements GeneralAccountFragment.Callbacks, AudioManagementFragment.Callbacks,
Alexandre Lisiona3650992013-11-13 14:19:35 -050072 AdvancedAccountFragment.Callbacks, SecurityAccountFragment.Callbacks, NestedSettingsFragment.Callbacks {
Alexandre Lision3cefec22013-11-14 17:26:35 -050073 private static final String TAG = AccountEditionActivity.class.getSimpleName();
Alexandre Lision227b59c2013-10-03 17:37:57 -040074
75 public static final String KEY_MODE = "mode";
Alexandre Lisiond16bad92013-10-09 17:16:20 -040076 private boolean mBound = false;
77 private ISipService service;
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040078 private Account acc_selected;
Alexandre Lision059da9d2013-10-22 11:17:25 -040079
Alexandre Lision3f1aa682013-11-18 10:49:28 -050080 private ViewPager mViewPager;
81
82 private NestedSettingsFragment toDisplay;
83
84 private Observer mAccountObserver = new Observer() {
85
86 @Override
87 public void update(Observable observable, Object data) {
88 processAccount();
89 }
90 };
91
Alexandre Lisionafd40e42013-10-15 13:48:37 -040092 PreferencesPagerAdapter mPreferencesPagerAdapter;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040093 private ServiceConnection mConnection = new ServiceConnection() {
94
95 @Override
96 public void onServiceConnected(ComponentName className, IBinder binder) {
97 service = ISipService.Stub.asInterface(binder);
98 mBound = true;
Alexandre Lision059da9d2013-10-22 11:17:25 -040099
100 ArrayList<Fragment> fragments = new ArrayList<Fragment>();
101 if (acc_selected.isIP2IP()) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400102 fragments.add(new AudioManagementFragment());
103 } else {
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500104 fragments.add(new GeneralAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400105 fragments.add(new AudioManagementFragment());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500106 fragments.add(new AdvancedAccountFragment());
107 fragments.add(new SecurityAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400108 }
109
Alexandre Lision3f1aa682013-11-18 10:49:28 -0500110 mViewPager = (ViewPager) findViewById(R.id.pager);
111
Alexandre Lision059da9d2013-10-22 11:17:25 -0400112 mPreferencesPagerAdapter = new PreferencesPagerAdapter(AccountEditionActivity.this, getFragmentManager(), fragments);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400113 mViewPager.setAdapter(mPreferencesPagerAdapter);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500114 mViewPager.setOffscreenPageLimit(3);
115
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500116 final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(findViewById(R.id.pager_sliding_strip));
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400117
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500118 strip.setViewPager(mViewPager);
Alexandre Lision3cefec22013-11-14 17:26:35 -0500119
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400120 }
121
122 @Override
123 public void onServiceDisconnected(ComponentName arg0) {
124
125 }
126 };
Alexandre Lision227b59c2013-10-03 17:37:57 -0400127
Alexandre Lision227b59c2013-10-03 17:37:57 -0400128 @Override
129 protected void onCreate(Bundle savedInstanceState) {
130 super.onCreate(savedInstanceState);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400131
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500132 setContentView(R.layout.activity_account_settings);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400133
134 getActionBar().setDisplayHomeAsUpEnabled(true);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400135
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400136 acc_selected = getIntent().getExtras().getParcelable("account");
Alexandre Lision059da9d2013-10-22 11:17:25 -0400137
Alexandre Lision3cefec22013-11-14 17:26:35 -0500138 acc_selected.addObserver(mAccountObserver);
139
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400140 if (!mBound) {
141 Log.i(TAG, "onCreate: Binding service...");
142 Intent intent = new Intent(this, SipService.class);
143 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
144 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400145
146 }
147
148 @Override
149 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400150 if (acc_selected.isIP2IP()) {
151 return true;
152 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400153 MenuInflater inflater = getMenuInflater();
154 inflater.inflate(R.menu.account_edition, menu);
155 return true;
156 }
157
158 @Override
159 public void onBackPressed() {
160
Alexandre Lision3cefec22013-11-14 17:26:35 -0500161 if (toDisplay != null) {
162 getFragmentManager().beginTransaction().remove(toDisplay).commit();
163 toDisplay = null;
164 return;
165 }
166
Alexandre Lision059da9d2013-10-22 11:17:25 -0400167 if (acc_selected.isIP2IP()) {
168 super.onBackPressed();
169 return;
170 }
171
Alexandre Lision3cefec22013-11-14 17:26:35 -0500172 super.onBackPressed();
173
Alexandre Lision227b59c2013-10-03 17:37:57 -0400174 }
175
176 @Override
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400177 protected void onDestroy() {
178
179 if (mBound) {
180 unbindService(mConnection);
181 mBound = false;
182 }
183
184 // stopService(new Intent(this, SipService.class));
185 // serviceIsOn = false;
186 super.onDestroy();
187 }
188
189 @Override
Alexandre Lision227b59c2013-10-03 17:37:57 -0400190 public boolean onOptionsItemSelected(MenuItem item) {
191
192 switch (item.getItemId()) {
193 case android.R.id.home:
Alexandre Lision3cefec22013-11-14 17:26:35 -0500194 if (toDisplay != null) {
195 getFragmentManager().beginTransaction().remove(toDisplay).commit();
196 toDisplay = null;
197 } else
198 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400199 return true;
200 case R.id.menuitem_delete:
201 AlertDialog dialog = createDeleteDialog();
202 dialog.show();
203 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400204 }
205
206 return true;
207 }
208
Alexandre Lision059da9d2013-10-22 11:17:25 -0400209 private void processAccount() {
Alexandre Lision3cefec22013-11-14 17:26:35 -0500210 try {
211 service.setCredentials(acc_selected.getAccountID(), acc_selected.getCredentialsHashMapList());
212 service.setAccountDetails(acc_selected.getAccountID(), acc_selected.getDetails());
213 } catch (RemoteException e) {
214 e.printStackTrace();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400215 }
216
217 }
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500218
Alexandre Lision059da9d2013-10-22 11:17:25 -0400219 public boolean validateAccountCreation(ArrayList<String> missingValue) {
220 boolean valid = true;
221 ArrayList<String> requiredFields = new ArrayList<String>();
222 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
223 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
224 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
225 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
226 for (String s : requiredFields) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500227
Alexandre Lision059da9d2013-10-22 11:17:25 -0400228 if (acc_selected.getBasicDetails().getDetailString(s).isEmpty()) {
229 valid = false;
230 missingValue.add(s);
231 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400232 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400233
234 return valid;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400235 }
236
Alexandre Lision227b59c2013-10-03 17:37:57 -0400237 private AlertDialog createDeleteDialog() {
238 Activity ownerActivity = this;
239 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
240 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
241 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
242 @Override
243 public void onClick(DialogInterface dialog, int whichButton) {
244 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400245 bundle.putString("AccountID", acc_selected.getAccountID());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400246
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400247 try {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400248 service.removeAccount(acc_selected.getAccountID());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400249 } catch (RemoteException e) {
250 e.printStackTrace();
251 }
252 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400253 }
254 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
255 @Override
256 public void onClick(DialogInterface dialog, int whichButton) {
257 /* Terminate with no action */
258 }
259 });
260
261 AlertDialog alertDialog = builder.create();
262 alertDialog.setOwnerActivity(ownerActivity);
263
264 return alertDialog;
265 }
266
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400267 public class PreferencesPagerAdapter extends FragmentStatePagerAdapter {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400268
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400269 Context mContext;
270 ArrayList<Fragment> fragments;
271
Alexandre Lision059da9d2013-10-22 11:17:25 -0400272 public PreferencesPagerAdapter(Context c, FragmentManager fm, ArrayList<Fragment> items) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400273 super(fm);
274 mContext = c;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400275 fragments = new ArrayList<Fragment>(items);
276
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400277 }
278
Alexandre Lision227b59c2013-10-03 17:37:57 -0400279 @Override
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400280 public int getCount() {
281 return fragments.size();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400282 }
283
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400284 @Override
285 public Fragment getItem(int position) {
286 return fragments.get(position);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400287 }
288
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400289 @Override
290 public CharSequence getPageTitle(int position) {
291 switch (position) {
292 case 0:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400293 if (acc_selected.isIP2IP()) {
294 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
295 } else {
296 return getString(R.string.account_preferences_basic).toUpperCase(Locale.getDefault());
297 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400298 case 1:
299 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500300 case 2:
301 return getString(R.string.account_preferences_advanced).toUpperCase(Locale.getDefault());
302 case 3:
303 return getString(R.string.account_preferences_security).toUpperCase(Locale.getDefault());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400304 default:
305 Log.e(TAG, "getPreferencePageTitle: unknown tab position " + position);
306 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400307 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400308 return null;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400309 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400310 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400311
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400312 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400313 public ISipService getService() {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400314 return service;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400315 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400316
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400317 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400318 public String getAccountID() {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400319 return acc_selected.getAccountID();
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400320 }
321
Alexandre Lision059da9d2013-10-22 11:17:25 -0400322 @Override
323 public Account getAccount() {
324 return acc_selected;
325 }
326
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500327 @Override
328 public void displayCredentialsScreen() {
Alexandre Lision3cefec22013-11-14 17:26:35 -0500329 toDisplay = new NestedSettingsFragment();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500330 Bundle b = new Bundle();
331 b.putInt("MODE", 0);
332 toDisplay.setArguments(b);
333 getFragmentManager().beginTransaction().replace(R.id.hidden_container, toDisplay).commit();
334 }
335
Alexandre Lision227b59c2013-10-03 17:37:57 -0400336}