blob: a11b80cc223f8c1be1f195d059cc8180b62703fb [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
alisiond9e29442013-04-17 16:10:18 -040034import java.util.ArrayList;
35import java.util.HashMap;
36
Alexandre Savard68838112012-10-30 11:34:43 -040037import android.app.Activity;
Alexandre Savard645b29c2012-10-30 17:22:26 -040038import android.app.AlertDialog;
39import android.app.Dialog;
Alexandre Savard645b29c2012-10-30 17:22:26 -040040import android.content.DialogInterface;
Alexandre Savard68838112012-10-30 11:34:43 -040041import android.content.Intent;
42import android.os.Bundle;
alisiond9e29442013-04-17 16:10:18 -040043import android.preference.CheckBoxPreference;
44import android.preference.EditTextPreference;
Alexandre Savard68838112012-10-30 11:34:43 -040045import android.preference.Preference;
46import android.preference.PreferenceActivity;
47import android.preference.PreferenceManager;
Alexandre Savard68838112012-10-30 11:34:43 -040048import android.util.Log;
Alexandre Savard5195d922012-10-30 16:58:50 -040049import android.view.Menu;
alisiond9e29442013-04-17 16:10:18 -040050import android.view.MenuInflater;
Alexandre Savard5195d922012-10-30 16:58:50 -040051import android.view.MenuItem;
Alexandre Savard68838112012-10-30 11:34:43 -040052
53import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040054import com.savoirfairelinux.sflphone.account.AccountDetail;
55import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced;
56import com.savoirfairelinux.sflphone.account.AccountDetailBasic;
57import com.savoirfairelinux.sflphone.account.AccountDetailSrtp;
58import com.savoirfairelinux.sflphone.account.AccountDetailTls;
Alexandre Savard68838112012-10-30 11:34:43 -040059
alisiond9e29442013-04-17 16:10:18 -040060public class AccountPreferenceActivity extends PreferenceActivity {
Alexandre Savard68838112012-10-30 11:34:43 -040061 private static final String TAG = "AccoutPreferenceActivity";
alisiond9e29442013-04-17 16:10:18 -040062
63 public static final String KEY_MODE = "mode";
64
65 public interface mode {
66 static final int CREATION_MODE = 0;
67 static final int EDITION_MODE = 1;
68 }
69
70 public interface result {
71 static final int ACCOUNT_CREATED = Activity.RESULT_FIRST_USER + 0;
72 static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 1;
73 static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2;
74 }
Alexandre Savard68838112012-10-30 11:34:43 -040075
Alexandre Savard833616f2012-10-30 16:02:30 -040076 private AccountDetailBasic basicDetails = null;
77 private AccountDetailAdvanced advancedDetails = null;
78 private AccountDetailSrtp srtpDetails = null;
79 private AccountDetailTls tlsDetails = null;
Alexandre Savard68838112012-10-30 11:34:43 -040080 private PreferenceManager mPreferenceManager;
Alexandre Savard68838112012-10-30 11:34:43 -040081 private String mAccountID;
alisiond9e29442013-04-17 16:10:18 -040082 private ArrayList<String> requiredFields = null;
Alexandre Savard68838112012-10-30 11:34:43 -040083
alisiond9e29442013-04-17 16:10:18 -040084
85 @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
alisiond9e29442013-04-17 16:10:18 -040091 Bundle b = getIntent().getExtras();
92
93 switch (b.getInt(KEY_MODE)) {
94 case mode.CREATION_MODE:
95 Log.i(TAG, "CREATION");
96 initCreation();
97 break;
98 case mode.EDITION_MODE:
99 Log.i(TAG, "ESDITION");
100 initEdition();
101 break;
102 }
103
104 requiredFields = new ArrayList<String>();
105 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
106 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
107 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
108 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
109
110 }
111
112 private void initCreation() {
113 basicDetails = new AccountDetailBasic();
114 advancedDetails = new AccountDetailAdvanced();
115 srtpDetails = new AccountDetailSrtp();
116 tlsDetails = new AccountDetailTls();
117
118 addPreferenceListener(basicDetails);
119 addPreferenceListener(advancedDetails);
120 addPreferenceListener(srtpDetails);
121 addPreferenceListener(tlsDetails);
122
123 }
124
125 private void initEdition() {
Alexandre Savard68838112012-10-30 11:34:43 -0400126
127 Bundle b = getIntent().getExtras();
128 mAccountID = b.getString("AccountID");
129 ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG);
130 ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG);
131 ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG);
132 ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG);
133
alisiond9e29442013-04-17 16:10:18 -0400134 basicDetails = new AccountDetailBasic(basicPreferenceList);
Alexandre Savard68838112012-10-30 11:34:43 -0400135 advancedDetails = new AccountDetailAdvanced(advancedPreferenceList);
136 srtpDetails = new AccountDetailSrtp(srtpPreferenceList);
137 tlsDetails = new AccountDetailTls(tlsPreferenceList);
138
139 setPreferenceDetails(basicDetails);
Alexandre Savard833616f2012-10-30 16:02:30 -0400140 setPreferenceDetails(advancedDetails);
141 setPreferenceDetails(srtpDetails);
142 setPreferenceDetails(tlsDetails);
143
144 addPreferenceListener(basicDetails);
145 addPreferenceListener(advancedDetails);
146 addPreferenceListener(srtpDetails);
147 addPreferenceListener(tlsDetails);
Alexandre Savard68838112012-10-30 11:34:43 -0400148 }
149
alisiond9e29442013-04-17 16:10:18 -0400150 @Override
151 public boolean onCreateOptionsMenu(Menu menu) {
152 MenuInflater inflater = getMenuInflater();
153
154 Bundle b = getIntent().getExtras();
155
156 switch (b.getInt(KEY_MODE)) {
157 case mode.CREATION_MODE:
158 Log.i(TAG, "CREATION");
159 inflater.inflate(R.menu.account_creation, menu);
160 break;
161 case mode.EDITION_MODE:
162 Log.i(TAG, "onCreateOptionsMenu: " + mAccountID);
163
164 if (mAccountID.equals("IP2IP"))
165 return true;
166
167 inflater.inflate(R.menu.account_edition, menu);
168 break;
169 }
170
171 return true;
172 }
173
174 @Override
175 public void onBackPressed() {
176 Bundle b = getIntent().getExtras();
177 switch (b.getInt(KEY_MODE)) {
178 case mode.CREATION_MODE:
179 Log.i(TAG, "CREATION");
180 AlertDialog dialog = createCancelDialog();
181 dialog.show();
182 break;
183 case mode.EDITION_MODE:
184 finish();
185 }
186
187 }
188
189
190 @Override
191 public boolean onOptionsItemSelected(MenuItem item) {
192
193 switch (item.getItemId()) {
194 case R.id.menuitem_delete:
195 AlertDialog dialog = createDeleteDialog();
196 dialog.show();
197 break;
198 case R.id.menuitem_create:
199 processAccount(result.ACCOUNT_CREATED);
200 break;
201 case R.id.menuitem_edit:
202 processAccount(result.ACCOUNT_MODIFIED);
203 break;
204
205 }
206
207 return true;
208 }
209
210 private void processAccount(int resultCode) {
211 AlertDialog dialog;
212 ArrayList<String> missingValue = new ArrayList<String>();
213 if (validateAccountCreation(missingValue)) {
214
215 Bundle bundle = new Bundle();
216 bundle.putString("AccountID", mAccountID);
217 HashMap<String, String> accountDetails = new HashMap<String, String>();
218
219 updateAccountDetails(accountDetails, basicDetails);
220 updateAccountDetails(accountDetails, advancedDetails);
221 updateAccountDetails(accountDetails, srtpDetails);
222 updateAccountDetails(accountDetails, tlsDetails);
223
224 bundle.putSerializable(AccountDetail.TAG, accountDetails);
225 Intent resultIntent = new Intent();
226 resultIntent.putExtras(bundle);
227
228 setResult(resultCode, resultIntent);
229 finish();
230 } else {
231 dialog = createCouldNotValidateDialog(missingValue);
232 dialog.show();
233 }
234
235 }
236
237 public boolean validateAccountCreation(ArrayList<String> missingValue) {
238 boolean valid = true;
239
240 for (String s : requiredFields) {
241 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
242 Log.i(TAG, "Looking for " + s);
243 if (pref.getText().isEmpty()) {
244 Log.i(TAG, " INVALIDATED " + s + " " + pref.getText() + ";");
245 valid = false;
246 missingValue.add(pref.getTitle().toString());
247 }
248 }
249
250 return valid;
251 }
252
253
254 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
255 for (AccountDetail.PreferenceEntry p : det.getDetailValues()) {
Alexandre Savard68838112012-10-30 11:34:43 -0400256 Preference pref = mPreferenceManager.findPreference(p.mKey);
alisiond9e29442013-04-17 16:10:18 -0400257 if (pref != null) {
258 if (p.isTwoState) {
259 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
260 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
261 } else {
262 EditTextPreference textPref = (EditTextPreference) pref;
263 accountDetails.put(p.mKey, textPref.getText());
264 }
265 }
266 }
267 }
268
269 Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() {
270 public boolean onPreferenceChange(Preference preference, Object newValue) {
271 preference.setSummary((CharSequence) newValue);
272 basicDetails.setDetailString(preference.getOrder(), ((CharSequence) newValue).toString());
273 return true;
274 }
275 };
276
277 private void setPreferenceDetails(AccountDetail details) {
278 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
279 Preference pref = mPreferenceManager.findPreference(p.mKey);
280 if (pref != null) {
281 if (!p.isTwoState) {
282 ((EditTextPreference) pref).setText(p.mValue);
283 pref.setSummary(p.mValue);
Alexandre Savard68838112012-10-30 11:34:43 -0400284 }
285 }
286 }
287 }
288
289 private void addPreferenceListener(AccountDetail details) {
alisiond9e29442013-04-17 16:10:18 -0400290 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
Alexandre Savard68838112012-10-30 11:34:43 -0400291 Preference pref = mPreferenceManager.findPreference(p.mKey);
alisiond9e29442013-04-17 16:10:18 -0400292 if (pref != null) {
293 if (!p.isTwoState) {
Alexandre Savard833616f2012-10-30 16:02:30 -0400294 pref.setOnPreferenceChangeListener(changeBasicPreferenceListener);
Alexandre Savard68838112012-10-30 11:34:43 -0400295 }
296 }
297 }
298 }
299
alisiond9e29442013-04-17 16:10:18 -0400300
301
302
303
304 /******************************************
305 *
306 * AlertDialogs
307 *
308 ******************************************/
309
310 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) {
311 String message = "The following parameters are missing:";
Alexandre Savard833616f2012-10-30 16:02:30 -0400312
alisiond9e29442013-04-17 16:10:18 -0400313 for (String s : missingValue)
314 message += "\n - " + s;
Alexandre Savard833616f2012-10-30 16:02:30 -0400315
alisiond9e29442013-04-17 16:10:18 -0400316 Activity ownerActivity = this;
317 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
318 builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
319 public void onClick(DialogInterface dialog, int whichButton) {
320 /* Nothing to be done */
321 }
322 });
323
324 AlertDialog alertDialog = builder.create();
325 return alertDialog;
Alexandre Savard833616f2012-10-30 16:02:30 -0400326 }
alisiond9e29442013-04-17 16:10:18 -0400327
328 private AlertDialog createCancelDialog() {
329 Activity ownerActivity = this;
330 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
331 builder.setMessage("All parameters will be lost").setTitle("Account Creation").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
332 public void onClick(DialogInterface dialog, int whichButton) {
333 Activity activity = ((Dialog) dialog).getOwnerActivity();
334 activity.finish();
335 }
336 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
337 public void onClick(DialogInterface dialog, int whichButton) {
338 /* Terminate with no action */
339 }
340 });
Alexandre Savard833616f2012-10-30 16:02:30 -0400341
alisiond9e29442013-04-17 16:10:18 -0400342 AlertDialog alertDialog = builder.create();
343 alertDialog.setOwnerActivity(ownerActivity);
344
345 return alertDialog;
Alexandre Savard5195d922012-10-30 16:58:50 -0400346 }
alisiond9e29442013-04-17 16:10:18 -0400347
348 private AlertDialog createDeleteDialog() {
Alexandre Savard645b29c2012-10-30 17:22:26 -0400349 Activity ownerActivity = this;
350 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
351 builder.setMessage("Do you realy want to delete this account").setTitle("Delete Account")
alisiond9e29442013-04-17 16:10:18 -0400352 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
353 public void onClick(DialogInterface dialog, int whichButton) {
354 Bundle bundle = new Bundle();
355 bundle.putString("AccountID", mAccountID);
Alexandre Savard645b29c2012-10-30 17:22:26 -0400356
alisiond9e29442013-04-17 16:10:18 -0400357 Intent resultIntent = new Intent();
358 resultIntent.putExtras(bundle);
Alexandre Savard645b29c2012-10-30 17:22:26 -0400359
alisiond9e29442013-04-17 16:10:18 -0400360 Activity activity = ((Dialog) dialog).getOwnerActivity();
361 activity.setResult(result.ACCOUNT_DELETED, resultIntent);
362 activity.finish();
363 }
364 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
365 public void onClick(DialogInterface dialog, int whichButton) {
366 /* Terminate with no action */
367 }
368 });
Alexandre Savard645b29c2012-10-30 17:22:26 -0400369
370 AlertDialog alertDialog = builder.create();
371 alertDialog.setOwnerActivity(ownerActivity);
372
373 return alertDialog;
374 }
375
Alexandre Savard68838112012-10-30 11:34:43 -0400376}