blob: 037371da48deadc5b6f12d0e3ec815c87e4e3eac [file] [log] [blame]
Alexandre Lision227b59c2013-10-03 17:37:57 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lision227b59c2013-10-03 17:37:57 -04003 *
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
Alexandre Lisiond16bad92013-10-09 17:16:20 -040075 private boolean mBound = false;
76 private ISipService service;
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040077 private Account acc_selected;
Alexandre Lision059da9d2013-10-22 11:17:25 -040078
Alexandre Lision3f1aa682013-11-18 10:49:28 -050079 private ViewPager mViewPager;
80
81 private NestedSettingsFragment toDisplay;
82
83 private Observer mAccountObserver = new Observer() {
84
85 @Override
86 public void update(Observable observable, Object data) {
87 processAccount();
88 }
89 };
90
Alexandre Lisionafd40e42013-10-15 13:48:37 -040091 PreferencesPagerAdapter mPreferencesPagerAdapter;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040092 private ServiceConnection mConnection = new ServiceConnection() {
93
94 @Override
95 public void onServiceConnected(ComponentName className, IBinder binder) {
96 service = ISipService.Stub.asInterface(binder);
97 mBound = true;
Alexandre Lision059da9d2013-10-22 11:17:25 -040098
99 ArrayList<Fragment> fragments = new ArrayList<Fragment>();
100 if (acc_selected.isIP2IP()) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400101 fragments.add(new AudioManagementFragment());
102 } else {
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500103 fragments.add(new GeneralAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400104 fragments.add(new AudioManagementFragment());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500105 fragments.add(new AdvancedAccountFragment());
106 fragments.add(new SecurityAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400107 }
108
Alexandre Lision3f1aa682013-11-18 10:49:28 -0500109 mViewPager = (ViewPager) findViewById(R.id.pager);
110
Alexandre Lision059da9d2013-10-22 11:17:25 -0400111 mPreferencesPagerAdapter = new PreferencesPagerAdapter(AccountEditionActivity.this, getFragmentManager(), fragments);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400112 mViewPager.setAdapter(mPreferencesPagerAdapter);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500113 mViewPager.setOffscreenPageLimit(3);
114
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500115 final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(findViewById(R.id.pager_sliding_strip));
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400116
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500117 strip.setViewPager(mViewPager);
Alexandre Lision3cefec22013-11-14 17:26:35 -0500118
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400119 }
120
121 @Override
122 public void onServiceDisconnected(ComponentName arg0) {
123
124 }
125 };
Alexandre Lision227b59c2013-10-03 17:37:57 -0400126
Alexandre Lision227b59c2013-10-03 17:37:57 -0400127 @Override
128 protected void onCreate(Bundle savedInstanceState) {
129 super.onCreate(savedInstanceState);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400130
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500131 setContentView(R.layout.activity_account_settings);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400132
133 getActionBar().setDisplayHomeAsUpEnabled(true);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400134
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400135 acc_selected = getIntent().getExtras().getParcelable("account");
Alexandre Lision059da9d2013-10-22 11:17:25 -0400136
Alexandre Lision3cefec22013-11-14 17:26:35 -0500137 acc_selected.addObserver(mAccountObserver);
138
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400139 if (!mBound) {
140 Log.i(TAG, "onCreate: Binding service...");
141 Intent intent = new Intent(this, SipService.class);
142 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
143 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400144
145 }
146
147 @Override
148 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400149 if (acc_selected.isIP2IP()) {
150 return true;
151 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400152 MenuInflater inflater = getMenuInflater();
153 inflater.inflate(R.menu.account_edition, menu);
154 return true;
155 }
156
157 @Override
158 public void onBackPressed() {
159
Alexandre Lision3cefec22013-11-14 17:26:35 -0500160 if (toDisplay != null) {
Alexandre Lision8ad39592013-11-21 13:23:58 -0500161 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down).remove(toDisplay).commit();
Alexandre Lision06412a32014-02-05 17:33:04 -0500162 ((SecurityAccountFragment) mPreferencesPagerAdapter.getItem(3)).updateSummaries();
Alexandre Lision3cefec22013-11-14 17:26:35 -0500163 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() {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500178 super.onDestroy();
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400179 if (mBound) {
180 unbindService(mConnection);
181 mBound = false;
182 }
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400183 }
184
185 @Override
Alexandre Lision227b59c2013-10-03 17:37:57 -0400186 public boolean onOptionsItemSelected(MenuItem item) {
187
188 switch (item.getItemId()) {
189 case android.R.id.home:
Alexandre Lision3cefec22013-11-14 17:26:35 -0500190 if (toDisplay != null) {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500191 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down).remove(toDisplay)
192 .commit();
Alexandre Lision06412a32014-02-05 17:33:04 -0500193 ((SecurityAccountFragment) mPreferencesPagerAdapter.getItem(3)).updateSummaries();
Alexandre Lision3cefec22013-11-14 17:26:35 -0500194 toDisplay = null;
195 } else
196 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400197 return true;
198 case R.id.menuitem_delete:
199 AlertDialog dialog = createDeleteDialog();
200 dialog.show();
201 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400202 }
203
204 return true;
205 }
206
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500207 @Override
208 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
209 super.onActivityResult(requestCode, resultCode, data);
210 }
211
Alexandre Lision059da9d2013-10-22 11:17:25 -0400212 private void processAccount() {
Alexandre Lision3cefec22013-11-14 17:26:35 -0500213 try {
214 service.setCredentials(acc_selected.getAccountID(), acc_selected.getCredentialsHashMapList());
215 service.setAccountDetails(acc_selected.getAccountID(), acc_selected.getDetails());
216 } catch (RemoteException e) {
217 e.printStackTrace();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400218 }
219
220 }
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500221
Alexandre Lision227b59c2013-10-03 17:37:57 -0400222 private AlertDialog createDeleteDialog() {
223 Activity ownerActivity = this;
224 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
225 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
226 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
227 @Override
228 public void onClick(DialogInterface dialog, int whichButton) {
229 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400230 bundle.putString("AccountID", acc_selected.getAccountID());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400231
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400232 try {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400233 service.removeAccount(acc_selected.getAccountID());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400234 } catch (RemoteException e) {
235 e.printStackTrace();
236 }
237 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400238 }
239 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
240 @Override
241 public void onClick(DialogInterface dialog, int whichButton) {
242 /* Terminate with no action */
243 }
244 });
245
246 AlertDialog alertDialog = builder.create();
247 alertDialog.setOwnerActivity(ownerActivity);
248
249 return alertDialog;
250 }
251
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400252 public class PreferencesPagerAdapter extends FragmentStatePagerAdapter {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400253
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400254 Context mContext;
255 ArrayList<Fragment> fragments;
256
Alexandre Lision059da9d2013-10-22 11:17:25 -0400257 public PreferencesPagerAdapter(Context c, FragmentManager fm, ArrayList<Fragment> items) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400258 super(fm);
259 mContext = c;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400260 fragments = new ArrayList<Fragment>(items);
261
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400262 }
263
Alexandre Lision227b59c2013-10-03 17:37:57 -0400264 @Override
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400265 public int getCount() {
266 return fragments.size();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400267 }
268
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400269 @Override
270 public Fragment getItem(int position) {
271 return fragments.get(position);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400272 }
273
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400274 @Override
275 public CharSequence getPageTitle(int position) {
276 switch (position) {
277 case 0:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400278 if (acc_selected.isIP2IP()) {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500279 return getString(R.string.account_preferences_audio_tab).toUpperCase(Locale.getDefault());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400280 } else {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500281 return getString(R.string.account_preferences_basic_tab).toUpperCase(Locale.getDefault());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400282 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400283 case 1:
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500284 return getString(R.string.account_preferences_audio_tab).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500285 case 2:
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500286 return getString(R.string.account_preferences_advanced_tab).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500287 case 3:
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500288 return getString(R.string.account_preferences_security_tab).toUpperCase(Locale.getDefault());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400289 default:
290 Log.e(TAG, "getPreferencePageTitle: unknown tab position " + position);
291 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400292 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400293 return null;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400294 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400295 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400296
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400297 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400298 public ISipService getService() {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400299 return service;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400300 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400301
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400302 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400303 public Account getAccount() {
304 return acc_selected;
305 }
306
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500307 @Override
308 public void displayCredentialsScreen() {
Alexandre Lision3cefec22013-11-14 17:26:35 -0500309 toDisplay = new NestedSettingsFragment();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500310 Bundle b = new Bundle();
311 b.putInt("MODE", 0);
312 toDisplay.setArguments(b);
Alexandre Lision8ad39592013-11-21 13:23:58 -0500313 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down)
314 .replace(R.id.hidden_container, toDisplay).commit();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500315 }
316
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500317 @Override
318 public void displaySRTPScreen() {
319 toDisplay = new NestedSettingsFragment();
320 Bundle b = new Bundle();
321 b.putInt("MODE", 1);
322 toDisplay.setArguments(b);
Alexandre Lision8ad39592013-11-21 13:23:58 -0500323 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down)
324 .replace(R.id.hidden_container, toDisplay).commit();
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500325 }
326
327 @Override
328 public void displayTLSScreen() {
329 toDisplay = new NestedSettingsFragment();
330 Bundle b = new Bundle();
331 b.putInt("MODE", 2);
332 toDisplay.setArguments(b);
Alexandre Lision8ad39592013-11-21 13:23:58 -0500333 getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slidein_up, R.animator.slideout_down)
334 .replace(R.id.hidden_container, toDisplay).commit();
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500335 }
336
Alexandre Lision227b59c2013-10-03 17:37:57 -0400337}