AccountSettings.java revision c0033f24a26a08c47aa38d957f42cf63cfa3c345
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.EmailStore; 25import com.android.email.provider.EmailStore.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 EmailStore.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 = EmailStore.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.getDescription()); 95 mAccountDescription.setText(mAccount.getDescription()); 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.getName()); 107 mAccountName.setText(mAccount.getName()); 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.getAutomaticCheckIntervalMinutes())); 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.getSyncWindow())); 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.mIsDefault); 162 163 mAccountNotify = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY); 164 mAccountNotify.setChecked(0 != 165 (mAccount.getFlags() & EmailStore.Account.FLAGS_NOTIFY_NEW_MAIL)); 166 167 mAccountRingtone = (RingtonePreference) findPreference(PREFERENCE_RINGTONE); 168 169 // XXX: The following two lines act as a workaround for the RingtonePreference 170 // which does not let us set/get the value programmatically 171 SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); 172 prefs.edit().putString(PREFERENCE_RINGTONE, mAccount.getRingtone()).commit(); 173 174 mAccountVibrate = (CheckBoxPreference) findPreference(PREFERENCE_VIBRATE); 175 mAccountVibrate.setChecked(0 != 176 (mAccount.getFlags() & EmailStore.Account.FLAGS_VIBRATE)); 177 178 findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener( 179 new Preference.OnPreferenceClickListener() { 180 public boolean onPreferenceClick(Preference preference) { 181 onIncomingSettings(); 182 return true; 183 } 184 }); 185 186 // Hide the outgoing account setup link if it's not activated 187 Preference prefOutgoing = findPreference(PREFERENCE_OUTGOING); 188 boolean showOutgoing = true; 189 try { 190 Sender sender = Sender.getInstance(mAccount.getSenderUri(this), getApplication()); 191 if (sender != null) { 192 Class<? extends android.app.Activity> setting = sender.getSettingActivityClass(); 193 showOutgoing = (setting != null); 194 } 195 } catch (MessagingException me) { 196 // just leave showOutgoing as true - bias towards showing it, so user can fix it 197 } 198 if (showOutgoing) { 199 prefOutgoing.setOnPreferenceClickListener( 200 new Preference.OnPreferenceClickListener() { 201 public boolean onPreferenceClick(Preference preference) { 202 onOutgoingSettings(); 203 return true; 204 } 205 }); 206 } else { 207 PreferenceCategory serverCategory = (PreferenceCategory) findPreference( 208 PREFERENCE_SERVER_CATERGORY); 209 serverCategory.removePreference(prefOutgoing); 210 } 211 212 findPreference(PREFERENCE_ADD_ACCOUNT).setOnPreferenceClickListener( 213 new Preference.OnPreferenceClickListener() { 214 public boolean onPreferenceClick(Preference preference) { 215 onAddNewAccount(); 216 return true; 217 } 218 }); 219 } 220 221 @Override 222 public void onResume() { 223 super.onResume(); 224 if (mAccountDirty) { 225 // if we are coming back from editing incoming or outgoing settings, 226 // we need to refresh them here so we don't accidentally overwrite the 227 // old values we're still holding here 228 mAccount.mHostAuthRecv = HostAuth.restoreHostAuthWithId( 229 this, mAccount.mHostAuthKeyRecv); 230 mAccount.mHostAuthSend = HostAuth.restoreHostAuthWithId( 231 this, mAccount.mHostAuthKeySend); 232 233 // TODO write me. 234 mAccountDirty = false; 235 } 236 } 237 238 private void saveSettings() { 239 int newFlags = mAccount.getFlags() & 240 ~(EmailStore.Account.FLAGS_NOTIFY_NEW_MAIL | EmailStore.Account.FLAGS_VIBRATE); 241 242 mAccount.setDefaultAccount(mAccountDefault.isChecked()); 243 mAccount.setDescription(mAccountDescription.getText()); 244 mAccount.setName(mAccountName.getText()); 245 newFlags |= mAccountNotify.isChecked() ? EmailStore.Account.FLAGS_NOTIFY_NEW_MAIL : 0; 246 mAccount.setAutomaticCheckIntervalMinutes(Integer.parseInt(mCheckFrequency.getValue())); 247 if (mSyncWindow != null) 248 { 249 mAccount.setSyncWindow(Integer.parseInt(mSyncWindow.getValue())); 250 } 251 newFlags |= mAccountVibrate.isChecked() ? EmailStore.Account.FLAGS_VIBRATE : 0; 252 SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); 253 mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null)); 254 mAccount.setFlags(newFlags); 255 256 mAccount.saveOrUpdate(this); 257 Email.setServicesEnabled(this); 258 } 259 260 @Override 261 public boolean onKeyDown(int keyCode, KeyEvent event) { 262 if (keyCode == KeyEvent.KEYCODE_BACK) { 263 saveSettings(); 264 } 265 return super.onKeyDown(keyCode, event); 266 } 267 268 private void onIncomingSettings() { 269 try { 270 Store store = Store.getInstance(mAccount.getStoreUri(this), getApplication(), null); 271 if (store != null) { 272 Class<? extends android.app.Activity> setting = store.getSettingActivityClass(); 273 if (setting != null) { 274 java.lang.reflect.Method m = setting.getMethod("actionEditIncomingSettings", 275 android.app.Activity.class, EmailStore.Account.class); 276 m.invoke(null, this, mAccount); 277 mAccountDirty = true; 278 } 279 } 280 } catch (Exception e) { 281 Log.d(Email.LOG_TAG, "Error while trying to invoke store settings.", e); 282 } 283 } 284 285 private void onOutgoingSettings() { 286 try { 287 Sender sender = Sender.getInstance(mAccount.getSenderUri(this), getApplication()); 288 if (sender != null) { 289 Class<? extends android.app.Activity> setting = sender.getSettingActivityClass(); 290 if (setting != null) { 291 java.lang.reflect.Method m = setting.getMethod("actionEditOutgoingSettings", 292 android.app.Activity.class, EmailStore.Account.class); 293 m.invoke(null, this, mAccount); 294 mAccountDirty = true; 295 } 296 } 297 } catch (Exception e) { 298 Log.d(Email.LOG_TAG, "Error while trying to invoke sender settings.", e); 299 } 300 } 301 302 private void onAddNewAccount() { 303 AccountSetupBasics.actionNewAccount(this); 304 finish(); 305 } 306} 307