blob: b083f4bce3106ad7ddcca0057813cc0c55c740c8 [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;
Alexandre Lision3b7148e2013-11-13 17:23:06 -050072import android.view.View;
Alexandre Lision227b59c2013-10-03 17:37:57 -040073
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050074public class AccountEditionActivity extends Activity implements TabListener, GeneralAccountFragment.Callbacks, AudioManagementFragment.Callbacks,
Alexandre Lisiona3650992013-11-13 14:19:35 -050075 AdvancedAccountFragment.Callbacks, SecurityAccountFragment.Callbacks, NestedSettingsFragment.Callbacks {
Alexandre Lision227b59c2013-10-03 17:37:57 -040076 private static final String TAG = "AccoutPreferenceActivity";
77
78 public static final String KEY_MODE = "mode";
Alexandre Lisiond16bad92013-10-09 17:16:20 -040079 private boolean mBound = false;
80 private ISipService service;
Alexandre Lision059da9d2013-10-22 11:17:25 -040081
Alexandre Lision3b7148e2013-11-13 17:23:06 -050082 private View mOverlayContainer;
83
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040084 private Account acc_selected;
Alexandre Lision059da9d2013-10-22 11:17:25 -040085
Alexandre Lisionafd40e42013-10-15 13:48:37 -040086 PreferencesPagerAdapter mPreferencesPagerAdapter;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040087 private ServiceConnection mConnection = new ServiceConnection() {
88
89 @Override
90 public void onServiceConnected(ComponentName className, IBinder binder) {
91 service = ISipService.Stub.asInterface(binder);
92 mBound = true;
Alexandre Lision059da9d2013-10-22 11:17:25 -040093
94 ArrayList<Fragment> fragments = new ArrayList<Fragment>();
95 if (acc_selected.isIP2IP()) {
Alexandre Lision059da9d2013-10-22 11:17:25 -040096 fragments.add(new AudioManagementFragment());
97 } else {
Alexandre Lision8907c1c2013-11-12 11:40:14 -050098 fragments.add(new GeneralAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -040099 fragments.add(new AudioManagementFragment());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500100 fragments.add(new AdvancedAccountFragment());
101 fragments.add(new SecurityAccountFragment());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400102 }
103
104 mPreferencesPagerAdapter = new PreferencesPagerAdapter(AccountEditionActivity.this, getFragmentManager(), fragments);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400105 mViewPager.setAdapter(mPreferencesPagerAdapter);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500106 mViewPager.setOffscreenPageLimit(3);
107
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500108 final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(findViewById(R.id.pager_sliding_strip));
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400109
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500110 // strip.setBackgroundColor(getResources().getColor(R.color.sfl_blue_0));
111 strip.setViewPager(mViewPager);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500112 // getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
113 // for (int i = 0; i < mPreferencesPagerAdapter.getCount(); i++) {
114 // getActionBar().addTab(
115 // getActionBar().newTab().setText(mPreferencesPagerAdapter.getPageTitle(i)).setTabListener(AccountEditionActivity.this));
116 //
117 // }
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400118 }
119
120 @Override
121 public void onServiceDisconnected(ComponentName arg0) {
122
123 }
124 };
Alexandre Lision227b59c2013-10-03 17:37:57 -0400125
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400126 private ViewPager mViewPager;
127
Alexandre Lision227b59c2013-10-03 17:37:57 -0400128 // private ArrayList<String> requiredFields = null;
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400129 // EditionFragment mEditionFragment;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400130
131 @Override
132 protected void onCreate(Bundle savedInstanceState) {
133 super.onCreate(savedInstanceState);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400134
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500135 setContentView(R.layout.activity_account_settings);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400136
137 getActionBar().setDisplayHomeAsUpEnabled(true);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400138 mViewPager = (ViewPager) findViewById(R.id.pager);
139 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
140 @Override
141 public void onPageSelected(int position) {
142 getActionBar().setSelectedNavigationItem(position);
143 }
144 });
145
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500146 mOverlayContainer = findViewById(R.id.hidden_container);
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400147 acc_selected = getIntent().getExtras().getParcelable("account");
Alexandre Lision059da9d2013-10-22 11:17:25 -0400148
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400149 if (!mBound) {
150 Log.i(TAG, "onCreate: Binding service...");
151 Intent intent = new Intent(this, SipService.class);
152 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
153 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400154
155 }
156
157 @Override
158 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400159 if (acc_selected.isIP2IP()) {
160 return true;
161 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400162 MenuInflater inflater = getMenuInflater();
163 inflater.inflate(R.menu.account_edition, menu);
164 return true;
165 }
166
167 @Override
168 public void onBackPressed() {
169
Alexandre Lision059da9d2013-10-22 11:17:25 -0400170 if (acc_selected.isIP2IP()) {
171 super.onBackPressed();
172 return;
173 }
174
Alexandre Lision8907c1c2013-11-12 11:40:14 -0500175 if (mPreferencesPagerAdapter.getItem(0) != null && ((GeneralAccountFragment) mPreferencesPagerAdapter.getItem(0)).isDifferent()) {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400176 AlertDialog dialog = createCancelDialog();
177 dialog.show();
178 } else {
179 super.onBackPressed();
180 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400181 }
182
183 @Override
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400184 protected void onDestroy() {
185
186 if (mBound) {
187 unbindService(mConnection);
188 mBound = false;
189 }
190
191 // stopService(new Intent(this, SipService.class));
192 // serviceIsOn = false;
193 super.onDestroy();
194 }
195
196 @Override
Alexandre Lision227b59c2013-10-03 17:37:57 -0400197 public boolean onOptionsItemSelected(MenuItem item) {
198
199 switch (item.getItemId()) {
200 case android.R.id.home:
201 finish();
202 return true;
203 case R.id.menuitem_delete:
204 AlertDialog dialog = createDeleteDialog();
205 dialog.show();
206 break;
207 case R.id.menuitem_edit:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400208 processAccount();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400209 break;
210
211 }
212
213 return true;
214 }
215
Alexandre Lision059da9d2013-10-22 11:17:25 -0400216 private void processAccount() {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400217 AlertDialog dialog;
218 ArrayList<String> missingValue = new ArrayList<String>();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500219
Alexandre Lision059da9d2013-10-22 11:17:25 -0400220 if (validateAccountCreation(missingValue)) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400221 try {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400222 service.setAccountDetails(acc_selected.getAccountID(), acc_selected.getDetails());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400223 } catch (RemoteException e) {
224 e.printStackTrace();
225 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400226 finish();
227 } else {
228 dialog = createCouldNotValidateDialog(missingValue);
229 dialog.show();
230 }
231
232 }
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500233
Alexandre Lision059da9d2013-10-22 11:17:25 -0400234 public boolean validateAccountCreation(ArrayList<String> missingValue) {
235 boolean valid = true;
236 ArrayList<String> requiredFields = new ArrayList<String>();
237 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
238 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
239 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
240 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
241 for (String s : requiredFields) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500242
Alexandre Lision059da9d2013-10-22 11:17:25 -0400243 if (acc_selected.getBasicDetails().getDetailString(s).isEmpty()) {
244 valid = false;
245 missingValue.add(s);
246 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400247 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400248
249 return valid;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400250 }
251
252 /******************************************
253 *
254 * AlertDialogs
255 *
256 ******************************************/
257
258 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) {
259 String message = "The following parameters are missing:";
260
261 for (String s : missingValue)
262 message += "\n - " + s;
263
264 Activity ownerActivity = this;
265 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400266 builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400267 @Override
268 public void onClick(DialogInterface dialog, int whichButton) {
269 /* Nothing to be done */
270 }
271 });
272
273 AlertDialog alertDialog = builder.create();
274 return alertDialog;
275 }
276
277 private AlertDialog createCancelDialog() {
278 Activity ownerActivity = this;
279 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400280 builder.setMessage("Modifications will be lost").setTitle("Account Edition")
281 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
282 @Override
283 public void onClick(DialogInterface dialog, int whichButton) {
284 Activity activity = ((Dialog) dialog).getOwnerActivity();
285 activity.finish();
286 }
287 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
288 @Override
289 public void onClick(DialogInterface dialog, int whichButton) {
290 /* Terminate with no action */
291 }
292 });
Alexandre Lision227b59c2013-10-03 17:37:57 -0400293
294 AlertDialog alertDialog = builder.create();
295 alertDialog.setOwnerActivity(ownerActivity);
296
297 return alertDialog;
298 }
299
300 private AlertDialog createDeleteDialog() {
301 Activity ownerActivity = this;
302 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
303 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
304 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
305 @Override
306 public void onClick(DialogInterface dialog, int whichButton) {
307 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400308 bundle.putString("AccountID", acc_selected.getAccountID());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400309
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400310 try {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400311 service.removeAccount(acc_selected.getAccountID());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400312 } catch (RemoteException e) {
313 e.printStackTrace();
314 }
315 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400316 }
317 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
318 @Override
319 public void onClick(DialogInterface dialog, int whichButton) {
320 /* Terminate with no action */
321 }
322 });
323
324 AlertDialog alertDialog = builder.create();
325 alertDialog.setOwnerActivity(ownerActivity);
326
327 return alertDialog;
328 }
329
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400330 public class PreferencesPagerAdapter extends FragmentStatePagerAdapter {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400331
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400332 Context mContext;
333 ArrayList<Fragment> fragments;
334
Alexandre Lision059da9d2013-10-22 11:17:25 -0400335 public PreferencesPagerAdapter(Context c, FragmentManager fm, ArrayList<Fragment> items) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400336 super(fm);
337 mContext = c;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400338 fragments = new ArrayList<Fragment>(items);
339
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400340 }
341
Alexandre Lision227b59c2013-10-03 17:37:57 -0400342 @Override
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400343 public int getCount() {
344 return fragments.size();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400345 }
346
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400347 @Override
348 public Fragment getItem(int position) {
349 return fragments.get(position);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400350 }
351
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400352 @Override
353 public CharSequence getPageTitle(int position) {
354 switch (position) {
355 case 0:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400356 if (acc_selected.isIP2IP()) {
357 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
358 } else {
359 return getString(R.string.account_preferences_basic).toUpperCase(Locale.getDefault());
360 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400361 case 1:
362 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500363 case 2:
364 return getString(R.string.account_preferences_advanced).toUpperCase(Locale.getDefault());
365 case 3:
366 return getString(R.string.account_preferences_security).toUpperCase(Locale.getDefault());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400367 default:
368 Log.e(TAG, "getPreferencePageTitle: unknown tab position " + position);
369 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400370 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400371 return null;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400372 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400373 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400374
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400375 @Override
376 public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
377 // TODO Stub de la méthode généré automatiquement
Alexandre Lision227b59c2013-10-03 17:37:57 -0400378
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400379 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400380
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400381 @Override
382 public void onTabSelected(Tab tab, FragmentTransaction ft) {
383 mViewPager.setCurrentItem(tab.getPosition());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400384
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400385 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400386
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400387 @Override
388 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
389 // TODO Stub de la méthode généré automatiquement
Alexandre Lision227b59c2013-10-03 17:37:57 -0400390
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400391 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400392
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400393 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400394 public ISipService getService() {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400395 return service;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400396 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400397
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400398 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400399 public String getAccountID() {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400400 return acc_selected.getAccountID();
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400401 }
402
Alexandre Lision059da9d2013-10-22 11:17:25 -0400403 @Override
404 public Account getAccount() {
405 return acc_selected;
406 }
407
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500408 @Override
409 public void displayCredentialsScreen() {
410 Fragment toDisplay = new NestedSettingsFragment();
411 Bundle b = new Bundle();
412 b.putInt("MODE", 0);
413 toDisplay.setArguments(b);
414 getFragmentManager().beginTransaction().replace(R.id.hidden_container, toDisplay).commit();
415 }
416
Alexandre Lision227b59c2013-10-03 17:37:57 -0400417}