Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | package com.savoirfairelinux.sflphone.client; |
| 33 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 37 | import android.app.Activity; |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 38 | import android.app.AlertDialog; |
| 39 | import android.app.Dialog; |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 40 | import android.content.DialogInterface; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 41 | import android.content.Intent; |
| 42 | import android.os.Bundle; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 43 | import android.preference.CheckBoxPreference; |
| 44 | import android.preference.EditTextPreference; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 45 | import android.preference.Preference; |
| 46 | import android.preference.PreferenceActivity; |
| 47 | import android.preference.PreferenceManager; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 48 | import android.util.Log; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 49 | import android.view.Menu; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 50 | import android.view.MenuInflater; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 51 | import android.view.MenuItem; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 52 | |
| 53 | import com.savoirfairelinux.sflphone.R; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 54 | import com.savoirfairelinux.sflphone.account.AccountDetail; |
| 55 | import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced; |
| 56 | import com.savoirfairelinux.sflphone.account.AccountDetailBasic; |
| 57 | import com.savoirfairelinux.sflphone.account.AccountDetailSrtp; |
| 58 | import com.savoirfairelinux.sflphone.account.AccountDetailTls; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 59 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 60 | public class AccountPreferenceActivity extends PreferenceActivity { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 61 | private static final String TAG = "AccoutPreferenceActivity"; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 62 | |
| 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 Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 75 | |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 76 | private AccountDetailBasic basicDetails = null; |
| 77 | private AccountDetailAdvanced advancedDetails = null; |
| 78 | private AccountDetailSrtp srtpDetails = null; |
| 79 | private AccountDetailTls tlsDetails = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 80 | private PreferenceManager mPreferenceManager; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 81 | private String mAccountID; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 82 | private ArrayList<String> requiredFields = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 83 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | protected void onCreate(Bundle savedInstanceState) { |
| 87 | super.onCreate(savedInstanceState); |
| 88 | addPreferencesFromResource(R.xml.account_creation_preferences); |
| 89 | mPreferenceManager = getPreferenceManager(); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 90 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 91 | 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 Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 126 | |
| 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 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 134 | basicDetails = new AccountDetailBasic(basicPreferenceList); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 135 | advancedDetails = new AccountDetailAdvanced(advancedPreferenceList); |
| 136 | srtpDetails = new AccountDetailSrtp(srtpPreferenceList); |
| 137 | tlsDetails = new AccountDetailTls(tlsPreferenceList); |
| 138 | |
| 139 | setPreferenceDetails(basicDetails); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 140 | setPreferenceDetails(advancedDetails); |
| 141 | setPreferenceDetails(srtpDetails); |
| 142 | setPreferenceDetails(tlsDetails); |
| 143 | |
| 144 | addPreferenceListener(basicDetails); |
| 145 | addPreferenceListener(advancedDetails); |
| 146 | addPreferenceListener(srtpDetails); |
| 147 | addPreferenceListener(tlsDetails); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 148 | } |
| 149 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 150 | @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 Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 256 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 257 | 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 Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private void addPreferenceListener(AccountDetail details) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 290 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 291 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 292 | if (pref != null) { |
| 293 | if (!p.isTwoState) { |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 294 | pref.setOnPreferenceChangeListener(changeBasicPreferenceListener); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 300 | |
| 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 312 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 313 | for (String s : missingValue) |
| 314 | message += "\n - " + s; |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 315 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 316 | 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 326 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 327 | |
| 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 341 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 342 | AlertDialog alertDialog = builder.create(); |
| 343 | alertDialog.setOwnerActivity(ownerActivity); |
| 344 | |
| 345 | return alertDialog; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 346 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 347 | |
| 348 | private AlertDialog createDeleteDialog() { |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 349 | 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") |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 352 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 353 | public void onClick(DialogInterface dialog, int whichButton) { |
| 354 | Bundle bundle = new Bundle(); |
| 355 | bundle.putString("AccountID", mAccountID); |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 356 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 357 | Intent resultIntent = new Intent(); |
| 358 | resultIntent.putExtras(bundle); |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 359 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 360 | 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 Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 369 | |
| 370 | AlertDialog alertDialog = builder.create(); |
| 371 | alertDialog.setOwnerActivity(ownerActivity); |
| 372 | |
| 373 | return alertDialog; |
| 374 | } |
| 375 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 376 | } |