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; |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 57 | import android.widget.Toast; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 58 | |
| 59 | import com.savoirfairelinux.sflphone.R; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 60 | import com.savoirfairelinux.sflphone.account.AccountDetail; |
| 61 | import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced; |
| 62 | import com.savoirfairelinux.sflphone.account.AccountDetailBasic; |
| 63 | import com.savoirfairelinux.sflphone.account.AccountDetailSrtp; |
| 64 | import com.savoirfairelinux.sflphone.account.AccountDetailTls; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 65 | import com.savoirfairelinux.sflphone.service.ServiceConstants; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 66 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 67 | public class AccountPreferenceActivity extends PreferenceActivity { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 68 | private static final String TAG = "AccoutPreferenceActivity"; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 69 | |
| 70 | public static final String KEY_MODE = "mode"; |
| 71 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 72 | public interface result { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 73 | static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 1; |
| 74 | static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2; |
| 75 | } |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 76 | |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 77 | private AccountDetailBasic basicDetails = null; |
| 78 | private AccountDetailAdvanced advancedDetails = null; |
| 79 | private AccountDetailSrtp srtpDetails = null; |
| 80 | private AccountDetailTls tlsDetails = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 81 | private PreferenceManager mPreferenceManager; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 82 | private String mAccountID; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 83 | private ArrayList<String> requiredFields = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 84 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 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 | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 91 | initEdition(); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 92 | |
alision | cc7bb42 | 2013-06-06 15:31:39 -0400 | [diff] [blame] | 93 | getActionBar().setDisplayHomeAsUpEnabled(true); |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame^] | 94 | |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 95 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 96 | 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 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 104 | private void initEdition() { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 105 | |
| 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); |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 112 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 113 | basicDetails = new AccountDetailBasic(basicPreferenceList); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 114 | advancedDetails = new AccountDetailAdvanced(advancedPreferenceList); |
| 115 | srtpDetails = new AccountDetailSrtp(srtpPreferenceList); |
| 116 | tlsDetails = new AccountDetailTls(tlsPreferenceList); |
| 117 | |
| 118 | setPreferenceDetails(basicDetails); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 119 | setPreferenceDetails(advancedDetails); |
| 120 | setPreferenceDetails(srtpDetails); |
| 121 | setPreferenceDetails(tlsDetails); |
| 122 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 123 | addPreferenceListener(basicDetails, changeBasicPreferenceListener); |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 124 | // addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener); |
| 125 | // addPreferenceListener(srtpDetails, changeSrtpPreferenceListener); |
| 126 | // addPreferenceListener(tlsDetails, changeTlsPreferenceListener); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 127 | } |
| 128 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 129 | @Override |
| 130 | public boolean onCreateOptionsMenu(Menu menu) { |
| 131 | MenuInflater inflater = getMenuInflater(); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 132 | inflater.inflate(R.menu.account_edition, menu); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onBackPressed() { |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 138 | |
| 139 | AlertDialog dialog = createCancelDialog(); |
| 140 | dialog.show(); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 141 | |
| 142 | } |
| 143 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 144 | @Override |
| 145 | public boolean onOptionsItemSelected(MenuItem item) { |
| 146 | |
| 147 | switch (item.getItemId()) { |
alision | cc7bb42 | 2013-06-06 15:31:39 -0400 | [diff] [blame] | 148 | case android.R.id.home: |
| 149 | finish(); |
| 150 | return true; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 151 | case R.id.menuitem_delete: |
| 152 | AlertDialog dialog = createDeleteDialog(); |
| 153 | dialog.show(); |
| 154 | break; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 155 | 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); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 177 | |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 178 | accountDetails.put("Account.type", "SIP"); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 179 | 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); |
alision | cc7bb42 | 2013-06-06 15:31:39 -0400 | [diff] [blame] | 198 | Log.i(TAG, "Value " + pref.getText()); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 199 | if (pref.getText().isEmpty()) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 200 | valid = false; |
| 201 | missingValue.add(pref.getTitle().toString()); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return valid; |
| 206 | } |
| 207 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 208 | private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) { |
| 209 | for (AccountDetail.PreferenceEntry p : det.getDetailValues()) { |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 210 | |
| 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 { |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 215 | // 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 | // } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 220 | } |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 221 | |
| 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 | // } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 241 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 242 | 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 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 253 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 254 | Preference.OnPreferenceChangeListener changeAdvancedPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 255 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 256 | preference.setSummary((CharSequence) newValue); |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 257 | advancedDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 261 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 262 | Preference.OnPreferenceChangeListener changeTlsPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 263 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 264 | preference.setSummary((CharSequence) newValue); |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 265 | tlsDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 269 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 270 | Preference.OnPreferenceChangeListener changeSrtpPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 271 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 272 | preference.setSummary((CharSequence) newValue); |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 273 | srtpDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 274 | return true; |
| 275 | } |
| 276 | }; |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 277 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 278 | private void setPreferenceDetails(AccountDetail details) { |
| 279 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 280 | Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 281 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 282 | if (pref != null) { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 283 | if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) { |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 284 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 285 | try { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 286 | |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 287 | for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) { |
| 288 | NetworkInterface i = list.nextElement(); |
| 289 | Log.e("network_interfaces", "display name " + i.getDisplayName()); |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 290 | if (i.isUp()) |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 291 | 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 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 303 | if (!p.isTwoState) { |
| 304 | ((EditTextPreference) pref).setText(p.mValue); |
| 305 | pref.setSummary(p.mValue); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 306 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 307 | } else { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 308 | Log.w(TAG, "pref not found"); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 313 | private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 314 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 315 | Log.i(TAG, "addPreferenceListener: pref " + p.mKey + p.mValue); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 316 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 317 | if (pref != null) { |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 318 | |
| 319 | pref.setOnPreferenceChangeListener(listener); |
| 320 | |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 321 | } else { |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 322 | Log.w(TAG, "addPreferenceListener: pref not found"); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 327 | /****************************************** |
| 328 | * |
| 329 | * AlertDialogs |
| 330 | * |
| 331 | ******************************************/ |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 332 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 333 | private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) { |
| 334 | String message = "The following parameters are missing:"; |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 335 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 336 | for (String s : missingValue) |
| 337 | message += "\n - " + s; |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 338 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 339 | 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 349 | } |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 350 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 351 | private AlertDialog createCancelDialog() { |
| 352 | Activity ownerActivity = this; |
| 353 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame^] | 354 | builder.setMessage("Modifications will be lost").setTitle("Account Edition").setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 355 | 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 Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 364 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 365 | AlertDialog alertDialog = builder.create(); |
| 366 | alertDialog.setOwnerActivity(ownerActivity); |
| 367 | |
| 368 | return alertDialog; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 369 | } |
alision | 7f18fc8 | 2013-05-01 09:37:33 -0400 | [diff] [blame] | 370 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 371 | private AlertDialog createDeleteDialog() { |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 372 | Activity ownerActivity = this; |
| 373 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 374 | builder.setMessage("Do you really want to delete this account").setTitle("Delete Account") |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 375 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 376 | public void onClick(DialogInterface dialog, int whichButton) { |
| 377 | Bundle bundle = new Bundle(); |
| 378 | bundle.putString("AccountID", mAccountID); |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 379 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 380 | Intent resultIntent = new Intent(); |
| 381 | resultIntent.putExtras(bundle); |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 382 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 383 | 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 Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 392 | |
| 393 | AlertDialog alertDialog = builder.create(); |
| 394 | alertDialog.setOwnerActivity(ownerActivity); |
| 395 | |
| 396 | return alertDialog; |
| 397 | } |
| 398 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 399 | } |