2010-08-09 17:36:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.activity.setup;
|
|
|
|
|
|
|
|
import com.android.email.Email;
|
|
|
|
import com.android.email.R;
|
2010-08-18 18:29:46 +00:00
|
|
|
import com.android.email.Utility;
|
2010-08-09 17:36:50 +00:00
|
|
|
import com.android.email.mail.Sender;
|
|
|
|
import com.android.email.mail.Store;
|
2010-10-07 22:04:20 +00:00
|
|
|
import com.android.email.provider.EmailContent;
|
2010-08-09 17:36:50 +00:00
|
|
|
import com.android.email.provider.EmailContent.Account;
|
|
|
|
import com.android.email.provider.EmailContent.HostAuth;
|
2011-02-10 02:47:43 +00:00
|
|
|
import com.android.emailcommon.mail.MessagingException;
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2010-08-19 23:28:52 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.DialogFragment;
|
2010-08-18 18:29:46 +00:00
|
|
|
import android.app.Fragment;
|
2010-09-13 02:38:10 +00:00
|
|
|
import android.app.FragmentTransaction;
|
2010-08-09 17:36:50 +00:00
|
|
|
import android.content.ContentResolver;
|
2010-12-22 21:55:19 +00:00
|
|
|
import android.content.ContentValues;
|
2010-08-09 17:36:50 +00:00
|
|
|
import android.content.Context;
|
2010-08-19 23:28:52 +00:00
|
|
|
import android.content.DialogInterface;
|
2010-08-09 17:36:50 +00:00
|
|
|
import android.content.SharedPreferences;
|
2010-08-18 18:29:46 +00:00
|
|
|
import android.os.AsyncTask;
|
2010-08-09 17:36:50 +00:00
|
|
|
import android.os.Bundle;
|
2010-11-05 04:13:45 +00:00
|
|
|
import android.os.Vibrator;
|
2010-08-09 17:36:50 +00:00
|
|
|
import android.preference.CheckBoxPreference;
|
|
|
|
import android.preference.EditTextPreference;
|
|
|
|
import android.preference.ListPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceCategory;
|
|
|
|
import android.preference.PreferenceFragment;
|
|
|
|
import android.preference.RingtonePreference;
|
|
|
|
import android.provider.Calendar;
|
|
|
|
import android.provider.ContactsContract;
|
2011-01-20 19:37:39 +00:00
|
|
|
import android.text.TextUtils;
|
2010-08-09 17:36:50 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment containing the main logic for account settings. This also calls out to other
|
|
|
|
* fragments for server settings.
|
|
|
|
*
|
2010-12-22 21:55:19 +00:00
|
|
|
* TODO: Remove or make async the mAccountDirty reload logic. Probably no longer needed.
|
2010-08-18 18:29:46 +00:00
|
|
|
* TODO: Can we defer calling addPreferencesFromResource() until after we load the account? This
|
|
|
|
* could reduce flicker.
|
2010-08-09 17:36:50 +00:00
|
|
|
*/
|
|
|
|
public class AccountSettingsFragment extends PreferenceFragment {
|
2010-08-18 18:29:46 +00:00
|
|
|
|
|
|
|
// Keys used for arguments bundle
|
|
|
|
private static final String BUNDLE_KEY_ACCOUNT_ID = "AccountSettingsFragment.AccountId";
|
|
|
|
|
2010-11-05 04:13:45 +00:00
|
|
|
private static final String PREFERENCE_CATEGORY_TOP = "account_settings";
|
2010-08-09 17:36:50 +00:00
|
|
|
private static final String PREFERENCE_DESCRIPTION = "account_description";
|
|
|
|
private static final String PREFERENCE_NAME = "account_name";
|
|
|
|
private static final String PREFERENCE_SIGNATURE = "account_signature";
|
|
|
|
private static final String PREFERENCE_FREQUENCY = "account_check_frequency";
|
2011-01-18 18:53:37 +00:00
|
|
|
private static final String PREFERENCE_BACKGROUND_ATTACHMENTS =
|
|
|
|
"account_background_attachments";
|
2010-08-09 17:36:50 +00:00
|
|
|
private static final String PREFERENCE_DEFAULT = "account_default";
|
2010-11-05 04:13:45 +00:00
|
|
|
private static final String PREFERENCE_CATEGORY_NOTIFICATIONS = "account_notifications";
|
2010-08-09 17:36:50 +00:00
|
|
|
private static final String PREFERENCE_NOTIFY = "account_notify";
|
|
|
|
private static final String PREFERENCE_VIBRATE_WHEN = "account_settings_vibrate_when";
|
|
|
|
private static final String PREFERENCE_RINGTONE = "account_ringtone";
|
2010-11-05 04:13:45 +00:00
|
|
|
private static final String PREFERENCE_CATEGORY_SERVER = "account_servers";
|
2010-08-09 17:36:50 +00:00
|
|
|
private static final String PREFERENCE_INCOMING = "incoming";
|
|
|
|
private static final String PREFERENCE_OUTGOING = "outgoing";
|
|
|
|
private static final String PREFERENCE_SYNC_CONTACTS = "account_sync_contacts";
|
|
|
|
private static final String PREFERENCE_SYNC_CALENDAR = "account_sync_calendar";
|
2010-10-07 22:04:20 +00:00
|
|
|
private static final String PREFERENCE_SYNC_EMAIL = "account_sync_email";
|
2010-08-19 23:28:52 +00:00
|
|
|
private static final String PREFERENCE_DELETE_ACCOUNT = "delete_account";
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
// These strings must match account_settings_vibrate_when_* strings in strings.xml
|
|
|
|
private static final String PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS = "always";
|
|
|
|
private static final String PREFERENCE_VALUE_VIBRATE_WHEN_SILENT = "silent";
|
|
|
|
private static final String PREFERENCE_VALUE_VIBRATE_WHEN_NEVER = "never";
|
|
|
|
|
|
|
|
private EditTextPreference mAccountDescription;
|
|
|
|
private EditTextPreference mAccountName;
|
|
|
|
private EditTextPreference mAccountSignature;
|
|
|
|
private ListPreference mCheckFrequency;
|
|
|
|
private ListPreference mSyncWindow;
|
2011-01-18 18:53:37 +00:00
|
|
|
private CheckBoxPreference mAccountBackgroundAttachments;
|
2010-08-09 17:36:50 +00:00
|
|
|
private CheckBoxPreference mAccountDefault;
|
|
|
|
private CheckBoxPreference mAccountNotify;
|
|
|
|
private ListPreference mAccountVibrateWhen;
|
|
|
|
private RingtonePreference mAccountRingtone;
|
|
|
|
private CheckBoxPreference mSyncContacts;
|
|
|
|
private CheckBoxPreference mSyncCalendar;
|
2010-10-07 22:04:20 +00:00
|
|
|
private CheckBoxPreference mSyncEmail;
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private Account mAccount;
|
|
|
|
private boolean mAccountDirty;
|
2010-12-22 21:55:19 +00:00
|
|
|
private long mDefaultAccountId;
|
2010-08-09 17:36:50 +00:00
|
|
|
private Callback mCallback = EmptyCallback.INSTANCE;
|
|
|
|
private boolean mStarted;
|
|
|
|
private boolean mLoaded;
|
2010-08-18 18:29:46 +00:00
|
|
|
private boolean mSaveOnExit;
|
|
|
|
|
|
|
|
// Async Tasks
|
|
|
|
private AsyncTask<?,?,?> mLoadAccountTask;
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
/**
|
2010-08-18 18:29:46 +00:00
|
|
|
* Callback interface that owning activities must provide
|
2010-08-09 17:36:50 +00:00
|
|
|
*/
|
|
|
|
public interface Callback {
|
2010-08-18 18:29:46 +00:00
|
|
|
public void onIncomingSettings(Account account);
|
|
|
|
public void onOutgoingSettings(Account account);
|
2010-08-09 17:36:50 +00:00
|
|
|
public void abandonEdit();
|
2010-08-19 23:28:52 +00:00
|
|
|
public void deleteAccount(Account account);
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class EmptyCallback implements Callback {
|
|
|
|
public static final Callback INSTANCE = new EmptyCallback();
|
2010-08-18 18:29:46 +00:00
|
|
|
@Override public void onIncomingSettings(Account account) { }
|
|
|
|
@Override public void onOutgoingSettings(Account account) { }
|
2010-08-09 17:36:50 +00:00
|
|
|
@Override public void abandonEdit() { }
|
2010-12-22 21:55:19 +00:00
|
|
|
@Override public void deleteAccount(Account account) { }
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 18:29:46 +00:00
|
|
|
/**
|
|
|
|
* If launching with an arguments bundle, use this method to build the arguments.
|
|
|
|
* @param accountId The account being modified
|
|
|
|
*/
|
|
|
|
public static Bundle buildArguments(long accountId) {
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putLong(BUNDLE_KEY_ACCOUNT_ID, accountId);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a fragment is first attached to its activity.
|
|
|
|
* {@link #onCreate(Bundle)} will be called after this.
|
|
|
|
*/
|
2010-10-07 22:04:20 +00:00
|
|
|
@Override
|
2010-08-18 18:29:46 +00:00
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
|
|
|
mContext = activity;
|
|
|
|
|
2010-09-03 21:30:21 +00:00
|
|
|
// Notify the activity that we're here.
|
|
|
|
if (activity instanceof AccountSettingsXL) {
|
|
|
|
((AccountSettingsXL)activity).onAttach(this);
|
|
|
|
}
|
2010-08-18 18:29:46 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 17:36:50 +00:00
|
|
|
/**
|
|
|
|
* Called to do initial creation of a fragment. This is called after
|
|
|
|
* {@link #onAttach(Activity)} and before {@link #onActivityCreated(Bundle)}.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onCreate");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
// Load the preferences from an XML resource
|
|
|
|
addPreferencesFromResource(R.xml.account_settings_preferences);
|
2010-08-18 18:29:46 +00:00
|
|
|
|
|
|
|
// Start loading the account data, if provided in the arguments
|
|
|
|
// If not, activity must call startLoadingAccount() directly
|
|
|
|
Bundle b = getArguments();
|
|
|
|
if (b != null) {
|
|
|
|
long accountId = b.getLong(BUNDLE_KEY_ACCOUNT_ID, -1);
|
|
|
|
if (accountId >= 0 && !mLoaded) {
|
|
|
|
startLoadingAccount(accountId);
|
|
|
|
}
|
|
|
|
}
|
2010-09-08 05:20:51 +00:00
|
|
|
|
|
|
|
mAccountDirty = false;
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onActivityCreated");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the Fragment is visible to the user.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onStart");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onStart();
|
|
|
|
mStarted = true;
|
2010-08-18 18:29:46 +00:00
|
|
|
|
|
|
|
// If the loaded account is ready now, load the UI
|
2010-08-09 17:36:50 +00:00
|
|
|
if (mAccount != null && !mLoaded) {
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the fragment is visible to the user and actively running.
|
2010-12-22 21:55:19 +00:00
|
|
|
* TODO: Don't read account data on UI thread. This should be fixed by removing the need
|
|
|
|
* to do this, not by spinning up yet another thread.
|
2010-08-09 17:36:50 +00:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onResume");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
if (mAccountDirty) {
|
|
|
|
// if we are coming back from editing incoming or outgoing settings,
|
|
|
|
// we need to refresh them here so we don't accidentally overwrite the
|
|
|
|
// old values we're still holding here
|
|
|
|
mAccount.mHostAuthRecv =
|
|
|
|
HostAuth.restoreHostAuthWithId(mContext, mAccount.mHostAuthKeyRecv);
|
|
|
|
mAccount.mHostAuthSend =
|
|
|
|
HostAuth.restoreHostAuthWithId(mContext, mAccount.mHostAuthKeySend);
|
|
|
|
// Because "delete policy" UI is on edit incoming settings, we have
|
|
|
|
// to refresh that as well.
|
|
|
|
Account refreshedAccount = Account.restoreAccountWithId(mContext, mAccount.mId);
|
|
|
|
if (refreshedAccount == null || mAccount.mHostAuthRecv == null
|
|
|
|
|| mAccount.mHostAuthSend == null) {
|
2010-08-18 18:29:46 +00:00
|
|
|
mSaveOnExit = false;
|
2010-08-09 17:36:50 +00:00
|
|
|
mCallback.abandonEdit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mAccount.setDeletePolicy(refreshedAccount.getDeletePolicy());
|
|
|
|
mAccountDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onPause");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onPause();
|
2010-12-22 21:55:19 +00:00
|
|
|
if (mSaveOnExit) {
|
|
|
|
saveSettings();
|
|
|
|
}
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the Fragment is no longer started.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onStop");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onStop();
|
|
|
|
mStarted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the fragment is no longer in use.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onDestroy");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onDestroy();
|
2010-08-18 18:29:46 +00:00
|
|
|
|
|
|
|
Utility.cancelTaskInterrupt(mLoadAccountTask);
|
|
|
|
mLoadAccountTask = null;
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
if (Email.DEBUG_LIFECYCLE && Email.DEBUG) {
|
2010-10-26 20:45:52 +00:00
|
|
|
Log.d(Email.LOG_TAG, "AccountSettingsFragment onSaveInstanceState");
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity provides callbacks here
|
|
|
|
*/
|
|
|
|
public void setCallback(Callback callback) {
|
|
|
|
mCallback = (callback == null) ? EmptyCallback.INSTANCE : callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-18 18:29:46 +00:00
|
|
|
* Start loading a single account in preparation for editing it
|
2010-08-09 17:36:50 +00:00
|
|
|
*/
|
2010-08-18 18:29:46 +00:00
|
|
|
public void startLoadingAccount(long accountId) {
|
|
|
|
Utility.cancelTaskInterrupt(mLoadAccountTask);
|
|
|
|
mLoadAccountTask = new LoadAccountTask().execute(accountId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Async task to load account in order to view/edit it
|
|
|
|
*/
|
2010-12-22 21:55:19 +00:00
|
|
|
private class LoadAccountTask extends AsyncTask<Long, Void, Object[]> {
|
2010-08-18 18:29:46 +00:00
|
|
|
@Override
|
2010-12-22 21:55:19 +00:00
|
|
|
protected Object[] doInBackground(Long... params) {
|
2010-08-18 18:29:46 +00:00
|
|
|
long accountId = params[0];
|
|
|
|
Account account = Account.restoreAccountWithId(mContext, accountId);
|
|
|
|
if (account != null) {
|
|
|
|
account.mHostAuthRecv =
|
|
|
|
HostAuth.restoreHostAuthWithId(mContext, account.mHostAuthKeyRecv);
|
|
|
|
account.mHostAuthSend =
|
|
|
|
HostAuth.restoreHostAuthWithId(mContext, account.mHostAuthKeySend);
|
|
|
|
if (account.mHostAuthRecv == null || account.mHostAuthSend == null) {
|
|
|
|
account = null;
|
|
|
|
}
|
|
|
|
}
|
2010-12-22 21:55:19 +00:00
|
|
|
long defaultAccountId = Account.getDefaultAccountId(mContext);
|
|
|
|
return new Object[] { account, Long.valueOf(defaultAccountId) };
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
2010-08-18 18:29:46 +00:00
|
|
|
|
|
|
|
@Override
|
2010-12-22 21:55:19 +00:00
|
|
|
protected void onPostExecute(Object[] results) {
|
|
|
|
if (results != null && !isCancelled()) {
|
|
|
|
Account account = (Account) results[0];
|
2010-08-18 18:29:46 +00:00
|
|
|
if (account == null) {
|
|
|
|
mSaveOnExit = false;
|
|
|
|
mCallback.abandonEdit();
|
|
|
|
} else {
|
|
|
|
mAccount = account;
|
2010-12-22 21:55:19 +00:00
|
|
|
mDefaultAccountId = (Long) results[1];
|
2010-08-18 18:29:46 +00:00
|
|
|
if (mStarted && !mLoaded) {
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load account data into preference UI
|
|
|
|
*/
|
|
|
|
private void loadSettings() {
|
|
|
|
// We can only do this once, so prevent repeat
|
|
|
|
mLoaded = true;
|
2010-08-18 18:29:46 +00:00
|
|
|
// Once loaded the data is ready to be saved, as well
|
2010-12-22 21:55:19 +00:00
|
|
|
mSaveOnExit = false;
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
PreferenceCategory topCategory =
|
2010-11-05 04:13:45 +00:00
|
|
|
(PreferenceCategory) findPreference(PREFERENCE_CATEGORY_TOP);
|
2010-08-09 17:36:50 +00:00
|
|
|
topCategory.setTitle(mContext.getString(R.string.account_settings_title_fmt));
|
|
|
|
|
|
|
|
mAccountDescription = (EditTextPreference) findPreference(PREFERENCE_DESCRIPTION);
|
|
|
|
mAccountDescription.setSummary(mAccount.getDisplayName());
|
|
|
|
mAccountDescription.setText(mAccount.getDisplayName());
|
|
|
|
mAccountDescription.setOnPreferenceChangeListener(
|
|
|
|
new Preference.OnPreferenceChangeListener() {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
2011-01-20 19:37:39 +00:00
|
|
|
String summary = newValue.toString().trim();
|
|
|
|
if (TextUtils.isEmpty(summary)) {
|
|
|
|
summary = mAccount.mEmailAddress;
|
|
|
|
}
|
2010-08-09 17:36:50 +00:00
|
|
|
mAccountDescription.setSummary(summary);
|
|
|
|
mAccountDescription.setText(summary);
|
2010-12-22 21:55:19 +00:00
|
|
|
onPreferenceChanged();
|
2010-08-09 17:36:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
mAccountName = (EditTextPreference) findPreference(PREFERENCE_NAME);
|
|
|
|
mAccountName.setSummary(mAccount.getSenderName());
|
|
|
|
mAccountName.setText(mAccount.getSenderName());
|
|
|
|
mAccountName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
2011-01-20 19:37:39 +00:00
|
|
|
final String summary = newValue.toString().trim();
|
|
|
|
if (!TextUtils.isEmpty(summary)) {
|
|
|
|
mAccountName.setSummary(summary);
|
|
|
|
mAccountName.setText(summary);
|
|
|
|
onPreferenceChanged();
|
|
|
|
}
|
2010-08-09 17:36:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mAccountSignature = (EditTextPreference) findPreference(PREFERENCE_SIGNATURE);
|
2011-01-20 19:37:39 +00:00
|
|
|
String signature = mAccount.getSignature();
|
2010-08-09 17:36:50 +00:00
|
|
|
mAccountSignature.setText(mAccount.getSignature());
|
|
|
|
mAccountSignature.setOnPreferenceChangeListener(
|
2011-01-24 20:01:43 +00:00
|
|
|
new Preference.OnPreferenceChangeListener() {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
// Clean up signature if it's only whitespace (which is easy to do on a
|
|
|
|
// soft keyboard) but leave whitespace in place otherwise, to give the user
|
|
|
|
// maximum flexibility, e.g. the ability to indent
|
|
|
|
String signature = newValue.toString();
|
|
|
|
if (signature.trim().isEmpty()) {
|
|
|
|
signature = "";
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
2011-01-24 20:01:43 +00:00
|
|
|
mAccountSignature.setText(signature);
|
|
|
|
onPreferenceChanged();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
mCheckFrequency = (ListPreference) findPreference(PREFERENCE_FREQUENCY);
|
|
|
|
|
|
|
|
// Before setting value, we may need to adjust the lists
|
|
|
|
Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(mContext),
|
|
|
|
mContext);
|
|
|
|
if (info.mPushSupported) {
|
|
|
|
mCheckFrequency.setEntries(R.array.account_settings_check_frequency_entries_push);
|
|
|
|
mCheckFrequency.setEntryValues(R.array.account_settings_check_frequency_values_push);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCheckFrequency.setValue(String.valueOf(mAccount.getSyncInterval()));
|
|
|
|
mCheckFrequency.setSummary(mCheckFrequency.getEntry());
|
|
|
|
mCheckFrequency.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
final String summary = newValue.toString();
|
|
|
|
int index = mCheckFrequency.findIndexOfValue(summary);
|
|
|
|
mCheckFrequency.setSummary(mCheckFrequency.getEntries()[index]);
|
|
|
|
mCheckFrequency.setValue(summary);
|
2010-12-22 21:55:19 +00:00
|
|
|
onPreferenceChanged();
|
2010-08-09 17:36:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add check window preference
|
|
|
|
mSyncWindow = null;
|
|
|
|
if (info.mVisibleLimitDefault == -1) {
|
|
|
|
mSyncWindow = new ListPreference(mContext);
|
|
|
|
mSyncWindow.setTitle(R.string.account_setup_options_mail_window_label);
|
|
|
|
mSyncWindow.setEntries(R.array.account_settings_mail_window_entries);
|
|
|
|
mSyncWindow.setEntryValues(R.array.account_settings_mail_window_values);
|
|
|
|
mSyncWindow.setValue(String.valueOf(mAccount.getSyncLookback()));
|
|
|
|
mSyncWindow.setSummary(mSyncWindow.getEntry());
|
|
|
|
mSyncWindow.setOrder(4);
|
|
|
|
mSyncWindow.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
final String summary = newValue.toString();
|
|
|
|
int index = mSyncWindow.findIndexOfValue(summary);
|
|
|
|
mSyncWindow.setSummary(mSyncWindow.getEntries()[index]);
|
|
|
|
mSyncWindow.setValue(summary);
|
2010-12-22 21:55:19 +00:00
|
|
|
onPreferenceChanged();
|
2010-08-09 17:36:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
topCategory.addPreference(mSyncWindow);
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:53:37 +00:00
|
|
|
// Show "background attachments" for IMAP & EAS - hide it for POP3.
|
|
|
|
mAccountBackgroundAttachments = (CheckBoxPreference)
|
|
|
|
findPreference(PREFERENCE_BACKGROUND_ATTACHMENTS);
|
|
|
|
if ("pop3".equals(mAccount.mHostAuthRecv.mProtocol)) {
|
|
|
|
topCategory.removePreference(mAccountBackgroundAttachments);
|
|
|
|
} else {
|
|
|
|
mAccountBackgroundAttachments.setChecked(
|
|
|
|
0 != (mAccount.getFlags() & Account.FLAGS_BACKGROUND_ATTACHMENTS));
|
|
|
|
mAccountBackgroundAttachments.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
|
|
|
}
|
|
|
|
|
2010-08-09 17:36:50 +00:00
|
|
|
mAccountDefault = (CheckBoxPreference) findPreference(PREFERENCE_DEFAULT);
|
2010-12-22 21:55:19 +00:00
|
|
|
mAccountDefault.setChecked(mAccount.mId == mDefaultAccountId);
|
|
|
|
mAccountDefault.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
mAccountNotify = (CheckBoxPreference) findPreference(PREFERENCE_NOTIFY);
|
|
|
|
mAccountNotify.setChecked(0 != (mAccount.getFlags() & Account.FLAGS_NOTIFY_NEW_MAIL));
|
2010-12-22 21:55:19 +00:00
|
|
|
mAccountNotify.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
mAccountRingtone = (RingtonePreference) findPreference(PREFERENCE_RINGTONE);
|
2010-12-22 21:55:19 +00:00
|
|
|
mAccountRingtone.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
// The following two lines act as a workaround for the RingtonePreference
|
|
|
|
// which does not let us set/get the value programmatically
|
|
|
|
SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences();
|
2010-11-01 15:51:26 +00:00
|
|
|
prefs.edit().putString(PREFERENCE_RINGTONE, mAccount.getRingtone()).apply();
|
2010-08-09 17:36:50 +00:00
|
|
|
|
2010-11-05 04:13:45 +00:00
|
|
|
// Set the vibrator value, or hide it on devices w/o a vibrator
|
2010-08-09 17:36:50 +00:00
|
|
|
mAccountVibrateWhen = (ListPreference) findPreference(PREFERENCE_VIBRATE_WHEN);
|
2010-11-05 04:13:45 +00:00
|
|
|
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
|
|
|
|
if (vibrator.hasVibrator()) {
|
|
|
|
boolean flagsVibrate = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_ALWAYS);
|
|
|
|
boolean flagsVibrateSilent =
|
|
|
|
0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_WHEN_SILENT);
|
|
|
|
mAccountVibrateWhen.setValue(
|
|
|
|
flagsVibrate ? PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS :
|
|
|
|
flagsVibrateSilent ? PREFERENCE_VALUE_VIBRATE_WHEN_SILENT :
|
|
|
|
PREFERENCE_VALUE_VIBRATE_WHEN_NEVER);
|
2010-12-22 21:55:19 +00:00
|
|
|
mAccountVibrateWhen.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-11-05 04:13:45 +00:00
|
|
|
} else {
|
|
|
|
PreferenceCategory notificationsCategory = (PreferenceCategory)
|
|
|
|
findPreference(PREFERENCE_CATEGORY_NOTIFICATIONS);
|
|
|
|
notificationsCategory.removePreference(mAccountVibrateWhen);
|
|
|
|
}
|
2010-08-09 17:36:50 +00:00
|
|
|
|
|
|
|
findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener(
|
|
|
|
new Preference.OnPreferenceClickListener() {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
mAccountDirty = true;
|
2010-08-18 18:29:46 +00:00
|
|
|
mCallback.onIncomingSettings(mAccount);
|
2010-08-09 17:36:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Hide the outgoing account setup link if it's not activated
|
|
|
|
Preference prefOutgoing = findPreference(PREFERENCE_OUTGOING);
|
|
|
|
boolean showOutgoing = true;
|
|
|
|
try {
|
|
|
|
Sender sender = Sender.getInstance(mContext, mAccount.getSenderUri(mContext));
|
|
|
|
if (sender != null) {
|
|
|
|
Class<? extends android.app.Activity> setting = sender.getSettingActivityClass();
|
|
|
|
showOutgoing = (setting != null);
|
|
|
|
}
|
|
|
|
} catch (MessagingException me) {
|
|
|
|
// just leave showOutgoing as true - bias towards showing it, so user can fix it
|
|
|
|
}
|
|
|
|
if (showOutgoing) {
|
|
|
|
prefOutgoing.setOnPreferenceClickListener(
|
|
|
|
new Preference.OnPreferenceClickListener() {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
mAccountDirty = true;
|
2010-08-18 18:29:46 +00:00
|
|
|
mCallback.onOutgoingSettings(mAccount);
|
2010-08-09 17:36:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
PreferenceCategory serverCategory = (PreferenceCategory) findPreference(
|
2010-11-05 04:13:45 +00:00
|
|
|
PREFERENCE_CATEGORY_SERVER);
|
2010-08-09 17:36:50 +00:00
|
|
|
serverCategory.removePreference(prefOutgoing);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSyncContacts = (CheckBoxPreference) findPreference(PREFERENCE_SYNC_CONTACTS);
|
|
|
|
mSyncCalendar = (CheckBoxPreference) findPreference(PREFERENCE_SYNC_CALENDAR);
|
2010-10-07 22:04:20 +00:00
|
|
|
mSyncEmail = (CheckBoxPreference) findPreference(PREFERENCE_SYNC_EMAIL);
|
2010-08-09 17:36:50 +00:00
|
|
|
if (mAccount.mHostAuthRecv.mProtocol.equals("eas")) {
|
|
|
|
android.accounts.Account acct = new android.accounts.Account(mAccount.mEmailAddress,
|
|
|
|
Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
|
|
|
|
mSyncContacts.setChecked(ContentResolver
|
|
|
|
.getSyncAutomatically(acct, ContactsContract.AUTHORITY));
|
2010-12-22 21:55:19 +00:00
|
|
|
mSyncContacts.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-08-09 17:36:50 +00:00
|
|
|
mSyncCalendar.setChecked(ContentResolver
|
|
|
|
.getSyncAutomatically(acct, Calendar.AUTHORITY));
|
2010-12-22 21:55:19 +00:00
|
|
|
mSyncCalendar.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-10-07 22:04:20 +00:00
|
|
|
mSyncEmail.setChecked(ContentResolver
|
|
|
|
.getSyncAutomatically(acct, EmailContent.AUTHORITY));
|
2010-12-22 21:55:19 +00:00
|
|
|
mSyncEmail.setOnPreferenceChangeListener(mPreferenceChangeListener);
|
2010-08-09 17:36:50 +00:00
|
|
|
} else {
|
|
|
|
PreferenceCategory serverCategory = (PreferenceCategory) findPreference(
|
2010-11-05 04:13:45 +00:00
|
|
|
PREFERENCE_CATEGORY_SERVER);
|
2010-08-09 17:36:50 +00:00
|
|
|
serverCategory.removePreference(mSyncContacts);
|
|
|
|
serverCategory.removePreference(mSyncCalendar);
|
2010-10-07 22:04:20 +00:00
|
|
|
serverCategory.removePreference(mSyncEmail);
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
2010-08-19 23:28:52 +00:00
|
|
|
|
|
|
|
// Temporary home for delete account
|
|
|
|
Preference prefDeleteAccount = findPreference(PREFERENCE_DELETE_ACCOUNT);
|
|
|
|
prefDeleteAccount.setOnPreferenceClickListener(
|
|
|
|
new Preference.OnPreferenceClickListener() {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
DeleteAccountFragment dialogFragment = DeleteAccountFragment.newInstance(
|
|
|
|
mAccount, AccountSettingsFragment.this);
|
2011-01-17 20:30:51 +00:00
|
|
|
FragmentTransaction ft = getFragmentManager().beginTransaction();
|
2010-09-13 02:38:10 +00:00
|
|
|
ft.addToBackStack(null);
|
|
|
|
dialogFragment.show(ft, DeleteAccountFragment.TAG);
|
2010-08-19 23:28:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 21:55:19 +00:00
|
|
|
/**
|
|
|
|
* Generic onPreferenceChanged listener for the preferences (above) that just need
|
|
|
|
* to be written, without extra tweaks
|
|
|
|
*/
|
|
|
|
private Preference.OnPreferenceChangeListener mPreferenceChangeListener =
|
|
|
|
new Preference.OnPreferenceChangeListener() {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
onPreferenceChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called any time a preference is changed.
|
|
|
|
*/
|
|
|
|
private void onPreferenceChanged() {
|
|
|
|
mSaveOnExit = true;
|
|
|
|
}
|
|
|
|
|
2010-08-18 18:29:46 +00:00
|
|
|
/*
|
2010-12-22 21:55:19 +00:00
|
|
|
* Note: This writes the settings on the UI thread. This has to be done so the settings are
|
|
|
|
* committed before we might be killed.
|
2010-08-18 18:29:46 +00:00
|
|
|
*/
|
2010-12-22 21:55:19 +00:00
|
|
|
private void saveSettings() {
|
2011-01-18 18:53:37 +00:00
|
|
|
// Turn off all controlled flags - will turn them back on while checking UI elements
|
2010-08-09 17:36:50 +00:00
|
|
|
int newFlags = mAccount.getFlags() &
|
|
|
|
~(Account.FLAGS_NOTIFY_NEW_MAIL |
|
2011-01-18 18:53:37 +00:00
|
|
|
Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_VIBRATE_WHEN_SILENT |
|
|
|
|
Account.FLAGS_BACKGROUND_ATTACHMENTS);
|
2010-08-09 17:36:50 +00:00
|
|
|
|
2011-01-18 18:53:37 +00:00
|
|
|
newFlags |= mAccountBackgroundAttachments.isChecked() ?
|
|
|
|
Account.FLAGS_BACKGROUND_ATTACHMENTS : 0;
|
2010-08-09 17:36:50 +00:00
|
|
|
mAccount.setDefaultAccount(mAccountDefault.isChecked());
|
2011-01-20 19:37:39 +00:00
|
|
|
// If the display name has been cleared, we'll reset it to the default value (email addr)
|
|
|
|
mAccount.setDisplayName(mAccountDescription.getText().trim());
|
|
|
|
// The sender name must never be empty (this is enforced by the preference editor)
|
|
|
|
mAccount.setSenderName(mAccountName.getText().trim());
|
2010-08-09 17:36:50 +00:00
|
|
|
mAccount.setSignature(mAccountSignature.getText());
|
|
|
|
newFlags |= mAccountNotify.isChecked() ? Account.FLAGS_NOTIFY_NEW_MAIL : 0;
|
|
|
|
mAccount.setSyncInterval(Integer.parseInt(mCheckFrequency.getValue()));
|
|
|
|
if (mSyncWindow != null) {
|
|
|
|
mAccount.setSyncLookback(Integer.parseInt(mSyncWindow.getValue()));
|
|
|
|
}
|
|
|
|
if (mAccountVibrateWhen.getValue().equals(PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS)) {
|
|
|
|
newFlags |= Account.FLAGS_VIBRATE_ALWAYS;
|
|
|
|
} else if (mAccountVibrateWhen.getValue().equals(PREFERENCE_VALUE_VIBRATE_WHEN_SILENT)) {
|
|
|
|
newFlags |= Account.FLAGS_VIBRATE_WHEN_SILENT;
|
|
|
|
}
|
|
|
|
SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences();
|
|
|
|
mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null));
|
|
|
|
mAccount.setFlags(newFlags);
|
|
|
|
|
|
|
|
if (mAccount.mHostAuthRecv.mProtocol.equals("eas")) {
|
|
|
|
android.accounts.Account acct = new android.accounts.Account(mAccount.mEmailAddress,
|
|
|
|
Email.EXCHANGE_ACCOUNT_MANAGER_TYPE);
|
|
|
|
ContentResolver.setSyncAutomatically(acct, ContactsContract.AUTHORITY,
|
|
|
|
mSyncContacts.isChecked());
|
|
|
|
ContentResolver.setSyncAutomatically(acct, Calendar.AUTHORITY,
|
|
|
|
mSyncCalendar.isChecked());
|
2010-10-07 22:04:20 +00:00
|
|
|
ContentResolver.setSyncAutomatically(acct, EmailContent.AUTHORITY,
|
|
|
|
mSyncEmail.isChecked());
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
2010-12-22 21:55:19 +00:00
|
|
|
|
|
|
|
// Commit the changes
|
|
|
|
// Note, this is done in the UI thread because at this point, we must commit
|
|
|
|
// all changes - any time after onPause completes, we could be killed. This is analogous
|
|
|
|
// to the way that SharedPreferences tries to work off-thread in apply(), but will pause
|
|
|
|
// until completion in onPause().
|
|
|
|
ContentValues cv = AccountSettingsUtils.getAccountContentValues(mAccount);
|
|
|
|
mAccount.update(mContext, cv);
|
|
|
|
|
|
|
|
// Run the remaining changes off-thread
|
2010-12-23 21:19:55 +00:00
|
|
|
Email.setServicesEnabledAsync(mContext);
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|
2010-08-19 23:28:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dialog fragment to show "remove account?" dialog
|
|
|
|
*/
|
|
|
|
public static class DeleteAccountFragment extends DialogFragment {
|
|
|
|
private final static String TAG = "DeleteAccountFragment";
|
|
|
|
|
|
|
|
// Argument bundle keys
|
|
|
|
private final static String BUNDLE_KEY_ACCOUNT_NAME = "DeleteAccountFragment.Name";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the dialog with parameters
|
|
|
|
*/
|
|
|
|
public static DeleteAccountFragment newInstance(Account account, Fragment parentFragment) {
|
|
|
|
DeleteAccountFragment f = new DeleteAccountFragment();
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putString(BUNDLE_KEY_ACCOUNT_NAME, account.getDisplayName());
|
|
|
|
f.setArguments(b);
|
|
|
|
f.setTargetFragment(parentFragment, 0);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
Context context = getActivity();
|
|
|
|
final String name = getArguments().getString(BUNDLE_KEY_ACCOUNT_NAME);
|
|
|
|
|
|
|
|
return new AlertDialog.Builder(context)
|
2011-01-18 18:14:14 +00:00
|
|
|
.setIconAttribute(android.R.attr.alertDialogIcon)
|
2010-08-19 23:28:52 +00:00
|
|
|
.setTitle(R.string.account_delete_dlg_title)
|
|
|
|
.setMessage(context.getString(R.string.account_delete_dlg_instructions_fmt, name))
|
|
|
|
.setPositiveButton(
|
|
|
|
R.string.okay_action,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton) {
|
|
|
|
Fragment f = getTargetFragment();
|
|
|
|
if (f instanceof AccountSettingsFragment) {
|
|
|
|
((AccountSettingsFragment)f).finishDeleteAccount();
|
|
|
|
}
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(
|
|
|
|
R.string.cancel_action,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton) {
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback from delete account dialog - passes the delete command up to the activity
|
|
|
|
*/
|
|
|
|
private void finishDeleteAccount() {
|
|
|
|
mSaveOnExit = false;
|
|
|
|
mCallback.deleteAccount(mAccount);
|
|
|
|
}
|
2010-08-09 17:36:50 +00:00
|
|
|
}
|