AccountSettings.java revision 9019315b2642d58691cf09d32c07c0cf902f0a41
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.Account; 20import com.android.email.Email; 21import com.android.email.Preferences; 22import com.android.email.R; 23import com.android.email.activity.Accounts; 24import com.android.email.mail.MessagingException; 25import com.android.email.mail.Sender; 26import com.android.email.mail.Store; 27import com.android.email.provider.EmailStore; 28 29import android.app.Activity; 30import android.content.Context; 31import android.content.Intent; 32import android.content.SharedPreferences; 33import android.os.Bundle; 34import android.preference.CheckBoxPreference; 35import android.preference.EditTextPreference; 36import android.preference.ListPreference; 37import android.preference.Preference; 38import android.preference.PreferenceActivity; 39import android.preference.PreferenceCategory; 40import android.preference.RingtonePreference; 41import android.util.Log; 42import android.view.KeyEvent; 43 44public class AccountSettings extends PreferenceActivity { 45 private static final String EXTRA_ACCOUNT = "account"; 46 private static final String EXTRA_ACCOUNT_ID = "account_id"; 47 48 private static final String PREFERENCE_TOP_CATEGORY = "account_settings"; 49 private static final String PREFERENCE_DESCRIPTION = "account_description"; 50 private static final String PREFERENCE_NAME = "account_name"; 51 private static final String PREFERENCE_FREQUENCY = "account_check_frequency"; 52 private static final String PREFERENCE_DEFAULT = "account_default"; 53 private static final String PREFERENCE_NOTIFY = "account_notify"; 54 private static final String PREFERENCE_VIBRATE = "account_vibrate"; 55 private static final String PREFERENCE_RINGTONE = "account_ringtone"; 56 private static final String PREFERENCE_SERVER_CATERGORY = "account_servers"; 57 private static final String PREFERENCE_INCOMING = "incoming"; 58 private static final String PREFERENCE_OUTGOING = "outgoing"; 59 private static final String PREFERENCE_ADD_ACCOUNT = "add_account"; 60 61 private long mAccountId; 62 private EmailStore.Account mAccount; 63 64 private EditTextPreference mAccountDescription; 65 private EditTextPreference mAccountName; 66 private ListPreference mCheckFrequency; 67 private ListPreference mSyncWindow; 68 private CheckBoxPreference mAccountDefault; 69 private CheckBoxPreference mAccountNotify; 70 private CheckBoxPreference mAccountVibrate; 71 private RingtonePreference mAccountRingtone; 72 73 /** 74 * Entry point using old-style 75 * TODO remove 76 */ 77 @Deprecated 78 public static void actionSettings(Activity fromActivity, Account account) { 79 Intent i = new Intent(fromActivity, AccountSettings.class); 80 i.putExtra(EXTRA_ACCOUNT, account); 81 fromActivity.startActivity(i); 82 } 83 84 /** 85 * Entry point using provider-based Account 86 */ 87 public static void actionSettings(Accounts fromActivity, long accountId) { 88 Intent i = new Intent(fromActivity, AccountSettings.class); 89 i.putExtra(EXTRA_ACCOUNT_ID, accountId); 90 fromActivity.startActivity(i); 91 } 92 93 @Override 94 public void onCreate(Bundle savedInstanceState) { 95 super.onCreate(savedInstanceState); 96 97 Intent i = getIntent(); 98 mAccountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1); 99 mAccount = EmailStore.Account.restoreAccountWithId(this, mAccountId); 100 101 addPreferencesFromResource(R.xml.account_settings_preferences); 102 103 PreferenceCategory topCategory = (PreferenceCategory) findPreference(PREFERENCE_TOP_CATEGORY); 104 topCategory.setTitle(getString(R.string.account_settings_title_fmt)); 105 106 mAccountDescription = (EditTextPreference) findPreference(PREFERENCE_DESCRIPTION); 107 mAccountDescription.setSummary(mAccount.getDescription()); 108 mAccountDescription.setText(mAccount.getDescription()); 109 mAccountDescription.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 110 public boolean onPreferenceChange(Preference preference, Object newValue) { 111 final String summary = newValue.toString(); 112 mAccountDescription.setSummary(summary); 113 mAccountDescription.setText(summary); 114 return false; 115 } 116 }); 117 118 mAccountName = (EditTextPreference) findPreference(PREFERENCE_NAME); 119 mAccountName.setSummary(mAccount.getName()); 120 mAccountName.setText(mAccount.getName()); 121 mAccountName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 122 public boolean onPreferenceChange(Preference preference, Object newValue) { 123 final String summary = newValue.toString(); 124 mAccountName.setSummary(summary); 125 mAccountName.setText(summary); 126 return false; 127 } 128 }); 129 130 mCheckFrequency = (ListPreference) findPreference(PREFERENCE_FREQUENCY); 131 132 // Before setting value, we may need to adjust the lists 133 Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(this), this); 134 if (info.mPushSupported) { 135 mCheckFrequency.setEntries(R.array.account_settings_check_frequency_entries_push); 136 mCheckFrequency.setEntryValues(R.array.account_settings_check_frequency_values_push); 137 } 138 139 mCheckFrequency.setValue(String.valueOf(mAccount.getAutomaticCheckIntervalMinutes())); 140 mCheckFrequency.setSummary(mCheckFrequency.getEntry()); 141 mCheckFrequency.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 142 public boolean onPreferenceChange(Preference preference, Object newValue) { 143 final String summary = newValue.toString(); 144 int index = mCheckFrequency.findIndexOfValue(summary); 145 mCheckFrequency.setSummary(mCheckFrequency.getEntries()[index]); 146 mCheckFrequency.setValue(summary); 147 return false; 148 } 149 }); 150 151 // Add check window preference 152 mSyncWindow = null; 153 if (info.mVisibleLimitDefault == -1) { 154 mSyncWindow = new ListPreference(this); 155 mSyncWindow.setTitle(R.string.account_setup_options_mail_window_label); 156 mSyncWindow.setEntries(R.array.account_settings_mail_window_entries); 157 mSyncWindow.setEntryValues(R.array.account_settings_mail_window_values); 158 mSyncWindow.setValue(String.valueOf(mAccount.getSyncWindow())); 159 mSyncWindow.setSummary(mSyncWindow.getEntry()); 160 mSyncWindow.setOrder(4); 161 mSyncWindow.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 162 public boolean onPreferenceChange(Preference preference, Object newValue) { 163 final String summary = newValue.toString(); 164 int index = mSyncWindow.findIndexOfValue(summary); 165 mSyncWindow.setSummary(mSyncWindow.getEntries()[index]); 166 mSyncWindow.setValue(summary); 167 return false; 168 } 169 }); 170 topCategory.addPreference(mSyncWindow); 171 } 172 173 mAccountDefault = (CheckBoxPreference) findPreference(PREFERENCE_DEFAULT); 174 mAccountDefault.setChecked(mAccount.mIsDefault); 175 176 mAccountNotify = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY); 177 mAccountNotify.setChecked(0 != 178 (mAccount.getFlags() & EmailStore.Account.FLAGS_NOTIFY_NEW_MAIL)); 179 180 mAccountRingtone = (RingtonePreference) findPreference(PREFERENCE_RINGTONE); 181 182 // XXX: The following two lines act as a workaround for the RingtonePreference 183 // which does not let us set/get the value programmatically 184 SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); 185 prefs.edit().putString(PREFERENCE_RINGTONE, mAccount.getRingtone()).commit(); 186 187 mAccountVibrate = (CheckBoxPreference) findPreference(PREFERENCE_VIBRATE); 188 mAccountVibrate.setChecked(0 != 189 (mAccount.getFlags() & EmailStore.Account.FLAGS_VIBRATE)); 190 191 findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener( 192 new Preference.OnPreferenceClickListener() { 193 public boolean onPreferenceClick(Preference preference) { 194 onIncomingSettings(); 195 return true; 196 } 197 }); 198 199 // Hide the outgoing account setup link if it's not activated 200 Preference prefOutgoing = findPreference(PREFERENCE_OUTGOING); 201 boolean showOutgoing = true; 202 try { 203 Sender sender = Sender.getInstance(mAccount.getSenderUri(this), getApplication()); 204 if (sender != null) { 205 Class<? extends android.app.Activity> setting = sender.getSettingActivityClass(); 206 showOutgoing = (setting != null); 207 } 208 } catch (MessagingException me) { 209 // just leave showOutgoing as true - bias towards showing it, so user can fix it 210 } 211 if (showOutgoing) { 212 prefOutgoing.setOnPreferenceClickListener( 213 new Preference.OnPreferenceClickListener() { 214 public boolean onPreferenceClick(Preference preference) { 215 onOutgoingSettings(); 216 return true; 217 } 218 }); 219 } else { 220 PreferenceCategory serverCategory = (PreferenceCategory) findPreference( 221 PREFERENCE_SERVER_CATERGORY); 222 serverCategory.removePreference(prefOutgoing); 223 } 224 225 findPreference(PREFERENCE_ADD_ACCOUNT).setOnPreferenceClickListener( 226 new Preference.OnPreferenceClickListener() { 227 public boolean onPreferenceClick(Preference preference) { 228 onAddNewAccount(); 229 return true; 230 } 231 }); 232 } 233 234 private void saveSettings() { 235 int newFlags = mAccount.getFlags() & 236 ~(EmailStore.Account.FLAGS_NOTIFY_NEW_MAIL | EmailStore.Account.FLAGS_VIBRATE); 237 238 mAccount.setDefaultAccount(mAccountDefault.isChecked()); 239 mAccount.setDescription(mAccountDescription.getText()); 240 mAccount.setName(mAccountName.getText()); 241 newFlags |= mAccountNotify.isChecked() ? EmailStore.Account.FLAGS_NOTIFY_NEW_MAIL : 0; 242 mAccount.setAutomaticCheckIntervalMinutes(Integer.parseInt(mCheckFrequency.getValue())); 243 if (mSyncWindow != null) 244 { 245 mAccount.setSyncWindow(Integer.parseInt(mSyncWindow.getValue())); 246 } 247 newFlags |= mAccountVibrate.isChecked() ? EmailStore.Account.FLAGS_VIBRATE : 0; 248 SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); 249 mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null)); 250 mAccount.setFlags(newFlags); 251 252 mAccount.saveOrUpdate(this); 253 Email.setServicesEnabled(this); 254 } 255 256 @Override 257 public boolean onKeyDown(int keyCode, KeyEvent event) { 258 if (keyCode == KeyEvent.KEYCODE_BACK) { 259 saveSettings(); 260 } 261 return super.onKeyDown(keyCode, event); 262 } 263 264 private void onIncomingSettings() { 265 try { 266 Store store = Store.getInstance(mAccount.getStoreUri(this), getApplication(), null); 267 if (store != null) { 268 Class<? extends android.app.Activity> setting = store.getSettingActivityClass(); 269 if (setting != null) { 270 java.lang.reflect.Method m = setting.getMethod("actionEditIncomingSettings", 271 android.app.Activity.class, EmailStore.Account.class); 272 m.invoke(null, this, mAccount); 273 } 274 } 275 } catch (Exception e) { 276 Log.d(Email.LOG_TAG, "Error while trying to invoke store settings.", e); 277 } 278 } 279 280 private void onOutgoingSettings() { 281 try { 282 Sender sender = Sender.getInstance(mAccount.getSenderUri(this), getApplication()); 283 if (sender != null) { 284 Class<? extends android.app.Activity> setting = sender.getSettingActivityClass(); 285 if (setting != null) { 286 java.lang.reflect.Method m = setting.getMethod("actionEditOutgoingSettings", 287 android.app.Activity.class, EmailStore.Account.class); 288 m.invoke(null, this, mAccount); 289 } 290 } 291 } catch (Exception e) { 292 Log.d(Email.LOG_TAG, "Error while trying to invoke sender settings.", e); 293 } 294 } 295 296 private void onAddNewAccount() { 297 AccountSetupBasics.actionNewAccount(this); 298 finish(); 299 } 300} 301