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