| /* |
| * Copyright (C) 2012 Google Inc. |
| * Licensed to 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.mail.preferences; |
| |
| import android.content.Context; |
| import android.content.SharedPreferences; |
| |
| import com.android.mail.MailIntentService; |
| import com.android.mail.providers.Account; |
| import com.android.mail.providers.UIProvider; |
| import com.android.mail.widget.BaseWidgetProvider; |
| import com.google.common.collect.ImmutableSet; |
| |
| import java.util.Set; |
| |
| /** |
| * A high-level API to store and retrieve unified mail preferences. |
| * <p> |
| * This will serve as an eventual replacement for Gmail's Persistence class. |
| */ |
| public final class MailPrefs extends VersionedPrefs { |
| |
| public static final boolean SHOW_EXPERIMENTAL_PREFS = false; |
| |
| private static final String PREFS_NAME = "UnifiedEmail"; |
| |
| private static MailPrefs sInstance; |
| |
| public static final class PreferenceKeys { |
| private static final String MIGRATED_VERSION = "migrated-version"; |
| |
| public static final String WIDGET_ACCOUNT_PREFIX = "widget-account-"; |
| |
| /** Hidden preference to indicate what version a "What's New" dialog was last shown for. */ |
| public static final String WHATS_NEW_LAST_SHOWN_VERSION = "whats-new-last-shown-version"; |
| |
| /** |
| * A boolean that, if <code>true</code>, means we should default all replies to "reply all" |
| */ |
| public static final String DEFAULT_REPLY_ALL = "default-reply-all"; |
| /** |
| * A boolean that, if <code>true</code>, means we should allow conversation list swiping |
| */ |
| public static final String CONVERSATION_LIST_SWIPE = "conversation-list-swipe"; |
| |
| /** A string indicating the user's removal action preference. */ |
| public static final String REMOVAL_ACTION = "removal-action"; |
| |
| /** A boolean indicating that the user has seen the removal action dialog. */ |
| public static final String REMOVAL_ACTION_DIALOG_SHOWN = "removal-action-dialog-shown"; |
| |
| /** Hidden preference used to cache the active notification set */ |
| private static final String CACHED_ACTIVE_NOTIFICATION_SET = |
| "cache-active-notification-set"; |
| |
| private static final String |
| CONVERSATION_PHOTO_TEASER_SHOWN = "conversation-photo-teaser-shown-two"; |
| |
| public static final ImmutableSet<String> BACKUP_KEYS = |
| new ImmutableSet.Builder<String>() |
| .add(DEFAULT_REPLY_ALL) |
| .add(CONVERSATION_LIST_SWIPE) |
| .add(REMOVAL_ACTION) |
| .add(REMOVAL_ACTION_DIALOG_SHOWN) |
| .build(); |
| |
| } |
| |
| public static final class ConversationListSwipeActions { |
| public static final String ARCHIVE = "archive"; |
| public static final String DELETE = "delete"; |
| public static final String DISABLED = "disabled"; |
| } |
| |
| public static final class RemovalActions { |
| public static final String ARCHIVE = "archive"; |
| public static final String DELETE = "delete"; |
| public static final String ARCHIVE_AND_DELETE = "archive-and-delete"; |
| } |
| |
| public static MailPrefs get(Context c) { |
| if (sInstance == null) { |
| sInstance = new MailPrefs(c); |
| } |
| return sInstance; |
| } |
| |
| private MailPrefs(Context c) { |
| super(c, PREFS_NAME); |
| } |
| |
| @Override |
| protected void performUpgrade(final int oldVersion, final int newVersion) { |
| if (oldVersion > newVersion) { |
| throw new IllegalStateException( |
| "You appear to have downgraded your app. Please clear app data."); |
| } else if (oldVersion == newVersion) { |
| return; |
| } |
| } |
| |
| @Override |
| protected boolean canBackup(final String key) { |
| return PreferenceKeys.BACKUP_KEYS.contains(key); |
| } |
| |
| @Override |
| protected boolean hasMigrationCompleted() { |
| return getSharedPreferences().getInt(PreferenceKeys.MIGRATED_VERSION, 0) |
| >= CURRENT_VERSION_NUMBER; |
| } |
| |
| @Override |
| protected void setMigrationComplete() { |
| getEditor().putInt(PreferenceKeys.MIGRATED_VERSION, CURRENT_VERSION_NUMBER).commit(); |
| } |
| |
| public boolean isWidgetConfigured(int appWidgetId) { |
| return getSharedPreferences().contains(PreferenceKeys.WIDGET_ACCOUNT_PREFIX + appWidgetId); |
| } |
| |
| public void configureWidget(int appWidgetId, Account account, final String folderUri) { |
| getEditor().putString(PreferenceKeys.WIDGET_ACCOUNT_PREFIX + appWidgetId, |
| createWidgetPreferenceValue(account, folderUri)).apply(); |
| } |
| |
| public String getWidgetConfiguration(int appWidgetId) { |
| return getSharedPreferences().getString(PreferenceKeys.WIDGET_ACCOUNT_PREFIX + appWidgetId, |
| null); |
| } |
| |
| private static String createWidgetPreferenceValue(Account account, String folderUri) { |
| return account.uri.toString() + BaseWidgetProvider.ACCOUNT_FOLDER_PREFERENCE_SEPARATOR |
| + folderUri; |
| |
| } |
| |
| public void clearWidgets(int[] appWidgetIds) { |
| for (int id : appWidgetIds) { |
| getEditor().remove(PreferenceKeys.WIDGET_ACCOUNT_PREFIX + id); |
| } |
| getEditor().apply(); |
| } |
| |
| /** If <code>true</code>, we should default all replies to "reply all" rather than "reply" */ |
| public boolean getDefaultReplyAll() { |
| return getSharedPreferences().getBoolean(PreferenceKeys.DEFAULT_REPLY_ALL, false); |
| } |
| |
| public void setDefaultReplyAll(final boolean replyAll) { |
| getEditor().putBoolean(PreferenceKeys.DEFAULT_REPLY_ALL, replyAll).apply(); |
| MailIntentService.broadcastBackupDataChanged(getContext()); |
| } |
| |
| /** |
| * Returns a string indicating the preferred removal action. |
| * Should be one of the {@link RemovalActions}. |
| */ |
| public String getRemovalAction(final boolean supportsArchive) { |
| final String defaultAction = supportsArchive |
| ? RemovalActions.ARCHIVE : RemovalActions.DELETE; |
| |
| final SharedPreferences sharedPreferences = getSharedPreferences(); |
| return sharedPreferences.getString(PreferenceKeys.REMOVAL_ACTION, defaultAction); |
| } |
| |
| /** |
| * Sets the removal action preference. |
| * @param removalAction The preferred {@link RemovalActions}. |
| */ |
| public void setRemovalAction(final String removalAction) { |
| getEditor().putString(PreferenceKeys.REMOVAL_ACTION, removalAction).apply(); |
| MailIntentService.broadcastBackupDataChanged(getContext()); |
| } |
| |
| /** |
| * Gets a boolean indicating whether conversation list swiping is enabled. |
| */ |
| public boolean getIsConversationListSwipeEnabled() { |
| final SharedPreferences sharedPreferences = getSharedPreferences(); |
| return sharedPreferences.getBoolean(PreferenceKeys.CONVERSATION_LIST_SWIPE, true); |
| } |
| |
| public void setConversationListSwipeEnabled(final boolean enabled) { |
| getEditor().putBoolean(PreferenceKeys.CONVERSATION_LIST_SWIPE, enabled).apply(); |
| MailIntentService.broadcastBackupDataChanged(getContext()); |
| } |
| |
| /** |
| * Gets the action to take (one of the values from {@link UIProvider.Swipe}) when an item in the |
| * conversation list is swiped. |
| * |
| * @param allowArchive <code>true</code> if Archive is an acceptable action (this will affect |
| * the default return value) |
| */ |
| public int getConversationListSwipeActionInteger(final boolean allowArchive) { |
| final boolean swipeEnabled = getIsConversationListSwipeEnabled(); |
| final boolean archive = !RemovalActions.DELETE.equals(getRemovalAction(allowArchive)); |
| |
| if (swipeEnabled) { |
| return archive ? UIProvider.Swipe.ARCHIVE : UIProvider.Swipe.DELETE; |
| } |
| |
| return UIProvider.Swipe.DISABLED; |
| } |
| |
| /** |
| * Returns the previously cached notification set |
| */ |
| public Set<String> getActiveNotificationSet() { |
| return getSharedPreferences() |
| .getStringSet(PreferenceKeys.CACHED_ACTIVE_NOTIFICATION_SET, null); |
| } |
| |
| /** |
| * Caches the current notification set. |
| */ |
| public void cacheActiveNotificationSet(final Set<String> notificationSet) { |
| getEditor().putStringSet(PreferenceKeys.CACHED_ACTIVE_NOTIFICATION_SET, notificationSet) |
| .apply(); |
| } |
| |
| /** |
| * Returns whether the teaser has bee shown before |
| */ |
| public boolean isConversationPhotoTeaserAlreadyShown() { |
| return getSharedPreferences() |
| .getBoolean(PreferenceKeys.CONVERSATION_PHOTO_TEASER_SHOWN, false); |
| } |
| |
| /** |
| * Notify that we have shown the teaser |
| */ |
| public void setConversationPhotoTeaserAlreadyShown() { |
| getEditor().putBoolean(PreferenceKeys.CONVERSATION_PHOTO_TEASER_SHOWN, true).apply(); |
| } |
| |
| /** |
| * Reset the flag so that next time, the teaser will be shown again |
| */ |
| public void resetConversationPhotoTeaserAlreadyShown() { |
| getEditor().putBoolean(PreferenceKeys.CONVERSATION_PHOTO_TEASER_SHOWN, false).apply(); |
| } |
| |
| /** |
| * @return <code>true</code> if the removal action dialog has been shown to the user, |
| * <code>false</code> otherwise |
| */ |
| public boolean hasRemovalActionDialogShown() { |
| return getSharedPreferences().getBoolean(PreferenceKeys.REMOVAL_ACTION_DIALOG_SHOWN, false); |
| } |
| |
| public void setRemovalActionDialogShown() { |
| getEditor().putBoolean(PreferenceKeys.REMOVAL_ACTION_DIALOG_SHOWN, true).apply(); |
| MailIntentService.broadcastBackupDataChanged(getContext()); |
| } |
| } |