1 /* 2 * Copyright (C) 2013 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 package com.android.mail.preferences; 17 18 import android.content.Context; 19 20 import com.android.mail.providers.Account; 21 import com.google.common.collect.ImmutableSet; 22 import com.google.common.collect.Maps; 23 24 import java.util.Map; 25 26 /** 27 * Preferences relevant to one specific account. 28 */ 29 public class AccountPreferences extends VersionedPrefs { 30 31 private static final String PREFS_NAME_PREFIX = "Account"; 32 33 private static Map<String, AccountPreferences> mInstances = Maps.newHashMap(); 34 35 public static final class PreferenceKeys { 36 /** 37 * A temporary preference that can be set during account setup, if we do not know what the 38 * default inbox is yet. This value should be moved into the appropriate 39 * {@link FolderPreferences} once we have the inbox, and removed from here. 40 */ 41 private static final String DEFAULT_INBOX_NOTIFICATIONS_ENABLED = 42 "inbox-notifications-enabled"; 43 44 /** Boolean value indicating whether notifications are enabled */ 45 public static final String NOTIFICATIONS_ENABLED = "notifications-enabled"; 46 47 /** 48 * Number of time user has dismissed / seen the toast for account sync is off message. 49 */ 50 public static final String ACCOUNT_SYNC_OFF_DISMISSES = "num-of-dismisses-account-sync-off"; 51 52 /** 53 * The count reported last time the "X unseen in Outbox" tip was displayed. 54 */ 55 public static final String LAST_SEEN_OUTBOX_COUNT = "last-seen-outbox-count"; 56 57 public static final ImmutableSet<String> BACKUP_KEYS = 58 new ImmutableSet.Builder<String>() 59 .add(NOTIFICATIONS_ENABLED).build(); 60 } 61 62 /** 63 * @param account The account id 64 */ 65 public AccountPreferences(final Context context, final String accountId) { 66 super(context, buildSharedPrefsName(accountId)); 67 } 68 69 private static String buildSharedPrefsName(final String accountId) { 70 return PREFS_NAME_PREFIX + '-' + accountId; 71 } 72 73 public static synchronized AccountPreferences get(Context context, Account account) { 74 final String id = account.getAccountId(); 75 AccountPreferences pref = mInstances.get(id); 76 if (pref == null) { 77 pref = new AccountPreferences(context, id); 78 mInstances.put(id, pref); 79 } 80 return pref; 81 } 82 83 @Override 84 protected void performUpgrade(final int oldVersion, final int newVersion) { 85 if (oldVersion > newVersion) { 86 throw new IllegalStateException( 87 "You appear to have downgraded your app. Please clear app data."); 88 } 89 } 90 91 @Override 92 protected boolean canBackup(final String key) { 93 return PreferenceKeys.BACKUP_KEYS.contains(key); 94 } 95 96 public boolean isDefaultInboxNotificationsEnabledSet() { 97 return getSharedPreferences().contains(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED); 98 } 99 100 public boolean getDefaultInboxNotificationsEnabled() { 101 return getSharedPreferences() 102 .getBoolean(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED, true); 103 } 104 105 public void setDefaultInboxNotificationsEnabled(final boolean enabled) { 106 getEditor().putBoolean(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED, enabled).apply(); 107 } 108 109 public void clearDefaultInboxNotificationsEnabled() { 110 getEditor().remove(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED).apply(); 111 } 112 113 public boolean areNotificationsEnabled() { 114 return getSharedPreferences().getBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, true); 115 } 116 117 public void setNotificationsEnabled(final boolean enabled) { 118 getEditor().putBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, enabled).apply(); 119 notifyBackupPreferenceChanged(); 120 } 121 122 public int getNumOfDismissesForAccountSyncOff() { 123 return getSharedPreferences().getInt(PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0); 124 } 125 126 public void resetNumOfDismissesForAccountSyncOff() { 127 final int value = getSharedPreferences().getInt( 128 PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0); 129 if (value != 0) { 130 getEditor().putInt(PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0).apply(); 131 } 132 } 133 134 public void incNumOfDismissesForAccountSyncOff() { 135 final int value = getSharedPreferences().getInt( 136 PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0); 137 getEditor().putInt(PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, value + 1).apply(); 138 } 139 140 public int getLastSeenOutboxCount() { 141 return getSharedPreferences().getInt(PreferenceKeys.LAST_SEEN_OUTBOX_COUNT, 0); 142 } 143 144 public void setLastSeenOutboxCount(final int count) { 145 getEditor().putInt(PreferenceKeys.LAST_SEEN_OUTBOX_COUNT, count).apply(); 146 } 147 } 148