blob: bbd970d397a8d96f5cc4c8a120c2e4ff982f9587 [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) {
Alexandre Lision8ad39592013-11-21 13:23:58 -0500162 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down).remove(toDisplay).commit();
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500163 ((SecurityAccountFragment) mPreferencesPagerAdapter.getItem(3)).setCredentialSummary();
Alexandre Lision3cefec22013-11-14 17:26:35 -0500164 toDisplay = null;
165 return;
166 }
167
Alexandre Lision059da9d2013-10-22 11:17:25 -0400168 if (acc_selected.isIP2IP()) {
169 super.onBackPressed();
170 return;
171 }
172
Alexandre Lision3cefec22013-11-14 17:26:35 -0500173 super.onBackPressed();
174
Alexandre Lision227b59c2013-10-03 17:37:57 -0400175 }
176
177 @Override
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400178 protected void onDestroy() {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500179 super.onDestroy();
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400180 if (mBound) {
181 unbindService(mConnection);
182 mBound = false;
183 }
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400184 }
185
186 @Override
Alexandre Lision227b59c2013-10-03 17:37:57 -0400187 public boolean onOptionsItemSelected(MenuItem item) {
188
189 switch (item.getItemId()) {
190 case android.R.id.home:
Alexandre Lision3cefec22013-11-14 17:26:35 -0500191 if (toDisplay != null) {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500192 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down).remove(toDisplay)
193 .commit();
194 ((SecurityAccountFragment) mPreferencesPagerAdapter.getItem(3)).setCredentialSummary();
Alexandre Lision3cefec22013-11-14 17:26:35 -0500195 toDisplay = null;
196 } else
197 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400198 return true;
199 case R.id.menuitem_delete:
200 AlertDialog dialog = createDeleteDialog();
201 dialog.show();
202 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400203 }
204
205 return true;
206 }
207
Alexandre Lision059da9d2013-10-22 11:17:25 -0400208 private void processAccount() {
Alexandre Lision3cefec22013-11-14 17:26:35 -0500209 try {
210 service.setCredentials(acc_selected.getAccountID(), acc_selected.getCredentialsHashMapList());
211 service.setAccountDetails(acc_selected.getAccountID(), acc_selected.getDetails());
212 } catch (RemoteException e) {
213 e.printStackTrace();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400214 }
215
216 }
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500217
Alexandre Lision059da9d2013-10-22 11:17:25 -0400218 public boolean validateAccountCreation(ArrayList<String> missingValue) {
219 boolean valid = true;
220 ArrayList<String> requiredFields = new ArrayList<String>();
221 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
222 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
223 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
224 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
225 for (String s : requiredFields) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500226
Alexandre Lision059da9d2013-10-22 11:17:25 -0400227 if (acc_selected.getBasicDetails().getDetailString(s).isEmpty()) {
228 valid = false;
229 missingValue.add(s);
230 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400231 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400232
233 return valid;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400234 }
235
Alexandre Lision227b59c2013-10-03 17:37:57 -0400236 private AlertDialog createDeleteDialog() {
237 Activity ownerActivity = this;
238 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
239 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
240 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
241 @Override
242 public void onClick(DialogInterface dialog, int whichButton) {
243 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400244 bundle.putString("AccountID", acc_selected.getAccountID());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400245
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400246 try {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400247 service.removeAccount(acc_selected.getAccountID());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400248 } catch (RemoteException e) {
249 e.printStackTrace();
250 }
251 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400252 }
253 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
254 @Override
255 public void onClick(DialogInterface dialog, int whichButton) {
256 /* Terminate with no action */
257 }
258 });
259
260 AlertDialog alertDialog = builder.create();
261 alertDialog.setOwnerActivity(ownerActivity);
262
263 return alertDialog;
264 }
265
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400266 public class PreferencesPagerAdapter extends FragmentStatePagerAdapter {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400267
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400268 Context mContext;
269 ArrayList<Fragment> fragments;
270
Alexandre Lision059da9d2013-10-22 11:17:25 -0400271 public PreferencesPagerAdapter(Context c, FragmentManager fm, ArrayList<Fragment> items) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400272 super(fm);
273 mContext = c;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400274 fragments = new ArrayList<Fragment>(items);
275
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400276 }
277
Alexandre Lision227b59c2013-10-03 17:37:57 -0400278 @Override
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400279 public int getCount() {
280 return fragments.size();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400281 }
282
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400283 @Override
284 public Fragment getItem(int position) {
285 return fragments.get(position);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400286 }
287
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400288 @Override
289 public CharSequence getPageTitle(int position) {
290 switch (position) {
291 case 0:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400292 if (acc_selected.isIP2IP()) {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500293 return getString(R.string.account_preferences_audio_tab).toUpperCase(Locale.getDefault());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400294 } else {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500295 return getString(R.string.account_preferences_basic_tab).toUpperCase(Locale.getDefault());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400296 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400297 case 1:
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500298 return getString(R.string.account_preferences_audio_tab).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500299 case 2:
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500300 return getString(R.string.account_preferences_advanced_tab).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500301 case 3:
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500302 return getString(R.string.account_preferences_security_tab).toUpperCase(Locale.getDefault());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400303 default:
304 Log.e(TAG, "getPreferencePageTitle: unknown tab position " + position);
305 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400306 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400307 return null;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400308 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400309 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400310
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400311 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400312 public ISipService getService() {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400313 return service;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400314 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400315
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400316 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400317 public Account getAccount() {
318 return acc_selected;
319 }
320
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500321 @Override
322 public void displayCredentialsScreen() {
Alexandre Lision3cefec22013-11-14 17:26:35 -0500323 toDisplay = new NestedSettingsFragment();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500324 Bundle b = new Bundle();
325 b.putInt("MODE", 0);
326 toDisplay.setArguments(b);
Alexandre Lision8ad39592013-11-21 13:23:58 -0500327 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down)
328 .replace(R.id.hidden_container, toDisplay).commit();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500329 }
330
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500331 @Override
332 public void displaySRTPScreen() {
333 toDisplay = new NestedSettingsFragment();
334 Bundle b = new Bundle();
335 b.putInt("MODE", 1);
336 toDisplay.setArguments(b);
Alexandre Lision8ad39592013-11-21 13:23:58 -0500337 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down)
338 .replace(R.id.hidden_container, toDisplay).commit();
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500339 }
340
341 @Override
342 public void displayTLSScreen() {
343 toDisplay = new NestedSettingsFragment();
344 Bundle b = new Bundle();
345 b.putInt("MODE", 2);
346 toDisplay.setArguments(b);
Alexandre Lision8ad39592013-11-21 13:23:58 -0500347 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down)
348 .replace(R.id.hidden_container, toDisplay).commit();
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500349 }
350
Alexandre Lision227b59c2013-10-03 17:37:57 -0400351}