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