AccountSettings.java revision c640cbbaf385566e1b6de361b2b23156e10f695d
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.email.activity.setup; 18 19import com.android.email.Email; 20import com.android.email.R; 21import com.android.email.mail.MessagingException; 22import com.android.email.mail.Sender; 23import com.android.email.mail.Store; 24import com.android.email.provider.EmailContent.Account; 25import com.android.email.provider.EmailContent.HostAuth; 26 27import android.app.Activity; 28import android.content.Intent; 29import android.content.SharedPreferences; 30import android.os.Bundle; 31import android.preference.CheckBoxPreference; 32import android.preference.EditTextPreference; 33import android.preference.ListPreference; 34import android.preference.Preference; 35import android.preference.PreferenceActivity; 36import android.preference.PreferenceCategory; 37import android.preference.RingtonePreference; 38import android.util.Log; 39import android.view.KeyEvent; 40 41public class AccountSettings extends PreferenceActivity { 42 private static final String EXTRA_ACCOUNT_ID = "account_id"; 43 44 private static final String PREFERENCE_TOP_CATEGORY = "account_settings"; 45 private static final String PREFERENCE_DESCRIPTION = "account_description"; 46 private static final String PREFERENCE_NAME = "account_name"; 47 private static final String PREFERENCE_FREQUENCY = "account_check_frequency"; 48 private static final String PREFERENCE_DEFAULT = "account_default"; 49 private static final String PREFERENCE_NOTIFY = "account_notify"; 50 private static final String PREFERENCE_VIBRATE = "account_vibrate"; 51 private static final String PREFERENCE_RINGTONE = "account_ringtone"; 52 private static final String PREFERENCE_SERVER_CATERGORY = "account_servers"; 53 private static final String PREFERENCE_INCOMING = "incoming"; 54 private static final String PREFERENCE_OUTGOING = "outgoing"; 55 private static final String PREFERENCE_ADD_ACCOUNT = "add_account"; 56 57 private long mAccountId; 58 private Account mAccount; 59 private boolean mAccountDirty; 60 61 private EditTextPreference mAccountDescription; 62 private EditTextPreference mAccountName; 63 private ListPreference mCheckFrequency; 64 private ListPreference mSyncWindow; 65 private CheckBoxPreference mAccountDefault; 66 private CheckBoxPreference mAccountNotify; 67 private CheckBoxPreference mAccountVibrate; 68 private RingtonePreference mAccountRingtone; 69 70 /** 71 * Display (and edit) settings for a specific account 72 */ 73 public static void actionSettings(Activity fromActivity, long accountId) { 74 Intent i = new Intent(fromActivity, AccountSettings.class); 75 i.putExtra(EXTRA_ACCOUNT_ID, accountId); 76 fromActivity.startActivity(i); 77 } 78 79 @Override 80 public void onCreate(Bundle savedInstanceState) { 81 super.onCreate(savedInstanceState); 82 83 Intent i = getIntent(); 84 mAccountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1); 85 mAccount = Account.restoreAccountWithId(this, mAccountId); 86 mAccountDirty = false; 87 88 addPreferencesFromResource(R.xml.account_settings_preferences); 89 90 PreferenceCategory topCategory = (PreferenceCategory) findPreference(PREFERENCE_TOP_CATEGORY); 91 topCategory.setTitle(getString(R.string.account_settings_title_fmt)); 92 93 mAccountDescription = (EditTextPreference) findPreference(PREFERENCE_DESCRIPTION); 94 mAccountDescription.setSummary(mAccount.getDisplayName()); 95 mAccountDescription.setText(mAccount.getDisplayName()); 96 mAccountDescription.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 97 public boolean onPreferenceChange(Preference preference, Object newValue) { 98 final String summary = newValue.toString(); 99 mAccountDescription.setSummary(summary); 100 mAccountDescription.setText(summary); 101 return false; 102 } 103 }); 104 105 mAccountName = (EditTextPreference) findPreference(PREFERENCE_NAME); 106 mAccountName.setSummary(mAccount.getSenderName()); 107 mAccountName.setText(mAccount.getSenderName()); 108 mAccountName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 109 public boolean onPreferenceChange(Preference preference, Object newValue) { 110 final String summary = newValue.toString(); 111 mAccountName.setSummary(summary); 112 mAccountName.setText(summary); 113 return false; 114 } 115 }); 116 117 mCheckFrequency = (ListPreference) findPreference(PREFERENCE_FREQUENCY); 118 119 // Before setting value, we may need to adjust the lists 120 Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(this), this); 121 if (info.mPushSupported) { 122 mCheckFrequency.setEntries(R.array.account_settings_check_frequency_entries_push); 123 mCheckFrequency.setEntryValues(R.array.account_settings_check_frequency_values_push); 124 } 125 126 mCheckFrequency.setValue(String.valueOf(mAccount.getSyncInterval())); 127 mCheckFrequency.setSummary(mCheckFrequency.getEntry()); 128 mCheckFrequency.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 129 public boolean onPreferenceChange(Preference preference, Object newValue) { 130 final String summary = newValue.toString(); 131 int index = mCheckFrequency.findIndexOfValue(summary); 132 mCheckFrequency.setSummary(mCheckFrequency.getEntries()[index]); 133 mCheckFrequency.setValue(summary); 134 return false; 135 } 136 }); 137 138 // Add check window preference 139 mSyncWindow = null; 140 if (info.mVisibleLimitDefault == -1) { 141 mSyncWindow = new ListPreference(this); 142 mSyncWindow.setTitle(R.string.account_setup_options_mail_window_label); 143 mSyncWindow.setEntries(R.array.account_settings_mail_window_entries); 144 mSyncWindow.setEntryValues(R.array.account_settings_mail_window_values); 145 mSyncWindow.setValue(String.valueOf(mAccount.getSyncLookback())); 146 mSyncWindow.setSummary(mSyncWindow.getEntry()); 147 mSyncWindow.setOrder(4); 148 mSyncWindow.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 149 public boolean onPreferenceChange(Preference preference, Object newValue) { 150 final String summary = newValue.toString(); 151 int index = mSyncWindow.findIndexOfValue(summary); 152 mSyncWindow.setSummary(mSyncWindow.getEntries()[index]); 153 mSyncWindow.setValue(summary); 154 return false; 155 } 156 }); 157 topCategory.addPreference(mSyncWindow); 158 } 159 160 mAccountDefault = (CheckBoxPreference) findPreference(PREFERENCE_DEFAULT); 161 mAccountDefault.setChecked(mAccount.mId == Account.getDefaultAccountId(this)); 162 163 mAccountNotify = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY); 164 mAccountNotify.setChecked(0 != (mAccount.getFlags() & Account.FLAGS_NOTIFY_NEW_MAIL)); 165 166 mAccountRingtone = (RingtonePreference) findPreference(PREFERENCE_RINGTONE); 167 168 // XXX: The following two lines act as a workaround for the RingtonePreference 169 // which does not let us set/get the value programmatically 170 SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); 171 prefs.edit().putString(PREFERENCE_RINGTONE, mAccount.getRingtone()).commit(); 172 173 mAccountVibrate = (CheckBoxPreference) findPreference(PREFERENCE_VIBRATE); 174 mAccountVibrate.setChecked(0 != 175 (mAccount.getFlags() & Account.FLAGS_VIBRATE)); 176 177 findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener( 178 new Preference.OnPreferenceClickListener() { 179 public boolean onPreferenceClick(Preference preference) { 180 onIncomingSettings(); 181 return true; 182 } 183 }); 184 185 // Hide the outgoing account setup link if it's not activated 186 Preference prefOutgoing = findPreference(PREFERENCE_OUTGOING); 187 boolean showOutgoing = true; 188 try { 189 Sender sender = Sender.getInstance(getApplication(), mAccount.getSenderUri(this)); 190 if (sender != null) { 191 Class<? extends android.app.Activity> setting = sender.getSettingActivityClass(); 192 showOutgoing = (setting != null); 193 } 194 } catch (MessagingException me) { 195 // just leave showOutgoing as true - bias towards showing it, so user can fix it 196 } 197 if (showOutgoing) { 198 prefOutgoing.setOnPreferenceClickListener( 199 new Preference.OnPreferenceClickListener() { 200 public boolean onPreferenceClick(Preference preference) { 201 onOutgoingSettings(); 202 return true; 203 } 204 }); 205 } else { 206 PreferenceCategory serverCategory = (PreferenceCategory) findPreference( 207 PREFERENCE_SERVER_CATERGORY); 208 serverCategory.removePreference(prefOutgoing); 209 } 210 211 findPreference(PREFERENCE_ADD_ACCOUNT).setOnPreferenceClickListener( 212 new Preference.OnPreferenceClickListener() { 213 public boolean onPreferenceClick(Preference preference) { 214 onAddNewAccount(); 215 return true; 216 } 217 }); 218 } 219 220 @Override 221 public void onResume() { 222 super.onResume(); 223 if (mAccountDirty) { 224 // if we are coming back from editing incoming or outgoing settings, 225 // we need to refresh them here so we don't accidentally overwrite the 226 // old values we're still holding here 227 mAccount.mHostAuthRecv = HostAuth.restoreHostAuthWithId( 228 this, mAccount.mHostAuthKeyRecv); 229 mAccount.mHostAuthSend = HostAuth.restoreHostAuthWithId( 230 this, mAccount.mHostAuthKeySend); 231 232 // TODO write me. 233 mAccountDirty = false; 234 } 235 } 236 237 private void saveSettings() { 238 int newFlags = mAccount.getFlags() & 239 ~(Account.FLAGS_NOTIFY_NEW_MAIL | Account.FLAGS_VIBRATE); 240 241 mAccount.setDefaultAccount(mAccountDefault.isChecked()); 242 mAccount.setDisplayName(mAccountDescription.getText()); 243 mAccount.setSenderName(mAccountName.getText()); 244 newFlags |= mAccountNotify.isChecked() ? Account.FLAGS_NOTIFY_NEW_MAIL : 0; 245 mAccount.setSyncInterval(Integer.parseInt(mCheckFrequency.getValue())); 246 if (mSyncWindow != null) 247 { 248 mAccount.setSyncLookback(Integer.parseInt(mSyncWindow.getValue())); 249 } 250 newFlags |= mAccountVibrate.isChecked() ? Account.FLAGS_VIBRATE : 0; 251 SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); 252 mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null)); 253 mAccount.setFlags(newFlags); 254 255 AccountSettingsUtils.commitSettings(this, mAccount); 256 Email.setServicesEnabled(this); 257 } 258 259 @Override 260 public boolean onKeyDown(int keyCode, KeyEvent event) { 261 if (keyCode == KeyEvent.KEYCODE_BACK) { 262 saveSettings(); 263 } 264 return super.onKeyDown(keyCode, event); 265 } 266 267 private void onIncomingSettings() { 268 try { 269 Store store = Store.getInstance(mAccount.getStoreUri(this), getApplication(), null); 270 if (store != null) { 271 Class<? extends android.app.Activity> setting = store.getSettingActivityClass(); 272 if (setting != null) { 273 java.lang.reflect.Method m = setting.getMethod("actionEditIncomingSettings", 274 android.app.Activity.class, Account.class); 275 m.invoke(null, this, mAccount); 276 mAccountDirty = true; 277 } 278 } 279 } catch (Exception e) { 280 Log.d(Email.LOG_TAG, "Error while trying to invoke store settings.", e); 281 } 282 } 283 284 private void onOutgoingSettings() { 285 try { 286 Sender sender = Sender.getInstance(getApplication(), mAccount.getSenderUri(this)); 287 if (sender != null) { 288 Class<? extends android.app.Activity> setting = sender.getSettingActivityClass(); 289 if (setting != null) { 290 java.lang.reflect.Method m = setting.getMethod("actionEditOutgoingSettings", 291 android.app.Activity.class, Account.class); 292 m.invoke(null, this, mAccount); 293 mAccountDirty = true; 294 } 295 } 296 } catch (Exception e) { 297 Log.d(Email.LOG_TAG, "Error while trying to invoke sender settings.", e); 298 } 299 } 300 301 private void onAddNewAccount() { 302 AccountSetupBasics.actionNewAccount(this); 303 finish(); 304 } 305} 306