blob: 6813e5372946dff90652a936d15f149444c851d1 [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;
alision5de91782013-07-10 10:47:30 -040057import android.widget.Toast;
Alexandre Savard68838112012-10-30 11:34:43 -040058
59import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040060import com.savoirfairelinux.sflphone.account.AccountDetail;
61import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced;
62import com.savoirfairelinux.sflphone.account.AccountDetailBasic;
63import com.savoirfairelinux.sflphone.account.AccountDetailSrtp;
64import com.savoirfairelinux.sflphone.account.AccountDetailTls;
alision5cfc35d2013-07-11 15:11:39 -040065import com.savoirfairelinux.sflphone.service.ServiceConstants;
Alexandre Savard68838112012-10-30 11:34:43 -040066
alisiond9e29442013-04-17 16:10:18 -040067public class AccountPreferenceActivity extends PreferenceActivity {
Alexandre Savard68838112012-10-30 11:34:43 -040068 private static final String TAG = "AccoutPreferenceActivity";
alisiond9e29442013-04-17 16:10:18 -040069
70 public static final String KEY_MODE = "mode";
71
alisiond9e29442013-04-17 16:10:18 -040072 public interface result {
alisiond9e29442013-04-17 16:10:18 -040073 static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 1;
74 static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2;
75 }
Alexandre Savard68838112012-10-30 11:34:43 -040076
Alexandre Savard833616f2012-10-30 16:02:30 -040077 private AccountDetailBasic basicDetails = null;
78 private AccountDetailAdvanced advancedDetails = null;
79 private AccountDetailSrtp srtpDetails = null;
80 private AccountDetailTls tlsDetails = null;
Alexandre Savard68838112012-10-30 11:34:43 -040081 private PreferenceManager mPreferenceManager;
Alexandre Savard68838112012-10-30 11:34:43 -040082 private String mAccountID;
alisiond9e29442013-04-17 16:10:18 -040083 private ArrayList<String> requiredFields = null;
Alexandre Savard68838112012-10-30 11:34:43 -040084
alisiond9e29442013-04-17 16:10:18 -040085 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 addPreferencesFromResource(R.xml.account_creation_preferences);
89 mPreferenceManager = getPreferenceManager();
Alexandre Savard68838112012-10-30 11:34:43 -040090
alision5cfc35d2013-07-11 15:11:39 -040091 initEdition();
alisiond9e29442013-04-17 16:10:18 -040092
alisioncc7bb422013-06-06 15:31:39 -040093 getActionBar().setDisplayHomeAsUpEnabled(true);
Alexandre Lisiona764c682013-09-09 10:02:07 -040094
alision5de91782013-07-10 10:47:30 -040095
alisiond9e29442013-04-17 16:10:18 -040096 requiredFields = new ArrayList<String>();
97 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
98 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
99 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
100 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
101
102 }
103
alisiond9e29442013-04-17 16:10:18 -0400104 private void initEdition() {
Alexandre Savard68838112012-10-30 11:34:43 -0400105
106 Bundle b = getIntent().getExtras();
107 mAccountID = b.getString("AccountID");
108 ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG);
109 ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG);
110 ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG);
111 ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG);
alision7f18fc82013-05-01 09:37:33 -0400112
alisiond9e29442013-04-17 16:10:18 -0400113 basicDetails = new AccountDetailBasic(basicPreferenceList);
Alexandre Savard68838112012-10-30 11:34:43 -0400114 advancedDetails = new AccountDetailAdvanced(advancedPreferenceList);
115 srtpDetails = new AccountDetailSrtp(srtpPreferenceList);
116 tlsDetails = new AccountDetailTls(tlsPreferenceList);
117
118 setPreferenceDetails(basicDetails);
Alexandre Savard833616f2012-10-30 16:02:30 -0400119 setPreferenceDetails(advancedDetails);
120 setPreferenceDetails(srtpDetails);
121 setPreferenceDetails(tlsDetails);
122
alisione2a38e12013-04-25 14:20:20 -0400123 addPreferenceListener(basicDetails, changeBasicPreferenceListener);
alision5de91782013-07-10 10:47:30 -0400124 // addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener);
125 // addPreferenceListener(srtpDetails, changeSrtpPreferenceListener);
126 // addPreferenceListener(tlsDetails, changeTlsPreferenceListener);
Alexandre Savard68838112012-10-30 11:34:43 -0400127 }
128
alisiond9e29442013-04-17 16:10:18 -0400129 @Override
130 public boolean onCreateOptionsMenu(Menu menu) {
131 MenuInflater inflater = getMenuInflater();
alision5cfc35d2013-07-11 15:11:39 -0400132 inflater.inflate(R.menu.account_edition, menu);
alisiond9e29442013-04-17 16:10:18 -0400133 return true;
134 }
135
136 @Override
137 public void onBackPressed() {
alision5cfc35d2013-07-11 15:11:39 -0400138
139 AlertDialog dialog = createCancelDialog();
140 dialog.show();
alisiond9e29442013-04-17 16:10:18 -0400141
142 }
143
alisiond9e29442013-04-17 16:10:18 -0400144 @Override
145 public boolean onOptionsItemSelected(MenuItem item) {
146
147 switch (item.getItemId()) {
alisioncc7bb422013-06-06 15:31:39 -0400148 case android.R.id.home:
149 finish();
150 return true;
alisiond9e29442013-04-17 16:10:18 -0400151 case R.id.menuitem_delete:
152 AlertDialog dialog = createDeleteDialog();
153 dialog.show();
154 break;
alisiond9e29442013-04-17 16:10:18 -0400155 case R.id.menuitem_edit:
156 processAccount(result.ACCOUNT_MODIFIED);
157 break;
158
159 }
160
161 return true;
162 }
163
164 private void processAccount(int resultCode) {
165 AlertDialog dialog;
166 ArrayList<String> missingValue = new ArrayList<String>();
167 if (validateAccountCreation(missingValue)) {
168
169 Bundle bundle = new Bundle();
170 bundle.putString("AccountID", mAccountID);
171 HashMap<String, String> accountDetails = new HashMap<String, String>();
172
173 updateAccountDetails(accountDetails, basicDetails);
174 updateAccountDetails(accountDetails, advancedDetails);
175 updateAccountDetails(accountDetails, srtpDetails);
176 updateAccountDetails(accountDetails, tlsDetails);
alision5cfc35d2013-07-11 15:11:39 -0400177
alision5de91782013-07-10 10:47:30 -0400178 accountDetails.put("Account.type", "SIP");
alisiond9e29442013-04-17 16:10:18 -0400179 bundle.putSerializable(AccountDetail.TAG, accountDetails);
180 Intent resultIntent = new Intent();
181 resultIntent.putExtras(bundle);
182
183 setResult(resultCode, resultIntent);
184 finish();
185 } else {
186 dialog = createCouldNotValidateDialog(missingValue);
187 dialog.show();
188 }
189
190 }
191
192 public boolean validateAccountCreation(ArrayList<String> missingValue) {
193 boolean valid = true;
194
195 for (String s : requiredFields) {
196 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
197 Log.i(TAG, "Looking for " + s);
alisioncc7bb422013-06-06 15:31:39 -0400198 Log.i(TAG, "Value " + pref.getText());
alisiond9e29442013-04-17 16:10:18 -0400199 if (pref.getText().isEmpty()) {
alisiond9e29442013-04-17 16:10:18 -0400200 valid = false;
201 missingValue.add(pref.getTitle().toString());
202 }
203 }
204
205 return valid;
206 }
207
alisiond9e29442013-04-17 16:10:18 -0400208 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
209 for (AccountDetail.PreferenceEntry p : det.getDetailValues()) {
alision5de91782013-07-10 10:47:30 -0400210
211 Log.i(TAG, "updateAccountDetails: pref " + p.mKey + " value " + det.getDetailString(p.mKey));
212 if (p.isTwoState) {
213 accountDetails.put(p.mKey, det.getDetailString(p.mKey));
214 } else {
alision5cfc35d2013-07-11 15:11:39 -0400215 // if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) {
216 // accountDetails.put(p.mKey, det.getDetailString(p.mKey));
217 // } else {
218 accountDetails.put(p.mKey, det.getDetailString(p.mKey));
219 // }
alisiond9e29442013-04-17 16:10:18 -0400220 }
alision5de91782013-07-10 10:47:30 -0400221
222 // Preference pref = mPreferenceManager.findPreference(p.mKey);
223 // if (pref != null) {
224 // if (p.isTwoState) {
225 // CheckBoxPreference boxPref = (CheckBoxPreference) pref;
226 // accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
227 // } else {
228 // if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) {
229 // ListPreference list = (ListPreference) pref;
230 // accountDetails.put(p.mKey, list.getValue());
231 // } else {
232 // EditTextPreference textPref = (EditTextPreference) pref;
233 // accountDetails.put(p.mKey, textPref.getText());
234 // }
235 // }
236 // }
alisiond9e29442013-04-17 16:10:18 -0400237 }
238 }
239
240 Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() {
241 public boolean onPreferenceChange(Preference preference, Object newValue) {
alision5de91782013-07-10 10:47:30 -0400242 if (preference instanceof CheckBoxPreference) {
243 if ((Boolean) newValue == true)
244 basicDetails.setDetailString(preference.getKey(), ((Boolean) newValue).toString());
245 } else {
246 preference.setSummary((CharSequence) newValue);
247 Log.i(TAG, "Changing preference value:" + (CharSequence) newValue);
248 basicDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString());
249 }
alisiond9e29442013-04-17 16:10:18 -0400250 return true;
251 }
252 };
alision7f18fc82013-05-01 09:37:33 -0400253
alisione2a38e12013-04-25 14:20:20 -0400254 Preference.OnPreferenceChangeListener changeAdvancedPreferenceListener = new Preference.OnPreferenceChangeListener() {
255 public boolean onPreferenceChange(Preference preference, Object newValue) {
256 preference.setSummary((CharSequence) newValue);
alision5de91782013-07-10 10:47:30 -0400257 advancedDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString());
alisione2a38e12013-04-25 14:20:20 -0400258 return true;
259 }
260 };
alision7f18fc82013-05-01 09:37:33 -0400261
alisione2a38e12013-04-25 14:20:20 -0400262 Preference.OnPreferenceChangeListener changeTlsPreferenceListener = new Preference.OnPreferenceChangeListener() {
263 public boolean onPreferenceChange(Preference preference, Object newValue) {
264 preference.setSummary((CharSequence) newValue);
alision5de91782013-07-10 10:47:30 -0400265 tlsDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString());
alisione2a38e12013-04-25 14:20:20 -0400266 return true;
267 }
268 };
alision7f18fc82013-05-01 09:37:33 -0400269
alisione2a38e12013-04-25 14:20:20 -0400270 Preference.OnPreferenceChangeListener changeSrtpPreferenceListener = new Preference.OnPreferenceChangeListener() {
271 public boolean onPreferenceChange(Preference preference, Object newValue) {
272 preference.setSummary((CharSequence) newValue);
alision5de91782013-07-10 10:47:30 -0400273 srtpDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString());
alisione2a38e12013-04-25 14:20:20 -0400274 return true;
275 }
276 };
alision7f18fc82013-05-01 09:37:33 -0400277
alisiond9e29442013-04-17 16:10:18 -0400278 private void setPreferenceDetails(AccountDetail details) {
279 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
alision7f18fc82013-05-01 09:37:33 -0400280 Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue);
alisiond9e29442013-04-17 16:10:18 -0400281 Preference pref = mPreferenceManager.findPreference(p.mKey);
282 if (pref != null) {
alision7f18fc82013-05-01 09:37:33 -0400283 if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) {
alision73424b62013-04-26 11:49:18 -0400284 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
285 try {
alision7f18fc82013-05-01 09:37:33 -0400286
alision73424b62013-04-26 11:49:18 -0400287 for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) {
288 NetworkInterface i = list.nextElement();
289 Log.e("network_interfaces", "display name " + i.getDisplayName());
alision7f18fc82013-05-01 09:37:33 -0400290 if (i.isUp())
alision73424b62013-04-26 11:49:18 -0400291 entries.add(i.getDisplayName());
292 }
293 } catch (SocketException e) {
294 Log.e(TAG, e.toString());
295 }
296 CharSequence[] display = new CharSequence[entries.size()];
297 entries.toArray(display);
298 ((ListPreference) pref).setEntries(display);
299 ((ListPreference) pref).setEntryValues(display);
300 pref.setSummary(p.mValue);
301 continue;
302 }
alisiond9e29442013-04-17 16:10:18 -0400303 if (!p.isTwoState) {
304 ((EditTextPreference) pref).setText(p.mValue);
305 pref.setSummary(p.mValue);
Alexandre Savard68838112012-10-30 11:34:43 -0400306 }
alision73424b62013-04-26 11:49:18 -0400307 } else {
alision7f18fc82013-05-01 09:37:33 -0400308 Log.w(TAG, "pref not found");
Alexandre Savard68838112012-10-30 11:34:43 -0400309 }
310 }
311 }
312
alisione2a38e12013-04-25 14:20:20 -0400313 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
alisiond9e29442013-04-17 16:10:18 -0400314 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
alision5de91782013-07-10 10:47:30 -0400315 Log.i(TAG, "addPreferenceListener: pref " + p.mKey + p.mValue);
Alexandre Savard68838112012-10-30 11:34:43 -0400316 Preference pref = mPreferenceManager.findPreference(p.mKey);
alisiond9e29442013-04-17 16:10:18 -0400317 if (pref != null) {
alision5de91782013-07-10 10:47:30 -0400318
319 pref.setOnPreferenceChangeListener(listener);
320
alision73424b62013-04-26 11:49:18 -0400321 } else {
alision7f18fc82013-05-01 09:37:33 -0400322 Log.w(TAG, "addPreferenceListener: pref not found");
Alexandre Savard68838112012-10-30 11:34:43 -0400323 }
324 }
325 }
326
alisiond9e29442013-04-17 16:10:18 -0400327 /******************************************
328 *
329 * AlertDialogs
330 *
331 ******************************************/
alision7f18fc82013-05-01 09:37:33 -0400332
alisiond9e29442013-04-17 16:10:18 -0400333 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) {
334 String message = "The following parameters are missing:";
Alexandre Savard833616f2012-10-30 16:02:30 -0400335
alisiond9e29442013-04-17 16:10:18 -0400336 for (String s : missingValue)
337 message += "\n - " + s;
Alexandre Savard833616f2012-10-30 16:02:30 -0400338
alisiond9e29442013-04-17 16:10:18 -0400339 Activity ownerActivity = this;
340 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
341 builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
342 public void onClick(DialogInterface dialog, int whichButton) {
343 /* Nothing to be done */
344 }
345 });
346
347 AlertDialog alertDialog = builder.create();
348 return alertDialog;
Alexandre Savard833616f2012-10-30 16:02:30 -0400349 }
alision7f18fc82013-05-01 09:37:33 -0400350
alisiond9e29442013-04-17 16:10:18 -0400351 private AlertDialog createCancelDialog() {
352 Activity ownerActivity = this;
353 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
Alexandre Lisiona764c682013-09-09 10:02:07 -0400354 builder.setMessage("Modifications will be lost").setTitle("Account Edition").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
alisiond9e29442013-04-17 16:10:18 -0400355 public void onClick(DialogInterface dialog, int whichButton) {
356 Activity activity = ((Dialog) dialog).getOwnerActivity();
357 activity.finish();
358 }
359 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
360 public void onClick(DialogInterface dialog, int whichButton) {
361 /* Terminate with no action */
362 }
363 });
Alexandre Savard833616f2012-10-30 16:02:30 -0400364
alisiond9e29442013-04-17 16:10:18 -0400365 AlertDialog alertDialog = builder.create();
366 alertDialog.setOwnerActivity(ownerActivity);
367
368 return alertDialog;
Alexandre Savard5195d922012-10-30 16:58:50 -0400369 }
alision7f18fc82013-05-01 09:37:33 -0400370
alisiond9e29442013-04-17 16:10:18 -0400371 private AlertDialog createDeleteDialog() {
Alexandre Savard645b29c2012-10-30 17:22:26 -0400372 Activity ownerActivity = this;
373 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
alision73424b62013-04-26 11:49:18 -0400374 builder.setMessage("Do you really want to delete this account").setTitle("Delete Account")
alisiond9e29442013-04-17 16:10:18 -0400375 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
376 public void onClick(DialogInterface dialog, int whichButton) {
377 Bundle bundle = new Bundle();
378 bundle.putString("AccountID", mAccountID);
Alexandre Savard645b29c2012-10-30 17:22:26 -0400379
alisiond9e29442013-04-17 16:10:18 -0400380 Intent resultIntent = new Intent();
381 resultIntent.putExtras(bundle);
Alexandre Savard645b29c2012-10-30 17:22:26 -0400382
alisiond9e29442013-04-17 16:10:18 -0400383 Activity activity = ((Dialog) dialog).getOwnerActivity();
384 activity.setResult(result.ACCOUNT_DELETED, resultIntent);
385 activity.finish();
386 }
387 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
388 public void onClick(DialogInterface dialog, int whichButton) {
389 /* Terminate with no action */
390 }
391 });
Alexandre Savard645b29c2012-10-30 17:22:26 -0400392
393 AlertDialog alertDialog = builder.create();
394 alertDialog.setOwnerActivity(ownerActivity);
395
396 return alertDialog;
397 }
398
Alexandre Savard68838112012-10-30 11:34:43 -0400399}