From 9b4240f7c5647c2952b050bb74f18eba274f51f8 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Wed, 21 Dec 2011 14:33:15 -0800 Subject: [PATCH] DO NOT MERGE: Handle UI part of email lookback limit Bug: 5792217 Change-Id: Iccd6b1db30e8fb1fbdb514ed2de6d1e789a6280a --- .../setup/AccountSettingsFragment.java | 3 +- .../activity/setup/AccountSetupOptions.java | 19 +++++++- .../email/activity/setup/MailboxSettings.java | 46 ++++++++++++++++--- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/com/android/email/activity/setup/AccountSettingsFragment.java b/src/com/android/email/activity/setup/AccountSettingsFragment.java index ed5e9f0e0..7e19076f4 100644 --- a/src/com/android/email/activity/setup/AccountSettingsFragment.java +++ b/src/com/android/email/activity/setup/AccountSettingsFragment.java @@ -455,10 +455,9 @@ public class AccountSettingsFragment extends PreferenceFragment { if (HostAuth.SCHEME_EAS.equals(protocol)) { 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()); + MailboxSettings.setupLookbackPreferenceOptions(mContext, mSyncWindow, mAccount); // Must correspond to the hole in the XML file that's reserved. mSyncWindow.setOrder(2); diff --git a/src/com/android/email/activity/setup/AccountSetupOptions.java b/src/com/android/email/activity/setup/AccountSetupOptions.java index c942faa87..1a246c9f5 100644 --- a/src/com/android/email/activity/setup/AccountSetupOptions.java +++ b/src/com/android/email/activity/setup/AccountSetupOptions.java @@ -44,6 +44,7 @@ import com.android.email.service.MailService; import com.android.emailcommon.Logging; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.HostAuth; +import com.android.emailcommon.provider.Policy; import com.android.emailcommon.service.SyncWindow; import com.android.emailcommon.utility.Utility; @@ -389,10 +390,24 @@ public class AccountSetupOptions extends AccountSetupActivity implements OnClick CharSequence[] windowEntries = getResources().getTextArray( R.array.account_settings_mail_window_entries); + // Find a proper maximum for email lookback, based on policy (if we have one) + int maxEntry = windowEntries.length; + Policy policy = SetupData.getAccount().mPolicy; + if (policy != null) { + int maxLookback = policy.mMaxEmailLookback; + if (maxLookback != 0) { + // Offset/Code 0 1 2 3 4 5 + // Entries auto, 1 day, 3 day, 1 week, 2 week, 1 month + // Lookback N/A 1 day, 3 day, 1 week, 2 week, 1 month + // Since our test below is i < maxEntry, we must set maxEntry to maxLookback + 1 + maxEntry = maxLookback + 1; + } + } + // Now create the array used by the Spinner - SpinnerOption[] windowOptions = new SpinnerOption[windowEntries.length]; + SpinnerOption[] windowOptions = new SpinnerOption[maxEntry]; int defaultIndex = -1; - for (int i = 0; i < windowEntries.length; i++) { + for (int i = 0; i < maxEntry; i++) { final int value = Integer.valueOf(windowValues[i].toString()); windowOptions[i] = new SpinnerOption(value, windowEntries[i].toString()); if (value == SYNC_WINDOW_EAS_DEFAULT) { diff --git a/src/com/android/email/activity/setup/MailboxSettings.java b/src/com/android/email/activity/setup/MailboxSettings.java index 2b0eb5851..ca17436f9 100644 --- a/src/com/android/email/activity/setup/MailboxSettings.java +++ b/src/com/android/email/activity/setup/MailboxSettings.java @@ -22,6 +22,7 @@ import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.preference.ListPreference; @@ -37,6 +38,7 @@ 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.Policy; import com.android.emailcommon.provider.EmailContent.AccountColumns; import com.android.emailcommon.provider.EmailContent.MailboxColumns; import com.android.emailcommon.provider.Mailbox; @@ -177,6 +179,42 @@ public class MailboxSettings extends PreferenceActivity { } } + /** + * Setup the entries and entry values for the sync lookback preference + * @param context the caller's context + * @param pref a ListPreference to be set up + * @param account the Account (or owner of a Mailbox) whose preference is being set + */ + public static void setupLookbackPreferenceOptions(Context context, ListPreference pref, + Account account) { + Resources resources = context.getResources(); + // Load the complete list of entries/values + CharSequence[] entries = + resources.getTextArray(R.array.account_settings_mail_window_entries); + CharSequence[] values = + resources.getTextArray(R.array.account_settings_mail_window_values); + // If we have a maximum lookback policy, enforce it + if (account.mPolicyKey > 0) { + Policy policy = Policy.restorePolicyWithId(context, account.mPolicyKey); + if (policy != null && (policy.mMaxEmailLookback != 0)) { + int maxEntry = policy.mMaxEmailLookback + 1; + // Copy the proper number of values into new entries/values array + CharSequence[] policyEntries = new CharSequence[maxEntry]; + CharSequence[] policyValues = new CharSequence[maxEntry]; + for (int i = 0; i < maxEntry; i++) { + policyEntries[i] = entries[i]; + policyValues[i] = values[i]; + } + // Point entries/values to the new arrays + entries = policyEntries; + values = policyValues; + } + } + // Set up the preference + pref.setEntries(entries); + pref.setEntryValues(values); + } + /** * Called when {@link #mAccount} and {@link #mMailbox} are loaded (either by the async task * or from the saved state). @@ -195,12 +233,7 @@ public class MailboxSettings extends PreferenceActivity { setTitle(getString(R.string.mailbox_settings_activity_title_with_mailbox, mailboxName)); } - - // 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); - } + setupLookbackPreferenceOptions(this, mSyncLookbackPref, mAccount); // Set default value & update summary mSyncIntervalPref.setValue(String.valueOf(getSyncInterval())); @@ -210,7 +243,6 @@ public class MailboxSettings extends PreferenceActivity { // Make then enabled enablePreferences(true); - } private void updatePreferenceSummary() {