blob: a84d1279e4bd58e87bffc25031d0b096daa84035 [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 Lision227b59c2013-10-03 17:37:57 -040037
38import org.sflphone.R;
Alexandre Lision227b59c2013-10-03 17:37:57 -040039import org.sflphone.account.AccountDetailBasic;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050040import org.sflphone.fragments.AdvancedAccountFragment;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040041import org.sflphone.fragments.AudioManagementFragment;
Alexandre Lision8907c1c2013-11-12 11:40:14 -050042import org.sflphone.fragments.GeneralAccountFragment;
Alexandre Lisiona3650992013-11-13 14:19:35 -050043import org.sflphone.fragments.NestedSettingsFragment;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050044import org.sflphone.fragments.SecurityAccountFragment;
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040045import org.sflphone.model.Account;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040046import org.sflphone.service.ISipService;
47import org.sflphone.service.SipService;
Alexandre Lision8907c1c2013-11-12 11:40:14 -050048import org.sflphone.views.PagerSlidingTabStrip;
Alexandre Lision227b59c2013-10-03 17:37:57 -040049
Alexandre Lisionafd40e42013-10-15 13:48:37 -040050import android.app.ActionBar.Tab;
51import android.app.ActionBar.TabListener;
Alexandre Lision227b59c2013-10-03 17:37:57 -040052import android.app.Activity;
53import android.app.AlertDialog;
54import android.app.Dialog;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040055import android.app.Fragment;
56import android.app.FragmentManager;
57import android.app.FragmentTransaction;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040058import android.content.ComponentName;
59import android.content.Context;
Alexandre Lision227b59c2013-10-03 17:37:57 -040060import android.content.DialogInterface;
61import android.content.Intent;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040062import android.content.ServiceConnection;
Alexandre Lision227b59c2013-10-03 17:37:57 -040063import android.os.Bundle;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040064import android.os.IBinder;
65import android.os.RemoteException;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040066import android.support.v13.app.FragmentStatePagerAdapter;
67import android.support.v4.view.ViewPager;
Alexandre Lision227b59c2013-10-03 17:37:57 -040068import android.util.Log;
69import android.view.Menu;
70import android.view.MenuInflater;
71import android.view.MenuItem;
72
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050073public class AccountEditionActivity extends Activity implements TabListener, GeneralAccountFragment.Callbacks, AudioManagementFragment.Callbacks,
Alexandre Lisiona3650992013-11-13 14:19:35 -050074 AdvancedAccountFragment.Callbacks, SecurityAccountFragment.Callbacks, NestedSettingsFragment.Callbacks {
Alexandre Lision227b59c2013-10-03 17:37:57 -040075 private static final String TAG = "AccoutPreferenceActivity";
76
77 public static final String KEY_MODE = "mode";
Alexandre Lisiond16bad92013-10-09 17:16:20 -040078 private boolean mBound = false;
79 private ISipService service;
Alexandre Lision059da9d2013-10-22 11:17:25 -040080
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040081 private Account acc_selected;
Alexandre Lision059da9d2013-10-22 11:17:25 -040082
Alexandre Lisionafd40e42013-10-15 13:48:37 -040083 PreferencesPagerAdapter mPreferencesPagerAdapter;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040084 private ServiceConnection mConnection = new ServiceConnection() {
85
86 @Override
87 public void onServiceConnected(ComponentName className, IBinder binder) {
88 service = ISipService.Stub.asInterface(binder);
89 mBound = true;
Alexandre Lision059da9d2013-10-22 11:17:25 -040090
91 ArrayList<Fragment> fragments = new ArrayList<Fragment>();
92 if (acc_selected.isIP2IP()) {
Alexandre Lision059da9d2013-10-22 11:17:25 -040093 fragments.add(new AudioManagementFragment());
94 } else {
Alexandre Lision8907c1c2013-11-12 11:40:14 -050095 fragments.add(new GeneralAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -040096 fragments.add(new AudioManagementFragment());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050097 fragments.add(new AdvancedAccountFragment());
98 fragments.add(new SecurityAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -040099 }
100
101 mPreferencesPagerAdapter = new PreferencesPagerAdapter(AccountEditionActivity.this, getFragmentManager(), fragments);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400102 mViewPager.setAdapter(mPreferencesPagerAdapter);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500103 mViewPager.setOffscreenPageLimit(3);
104
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500105 final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(findViewById(R.id.pager_sliding_strip));
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400106
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500107 // strip.setBackgroundColor(getResources().getColor(R.color.sfl_blue_0));
108 strip.setViewPager(mViewPager);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500109 // getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
110 // for (int i = 0; i < mPreferencesPagerAdapter.getCount(); i++) {
111 // getActionBar().addTab(
112 // getActionBar().newTab().setText(mPreferencesPagerAdapter.getPageTitle(i)).setTabListener(AccountEditionActivity.this));
113 //
114 // }
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400115 }
116
117 @Override
118 public void onServiceDisconnected(ComponentName arg0) {
119
120 }
121 };
Alexandre Lision227b59c2013-10-03 17:37:57 -0400122
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400123 private ViewPager mViewPager;
124
Alexandre Lision227b59c2013-10-03 17:37:57 -0400125 // private ArrayList<String> requiredFields = null;
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400126 // EditionFragment mEditionFragment;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400127
128 @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 mViewPager = (ViewPager) findViewById(R.id.pager);
136 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
137 @Override
138 public void onPageSelected(int position) {
139 getActionBar().setSelectedNavigationItem(position);
140 }
141 });
142
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400143 acc_selected = getIntent().getExtras().getParcelable("account");
Alexandre Lision059da9d2013-10-22 11:17:25 -0400144
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400145 if (!mBound) {
146 Log.i(TAG, "onCreate: Binding service...");
147 Intent intent = new Intent(this, SipService.class);
148 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
149 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400150
151 }
152
153 @Override
154 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400155 if (acc_selected.isIP2IP()) {
156 return true;
157 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400158 MenuInflater inflater = getMenuInflater();
159 inflater.inflate(R.menu.account_edition, menu);
160 return true;
161 }
162
163 @Override
164 public void onBackPressed() {
165
Alexandre Lision059da9d2013-10-22 11:17:25 -0400166 if (acc_selected.isIP2IP()) {
167 super.onBackPressed();
168 return;
169 }
170
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500171 if (mPreferencesPagerAdapter.getItem(0) != null && ((GeneralAccountFragment) mPreferencesPagerAdapter.getItem(0)).isDifferent()) {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400172 AlertDialog dialog = createCancelDialog();
173 dialog.show();
174 } else {
175 super.onBackPressed();
176 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400177 }
178
179 @Override
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400180 protected void onDestroy() {
181
182 if (mBound) {
183 unbindService(mConnection);
184 mBound = false;
185 }
186
187 // stopService(new Intent(this, SipService.class));
188 // serviceIsOn = false;
189 super.onDestroy();
190 }
191
192 @Override
Alexandre Lision227b59c2013-10-03 17:37:57 -0400193 public boolean onOptionsItemSelected(MenuItem item) {
194
195 switch (item.getItemId()) {
196 case android.R.id.home:
197 finish();
198 return true;
199 case R.id.menuitem_delete:
200 AlertDialog dialog = createDeleteDialog();
201 dialog.show();
202 break;
203 case R.id.menuitem_edit:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400204 processAccount();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400205 break;
206
207 }
208
209 return true;
210 }
211
Alexandre Lision059da9d2013-10-22 11:17:25 -0400212 private void processAccount() {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400213 AlertDialog dialog;
214 ArrayList<String> missingValue = new ArrayList<String>();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500215
Alexandre Lision059da9d2013-10-22 11:17:25 -0400216 if (validateAccountCreation(missingValue)) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400217 try {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400218 service.setAccountDetails(acc_selected.getAccountID(), acc_selected.getDetails());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400219 } catch (RemoteException e) {
220 e.printStackTrace();
221 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400222 finish();
223 } else {
224 dialog = createCouldNotValidateDialog(missingValue);
225 dialog.show();
226 }
227
228 }
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500229
Alexandre Lision059da9d2013-10-22 11:17:25 -0400230 public boolean validateAccountCreation(ArrayList<String> missingValue) {
231 boolean valid = true;
232 ArrayList<String> requiredFields = new ArrayList<String>();
233 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
234 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
235 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
236 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
237 for (String s : requiredFields) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500238
Alexandre Lision059da9d2013-10-22 11:17:25 -0400239 if (acc_selected.getBasicDetails().getDetailString(s).isEmpty()) {
240 valid = false;
241 missingValue.add(s);
242 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400243 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400244
245 return valid;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400246 }
247
248 /******************************************
249 *
250 * AlertDialogs
251 *
252 ******************************************/
253
254 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) {
255 String message = "The following parameters are missing:";
256
257 for (String s : missingValue)
258 message += "\n - " + s;
259
260 Activity ownerActivity = this;
261 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400262 builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400263 @Override
264 public void onClick(DialogInterface dialog, int whichButton) {
265 /* Nothing to be done */
266 }
267 });
268
269 AlertDialog alertDialog = builder.create();
270 return alertDialog;
271 }
272
273 private AlertDialog createCancelDialog() {
274 Activity ownerActivity = this;
275 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400276 builder.setMessage("Modifications will be lost").setTitle("Account Edition")
277 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
278 @Override
279 public void onClick(DialogInterface dialog, int whichButton) {
280 Activity activity = ((Dialog) dialog).getOwnerActivity();
281 activity.finish();
282 }
283 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
284 @Override
285 public void onClick(DialogInterface dialog, int whichButton) {
286 /* Terminate with no action */
287 }
288 });
Alexandre Lision227b59c2013-10-03 17:37:57 -0400289
290 AlertDialog alertDialog = builder.create();
291 alertDialog.setOwnerActivity(ownerActivity);
292
293 return alertDialog;
294 }
295
296 private AlertDialog createDeleteDialog() {
297 Activity ownerActivity = this;
298 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
299 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
300 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
301 @Override
302 public void onClick(DialogInterface dialog, int whichButton) {
303 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400304 bundle.putString("AccountID", acc_selected.getAccountID());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400305
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400306 try {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400307 service.removeAccount(acc_selected.getAccountID());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400308 } catch (RemoteException e) {
309 e.printStackTrace();
310 }
311 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400312 }
313 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
314 @Override
315 public void onClick(DialogInterface dialog, int whichButton) {
316 /* Terminate with no action */
317 }
318 });
319
320 AlertDialog alertDialog = builder.create();
321 alertDialog.setOwnerActivity(ownerActivity);
322
323 return alertDialog;
324 }
325
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400326 public class PreferencesPagerAdapter extends FragmentStatePagerAdapter {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400327
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400328 Context mContext;
329 ArrayList<Fragment> fragments;
330
Alexandre Lision059da9d2013-10-22 11:17:25 -0400331 public PreferencesPagerAdapter(Context c, FragmentManager fm, ArrayList<Fragment> items) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400332 super(fm);
333 mContext = c;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400334 fragments = new ArrayList<Fragment>(items);
335
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400336 }
337
Alexandre Lision227b59c2013-10-03 17:37:57 -0400338 @Override
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400339 public int getCount() {
340 return fragments.size();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400341 }
342
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400343 @Override
344 public Fragment getItem(int position) {
345 return fragments.get(position);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400346 }
347
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400348 @Override
349 public CharSequence getPageTitle(int position) {
350 switch (position) {
351 case 0:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400352 if (acc_selected.isIP2IP()) {
353 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
354 } else {
355 return getString(R.string.account_preferences_basic).toUpperCase(Locale.getDefault());
356 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400357 case 1:
358 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500359 case 2:
360 return getString(R.string.account_preferences_advanced).toUpperCase(Locale.getDefault());
361 case 3:
362 return getString(R.string.account_preferences_security).toUpperCase(Locale.getDefault());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400363 default:
364 Log.e(TAG, "getPreferencePageTitle: unknown tab position " + position);
365 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400366 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400367 return null;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400368 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400369 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400370
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400371 @Override
372 public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
373 // TODO Stub de la méthode généré automatiquement
Alexandre Lision227b59c2013-10-03 17:37:57 -0400374
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400375 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400376
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400377 @Override
378 public void onTabSelected(Tab tab, FragmentTransaction ft) {
379 mViewPager.setCurrentItem(tab.getPosition());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400380
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400381 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400382
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400383 @Override
384 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
385 // TODO Stub de la méthode généré automatiquement
Alexandre Lision227b59c2013-10-03 17:37:57 -0400386
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400387 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400388
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400389 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400390 public ISipService getService() {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400391 return service;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400392 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400393
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400394 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400395 public String getAccountID() {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400396 return acc_selected.getAccountID();
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400397 }
398
Alexandre Lision059da9d2013-10-22 11:17:25 -0400399 @Override
400 public Account getAccount() {
401 return acc_selected;
402 }
403
Alexandre Lision227b59c2013-10-03 17:37:57 -0400404}