AccountPreferences.java revision d5edd2d02649dffb40065fdb6a16acf91552b800
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;
19
20import android.content.Context;
21
22import com.android.mail.MailIntentService;
23
24/**
25 * Preferences relevant to one specific account.
26 */
27public class AccountPreferences extends VersionedPrefs {
28
29    private static final String PREFS_NAME_PREFIX = "Account";
30
31    public static final class PreferenceKeys {
32        /**
33         * A temporary preference that can be set during account setup, if we do not know what the
34         * default inbox is yet. This value should be moved into the appropriate
35         * {@link FolderPreferences} once we have the inbox, and removed from here.
36         */
37        private static final String DEFAULT_INBOX_NOTIFICATIONS_ENABLED =
38                "inbox-notifications-enabled";
39
40        /** Boolean value indicating whether notifications are enabled */
41        public static final String NOTIFICATIONS_ENABLED = "notifications-enabled";
42
43        public static final ImmutableSet<String> BACKUP_KEYS =
44                new ImmutableSet.Builder<String>().add(NOTIFICATIONS_ENABLED).build();
45    }
46
47    /**
48     * @param account The account name. This must never change for the account.
49     */
50    public AccountPreferences(final Context context, final String account) {
51        super(context, buildSharedPrefsName(account));
52    }
53
54    private static String buildSharedPrefsName(final String account) {
55        return PREFS_NAME_PREFIX + '-' + account;
56    }
57
58    @Override
59    protected void performUpgrade(final int oldVersion, final int newVersion) {
60        if (oldVersion > newVersion) {
61            throw new IllegalStateException(
62                    "You appear to have downgraded your app. Please clear app data.");
63        }
64    }
65
66    @Override
67    protected boolean canBackup(final String key) {
68        return PreferenceKeys.BACKUP_KEYS.contains(key);
69    }
70
71    public boolean isDefaultInboxNotificationsEnabledSet() {
72        return getSharedPreferences().contains(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED);
73    }
74
75    public boolean getDefaultInboxNotificationsEnabled() {
76        return getSharedPreferences()
77                .getBoolean(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED, true);
78    }
79
80    public void setDefaultInboxNotificationsEnabled(final boolean enabled) {
81        getEditor().putBoolean(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED, enabled).apply();
82    }
83
84    public void clearDefaultInboxNotificationsEnabled() {
85        getEditor().remove(PreferenceKeys.DEFAULT_INBOX_NOTIFICATIONS_ENABLED).apply();
86    }
87
88    public boolean areNotificationsEnabled() {
89        return getSharedPreferences().getBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, true);
90    }
91
92    public void setNotificationsEnabled(final boolean enabled) {
93        getEditor().putBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, enabled).apply();
94        MailIntentService.broadcastBackupDataChanged(getContext());
95    }
96}
97