Home | History | Annotate | Download | only in preferences
      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 com.google.common.collect.ImmutableSet;
     19 import com.google.common.collect.Maps;
     20 
     21 import android.content.Context;
     22 
     23 import java.util.Map;
     24 
     25 /**
     26  * Preferences relevant to one specific account.
     27  */
     28 public 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         /**
     52          * The count reported last time the "X unseen in Outbox" tip was displayed.
     53          */
     54         public static final String LAST_SEEN_OUTBOX_COUNT = "last-seen-outbox-count";
     55 
     56         public static final ImmutableSet<String> BACKUP_KEYS =
     57                 new ImmutableSet.Builder<String>().add(NOTIFICATIONS_ENABLED).build();
     58     }
     59 
     60     /**
     61      * @param account The account email. This must never change for the account.
     62      */
     63     public AccountPreferences(final Context context, final String account) {
     64         super(context, buildSharedPrefsName(account));
     65     }
     66 
     67     private static String buildSharedPrefsName(final String account) {
     68         return PREFS_NAME_PREFIX + '-' + account;
     69     }
     70 
     71     public static synchronized AccountPreferences get(Context context, String accountEmail) {
     72         AccountPreferences pref = mInstances.get(accountEmail);
     73         if (pref == null) {
     74             pref = new AccountPreferences(context, accountEmail);
     75             mInstances.put(accountEmail, pref);
     76         }
     77         return pref;
     78     }
     79 
     80     @Override
     81     protected void performUpgrade(final int oldVersion, final int newVersion) {
     82         if (oldVersion > newVersion) {
     83             throw new IllegalStateException(
     84                     "You appear to have downgraded your app. Please clear app data.");
     85         }
     86     }
     87 
     88     @Override
     89     protected boolean canBackup(final String key) {
     90         return PreferenceKeys.BACKUP_KEYS.contains(key);
     91     }
     92 
     93     public boolean isDefaultInboxNotificationsEnabledSet() {
     94         return getSharedPreferences().contains(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED);
     95     }
     96 
     97     public boolean getDefaultInboxNotificationsEnabled() {
     98         return getSharedPreferences()
     99                 .getBoolean(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED, true);
    100     }
    101 
    102     public void setDefaultInboxNotificationsEnabled(final boolean enabled) {
    103         getEditor().putBoolean(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED, enabled).apply();
    104     }
    105 
    106     public void clearDefaultInboxNotificationsEnabled() {
    107         getEditor().remove(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED).apply();
    108     }
    109 
    110     public boolean areNotificationsEnabled() {
    111         return getSharedPreferences().getBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, true);
    112     }
    113 
    114     public void setNotificationsEnabled(final boolean enabled) {
    115         getEditor().putBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, enabled).apply();
    116         notifyBackupPreferenceChanged();
    117     }
    118 
    119     public int getNumOfDismissesForAccountSyncOff() {
    120         return getSharedPreferences().getInt(PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0);
    121     }
    122 
    123     public void resetNumOfDismissesForAccountSyncOff() {
    124         final int value = getSharedPreferences().getInt(
    125                 PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0);
    126         if (value != 0) {
    127             getEditor().putInt(PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0).apply();
    128         }
    129     }
    130 
    131     public void incNumOfDismissesForAccountSyncOff() {
    132         final int value = getSharedPreferences().getInt(
    133                 PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, 0);
    134         getEditor().putInt(PreferenceKeys.ACCOUNT_SYNC_OFF_DISMISSES, value + 1).apply();
    135     }
    136 
    137     public int getLastSeenOutboxCount() {
    138         return getSharedPreferences().getInt(PreferenceKeys.LAST_SEEN_OUTBOX_COUNT, 0);
    139     }
    140 
    141     public void setLastSeenOutboxCount(final int count) {
    142         getEditor().putInt(PreferenceKeys.LAST_SEEN_OUTBOX_COUNT, count).apply();
    143     }
    144 }
    145