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