Per-mailbox settings
- Use PreferenceActivity, in the old style, meaning without PreferenceFragment. - If setting Inbox, change the account settings instead of mailbox settings. - Use the DialogWhenLarge theme, meaning it's a full-screen avitity on the phone and a dialog on the tablet. - Also fixed the bug that we the menu items that are made invisible by UIControllerBase may be made re-visible by the 1-pane controller. TODO The menu item shouldn't be shown for non-syncable mailboxes. Change-Id: I02b2faf6f593e1e2eb370217c27801aa58ca7e6c
This commit is contained in:
parent
8067daaa0f
commit
1ddc2ca4a7
|
@ -262,6 +262,13 @@
|
|||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".activity.setup.MailboxSettings"
|
||||
android:label="@string/mailbox_settings_activity_title"
|
||||
android:theme="@android:style/Theme.Holo.DialogWhenLarge"
|
||||
>
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".service.AttachmentDownloadService$Watchdog"
|
||||
android:enabled="true"/>
|
||||
|
|
|
@ -58,15 +58,9 @@
|
|||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/sync_frequency"
|
||||
android:id="@+id/mailbox_settings"
|
||||
android:orderInCategory="3000"
|
||||
android:title="@string/mailbox_options_check_frequency_label"
|
||||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/sync_lookback"
|
||||
android:orderInCategory="3100"
|
||||
android:title="@string/mailbox_options_lookback_label"
|
||||
android:title="@string/mailbox_settings_action"
|
||||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
/>
|
||||
</menu>
|
||||
|
|
|
@ -63,15 +63,9 @@
|
|||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/sync_frequency"
|
||||
android:id="@+id/mailbox_settings"
|
||||
android:orderInCategory="3000"
|
||||
android:title="@string/mailbox_options_check_frequency_label"
|
||||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/sync_lookback"
|
||||
android:orderInCategory="3100"
|
||||
android:title="@string/mailbox_options_lookback_label"
|
||||
android:title="@string/mailbox_settings_action"
|
||||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
/>
|
||||
</menu>
|
||||
|
|
|
@ -83,6 +83,32 @@
|
|||
<item>6</item>
|
||||
</string-array>
|
||||
|
||||
<!-- Mail sync window sizes for EAS mailboxes. It's the same thing as
|
||||
account_settings_mail_window_entries, but has 0=default. -->
|
||||
<string-array name="account_settings_mail_window_entries_with_default">
|
||||
<item>@string/account_setup_options_mail_window_default</item>
|
||||
<item>@string/account_setup_options_mail_window_auto</item>
|
||||
<item>@string/account_setup_options_mail_window_1day</item>
|
||||
<item>@string/account_setup_options_mail_window_3days</item>
|
||||
<item>@string/account_setup_options_mail_window_1week</item>
|
||||
<item>@string/account_setup_options_mail_window_2weeks</item>
|
||||
<item>@string/account_setup_options_mail_window_1month</item>
|
||||
<item>@string/account_setup_options_mail_window_all</item>
|
||||
</string-array>
|
||||
|
||||
<!-- Mail sync window size codes for EAS milboxes. It's the same thing as
|
||||
account_settings_mail_window_values, but has 0=default. -->
|
||||
<string-array name="account_settings_mail_window_values_with_default" translatable="false">
|
||||
<item>0</item>
|
||||
<item>-2</item>
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
<item>5</item>
|
||||
<item>6</item>
|
||||
</string-array>
|
||||
|
||||
<!-- The vibrate notification modes -->
|
||||
<string-array name="account_settings_vibrate_when_entries">
|
||||
<!-- Always -->
|
||||
|
|
|
@ -128,6 +128,8 @@
|
|||
<string name="search_action">Search</string>
|
||||
<!-- Menu item -->
|
||||
<string name="account_settings_action">Account settings</string>
|
||||
<!-- Menu item [CHAR LIMIT=32] -->
|
||||
<string name="mailbox_settings_action">Folder settings</string>
|
||||
<!-- Menu item -->
|
||||
<string name="mark_as_unread_action">Mark as unread</string>
|
||||
<!-- Menu item for moving messages to folders [CHAR LIMIT=10] -->
|
||||
|
@ -775,6 +777,8 @@ save attachment.</string>
|
|||
<string name="account_setup_options_mail_window_1month">One month</string>
|
||||
<!-- A sync window length setting (i.e. load messages this far back) [CHAR LIMIT=25] -->
|
||||
<string name="account_setup_options_mail_window_all">All</string>
|
||||
<!-- Per-mailbox sync window setting. This means "use the account's sync window setting" [CHAR LIMIT=25] -->
|
||||
<string name="account_setup_options_mail_window_default">Use account\'s default</string>
|
||||
|
||||
<!-- "Setup could not finish" dialog text; e.g., Username or password incorrect -->
|
||||
<string name="account_setup_failed_dlg_auth_message">Username or password incorrect.</string>
|
||||
|
@ -1014,6 +1018,17 @@ save attachment.</string>
|
|||
<!-- On Settings screen, settings option for delete account [CHAR LIMIT=none] -->
|
||||
<string name="account_settings_delete_account_label">Remove account</string>
|
||||
|
||||
<!-- Mailbox settings activity title [CHAR LIMIT=none] -->
|
||||
<string name="mailbox_settings_activity_title">Folder settings</string>
|
||||
<!-- Mailbox settings activity title, with the target folder name [CHAR LIMIT=none] -->
|
||||
<string name="mailbox_settings_activity_title_with_mailbox">Folder settings (<xliff:g id="mailboxx_name" example="Family">%s</xliff:g>)</string>
|
||||
<!-- Mailbox "sync settings" preference header [CHAR LIMIT=none] -->
|
||||
<string name="mailbox_preferences_header">Sync settings</string>
|
||||
<!-- On mailbox settings screen: Mailbox check frequency setting label [CHAR LIMIT=none] -->
|
||||
<string name="mailbox_settings_mailbox_check_frequency_label">Check frequency</string>
|
||||
<!-- On mailbox settings screen: Mailbox sync window (the number of days to synchronize email for) setting label [CHAR LIMIT=none] -->
|
||||
<string name="mailbox_settings_mailbox_sync_window_label">Amount to synchronize</string>
|
||||
|
||||
<!-- Strings used for account shortcut picker -->
|
||||
<!-- String displayed in launcher [CHAR_LIMIT=10] -->
|
||||
<string name="account_shortcut_picker_name">Email account</string>
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2011 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.
|
||||
-->
|
||||
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/mailbox_preferences_header">
|
||||
|
||||
<!-- summary is set at runtime -->
|
||||
<ListPreference
|
||||
android:key="check_frequency"
|
||||
android:title="@string/mailbox_settings_mailbox_check_frequency_label"
|
||||
android:dialogTitle="@string/mailbox_settings_mailbox_check_frequency_label"
|
||||
android:entries="@array/account_settings_check_frequency_entries_push"
|
||||
android:entryValues="@array/account_settings_check_frequency_values_push"
|
||||
android:persistent="false"
|
||||
/>
|
||||
|
||||
<!-- summary is set at runtime -->
|
||||
<ListPreference
|
||||
android:key="sync_window"
|
||||
android:title="@string/mailbox_settings_mailbox_sync_window_label"
|
||||
android:dialogTitle="@string/mailbox_settings_mailbox_sync_window_label"
|
||||
android:entries="@array/account_settings_mail_window_entries_with_default"
|
||||
android:entryValues="@array/account_settings_mail_window_values_with_default"
|
||||
android:persistent="false"
|
||||
/>
|
||||
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
|
@ -17,12 +17,7 @@
|
|||
package com.android.email.activity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.app.Fragment;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
|
@ -42,7 +37,6 @@ import com.android.email.R;
|
|||
import com.android.emailcommon.Logging;
|
||||
import com.android.emailcommon.mail.MessagingException;
|
||||
import com.android.emailcommon.provider.Account;
|
||||
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
|
||||
import com.android.emailcommon.provider.EmailContent.Message;
|
||||
import com.android.emailcommon.provider.Mailbox;
|
||||
import com.android.emailcommon.utility.EmailAsyncTask;
|
||||
|
@ -83,9 +77,6 @@ public class EmailActivity extends Activity implements View.OnClickListener, Fra
|
|||
/** Id of the account that had a messaging exception most recently. */
|
||||
private long mLastErrorAccountId;
|
||||
|
||||
// STOPSHIP Temporary mailbox settings UI
|
||||
private int mDialogSelection = -1;
|
||||
|
||||
/**
|
||||
* Create an intent to launch and open account's inbox.
|
||||
*
|
||||
|
@ -333,119 +324,15 @@ public class EmailActivity extends Activity implements View.OnClickListener, Fra
|
|||
return true; // Event handled.
|
||||
}
|
||||
|
||||
// STOPSHIP Set column from user options
|
||||
private void setMailboxColumn(long mailboxId, String column, String value) {
|
||||
if (mailboxId > 0) {
|
||||
ContentValues cv = new ContentValues();
|
||||
cv.put(column, value);
|
||||
getContentResolver().update(
|
||||
ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId),
|
||||
cv, null, null);
|
||||
mUIController.onRefresh();
|
||||
}
|
||||
}
|
||||
// STOPSHIP Temporary mailbox settings UI. If this ends up being useful, it should
|
||||
// be moved to Utility (emailcommon)
|
||||
private int findInStringArray(String[] array, String item) {
|
||||
int i = 0;
|
||||
for (String str: array) {
|
||||
if (str.equals(item)) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// STOPSHIP Temporary mailbox settings UI
|
||||
private final DialogInterface.OnClickListener mSelectionListener =
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mDialogSelection = which;
|
||||
}
|
||||
};
|
||||
|
||||
// STOPSHIP Temporary mailbox settings UI
|
||||
private final DialogInterface.OnClickListener mCancelListener =
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
}
|
||||
};
|
||||
|
||||
// STOPSHIP Temporary mailbox settings UI
|
||||
@Override
|
||||
@Deprecated
|
||||
protected Dialog onCreateDialog(int id, Bundle args) {
|
||||
final long mailboxId = mUIController.getMailboxSettingsMailboxId();
|
||||
if (mailboxId < 0) {
|
||||
return null;
|
||||
}
|
||||
final Mailbox mailbox = Mailbox.restoreMailboxWithId(this, mailboxId);
|
||||
if (mailbox == null) return null;
|
||||
switch (id) {
|
||||
case MAILBOX_SYNC_FREQUENCY_DIALOG:
|
||||
String freq = Integer.toString(mailbox.mSyncInterval);
|
||||
final String[] freqValues = getResources().getStringArray(
|
||||
R.array.account_settings_check_frequency_values_push);
|
||||
int selection = findInStringArray(freqValues, freq);
|
||||
// If not found, this is a push mailbox; trust me on this
|
||||
if (selection == -1) selection = 0;
|
||||
return new AlertDialog.Builder(this)
|
||||
.setIconAttribute(android.R.attr.dialogIcon)
|
||||
.setTitle(R.string.mailbox_options_check_frequency_label)
|
||||
.setSingleChoiceItems(R.array.account_settings_check_frequency_entries_push,
|
||||
selection,
|
||||
mSelectionListener)
|
||||
.setPositiveButton(R.string.okay_action, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
setMailboxColumn(mailboxId, MailboxColumns.SYNC_INTERVAL,
|
||||
freqValues[mDialogSelection]);
|
||||
}})
|
||||
.setNegativeButton(R.string.cancel_action, mCancelListener)
|
||||
.create();
|
||||
|
||||
case MAILBOX_SYNC_LOOKBACK_DIALOG:
|
||||
freq = Integer.toString(mailbox.mSyncLookback);
|
||||
final String[] windowValues = getResources().getStringArray(
|
||||
R.array.account_settings_mail_window_values);
|
||||
selection = findInStringArray(windowValues, freq);
|
||||
return new AlertDialog.Builder(this)
|
||||
.setIconAttribute(android.R.attr.dialogIcon)
|
||||
.setTitle(R.string.mailbox_options_lookback_label)
|
||||
.setSingleChoiceItems(R.array.account_settings_mail_window_entries,
|
||||
selection,
|
||||
mSelectionListener)
|
||||
.setPositiveButton(R.string.okay_action, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
setMailboxColumn(mailboxId, MailboxColumns.SYNC_LOOKBACK,
|
||||
windowValues[mDialogSelection]);
|
||||
}})
|
||||
.setNegativeButton(R.string.cancel_action, mCancelListener)
|
||||
.create();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (mUIController.onOptionsItemSelected(item)) {
|
||||
return true;
|
||||
}
|
||||
switch (item.getItemId()) {
|
||||
// STOPSHIP Temporary mailbox settings UI
|
||||
case R.id.sync_lookback:
|
||||
showDialog(MAILBOX_SYNC_LOOKBACK_DIALOG);
|
||||
return true;
|
||||
// STOPSHIP Temporary mailbox settings UI
|
||||
case R.id.sync_frequency:
|
||||
showDialog(MAILBOX_SYNC_FREQUENCY_DIALOG);
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A {@link Controller.Result} to detect connection status.
|
||||
*/
|
||||
|
|
|
@ -33,6 +33,7 @@ import com.android.email.Preferences;
|
|||
import com.android.email.R;
|
||||
import com.android.email.RefreshManager;
|
||||
import com.android.email.activity.setup.AccountSettings;
|
||||
import com.android.email.activity.setup.MailboxSettings;
|
||||
import com.android.emailcommon.Logging;
|
||||
import com.android.emailcommon.provider.Account;
|
||||
import com.android.emailcommon.provider.EmailContent.Message;
|
||||
|
@ -735,8 +736,9 @@ abstract class UIControllerBase implements MailboxListFragment.Callback,
|
|||
|
||||
// TODO: Should use an isSyncable call to prevent drafts/outbox from allowing this
|
||||
menu.findItem(R.id.search).setVisible(accountSearchable && isMessageListReady());
|
||||
menu.findItem(R.id.sync_lookback).setVisible(isEas);
|
||||
menu.findItem(R.id.sync_frequency).setVisible(isEas);
|
||||
// TODO Show only for syncable mailbox as well.
|
||||
menu.findItem(R.id.mailbox_settings).setVisible(isEas
|
||||
&& (getMailboxSettingsMailboxId() != Mailbox.NO_MAILBOX));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -762,6 +764,12 @@ abstract class UIControllerBase implements MailboxListFragment.Callback,
|
|||
case R.id.search:
|
||||
onSearchRequested();
|
||||
return true;
|
||||
case R.id.mailbox_settings:
|
||||
final long mailboxId = getMailboxSettingsMailboxId();
|
||||
if (mailboxId != Mailbox.NO_MAILBOX) {
|
||||
MailboxSettings.start(mActivity, mailboxId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -797,18 +805,14 @@ abstract class UIControllerBase implements MailboxListFragment.Callback,
|
|||
|
||||
|
||||
/**
|
||||
* STOPSHIP For experimental UI. Remove this.
|
||||
*
|
||||
* @return mailbox ID for "mailbox settings" option.
|
||||
*/
|
||||
public abstract long getMailboxSettingsMailboxId();
|
||||
protected abstract long getMailboxSettingsMailboxId();
|
||||
|
||||
/**
|
||||
* STOPSHIP For experimental UI. Make it abstract protected.
|
||||
*
|
||||
* Performs "refesh".
|
||||
*/
|
||||
public abstract void onRefresh();
|
||||
protected abstract void onRefresh();
|
||||
|
||||
/**
|
||||
* @return true if refresh is in progress for the current mailbox.
|
||||
|
|
|
@ -562,11 +562,8 @@ class UIControllerOnePane extends UIControllerBase {
|
|||
super.installMessageListFragment(fragment);
|
||||
}
|
||||
|
||||
/*
|
||||
* STOPSHIP Remove this -- see the base class method.
|
||||
*/
|
||||
@Override
|
||||
public long getMailboxSettingsMailboxId() {
|
||||
protected long getMailboxSettingsMailboxId() {
|
||||
return isMessageListInstalled()
|
||||
? getMessageListFragment().getMailboxId()
|
||||
: Mailbox.NO_MAILBOX;
|
||||
|
@ -579,13 +576,13 @@ class UIControllerOnePane extends UIControllerBase {
|
|||
|
||||
// Then override
|
||||
final boolean messageViewVisible = isMessageViewInstalled();
|
||||
menu.findItem(R.id.search).setVisible(!messageViewVisible);
|
||||
menu.findItem(R.id.compose).setVisible(!messageViewVisible);
|
||||
menu.findItem(R.id.refresh).setVisible(!messageViewVisible);
|
||||
menu.findItem(R.id.account_settings).setVisible(!messageViewVisible);
|
||||
menu.findItem(R.id.sync_frequency).setVisible(!messageViewVisible);
|
||||
menu.findItem(R.id.sync_lookback).setVisible(!messageViewVisible);
|
||||
if (messageViewVisible) {
|
||||
menu.findItem(R.id.search).setVisible(false);
|
||||
menu.findItem(R.id.compose).setVisible(false);
|
||||
menu.findItem(R.id.refresh).setVisible(false);
|
||||
menu.findItem(R.id.account_settings).setVisible(false);
|
||||
menu.findItem(R.id.mailbox_settings).setVisible(false);
|
||||
|
||||
final MessageOrderManager om = getMessageOrderManager();
|
||||
menu.findItem(R.id.newer).setVisible(true);
|
||||
menu.findItem(R.id.older).setVisible(true);
|
||||
|
@ -618,7 +615,7 @@ class UIControllerOnePane extends UIControllerBase {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
protected void onRefresh() {
|
||||
if (!isRefreshEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -282,10 +282,6 @@ class UIControllerTwoPane extends UIControllerBase implements ThreePaneLayout.Ca
|
|||
:Account.NO_ACCOUNT;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* STOPSHIP Remove this -- see the base class method.
|
||||
*/
|
||||
@Override
|
||||
public long getMailboxSettingsMailboxId() {
|
||||
return getMessageListMailboxId();
|
||||
|
@ -545,12 +541,8 @@ class UIControllerTwoPane extends UIControllerBase implements ThreePaneLayout.Ca
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the "refresh" option item. Opens the settings activity.
|
||||
* TODO used by experimental code in the activity -- otherwise can be private.
|
||||
*/
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
protected void onRefresh() {
|
||||
// Cancel previously running instance if any.
|
||||
new RefreshTask(mTaskTracker, mActivity, getActualAccountId(),
|
||||
getMessageListMailboxId()).cancelPreviousAndExecuteParallel();
|
||||
|
|
|
@ -0,0 +1,321 @@
|
|||
/*
|
||||
* Copyright (C) 2011 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.FolderProperties;
|
||||
import com.android.email.R;
|
||||
import com.android.email.RefreshManager;
|
||||
import com.android.emailcommon.Logging;
|
||||
import com.android.emailcommon.provider.Account;
|
||||
import com.android.emailcommon.provider.EmailContent.AccountColumns;
|
||||
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
|
||||
import com.android.emailcommon.provider.Mailbox;
|
||||
import com.android.emailcommon.utility.EmailAsyncTask;
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* "Mailbox settings" activity.
|
||||
*
|
||||
* It's used to update per-mailbox sync settings. It normally updates Mailbox settings, unless
|
||||
* the target mailbox is Inbox, in which case it updates Account settings instead.
|
||||
*
|
||||
* All changes made by the user will not be immediately saved to the database, as changing the
|
||||
* sync window may result in removal of messages. Instead, we only save to the database in {@link
|
||||
* #onDestroy()}, unless it's called for configuration changes.
|
||||
*/
|
||||
public class MailboxSettings extends PreferenceActivity {
|
||||
private static final String EXTRA_MAILBOX_ID = "MAILBOX_ID";
|
||||
private static final String BUNDLE_ACCOUNT = "MailboxSettings.account";
|
||||
private static final String BUNDLE_MAILBOX = "MailboxSettings.mailbox";
|
||||
private static final String BUNDLE_NEEDS_SAVE = "MailboxSettings.needsSave";
|
||||
|
||||
private static final String PREF_CHECK_FREQUENCY_KEY = "check_frequency";
|
||||
private static final String PREF_SYNC_WINDOW_KEY = "sync_window";
|
||||
|
||||
private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
|
||||
|
||||
// Account and Mailbox -- directly loaded by LoadMailboxTask
|
||||
private Account mAccount;
|
||||
private Mailbox mMailbox;
|
||||
private boolean mNeedsSave;
|
||||
|
||||
private ListPreference mSyncIntervalPref;
|
||||
private ListPreference mSyncLookbackPref;
|
||||
|
||||
/**
|
||||
* Starts the activity for a mailbox.
|
||||
*/
|
||||
public static final void start(Activity parent, long mailboxId) {
|
||||
Intent i = new Intent(parent, MailboxSettings.class);
|
||||
i.putExtra(EXTRA_MAILBOX_ID, mailboxId);
|
||||
parent.startActivity(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final long mailboxId = getIntent().getLongExtra(EXTRA_MAILBOX_ID, Mailbox.NO_MAILBOX);
|
||||
if (mailboxId == Mailbox.NO_MAILBOX) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
addPreferencesFromResource(R.xml.mailbox_preferences);
|
||||
|
||||
mSyncIntervalPref = (ListPreference) findPreference(PREF_CHECK_FREQUENCY_KEY);
|
||||
mSyncLookbackPref = (ListPreference) findPreference(PREF_SYNC_WINDOW_KEY);
|
||||
|
||||
mSyncIntervalPref.setOnPreferenceChangeListener(mPreferenceChanged);
|
||||
mSyncLookbackPref.setOnPreferenceChangeListener(mPreferenceChanged);
|
||||
|
||||
// Make them disabled until we load data
|
||||
enablePreferences(false);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
mAccount = savedInstanceState.getParcelable(BUNDLE_ACCOUNT);
|
||||
mMailbox = savedInstanceState.getParcelable(BUNDLE_MAILBOX);
|
||||
mNeedsSave = savedInstanceState.getBoolean(BUNDLE_NEEDS_SAVE);
|
||||
}
|
||||
if (mAccount == null) {
|
||||
new LoadMailboxTask(mailboxId).executeParallel((Void[]) null);
|
||||
} else {
|
||||
onDataLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
private void enablePreferences(boolean enabled) {
|
||||
mSyncIntervalPref.setEnabled(enabled);
|
||||
mSyncLookbackPref.setEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putParcelable(BUNDLE_ACCOUNT, mAccount);
|
||||
outState.putParcelable(BUNDLE_MAILBOX, mMailbox);
|
||||
outState.putBoolean(BUNDLE_NEEDS_SAVE, mNeedsSave);
|
||||
}
|
||||
|
||||
/**
|
||||
* We save all the settings in onDestroy, *unless it's for configuration changes*.
|
||||
*/
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
mTaskTracker.cancellAllInterrupt();
|
||||
if (!isChangingConfigurations()) {
|
||||
saveToDatabase();
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads {@link #mAccount} and {@link #mMailbox}.
|
||||
*/
|
||||
private class LoadMailboxTask extends EmailAsyncTask<Void, Void, Void> {
|
||||
private final long mMailboxId;
|
||||
|
||||
public LoadMailboxTask(long mailboxId) {
|
||||
super(mTaskTracker);
|
||||
mMailboxId = mailboxId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
final Context c = MailboxSettings.this;
|
||||
mMailbox = Mailbox.restoreMailboxWithId(c, mMailboxId);
|
||||
if (mMailbox != null) {
|
||||
mAccount = Account.restoreAccountWithId(c, mMailbox.mAccountKey);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSuccess(Void result) {
|
||||
if ((mAccount == null) || (mMailbox == null)) {
|
||||
finish(); // Account or mailbox removed.
|
||||
return;
|
||||
}
|
||||
onDataLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when {@link #mAccount} and {@link #mMailbox} are loaded (either by the async task
|
||||
* or from the saved state).
|
||||
*/
|
||||
private void onDataLoaded() {
|
||||
Preconditions.checkNotNull(mAccount);
|
||||
Preconditions.checkNotNull(mMailbox);
|
||||
|
||||
// Update the title with the mailbox name.
|
||||
setTitle(getString(R.string.mailbox_settings_activity_title_with_mailbox,
|
||||
FolderProperties.getInstance(this).getDisplayName(mMailbox)));
|
||||
|
||||
// Special case: If setting inbox, don't show "Use account's default".
|
||||
if (mMailbox.mType == Mailbox.TYPE_INBOX) {
|
||||
mSyncLookbackPref.setEntries(R.array.account_settings_mail_window_entries);
|
||||
mSyncLookbackPref.setEntryValues(R.array.account_settings_mail_window_values);
|
||||
}
|
||||
|
||||
// Set default value & update summary
|
||||
mSyncIntervalPref.setValue(String.valueOf(getSyncInterval()));
|
||||
mSyncLookbackPref.setValue(String.valueOf(getSyncLookback()));
|
||||
|
||||
updatePreferenceSummary();
|
||||
|
||||
// Make then enabled
|
||||
enablePreferences(true);
|
||||
}
|
||||
|
||||
private void updatePreferenceSummary() {
|
||||
mSyncIntervalPref.setSummary(mSyncIntervalPref.getEntry());
|
||||
mSyncLookbackPref.setSummary(mSyncLookbackPref.getEntry());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return current sync interval setting from the objects
|
||||
*/
|
||||
private int getSyncInterval() {
|
||||
if (mMailbox.mType == Mailbox.TYPE_INBOX) {
|
||||
return mAccount.mSyncInterval;
|
||||
} else {
|
||||
if (mMailbox.mSyncInterval == 0) {
|
||||
// 0 is the default value, and it means "don't sync" (for non-inbox mailboxes)
|
||||
return Mailbox.CHECK_INTERVAL_NEVER;
|
||||
}
|
||||
return mMailbox.mSyncInterval;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return current sync lookback setting from the objects
|
||||
*/
|
||||
private int getSyncLookback() {
|
||||
if (mMailbox.mType == Mailbox.TYPE_INBOX) {
|
||||
return mAccount.mSyncLookback;
|
||||
} else {
|
||||
// Here, 0 is valid and means "use the account default sync window".
|
||||
return mMailbox.mSyncLookback;
|
||||
}
|
||||
}
|
||||
|
||||
private final OnPreferenceChangeListener mPreferenceChanged = new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
final ListPreference lp = (ListPreference) preference;
|
||||
if (Objects.equal(lp.getValue(), newValue)) {
|
||||
return false;
|
||||
}
|
||||
mNeedsSave = true;
|
||||
if (Email.DEBUG) {
|
||||
Log.i(Logging.LOG_TAG, "Setting changed");
|
||||
}
|
||||
// In order to set the current entry to the summary, we need to udpate the value
|
||||
// manually, rather than letting the framework do that (by returning true).
|
||||
lp.setValue((String) newValue);
|
||||
updatePreferenceSummary();
|
||||
updateObjects();
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates {@link #mAccount}/{@link #mMailbox}, but doesn't save to the database yet.
|
||||
*/
|
||||
private void updateObjects() {
|
||||
final int syncInterval = Integer.valueOf(mSyncIntervalPref.getValue());
|
||||
final int syncLookback = Integer.valueOf(mSyncLookbackPref.getValue());
|
||||
if (Email.DEBUG) {
|
||||
Log.i(Logging.LOG_TAG, "Updating object: " + syncInterval + "," + syncLookback);
|
||||
}
|
||||
if (mMailbox.mType == Mailbox.TYPE_INBOX) {
|
||||
mAccount.mSyncInterval = syncInterval;
|
||||
mAccount.mSyncLookback = syncLookback;
|
||||
} else {
|
||||
mMailbox.mSyncInterval = syncInterval;
|
||||
mMailbox.mSyncLookback = syncLookback;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save changes to the database.
|
||||
*
|
||||
* Note it's called from {@link #onDestroy()}, which is called on the UI thread where we're not
|
||||
* allowed to touch the database, so it uses {@link EmailAsyncTask} to do the save on a bg
|
||||
* thread. This unfortunately means there's a chance that the app gets killed before the save is
|
||||
* finished.
|
||||
*/
|
||||
private void saveToDatabase() {
|
||||
if (!mNeedsSave) {
|
||||
return;
|
||||
}
|
||||
Log.i(Logging.LOG_TAG, "Saving mailbox settings...");
|
||||
enablePreferences(false);
|
||||
|
||||
// Since the activity will be destroyed...
|
||||
// Create local references (Although it's really okay to touch members of a destroyed
|
||||
// activity...)
|
||||
final Account account = mAccount;
|
||||
final Mailbox mailbox = mMailbox;
|
||||
final Context context = this.getApplicationContext();
|
||||
|
||||
new EmailAsyncTask<Void, Void, Void> (null /* no cancel */) {
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
final ContentValues cv = new ContentValues();
|
||||
final Uri uri;
|
||||
|
||||
if (mailbox.mType == Mailbox.TYPE_INBOX) {
|
||||
cv.put(AccountColumns.SYNC_INTERVAL, account.mSyncInterval);
|
||||
cv.put(AccountColumns.SYNC_LOOKBACK, account.mSyncLookback);
|
||||
uri = ContentUris.withAppendedId(Account.CONTENT_URI, account.mId);
|
||||
} else {
|
||||
cv.put(MailboxColumns.SYNC_INTERVAL, mailbox.mSyncInterval);
|
||||
cv.put(MailboxColumns.SYNC_LOOKBACK, mailbox.mSyncLookback);
|
||||
uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailbox.mId);
|
||||
}
|
||||
context.getContentResolver().update(uri, cv, null, null);
|
||||
|
||||
Log.i(Logging.LOG_TAG, "Saved: " + uri);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSuccess(Void result) {
|
||||
// must be called on the ui thread
|
||||
RefreshManager.getInstance(context).refreshMessageList(account.mId, mailbox.mId,
|
||||
true);
|
||||
}
|
||||
}.executeSerial((Void [])null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue