blob: 13e848a9b8483b84fd03430b3d7f9e5e69750a55 [file] [log] [blame]
Alexandre Savard68838112012-10-30 11:34:43 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
32package com.savoirfairelinux.sflphone.client;
33
alision73424b62013-04-26 11:49:18 -040034import java.net.NetworkInterface;
35import java.net.SocketException;
alisiond9e29442013-04-17 16:10:18 -040036import java.util.ArrayList;
alision73424b62013-04-26 11:49:18 -040037import java.util.Enumeration;
alisiond9e29442013-04-17 16:10:18 -040038import java.util.HashMap;
39
Alexandre Savard68838112012-10-30 11:34:43 -040040import android.app.Activity;
Alexandre Savard645b29c2012-10-30 17:22:26 -040041import android.app.AlertDialog;
42import android.app.Dialog;
Alexandre Savard645b29c2012-10-30 17:22:26 -040043import android.content.DialogInterface;
Alexandre Savard68838112012-10-30 11:34:43 -040044import android.content.Intent;
45import android.os.Bundle;
alisiond9e29442013-04-17 16:10:18 -040046import android.preference.CheckBoxPreference;
47import android.preference.EditTextPreference;
alision73424b62013-04-26 11:49:18 -040048import android.preference.ListPreference;
Alexandre Savard68838112012-10-30 11:34:43 -040049import android.preference.Preference;
alisione2a38e12013-04-25 14:20:20 -040050import android.preference.Preference.OnPreferenceChangeListener;
Alexandre Savard68838112012-10-30 11:34:43 -040051import android.preference.PreferenceActivity;
52import android.preference.PreferenceManager;
Alexandre Savard68838112012-10-30 11:34:43 -040053import android.util.Log;
Alexandre Savard5195d922012-10-30 16:58:50 -040054import android.view.Menu;
alisiond9e29442013-04-17 16:10:18 -040055import android.view.MenuInflater;
Alexandre Savard5195d922012-10-30 16:58:50 -040056import android.view.MenuItem;
Alexandre Savard68838112012-10-30 11:34:43 -040057
58import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040059import com.savoirfairelinux.sflphone.account.AccountDetail;
60import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced;
61import com.savoirfairelinux.sflphone.account.AccountDetailBasic;
62import com.savoirfairelinux.sflphone.account.AccountDetailSrtp;
63import com.savoirfairelinux.sflphone.account.AccountDetailTls;
Alexandre Savard68838112012-10-30 11:34:43 -040064
alisiond9e29442013-04-17 16:10:18 -040065public class AccountPreferenceActivity extends PreferenceActivity {
Alexandre Savard68838112012-10-30 11:34:43 -040066 private static final String TAG = "AccoutPreferenceActivity";
alisiond9e29442013-04-17 16:10:18 -040067
68 public static final String KEY_MODE = "mode";
69
70 public interface mode {
71 static final int CREATION_MODE = 0;
72 static final int EDITION_MODE = 1;
73 }
74
75 public interface result {
76 static final int ACCOUNT_CREATED = Activity.RESULT_FIRST_USER + 0;
77 static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 1;
78 static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2;
79 }
Alexandre Savard68838112012-10-30 11:34:43 -040080
Alexandre Savard833616f2012-10-30 16:02:30 -040081 private AccountDetailBasic basicDetails = null;
82 private AccountDetailAdvanced advancedDetails = null;
83 private AccountDetailSrtp srtpDetails = null;
84 private AccountDetailTls tlsDetails = null;
Alexandre Savard68838112012-10-30 11:34:43 -040085 private PreferenceManager mPreferenceManager;
Alexandre Savard68838112012-10-30 11:34:43 -040086 private String mAccountID;
alisiond9e29442013-04-17 16:10:18 -040087 private ArrayList<String> requiredFields = null;
Alexandre Savard68838112012-10-30 11:34:43 -040088
alisiond9e29442013-04-17 16:10:18 -040089 @Override
90 protected void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 addPreferencesFromResource(R.xml.account_creation_preferences);
93 mPreferenceManager = getPreferenceManager();
Alexandre Savard68838112012-10-30 11:34:43 -040094
alisiond9e29442013-04-17 16:10:18 -040095 Bundle b = getIntent().getExtras();
96
97 switch (b.getInt(KEY_MODE)) {
98 case mode.CREATION_MODE:
99 Log.i(TAG, "CREATION");
100 initCreation();
101 break;
102 case mode.EDITION_MODE:
alision73424b62013-04-26 11:49:18 -0400103 Log.i(TAG, "EDITION");
alisiond9e29442013-04-17 16:10:18 -0400104 initEdition();
105 break;
106 }
107
108 requiredFields = new ArrayList<String>();
109 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
110 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
111 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
112 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
113
114 }
115
116 private void initCreation() {
117 basicDetails = new AccountDetailBasic();
118 advancedDetails = new AccountDetailAdvanced();
119 srtpDetails = new AccountDetailSrtp();
120 tlsDetails = new AccountDetailTls();
121
alisione2a38e12013-04-25 14:20:20 -0400122 addPreferenceListener(basicDetails, changeBasicPreferenceListener);
123 addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener);
124 addPreferenceListener(srtpDetails, changeSrtpPreferenceListener);
125 addPreferenceListener(tlsDetails, changeTlsPreferenceListener);
alisiond9e29442013-04-17 16:10:18 -0400126
127 }
alision7f18fc82013-05-01 09:37:33 -0400128
alisiond9e29442013-04-17 16:10:18 -0400129 private void initEdition() {
Alexandre Savard68838112012-10-30 11:34:43 -0400130
131 Bundle b = getIntent().getExtras();
132 mAccountID = b.getString("AccountID");
133 ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG);
134 ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG);
135 ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG);
136 ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG);
alision7f18fc82013-05-01 09:37:33 -0400137
alisiond9e29442013-04-17 16:10:18 -0400138 basicDetails = new AccountDetailBasic(basicPreferenceList);
Alexandre Savard68838112012-10-30 11:34:43 -0400139 advancedDetails = new AccountDetailAdvanced(advancedPreferenceList);
140 srtpDetails = new AccountDetailSrtp(srtpPreferenceList);
141 tlsDetails = new AccountDetailTls(tlsPreferenceList);
142
143 setPreferenceDetails(basicDetails);
Alexandre Savard833616f2012-10-30 16:02:30 -0400144 setPreferenceDetails(advancedDetails);
145 setPreferenceDetails(srtpDetails);
146 setPreferenceDetails(tlsDetails);
147
alisione2a38e12013-04-25 14:20:20 -0400148 addPreferenceListener(basicDetails, changeBasicPreferenceListener);
149 addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener);
150 addPreferenceListener(srtpDetails, changeSrtpPreferenceListener);
151 addPreferenceListener(tlsDetails, changeTlsPreferenceListener);
Alexandre Savard68838112012-10-30 11:34:43 -0400152 }
153
alisiond9e29442013-04-17 16:10:18 -0400154 @Override
155 public boolean onCreateOptionsMenu(Menu menu) {
156 MenuInflater inflater = getMenuInflater();
157
158 Bundle b = getIntent().getExtras();
159
160 switch (b.getInt(KEY_MODE)) {
161 case mode.CREATION_MODE:
162 Log.i(TAG, "CREATION");
163 inflater.inflate(R.menu.account_creation, menu);
164 break;
165 case mode.EDITION_MODE:
166 Log.i(TAG, "onCreateOptionsMenu: " + mAccountID);
167
168 if (mAccountID.equals("IP2IP"))
169 return true;
170
171 inflater.inflate(R.menu.account_edition, menu);
172 break;
173 }
174
175 return true;
176 }
177
178 @Override
179 public void onBackPressed() {
180 Bundle b = getIntent().getExtras();
181 switch (b.getInt(KEY_MODE)) {
182 case mode.CREATION_MODE:
183 Log.i(TAG, "CREATION");
184 AlertDialog dialog = createCancelDialog();
185 dialog.show();
186 break;
187 case mode.EDITION_MODE:
188 finish();
189 }
190
191 }
192
alisiond9e29442013-04-17 16:10:18 -0400193 @Override
194 public boolean onOptionsItemSelected(MenuItem item) {
195
196 switch (item.getItemId()) {
197 case R.id.menuitem_delete:
198 AlertDialog dialog = createDeleteDialog();
199 dialog.show();
200 break;
201 case R.id.menuitem_create:
202 processAccount(result.ACCOUNT_CREATED);
203 break;
204 case R.id.menuitem_edit:
205 processAccount(result.ACCOUNT_MODIFIED);
206 break;
207
208 }
209
210 return true;
211 }
212
213 private void processAccount(int resultCode) {
214 AlertDialog dialog;
215 ArrayList<String> missingValue = new ArrayList<String>();
216 if (validateAccountCreation(missingValue)) {
217
218 Bundle bundle = new Bundle();
219 bundle.putString("AccountID", mAccountID);
220 HashMap<String, String> accountDetails = new HashMap<String, String>();
221
222 updateAccountDetails(accountDetails, basicDetails);
223 updateAccountDetails(accountDetails, advancedDetails);
224 updateAccountDetails(accountDetails, srtpDetails);
225 updateAccountDetails(accountDetails, tlsDetails);
226
227 bundle.putSerializable(AccountDetail.TAG, accountDetails);
228 Intent resultIntent = new Intent();
229 resultIntent.putExtras(bundle);
230
231 setResult(resultCode, resultIntent);
232 finish();
233 } else {
234 dialog = createCouldNotValidateDialog(missingValue);
235 dialog.show();
236 }
237
238 }
239
240 public boolean validateAccountCreation(ArrayList<String> missingValue) {
241 boolean valid = true;
242
243 for (String s : requiredFields) {
244 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
245 Log.i(TAG, "Looking for " + s);
246 if (pref.getText().isEmpty()) {
247 Log.i(TAG, " INVALIDATED " + s + " " + pref.getText() + ";");
248 valid = false;
249 missingValue.add(pref.getTitle().toString());
250 }
251 }
252
253 return valid;
254 }
255
alisiond9e29442013-04-17 16:10:18 -0400256 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
257 for (AccountDetail.PreferenceEntry p : det.getDetailValues()) {
Alexandre Savard68838112012-10-30 11:34:43 -0400258 Preference pref = mPreferenceManager.findPreference(p.mKey);
alisiond9e29442013-04-17 16:10:18 -0400259 if (pref != null) {
260 if (p.isTwoState) {
261 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
262 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
263 } else {
alision7f18fc82013-05-01 09:37:33 -0400264 if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) {
265 ListPreference list = (ListPreference) pref;
266 accountDetails.put(p.mKey, list.getValue());
267 } else {
268 EditTextPreference textPref = (EditTextPreference) pref;
269 accountDetails.put(p.mKey, textPref.getText());
270 }
alisiond9e29442013-04-17 16:10:18 -0400271 }
272 }
273 }
274 }
275
276 Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() {
277 public boolean onPreferenceChange(Preference preference, Object newValue) {
278 preference.setSummary((CharSequence) newValue);
279 basicDetails.setDetailString(preference.getOrder(), ((CharSequence) newValue).toString());
280 return true;
281 }
282 };
alision7f18fc82013-05-01 09:37:33 -0400283
alisione2a38e12013-04-25 14:20:20 -0400284 Preference.OnPreferenceChangeListener changeAdvancedPreferenceListener = new Preference.OnPreferenceChangeListener() {
285 public boolean onPreferenceChange(Preference preference, Object newValue) {
286 preference.setSummary((CharSequence) newValue);
287 advancedDetails.setDetailString(preference.getOrder(), ((CharSequence) newValue).toString());
288 return true;
289 }
290 };
alision7f18fc82013-05-01 09:37:33 -0400291
alisione2a38e12013-04-25 14:20:20 -0400292 Preference.OnPreferenceChangeListener changeTlsPreferenceListener = new Preference.OnPreferenceChangeListener() {
293 public boolean onPreferenceChange(Preference preference, Object newValue) {
294 preference.setSummary((CharSequence) newValue);
295 tlsDetails.setDetailString(preference.getOrder(), ((CharSequence) newValue).toString());
296 return true;
297 }
298 };
alision7f18fc82013-05-01 09:37:33 -0400299
alisione2a38e12013-04-25 14:20:20 -0400300 Preference.OnPreferenceChangeListener changeSrtpPreferenceListener = new Preference.OnPreferenceChangeListener() {
301 public boolean onPreferenceChange(Preference preference, Object newValue) {
302 preference.setSummary((CharSequence) newValue);
303 srtpDetails.setDetailString(preference.getOrder(), ((CharSequence) newValue).toString());
304 return true;
305 }
306 };
alision7f18fc82013-05-01 09:37:33 -0400307
alisiond9e29442013-04-17 16:10:18 -0400308 private void setPreferenceDetails(AccountDetail details) {
309 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
alision7f18fc82013-05-01 09:37:33 -0400310 Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue);
alisiond9e29442013-04-17 16:10:18 -0400311 Preference pref = mPreferenceManager.findPreference(p.mKey);
312 if (pref != null) {
alision7f18fc82013-05-01 09:37:33 -0400313 if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) {
alision73424b62013-04-26 11:49:18 -0400314 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
315 try {
alision7f18fc82013-05-01 09:37:33 -0400316
alision73424b62013-04-26 11:49:18 -0400317 for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) {
318 NetworkInterface i = list.nextElement();
319 Log.e("network_interfaces", "display name " + i.getDisplayName());
alision7f18fc82013-05-01 09:37:33 -0400320 if (i.isUp())
alision73424b62013-04-26 11:49:18 -0400321 entries.add(i.getDisplayName());
322 }
323 } catch (SocketException e) {
324 Log.e(TAG, e.toString());
325 }
326 CharSequence[] display = new CharSequence[entries.size()];
327 entries.toArray(display);
328 ((ListPreference) pref).setEntries(display);
329 ((ListPreference) pref).setEntryValues(display);
330 pref.setSummary(p.mValue);
331 continue;
332 }
alisiond9e29442013-04-17 16:10:18 -0400333 if (!p.isTwoState) {
334 ((EditTextPreference) pref).setText(p.mValue);
335 pref.setSummary(p.mValue);
Alexandre Savard68838112012-10-30 11:34:43 -0400336 }
alision73424b62013-04-26 11:49:18 -0400337 } else {
alision7f18fc82013-05-01 09:37:33 -0400338 Log.w(TAG, "pref not found");
Alexandre Savard68838112012-10-30 11:34:43 -0400339 }
340 }
341 }
342
alisione2a38e12013-04-25 14:20:20 -0400343 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
alisiond9e29442013-04-17 16:10:18 -0400344 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
alision7f18fc82013-05-01 09:37:33 -0400345 Log.i(TAG, "addPreferenceListener: pref " + p.mKey);
Alexandre Savard68838112012-10-30 11:34:43 -0400346 Preference pref = mPreferenceManager.findPreference(p.mKey);
alisiond9e29442013-04-17 16:10:18 -0400347 if (pref != null) {
348 if (!p.isTwoState) {
alisione2a38e12013-04-25 14:20:20 -0400349 pref.setOnPreferenceChangeListener(listener);
Alexandre Savard68838112012-10-30 11:34:43 -0400350 }
alision73424b62013-04-26 11:49:18 -0400351 } else {
alision7f18fc82013-05-01 09:37:33 -0400352 Log.w(TAG, "addPreferenceListener: pref not found");
Alexandre Savard68838112012-10-30 11:34:43 -0400353 }
354 }
355 }
356
alisiond9e29442013-04-17 16:10:18 -0400357 /******************************************
358 *
359 * AlertDialogs
360 *
361 ******************************************/
alision7f18fc82013-05-01 09:37:33 -0400362
alisiond9e29442013-04-17 16:10:18 -0400363 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) {
364 String message = "The following parameters are missing:";
Alexandre Savard833616f2012-10-30 16:02:30 -0400365
alisiond9e29442013-04-17 16:10:18 -0400366 for (String s : missingValue)
367 message += "\n - " + s;
Alexandre Savard833616f2012-10-30 16:02:30 -0400368
alisiond9e29442013-04-17 16:10:18 -0400369 Activity ownerActivity = this;
370 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
371 builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
372 public void onClick(DialogInterface dialog, int whichButton) {
373 /* Nothing to be done */
374 }
375 });
376
377 AlertDialog alertDialog = builder.create();
378 return alertDialog;
Alexandre Savard833616f2012-10-30 16:02:30 -0400379 }
alision7f18fc82013-05-01 09:37:33 -0400380
alisiond9e29442013-04-17 16:10:18 -0400381 private AlertDialog createCancelDialog() {
382 Activity ownerActivity = this;
383 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
384 builder.setMessage("All parameters will be lost").setTitle("Account Creation").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
385 public void onClick(DialogInterface dialog, int whichButton) {
386 Activity activity = ((Dialog) dialog).getOwnerActivity();
387 activity.finish();
388 }
389 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
390 public void onClick(DialogInterface dialog, int whichButton) {
391 /* Terminate with no action */
392 }
393 });
Alexandre Savard833616f2012-10-30 16:02:30 -0400394
alisiond9e29442013-04-17 16:10:18 -0400395 AlertDialog alertDialog = builder.create();
396 alertDialog.setOwnerActivity(ownerActivity);
397
398 return alertDialog;
Alexandre Savard5195d922012-10-30 16:58:50 -0400399 }
alision7f18fc82013-05-01 09:37:33 -0400400
alisiond9e29442013-04-17 16:10:18 -0400401 private AlertDialog createDeleteDialog() {
Alexandre Savard645b29c2012-10-30 17:22:26 -0400402 Activity ownerActivity = this;
403 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
alision73424b62013-04-26 11:49:18 -0400404 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
alisiond9e29442013-04-17 16:10:18 -0400405 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
406 public void onClick(DialogInterface dialog, int whichButton) {
407 Bundle bundle = new Bundle();
408 bundle.putString("AccountID", mAccountID);
Alexandre Savard645b29c2012-10-30 17:22:26 -0400409
alisiond9e29442013-04-17 16:10:18 -0400410 Intent resultIntent = new Intent();
411 resultIntent.putExtras(bundle);
Alexandre Savard645b29c2012-10-30 17:22:26 -0400412
alisiond9e29442013-04-17 16:10:18 -0400413 Activity activity = ((Dialog) dialog).getOwnerActivity();
414 activity.setResult(result.ACCOUNT_DELETED, resultIntent);
415 activity.finish();
416 }
417 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
418 public void onClick(DialogInterface dialog, int whichButton) {
419 /* Terminate with no action */
420 }
421 });
Alexandre Savard645b29c2012-10-30 17:22:26 -0400422
423 AlertDialog alertDialog = builder.create();
424 alertDialog.setOwnerActivity(ownerActivity);
425
426 return alertDialog;
427 }
428
Alexandre Savard68838112012-10-30 11:34:43 -0400429}