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