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 | |
| 34 | import android.app.Activity; |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 35 | import android.app.AlertDialog; |
| 36 | import android.app.Dialog; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 37 | import android.content.ComponentName; |
| 38 | import android.content.Context; |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 39 | import android.content.DialogInterface; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 40 | import android.content.Intent; |
| 41 | import android.os.Bundle; |
| 42 | import android.os.IBinder; |
| 43 | import android.os.RemoteException; |
| 44 | import android.preference.Preference; |
| 45 | import android.preference.PreferenceActivity; |
| 46 | import android.preference.PreferenceManager; |
| 47 | import android.preference.PreferenceScreen; |
| 48 | import android.preference.EditTextPreference; |
| 49 | import android.preference.CheckBoxPreference; |
| 50 | import android.util.Log; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 51 | import android.view.Menu; |
| 52 | import android.view.MenuItem; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 53 | |
| 54 | import com.savoirfairelinux.sflphone.R; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 55 | import com.savoirfairelinux.sflphone.utils.AccountDetail; |
| 56 | import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler; |
| 57 | import com.savoirfairelinux.sflphone.utils.AccountDetailBasic; |
| 58 | import com.savoirfairelinux.sflphone.utils.AccountDetailAdvanced; |
| 59 | import com.savoirfairelinux.sflphone.utils.AccountDetailSrtp; |
| 60 | import com.savoirfairelinux.sflphone.utils.AccountDetailTls; |
| 61 | |
| 62 | import java.util.HashMap; |
| 63 | import java.util.ArrayList; |
| 64 | |
| 65 | public class AccountPreferenceActivity extends PreferenceActivity |
| 66 | { |
| 67 | private static final String TAG = "AccoutPreferenceActivity"; |
| 68 | public static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 0; |
| 69 | public static final int ACCOUNT_NOT_MODIFIED = Activity.RESULT_FIRST_USER + 1; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 70 | public static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 71 | |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 72 | private AccountDetailBasic basicDetails = null; |
| 73 | private AccountDetailAdvanced advancedDetails = null; |
| 74 | private AccountDetailSrtp srtpDetails = null; |
| 75 | private AccountDetailTls tlsDetails = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 76 | private PreferenceManager mPreferenceManager; |
| 77 | private HashMap<String, String> mPreferenceMap; |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 78 | private MenuItem deleteAccountAction = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 79 | private String mAccountID; |
| 80 | |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 81 | Preference.OnPreferenceChangeListener changeBasicPreferenceListener = |
| 82 | new Preference.OnPreferenceChangeListener() { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 83 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 84 | preference.setSummary(getString(R.string.account_current_value_label)+(CharSequence)newValue); |
| 85 | |
| 86 | // String preferenceKey = basicDetailKeys.get(preference.getOrder()).mKey; |
| 87 | // accountPreference.preferenceMap.put(preferenceKey, ((CharSequence)newValue).toString()); |
| 88 | basicDetails.setDetailString(preference.getOrder(), ((CharSequence)newValue).toString()); |
| 89 | // if(preferenceKey == AccountDetailBasic.CONFIG_ACCOUNT_ALIAS) |
| 90 | // accountPreference.mScreen.setTitle(((CharSequence)newValue.toString())); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | }; |
| 94 | |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 95 | Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener = |
| 96 | new Preference.OnPreferenceChangeListener() { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 97 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 98 | return true; |
| 99 | } |
| 100 | }; |
| 101 | |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 102 | private void init() { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 103 | |
| 104 | Bundle b = getIntent().getExtras(); |
| 105 | mAccountID = b.getString("AccountID"); |
| 106 | ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG); |
| 107 | ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG); |
| 108 | ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG); |
| 109 | ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG); |
| 110 | |
| 111 | basicDetails = new AccountDetailBasic(basicPreferenceList); |
| 112 | advancedDetails = new AccountDetailAdvanced(advancedPreferenceList); |
| 113 | srtpDetails = new AccountDetailSrtp(srtpPreferenceList); |
| 114 | tlsDetails = new AccountDetailTls(tlsPreferenceList); |
| 115 | |
| 116 | setPreferenceDetails(basicDetails); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 117 | setPreferenceDetails(advancedDetails); |
| 118 | setPreferenceDetails(srtpDetails); |
| 119 | setPreferenceDetails(tlsDetails); |
| 120 | |
| 121 | addPreferenceListener(basicDetails); |
| 122 | addPreferenceListener(advancedDetails); |
| 123 | addPreferenceListener(srtpDetails); |
| 124 | addPreferenceListener(tlsDetails); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private void setPreferenceDetails(AccountDetail details) { |
| 128 | for(AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 129 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 130 | if(pref != null) { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 131 | if(!p.isTwoState) { |
| 132 | ((EditTextPreference)pref).setText(p.mValue); |
| 133 | pref.setSummary(getString(R.string.account_current_value_label) + p.mValue); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private void addPreferenceListener(AccountDetail details) { |
| 140 | for(AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 141 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 142 | if(pref != null) { |
| 143 | if(!p.isTwoState) { |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 144 | pref.setOnPreferenceChangeListener(changeBasicPreferenceListener); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 151 | protected void onCreate(Bundle savedInstanceState) { |
| 152 | super.onCreate(savedInstanceState); |
| 153 | |
| 154 | addPreferencesFromResource(R.xml.account_creation_preferences); |
| 155 | mPreferenceManager = getPreferenceManager(); |
| 156 | |
| 157 | init(); |
| 158 | } |
| 159 | |
| 160 | @Override |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 161 | public boolean onCreateOptionsMenu(Menu menu) { |
Alexandre Savard | e32a173 | 2012-11-01 13:17:32 -0400 | [diff] [blame] | 162 | Log.i(TAG, "onCreateOptionsMenu: " + mAccountID); |
| 163 | if(mAccountID.equals("IP2IP")) |
| 164 | return true; |
| 165 | |
| 166 | |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 167 | deleteAccountAction = menu.add("Delete Account"); |
| 168 | deleteAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | @Override |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 173 | protected void onStart() { |
| 174 | super.onStart(); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void onStop() { |
| 179 | super.onStop(); |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | protected void onDestroy() { |
| 184 | super.onDestroy(); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void onBackPressed() { |
| 189 | Bundle bundle = new Bundle(); |
| 190 | bundle.putString("AccountID", mAccountID); |
| 191 | bundle.putStringArrayList(AccountDetailBasic.BUNDLE_TAG, basicDetails.getValuesOnly()); |
| 192 | bundle.putStringArrayList(AccountDetailAdvanced.BUNDLE_TAG, advancedDetails.getValuesOnly()); |
| 193 | bundle.putStringArrayList(AccountDetailSrtp.BUNDLE_TAG, srtpDetails.getValuesOnly()); |
| 194 | bundle.putStringArrayList(AccountDetailTls.BUNDLE_TAG, tlsDetails.getValuesOnly()); |
| 195 | |
| 196 | Intent resultIntent = new Intent(); |
| 197 | resultIntent.putExtras(bundle); |
| 198 | |
| 199 | setResult(ACCOUNT_MODIFIED, resultIntent); |
| 200 | finish(); |
| 201 | } |
| 202 | |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 203 | @Override |
| 204 | public boolean onOptionsItemSelected(MenuItem item) { |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 205 | AlertDialog dialog = createAlertDialog(); |
| 206 | dialog.show(); |
| 207 | |
Alexandre Savard | 5195d92 | 2012-10-30 16:58:50 -0400 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
Alexandre Savard | 645b29c | 2012-10-30 17:22:26 -0400 | [diff] [blame] | 211 | private AlertDialog createAlertDialog() |
| 212 | { |
| 213 | Activity ownerActivity = this; |
| 214 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 215 | builder.setMessage("Do you realy want to delete this account").setTitle("Delete Account") |
| 216 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 217 | public void onClick(DialogInterface dialog, int whichButton) { |
| 218 | Bundle bundle = new Bundle(); |
| 219 | bundle.putString("AccountID", mAccountID); |
| 220 | |
| 221 | Intent resultIntent = new Intent(); |
| 222 | resultIntent.putExtras(bundle); |
| 223 | |
| 224 | Activity activity = ((Dialog)dialog).getOwnerActivity(); |
| 225 | activity.setResult(ACCOUNT_DELETED, resultIntent); |
| 226 | activity.finish(); |
| 227 | } |
| 228 | }) |
| 229 | .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 230 | public void onClick(DialogInterface dialog, int whichButton) { |
| 231 | /* Terminate with no action */ |
| 232 | } |
| 233 | }); |
| 234 | |
| 235 | AlertDialog alertDialog = builder.create(); |
| 236 | alertDialog.setOwnerActivity(ownerActivity); |
| 237 | |
| 238 | return alertDialog; |
| 239 | } |
| 240 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 241 | private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) { |
| 242 | for(AccountDetail.PreferenceEntry p : det.getDetailValues()) { |
| 243 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 244 | if(pref != null) { |
| 245 | if(p.isTwoState) { |
| 246 | CheckBoxPreference boxPref = (CheckBoxPreference) pref; |
| 247 | accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false"); |
| 248 | } |
| 249 | else { |
| 250 | EditTextPreference textPref = (EditTextPreference)pref; |
| 251 | accountDetails.put(p.mKey, textPref.getText()); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | private void updateAccountDetails() { |
| 258 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
| 259 | |
| 260 | updateAccountDetails(accountDetails, basicDetails); |
| 261 | updateAccountDetails(accountDetails, advancedDetails); |
| 262 | updateAccountDetails(accountDetails, srtpDetails); |
| 263 | updateAccountDetails(accountDetails, tlsDetails); |
| 264 | } |
| 265 | } |
| 266 | |