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