FolderPreferences.java revision cde6eb0eb380f6212b7091d9381b3fec5006f6a8
1d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/*
2d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Copyright (C) 2012 The Android Open Source Project
3d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
4d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
5d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * you may not use this file except in compliance with the License.
6d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * You may obtain a copy of the License at
7d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
8d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
9d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
10d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Unless required by applicable law or agreed to in writing, software
11d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
12d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * See the License for the specific language governing permissions and
14d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * limitations under the License.
15d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
16d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypackage com.android.mail.preferences;
17d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
18d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.google.android.common.base.Strings;
19d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.google.common.collect.ImmutableSet;
20d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
21d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentUris;
22d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Context;
23d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.database.Cursor;
24d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.media.RingtoneManager;
25d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.net.Uri;
26d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.Settings;
27d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
28d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
29cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedyimport com.android.mail.providers.Account;
30d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Folder;
31cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedyimport com.android.mail.providers.UIProvider.AccountCapabilities;
32d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.UIProvider.FolderCapabilities;
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationActionType;
34d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
35d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.LinkedHashSet;
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/**
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Preferences relevant to one specific folder. In Email, this would only be used for an account's
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * inbox. In Gmail, this is used for every account/label pair.
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class FolderPreferences extends VersionedPrefs {
43d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final String PREFS_NAME_PREFIX = "Folder";
45d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static final class PreferenceKeys {
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** Boolean value indicating whether notifications are enabled */
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATIONS_ENABLED = "notifications-enabled";
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** String value of the notification ringtone URI */
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_RINGTONE = "notification-ringtone";
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** Boolean value indicating whether we should explicitly vibrate */
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_VIBRATE = "notification-vibrate";
53d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
54d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Boolean value indicating whether we notify for every message (<code>true</code>), or just
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * once for the folder (<code>false</code>)
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_NOTIFY_EVERY_MESSAGE =
58d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                "notification-notify-every-message";
59d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** String set of the notification actions (from {@link NotificationActionType} */
60d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_ACTIONS = "notification-actions";
61d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
62d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final ImmutableSet<String> BACKUP_KEYS =
63d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new ImmutableSet.Builder<String>()
64d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATIONS_ENABLED)
65d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATION_RINGTONE)
66d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATION_VIBRATE)
67d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATION_NOTIFY_EVERY_MESSAGE)
68d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .build();
69d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
70d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
71d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private final Folder mFolder;
72d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** An id that is constant across app installations. */
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private final String mPersistentId;
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private final boolean mUseInboxDefaultNotificationSettings;
75d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param account The account name. This must never change for the account.
7861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy     * @param folder The folder
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
80d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public FolderPreferences(final Context context, final String account, final Folder folder,
81d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean useInboxDefaultNotificationSettings) {
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        this(context, account, folder, folder.persistentId, useInboxDefaultNotificationSettings);
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * A constructor that can be used when no {@link Folder} object is available (like during a
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * restore). While this will probably function as expected at other times,
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link #FolderPreferences(Context, String, Folder, boolean)} should be used if at all
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * possible.
90d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
91d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param account The account name. This must never change for the account.
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param persistentId An identifier for the folder that does not change across app
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *        installations.
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public FolderPreferences(final Context context, final String account, final String persistentId,
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean useInboxDefaultNotificationSettings) {
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        this(context, account, null, persistentId, useInboxDefaultNotificationSettings);
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
99d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private FolderPreferences(final Context context, final String account, final Folder folder,
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String persistentId, final boolean useInboxDefaultNotificationSettings) {
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        super(context, buildSharedPrefsName(account, persistentId));
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        mFolder = folder;
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        mPersistentId = persistentId;
105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        mUseInboxDefaultNotificationSettings = useInboxDefaultNotificationSettings;
106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String buildSharedPrefsName(final String account, final String persistentId) {
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return PREFS_NAME_PREFIX + '-' + account + '-' + persistentId;
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected void performUpgrade(final int oldVersion, final int newVersion) {
114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (oldVersion > newVersion) {
115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            throw new IllegalStateException(
116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    "You appear to have downgraded your app. Please clear app data.");
117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public String getPersistentId() {
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return mPersistentId;
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected boolean canBackup(final String key) {
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (mPersistentId == null) {
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return false;
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return PreferenceKeys.BACKUP_KEYS.contains(key);
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected Object getBackupValue(final String key, final Object value) {
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (PreferenceKeys.NOTIFICATION_RINGTONE.equals(key)) {
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return getRingtoneTitle((String) value);
137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return super.getBackupValue(key, value);
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected Object getRestoreValue(final String key, final Object value) {
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (PreferenceKeys.NOTIFICATION_RINGTONE.equals(key)) {
145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return getRingtoneUri((String) value);
146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return super.getBackupValue(key, value);
149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private String getRingtoneTitle(final String ringtoneUriString) {
152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (ringtoneUriString.length() == 0) {
153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return ringtoneUriString;
154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Uri uri = Uri.parse(ringtoneUriString);
156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (RingtoneManager.isDefault(uri)) {
157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return ringtoneUriString;
158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final RingtoneManager ringtoneManager = new RingtoneManager(getContext());
160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ringtoneManager.setType(RingtoneManager.TYPE_NOTIFICATION);
161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Cursor cursor = ringtoneManager.getCursor();
162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (cursor.moveToNext()) {
164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri cursorUri = ContentUris.withAppendedId(
165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX)),
166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        cursor.getLong(RingtoneManager.ID_COLUMN_INDEX));
167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursorUri.toString().equals(ringtoneUriString)) {
168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!Strings.isNullOrEmpty(title)) {
170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return title;
171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor.close();
176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return null;
178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private String getRingtoneUri(final String name) {
181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (name.length() == 0 || RingtoneManager.isDefault(Uri.parse(name))) {
182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return name;
183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final RingtoneManager ringtoneManager = new RingtoneManager(getContext());
186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ringtoneManager.setType(RingtoneManager.TYPE_NOTIFICATION);
187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Cursor cursor = ringtoneManager.getCursor();
188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (cursor.moveToNext()) {
190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (name.equals(title)) {
192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Uri uri = ContentUris.withAppendedId(
193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX)),
194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor.getLong(RingtoneManager.ID_COLUMN_INDEX));
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return uri.toString();
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor.close();
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return null;
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * If <code>true</code>, we use inbox-defaults for notification settings. If <code>false</code>,
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * we use standard defaults.
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private boolean getUseInboxDefaultNotificationSettings() {
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return mUseInboxDefaultNotificationSettings;
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean isNotificationsEnabledSet() {
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().contains(PreferenceKeys.NOTIFICATIONS_ENABLED);
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean areNotificationsEnabled() {
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().getBoolean(
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                PreferenceKeys.NOTIFICATIONS_ENABLED, getUseInboxDefaultNotificationSettings());
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setNotificationsEnabled(final boolean enabled) {
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, enabled).apply();
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        MailIntentService.broadcastBackupDataChanged(getContext());
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public String getNotificationRingtoneUri() {
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().getString(PreferenceKeys.NOTIFICATION_RINGTONE,
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                Settings.System.DEFAULT_NOTIFICATION_URI.toString());
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setNotificationRingtoneUri(final String uri) {
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putString(PreferenceKeys.NOTIFICATION_RINGTONE, uri).apply();
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        MailIntentService.broadcastBackupDataChanged(getContext());
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean isNotificationVibrateEnabled() {
237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().getBoolean(PreferenceKeys.NOTIFICATION_VIBRATE, false);
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setNotificationVibrateEnabled(final boolean enabled) {
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putBoolean(PreferenceKeys.NOTIFICATION_VIBRATE, enabled).apply();
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        MailIntentService.broadcastBackupDataChanged(getContext());
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean isEveryMessageNotificationEnabled() {
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences()
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                .getBoolean(PreferenceKeys.NOTIFICATION_NOTIFY_EVERY_MESSAGE, false);
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setEveryMessageNotificationEnabled(final boolean enabled) {
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putBoolean(PreferenceKeys.NOTIFICATION_NOTIFY_EVERY_MESSAGE, enabled).apply();
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        MailIntentService.broadcastBackupDataChanged(getContext());
253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
255cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy    private Set<String> getDefaultNotificationActions(final Context context,
256cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy            final Account account) {
257cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy        final boolean supportsArchiveRemoveLabel =
258cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                account.supportsCapability(AccountCapabilities.ARCHIVE)
259cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                && (mFolder.supportsCapability(FolderCapabilities.ARCHIVE)
260cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                || mFolder.supportsCapability(FolderCapabilities.ALLOWS_REMOVE_CONVERSATION));
261ae5eb02397318c432d3d77e30fb4de842a34cfeaScott Kennedy        // Use the swipe setting, since it is essentially a way to allow the user to specify
262ae5eb02397318c432d3d77e30fb4de842a34cfeaScott Kennedy        // whether they prefer archive or delete, without adding another setting
263ae5eb02397318c432d3d77e30fb4de842a34cfeaScott Kennedy        final boolean preferDelete =
264ae5eb02397318c432d3d77e30fb4de842a34cfeaScott Kennedy                MailPrefs.ConversationListSwipeActions.DELETE.equals(MailPrefs.get(context)
265ae5eb02397318c432d3d77e30fb4de842a34cfeaScott Kennedy                        .getConversationListSwipeAction(true /* supportsArchive */));
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationActionType destructiveActionType =
267cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                supportsArchiveRemoveLabel && !preferDelete ?
26885db140774e0691b703a3aa94a4640631ef814d0Scott Kennedy                        NotificationActionType.ARCHIVE_REMOVE_LABEL : NotificationActionType.DELETE;
269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String destructiveAction = destructiveActionType.getPersistedValue();
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String replyAction =
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                MailPrefs.get(context).getDefaultReplyAll() ? NotificationActionType.REPLY_ALL
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .getPersistedValue()
274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        : NotificationActionType.REPLY.getPersistedValue();
275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<String> notificationActions = new LinkedHashSet<String>(2);
277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationActions.add(destructiveAction);
278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationActions.add(replyAction);
279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return notificationActions;
281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the notification settings configured for this account and label, or the default if none
285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * have been set.
286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
287cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy    public Set<String> getNotificationActions(final Account account) {
288cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy        return getSharedPreferences().getStringSet(PreferenceKeys.NOTIFICATION_ACTIONS,
289cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                getDefaultNotificationActions(getContext(), account));
290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
292