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 | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 34 | import java.net.NetworkInterface; |
| 35 | import java.net.SocketException; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 36 | import java.util.ArrayList; |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 37 | import java.util.Enumeration; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 38 | import java.util.HashMap; |
| 39 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 40 | import android.app.Activity; |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 41 | import android.app.AlertDialog; |
| 42 | import android.app.Dialog; |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 43 | import android.content.DialogInterface; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 44 | import android.content.Intent; |
| 45 | import android.os.Bundle; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 46 | import android.preference.CheckBoxPreference; |
| 47 | import android.preference.EditTextPreference; |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 48 | import android.preference.ListPreference; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 49 | import android.preference.Preference; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 50 | import android.preference.Preference.OnPreferenceChangeListener; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 51 | import android.preference.PreferenceActivity; |
| 52 | import android.preference.PreferenceManager; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 53 | import android.util.Log; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 54 | import android.view.Menu; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 55 | import android.view.MenuInflater; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 56 | import android.view.MenuItem; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 57 | |
| 58 | import com.savoirfairelinux.sflphone.R; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 59 | import com.savoirfairelinux.sflphone.account.AccountDetail; |
| 60 | import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced; |
| 61 | import com.savoirfairelinux.sflphone.account.AccountDetailBasic; |
| 62 | import com.savoirfairelinux.sflphone.account.AccountDetailSrtp; |
| 63 | import com.savoirfairelinux.sflphone.account.AccountDetailTls; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 64 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 65 | public class AccountPreferenceActivity extends PreferenceActivity { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 66 | private static final String TAG = "AccoutPreferenceActivity"; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 67 | |
| 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 Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 80 | |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 81 | private AccountDetailBasic basicDetails = null; |
| 82 | private AccountDetailAdvanced advancedDetails = null; |
| 83 | private AccountDetailSrtp srtpDetails = null; |
| 84 | private AccountDetailTls tlsDetails = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 85 | private PreferenceManager mPreferenceManager; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 86 | private String mAccountID; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 87 | private ArrayList<String> requiredFields = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 88 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 89 | @Override |
| 90 | protected void onCreate(Bundle savedInstanceState) { |
| 91 | super.onCreate(savedInstanceState); |
| 92 | addPreferencesFromResource(R.xml.account_creation_preferences); |
| 93 | mPreferenceManager = getPreferenceManager(); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 94 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 95 | 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: |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 103 | Log.i(TAG, "EDITION"); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 104 | 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 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 122 | addPreferenceListener(basicDetails, changeBasicPreferenceListener); |
| 123 | addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener); |
| 124 | addPreferenceListener(srtpDetails, changeSrtpPreferenceListener); |
| 125 | addPreferenceListener(tlsDetails, changeTlsPreferenceListener); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 126 | |
| 127 | } |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 128 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 129 | private void initEdition() { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 130 | |
| 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); |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 137 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 138 | basicDetails = new AccountDetailBasic(basicPreferenceList); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 139 | advancedDetails = new AccountDetailAdvanced(advancedPreferenceList); |
| 140 | srtpDetails = new AccountDetailSrtp(srtpPreferenceList); |
| 141 | tlsDetails = new AccountDetailTls(tlsPreferenceList); |
| 142 | |
| 143 | setPreferenceDetails(basicDetails); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 144 | setPreferenceDetails(advancedDetails); |
| 145 | setPreferenceDetails(srtpDetails); |
| 146 | setPreferenceDetails(tlsDetails); |
| 147 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 148 | addPreferenceListener(basicDetails, changeBasicPreferenceListener); |
| 149 | addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener); |
| 150 | addPreferenceListener(srtpDetails, changeSrtpPreferenceListener); |
| 151 | addPreferenceListener(tlsDetails, changeTlsPreferenceListener); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 152 | } |
| 153 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 154 | @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 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 193 | @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 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 256 | private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) { |
| 257 | for (AccountDetail.PreferenceEntry p : det.getDetailValues()) { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 258 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 259 | if (pref != null) { |
| 260 | if (p.isTwoState) { |
| 261 | CheckBoxPreference boxPref = (CheckBoxPreference) pref; |
| 262 | accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false"); |
| 263 | } else { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 264 | 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 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 271 | } |
| 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 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 283 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 284 | 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 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 291 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 292 | 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 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 299 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 300 | 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 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 307 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 308 | private void setPreferenceDetails(AccountDetail details) { |
| 309 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 310 | Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 311 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 312 | if (pref != null) { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 313 | if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) { |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 314 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 315 | try { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 316 | |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 317 | for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) { |
| 318 | NetworkInterface i = list.nextElement(); |
| 319 | Log.e("network_interfaces", "display name " + i.getDisplayName()); |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 320 | if (i.isUp()) |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 321 | 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 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 333 | if (!p.isTwoState) { |
| 334 | ((EditTextPreference) pref).setText(p.mValue); |
| 335 | pref.setSummary(p.mValue); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 336 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 337 | } else { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 338 | Log.w(TAG, "pref not found"); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 343 | private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 344 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 345 | Log.i(TAG, "addPreferenceListener: pref " + p.mKey); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 346 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 347 | if (pref != null) { |
| 348 | if (!p.isTwoState) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 349 | pref.setOnPreferenceChangeListener(listener); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 350 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 351 | } else { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 352 | Log.w(TAG, "addPreferenceListener: pref not found"); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 357 | /****************************************** |
| 358 | * |
| 359 | * AlertDialogs |
| 360 | * |
| 361 | ******************************************/ |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 362 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 363 | private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) { |
| 364 | String message = "The following parameters are missing:"; |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 365 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 366 | for (String s : missingValue) |
| 367 | message += "\n - " + s; |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 368 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 369 | 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 379 | } |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 380 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 381 | 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 394 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 395 | AlertDialog alertDialog = builder.create(); |
| 396 | alertDialog.setOwnerActivity(ownerActivity); |
| 397 | |
| 398 | return alertDialog; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 399 | } |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 400 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 401 | private AlertDialog createDeleteDialog() { |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 402 | Activity ownerActivity = this; |
| 403 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 404 | builder.setMessage("Do you really want to delete this account").setTitle("Delete Account") |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 405 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 406 | public void onClick(DialogInterface dialog, int whichButton) { |
| 407 | Bundle bundle = new Bundle(); |
| 408 | bundle.putString("AccountID", mAccountID); |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 409 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 410 | Intent resultIntent = new Intent(); |
| 411 | resultIntent.putExtras(bundle); |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 412 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 413 | 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 Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 422 | |
| 423 | AlertDialog alertDialog = builder.create(); |
| 424 | alertDialog.setOwnerActivity(ownerActivity); |
| 425 | |
| 426 | return alertDialog; |
| 427 | } |
| 428 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 429 | } |