blob: cd815f76dfe33dcce0f0414baec9dc30404486d8 [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 Lisionafd40e42013-10-15 13:48:37 -040040import org.sflphone.fragments.AudioManagementFragment;
41import org.sflphone.fragments.EditionFragment;
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040042import org.sflphone.model.Account;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040043import org.sflphone.service.ISipService;
44import org.sflphone.service.SipService;
Alexandre Lision227b59c2013-10-03 17:37:57 -040045
Alexandre Lisionafd40e42013-10-15 13:48:37 -040046import android.app.ActionBar;
47import android.app.ActionBar.Tab;
48import android.app.ActionBar.TabListener;
Alexandre Lision227b59c2013-10-03 17:37:57 -040049import android.app.Activity;
50import android.app.AlertDialog;
51import android.app.Dialog;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040052import android.app.Fragment;
53import android.app.FragmentManager;
54import android.app.FragmentTransaction;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040055import android.content.ComponentName;
56import android.content.Context;
Alexandre Lision227b59c2013-10-03 17:37:57 -040057import android.content.DialogInterface;
58import android.content.Intent;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040059import android.content.ServiceConnection;
Alexandre Lision227b59c2013-10-03 17:37:57 -040060import android.os.Bundle;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040061import android.os.IBinder;
62import android.os.RemoteException;
Alexandre Lision059da9d2013-10-22 11:17:25 -040063import android.preference.EditTextPreference;
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 Lision933ef0a2013-10-15 17:28:40 -040071public class AccountEditionActivity extends Activity implements TabListener, EditionFragment.Callbacks, AudioManagementFragment.Callbacks {
Alexandre Lision227b59c2013-10-03 17:37:57 -040072 private static final String TAG = "AccoutPreferenceActivity";
73
74 public static final String KEY_MODE = "mode";
Alexandre Lisiond16bad92013-10-09 17:16:20 -040075 private boolean mBound = false;
76 private ISipService service;
Alexandre Lision059da9d2013-10-22 11:17:25 -040077
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040078 private Account acc_selected;
Alexandre Lision059da9d2013-10-22 11:17:25 -040079
Alexandre Lisionafd40e42013-10-15 13:48:37 -040080 PreferencesPagerAdapter mPreferencesPagerAdapter;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040081 private ServiceConnection mConnection = new ServiceConnection() {
82
83 @Override
84 public void onServiceConnected(ComponentName className, IBinder binder) {
85 service = ISipService.Stub.asInterface(binder);
86 mBound = true;
Alexandre Lision059da9d2013-10-22 11:17:25 -040087
88 ArrayList<Fragment> fragments = new ArrayList<Fragment>();
89 if (acc_selected.isIP2IP()) {
90
91 fragments.add(new AudioManagementFragment());
92 } else {
93 fragments.add(new EditionFragment());
94 fragments.add(new AudioManagementFragment());
95 }
96
97 mPreferencesPagerAdapter = new PreferencesPagerAdapter(AccountEditionActivity.this, getFragmentManager(), fragments);
Alexandre Lisionafd40e42013-10-15 13:48:37 -040098 mViewPager.setAdapter(mPreferencesPagerAdapter);
99 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
100 for (int i = 0; i < mPreferencesPagerAdapter.getCount(); i++) {
101 getActionBar().addTab(
102 getActionBar().newTab().setText(mPreferencesPagerAdapter.getPageTitle(i)).setTabListener(AccountEditionActivity.this));
103
104 }
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400105 }
106
107 @Override
108 public void onServiceDisconnected(ComponentName arg0) {
109
110 }
111 };
Alexandre Lision227b59c2013-10-03 17:37:57 -0400112
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400113 private ViewPager mViewPager;
114
Alexandre Lision227b59c2013-10-03 17:37:57 -0400115 // private ArrayList<String> requiredFields = null;
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400116 // EditionFragment mEditionFragment;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400117
118 @Override
119 protected void onCreate(Bundle savedInstanceState) {
120 super.onCreate(savedInstanceState);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400121
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400122 setContentView(R.layout.activity_wizard);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400123
124 getActionBar().setDisplayHomeAsUpEnabled(true);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400125 mViewPager = (ViewPager) findViewById(R.id.pager);
126 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
127 @Override
128 public void onPageSelected(int position) {
129 getActionBar().setSelectedNavigationItem(position);
130 }
131 });
132
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400133 acc_selected = getIntent().getExtras().getParcelable("account");
Alexandre Lision059da9d2013-10-22 11:17:25 -0400134
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400135 if (!mBound) {
136 Log.i(TAG, "onCreate: Binding service...");
137 Intent intent = new Intent(this, SipService.class);
138 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
139 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400140
141 }
142
143 @Override
144 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400145 if (acc_selected.isIP2IP()) {
146 return true;
147 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400148 MenuInflater inflater = getMenuInflater();
149 inflater.inflate(R.menu.account_edition, menu);
150 return true;
151 }
152
153 @Override
154 public void onBackPressed() {
155
Alexandre Lision059da9d2013-10-22 11:17:25 -0400156 if (acc_selected.isIP2IP()) {
157 super.onBackPressed();
158 return;
159 }
160
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400161 if (mPreferencesPagerAdapter.getItem(0) != null && ((EditionFragment) mPreferencesPagerAdapter.getItem(0)).isDifferent()) {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400162 AlertDialog dialog = createCancelDialog();
163 dialog.show();
164 } else {
165 super.onBackPressed();
166 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400167 }
168
169 @Override
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400170 protected void onDestroy() {
171
172 if (mBound) {
173 unbindService(mConnection);
174 mBound = false;
175 }
176
177 // stopService(new Intent(this, SipService.class));
178 // serviceIsOn = false;
179 super.onDestroy();
180 }
181
182 @Override
Alexandre Lision227b59c2013-10-03 17:37:57 -0400183 public boolean onOptionsItemSelected(MenuItem item) {
184
185 switch (item.getItemId()) {
186 case android.R.id.home:
187 finish();
188 return true;
189 case R.id.menuitem_delete:
190 AlertDialog dialog = createDeleteDialog();
191 dialog.show();
192 break;
193 case R.id.menuitem_edit:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400194 processAccount();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400195 break;
196
197 }
198
199 return true;
200 }
201
Alexandre Lision059da9d2013-10-22 11:17:25 -0400202 private void processAccount() {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400203 AlertDialog dialog;
204 ArrayList<String> missingValue = new ArrayList<String>();
Alexandre Lision059da9d2013-10-22 11:17:25 -0400205 if (validateAccountCreation(missingValue)) {
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400206 try {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400207 service.setAccountDetails(acc_selected.getAccountID(), acc_selected.getDetails());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400208 } catch (RemoteException e) {
209 e.printStackTrace();
210 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400211 finish();
212 } else {
213 dialog = createCouldNotValidateDialog(missingValue);
214 dialog.show();
215 }
216
217 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400218
219 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) {
227
228 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
237 /******************************************
238 *
239 * AlertDialogs
240 *
241 ******************************************/
242
243 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) {
244 String message = "The following parameters are missing:";
245
246 for (String s : missingValue)
247 message += "\n - " + s;
248
249 Activity ownerActivity = this;
250 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400251 builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400252 @Override
253 public void onClick(DialogInterface dialog, int whichButton) {
254 /* Nothing to be done */
255 }
256 });
257
258 AlertDialog alertDialog = builder.create();
259 return alertDialog;
260 }
261
262 private AlertDialog createCancelDialog() {
263 Activity ownerActivity = this;
264 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400265 builder.setMessage("Modifications will be lost").setTitle("Account Edition")
266 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
267 @Override
268 public void onClick(DialogInterface dialog, int whichButton) {
269 Activity activity = ((Dialog) dialog).getOwnerActivity();
270 activity.finish();
271 }
272 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
273 @Override
274 public void onClick(DialogInterface dialog, int whichButton) {
275 /* Terminate with no action */
276 }
277 });
Alexandre Lision227b59c2013-10-03 17:37:57 -0400278
279 AlertDialog alertDialog = builder.create();
280 alertDialog.setOwnerActivity(ownerActivity);
281
282 return alertDialog;
283 }
284
285 private AlertDialog createDeleteDialog() {
286 Activity ownerActivity = this;
287 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
288 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
289 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
290 @Override
291 public void onClick(DialogInterface dialog, int whichButton) {
292 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400293 bundle.putString("AccountID", acc_selected.getAccountID());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400294
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400295 try {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400296 service.removeAccount(acc_selected.getAccountID());
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400297 } catch (RemoteException e) {
298 e.printStackTrace();
299 }
300 finish();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400301 }
302 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
303 @Override
304 public void onClick(DialogInterface dialog, int whichButton) {
305 /* Terminate with no action */
306 }
307 });
308
309 AlertDialog alertDialog = builder.create();
310 alertDialog.setOwnerActivity(ownerActivity);
311
312 return alertDialog;
313 }
314
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400315 public class PreferencesPagerAdapter extends FragmentStatePagerAdapter {
Alexandre Lision227b59c2013-10-03 17:37:57 -0400316
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400317 Context mContext;
318 ArrayList<Fragment> fragments;
319
Alexandre Lision059da9d2013-10-22 11:17:25 -0400320 public PreferencesPagerAdapter(Context c, FragmentManager fm, ArrayList<Fragment> items) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400321 super(fm);
322 mContext = c;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400323 fragments = new ArrayList<Fragment>(items);
324
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400325 }
326
Alexandre Lision227b59c2013-10-03 17:37:57 -0400327 @Override
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400328 public int getCount() {
329 return fragments.size();
Alexandre Lision227b59c2013-10-03 17:37:57 -0400330 }
331
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400332 @Override
333 public Fragment getItem(int position) {
334 return fragments.get(position);
Alexandre Lision227b59c2013-10-03 17:37:57 -0400335 }
336
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400337 @Override
338 public CharSequence getPageTitle(int position) {
339 switch (position) {
340 case 0:
Alexandre Lision059da9d2013-10-22 11:17:25 -0400341 if (acc_selected.isIP2IP()) {
342 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
343 } else {
344 return getString(R.string.account_preferences_basic).toUpperCase(Locale.getDefault());
345 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400346 case 1:
347 return getString(R.string.account_preferences_audio).toUpperCase(Locale.getDefault());
348 default:
349 Log.e(TAG, "getPreferencePageTitle: unknown tab position " + position);
350 break;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400351 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400352 return null;
Alexandre Lision227b59c2013-10-03 17:37:57 -0400353 }
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400354 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400355
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400356 @Override
357 public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
358 // TODO Stub de la méthode généré automatiquement
Alexandre Lision227b59c2013-10-03 17:37:57 -0400359
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400360 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400361
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400362 @Override
363 public void onTabSelected(Tab tab, FragmentTransaction ft) {
364 mViewPager.setCurrentItem(tab.getPosition());
Alexandre Lision227b59c2013-10-03 17:37:57 -0400365
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400366 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400367
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400368 @Override
369 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
370 // TODO Stub de la méthode généré automatiquement
Alexandre Lision227b59c2013-10-03 17:37:57 -0400371
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400372 }
Alexandre Lision227b59c2013-10-03 17:37:57 -0400373
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400374 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400375 public ISipService getService() {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400376 return service;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400377 }
Alexandre Lision059da9d2013-10-22 11:17:25 -0400378
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400379 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400380 public String getAccountID() {
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400381 return acc_selected.getAccountID();
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400382 }
383
Alexandre Lision059da9d2013-10-22 11:17:25 -0400384 @Override
385 public Account getAccount() {
386 return acc_selected;
387 }
388
Alexandre Lision227b59c2013-10-03 17:37:57 -0400389}