FolderPreferences.java revision afb10d038c70c0a8646f0315836c49c704765ff9
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
181bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.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
28cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedyimport com.android.mail.providers.Account;
29d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Folder;
30cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedyimport com.android.mail.providers.UIProvider.AccountCapabilities;
31d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.UIProvider.FolderCapabilities;
32d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationActionType;
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
34d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.LinkedHashSet;
35d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/**
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Preferences relevant to one specific folder. In Email, this would only be used for an account's
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * inbox. In Gmail, this is used for every account/label pair.
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class FolderPreferences extends VersionedPrefs {
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
43d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final String PREFS_NAME_PREFIX = "Folder";
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
45d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static final class PreferenceKeys {
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** Boolean value indicating whether notifications are enabled */
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATIONS_ENABLED = "notifications-enabled";
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** String value of the notification ringtone URI */
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_RINGTONE = "notification-ringtone";
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /** Boolean value indicating whether we should explicitly vibrate */
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_VIBRATE = "notification-vibrate";
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
53d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Boolean value indicating whether we notify for every message (<code>true</code>), or just
54d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * once for the folder (<code>false</code>)
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final String NOTIFICATION_NOTIFY_EVERY_MESSAGE =
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                "notification-notify-every-message";
58d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
59d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public static final ImmutableSet<String> BACKUP_KEYS =
60d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new ImmutableSet.Builder<String>()
61d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATIONS_ENABLED)
62d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATION_RINGTONE)
63d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATION_VIBRATE)
64d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .add(NOTIFICATION_NOTIFY_EVERY_MESSAGE)
65d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        .build();
66d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
67d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
68d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private final Folder mFolder;
69d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** An id that is constant across app installations. */
70d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private final String mPersistentId;
71d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private final boolean mUseInboxDefaultNotificationSettings;
72d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
74afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler     * @param accountEmail The account email. This must never change for the account.
7561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy     * @param folder The folder
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
77afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler    public FolderPreferences(final Context context, final String accountEmail, final Folder folder,
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean useInboxDefaultNotificationSettings) {
79afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler        this(context, accountEmail, folder, folder.persistentId,
80afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                useInboxDefaultNotificationSettings);
81d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * A constructor that can be used when no {@link Folder} object is available (like during a
85f3219a170b4518b4f0ee96ab4c8c0d6855e56d6fScott Kennedy     * restore). This will function as expected except when calling
86f3219a170b4518b4f0ee96ab4c8c0d6855e56d6fScott Kennedy     * {@link #getDefaultNotificationActions(Context)}, so
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link #FolderPreferences(Context, String, Folder, boolean)} should be used if at all
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * possible.
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
90afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler     * @param accountEmail The account email. This must never change for the account.
91d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param persistentId An identifier for the folder that does not change across app
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *        installations.
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
94afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler    public FolderPreferences(final Context context, final String accountEmail, final String persistentId,
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean useInboxDefaultNotificationSettings) {
96afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler        this(context, accountEmail, null, persistentId, useInboxDefaultNotificationSettings);
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
99afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler    private FolderPreferences(final Context context, final String accountEmail, final Folder folder,
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String persistentId, final boolean useInboxDefaultNotificationSettings) {
101afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler        super(context, buildSharedPrefsName(accountEmail, persistentId));
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        mFolder = folder;
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        mPersistentId = persistentId;
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        mUseInboxDefaultNotificationSettings = useInboxDefaultNotificationSettings;
105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String buildSharedPrefsName(final String account, final String persistentId) {
108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return PREFS_NAME_PREFIX + '-' + account + '-' + persistentId;
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected void performUpgrade(final int oldVersion, final int newVersion) {
113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (oldVersion > newVersion) {
114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            throw new IllegalStateException(
115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    "You appear to have downgraded your app. Please clear app data.");
116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected boolean canBackup(final String key) {
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (mPersistentId == null) {
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return false;
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return PreferenceKeys.BACKUP_KEYS.contains(key);
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected Object getBackupValue(final String key, final Object value) {
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (PreferenceKeys.NOTIFICATION_RINGTONE.equals(key)) {
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return getRingtoneTitle((String) value);
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return super.getBackupValue(key, value);
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    @Override
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    protected Object getRestoreValue(final String key, final Object value) {
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (PreferenceKeys.NOTIFICATION_RINGTONE.equals(key)) {
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return getRingtoneUri((String) value);
141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return super.getBackupValue(key, value);
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private String getRingtoneTitle(final String ringtoneUriString) {
147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (ringtoneUriString.length() == 0) {
148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return ringtoneUriString;
149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Uri uri = Uri.parse(ringtoneUriString);
151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (RingtoneManager.isDefault(uri)) {
152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return ringtoneUriString;
153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final RingtoneManager ringtoneManager = new RingtoneManager(getContext());
155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ringtoneManager.setType(RingtoneManager.TYPE_NOTIFICATION);
156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Cursor cursor = ringtoneManager.getCursor();
157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (cursor.moveToNext()) {
159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri cursorUri = ContentUris.withAppendedId(
160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX)),
161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        cursor.getLong(RingtoneManager.ID_COLUMN_INDEX));
162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursorUri.toString().equals(ringtoneUriString)) {
163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!Strings.isNullOrEmpty(title)) {
165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return title;
166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor.close();
171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return null;
173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private String getRingtoneUri(final String name) {
176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (name.length() == 0 || RingtoneManager.isDefault(Uri.parse(name))) {
177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return name;
178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final RingtoneManager ringtoneManager = new RingtoneManager(getContext());
181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ringtoneManager.setType(RingtoneManager.TYPE_NOTIFICATION);
182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Cursor cursor = ringtoneManager.getCursor();
183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (cursor.moveToNext()) {
185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (name.equals(title)) {
187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Uri uri = ContentUris.withAppendedId(
188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX)),
189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor.getLong(RingtoneManager.ID_COLUMN_INDEX));
190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return uri.toString();
191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor.close();
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return null;
197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * If <code>true</code>, we use inbox-defaults for notification settings. If <code>false</code>,
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * we use standard defaults.
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private boolean getUseInboxDefaultNotificationSettings() {
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return mUseInboxDefaultNotificationSettings;
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean isNotificationsEnabledSet() {
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().contains(PreferenceKeys.NOTIFICATIONS_ENABLED);
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean areNotificationsEnabled() {
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().getBoolean(
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                PreferenceKeys.NOTIFICATIONS_ENABLED, getUseInboxDefaultNotificationSettings());
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setNotificationsEnabled(final boolean enabled) {
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putBoolean(PreferenceKeys.NOTIFICATIONS_ENABLED, enabled).apply();
218f9024cc52785c8740654dc89296a10a18da83077Scott Kennedy        notifyBackupPreferenceChanged();
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public String getNotificationRingtoneUri() {
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().getString(PreferenceKeys.NOTIFICATION_RINGTONE,
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                Settings.System.DEFAULT_NOTIFICATION_URI.toString());
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setNotificationRingtoneUri(final String uri) {
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putString(PreferenceKeys.NOTIFICATION_RINGTONE, uri).apply();
228f9024cc52785c8740654dc89296a10a18da83077Scott Kennedy        notifyBackupPreferenceChanged();
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean isNotificationVibrateEnabled() {
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences().getBoolean(PreferenceKeys.NOTIFICATION_VIBRATE, false);
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setNotificationVibrateEnabled(final boolean enabled) {
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putBoolean(PreferenceKeys.NOTIFICATION_VIBRATE, enabled).apply();
237f9024cc52785c8740654dc89296a10a18da83077Scott Kennedy        notifyBackupPreferenceChanged();
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public boolean isEveryMessageNotificationEnabled() {
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return getSharedPreferences()
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                .getBoolean(PreferenceKeys.NOTIFICATION_NOTIFY_EVERY_MESSAGE, false);
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void setEveryMessageNotificationEnabled(final boolean enabled) {
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        getEditor().putBoolean(PreferenceKeys.NOTIFICATION_NOTIFY_EVERY_MESSAGE, enabled).apply();
247f9024cc52785c8740654dc89296a10a18da83077Scott Kennedy        notifyBackupPreferenceChanged();
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
25026f2b30a88a35bc0ae6992c3f4297ebf20bbca53Scott Kennedy    public Set<String> getNotificationActions(final Account account) {
251cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy        final boolean supportsArchiveRemoveLabel =
252d4796d78b3d6994d77edaf91b176fc3c79bb7362Scott Kennedy                mFolder.supportsCapability(FolderCapabilities.ARCHIVE)
253d4796d78b3d6994d77edaf91b176fc3c79bb7362Scott Kennedy                || mFolder.supportsCapability(FolderCapabilities.ALLOWS_REMOVE_CONVERSATION);
25443a1cc1adc835f9a5df30ca2558a2aee35019706Andrew Sapperstein        final boolean preferDelete = MailPrefs.RemovalActions.DELETE.equals(
255d4796d78b3d6994d77edaf91b176fc3c79bb7362Scott Kennedy                MailPrefs.get(getContext()).getRemovalAction(
256d4796d78b3d6994d77edaf91b176fc3c79bb7362Scott Kennedy                        account.supportsCapability(AccountCapabilities.ARCHIVE)));
257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationActionType destructiveActionType =
258cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                supportsArchiveRemoveLabel && !preferDelete ?
25985db140774e0691b703a3aa94a4640631ef814d0Scott Kennedy                        NotificationActionType.ARCHIVE_REMOVE_LABEL : NotificationActionType.DELETE;
260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String destructiveAction = destructiveActionType.getPersistedValue();
261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String replyAction =
26322cf7e5a491382bacbfb97ccc2dcba86f03f9837Scott Kennedy                MailPrefs.get(getContext()).getDefaultReplyAll()
26422cf7e5a491382bacbfb97ccc2dcba86f03f9837Scott Kennedy                        ? NotificationActionType.REPLY_ALL.getPersistedValue()
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        : NotificationActionType.REPLY.getPersistedValue();
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<String> notificationActions = new LinkedHashSet<String>(2);
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationActions.add(destructiveAction);
269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationActions.add(replyAction);
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return notificationActions;
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
274