Preferences.java revision e05f511a4ea9ae28841b385987531d8be1d2d87f
1/*
2 * Copyright (C) 2008 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 */
16
17package com.android.email;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.text.TextUtils;
22import android.util.Log;
23
24import com.android.emailcommon.Logging;
25
26import org.json.JSONArray;
27import org.json.JSONException;
28
29import java.util.HashSet;
30import java.util.UUID;
31
32public class Preferences {
33
34    // Preferences file
35    private static final String PREFERENCES_FILE = "AndroidMail.Main";
36
37    // Preferences field names
38    private static final String ACCOUNT_UUIDS = "accountUuids";
39    private static final String ENABLE_DEBUG_LOGGING = "enableDebugLogging";
40    private static final String ENABLE_EXCHANGE_LOGGING = "enableExchangeLogging";
41    private static final String ENABLE_EXCHANGE_FILE_LOGGING = "enableExchangeFileLogging";
42    private static final String INHIBIT_GRAPHICS_ACCELERATION = "inhibitGraphicsAcceleration";
43    private static final String FORCE_ONE_MINUTE_REFRESH = "forceOneMinuteRefresh";
44    private static final String ENABLE_STRICT_MODE = "enableStrictMode";
45    private static final String DEVICE_UID = "deviceUID";
46    private static final String ONE_TIME_INITIALIZATION_PROGRESS = "oneTimeInitializationProgress";
47    private static final String AUTO_ADVANCE_DIRECTION = "autoAdvance";
48    private static final String TEXT_ZOOM = "textZoom";
49    private static final String BACKGROUND_ATTACHMENTS = "backgroundAttachments";
50    private static final String TRUSTED_SENDERS = "trustedSenders";
51
52    public static final int AUTO_ADVANCE_NEWER = 0;
53    public static final int AUTO_ADVANCE_OLDER = 1;
54    public static final int AUTO_ADVANCE_MESSAGE_LIST = 2;
55    // "move to older" was the behavior on older versions.
56    private static final int AUTO_ADVANCE_DEFAULT = AUTO_ADVANCE_OLDER;
57
58    // The following constants are used as offsets into TEXT_ZOOM_ARRAY (below)
59    public static final int TEXT_ZOOM_TINY = 0;
60    public static final int TEXT_ZOOM_SMALL = 1;
61    public static final int TEXT_ZOOM_NORMAL = 2;
62    public static final int TEXT_ZOOM_LARGE = 3;
63    public static final int TEXT_ZOOM_HUGE = 4;
64    // "normal" will be the default
65    public static final int TEXT_ZOOM_DEFAULT = TEXT_ZOOM_NORMAL;
66
67    private static Preferences sPreferences;
68
69    private final SharedPreferences mSharedPreferences;
70
71    /**
72     * A set of trusted senders for whom images and external resources should automatically be
73     * loaded for.
74     * Lazilly created.
75     */
76    private HashSet<String> mTrustedSenders = null;
77
78    private Preferences(Context context) {
79        mSharedPreferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
80    }
81
82    /**
83     * TODO need to think about what happens if this gets GCed along with the
84     * Activity that initialized it. Do we lose ability to read Preferences in
85     * further Activities? Maybe this should be stored in the Application
86     * context.
87     */
88    public static synchronized Preferences getPreferences(Context context) {
89        if (sPreferences == null) {
90            sPreferences = new Preferences(context);
91        }
92        return sPreferences;
93    }
94
95    public static String getLegacyBackupPreference(Context context) {
96        return getPreferences(context).mSharedPreferences.getString(ACCOUNT_UUIDS, null);
97    }
98
99    public static void clearLegacyBackupPreference(Context context) {
100        getPreferences(context).mSharedPreferences.edit().remove(ACCOUNT_UUIDS).apply();
101    }
102
103    public void setEnableDebugLogging(boolean value) {
104        mSharedPreferences.edit().putBoolean(ENABLE_DEBUG_LOGGING, value).apply();
105    }
106
107    public boolean getEnableDebugLogging() {
108        return mSharedPreferences.getBoolean(ENABLE_DEBUG_LOGGING, false);
109    }
110
111    public void setEnableExchangeLogging(boolean value) {
112        mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_LOGGING, value).apply();
113    }
114
115    public boolean getEnableExchangeLogging() {
116        return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_LOGGING, false);
117    }
118
119    public void setEnableExchangeFileLogging(boolean value) {
120        mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_FILE_LOGGING, value).apply();
121    }
122
123    public boolean getEnableExchangeFileLogging() {
124        return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_FILE_LOGGING, false);
125    }
126
127    public void setInhibitGraphicsAcceleration(boolean value) {
128        mSharedPreferences.edit().putBoolean(INHIBIT_GRAPHICS_ACCELERATION, value).apply();
129    }
130
131    public boolean getInhibitGraphicsAcceleration() {
132        return mSharedPreferences.getBoolean(INHIBIT_GRAPHICS_ACCELERATION, false);
133    }
134
135    public void setForceOneMinuteRefresh(boolean value) {
136        mSharedPreferences.edit().putBoolean(FORCE_ONE_MINUTE_REFRESH, value).apply();
137    }
138
139    public boolean getForceOneMinuteRefresh() {
140        return mSharedPreferences.getBoolean(FORCE_ONE_MINUTE_REFRESH, false);
141    }
142
143    public void setEnableStrictMode(boolean value) {
144        mSharedPreferences.edit().putBoolean(ENABLE_STRICT_MODE, value).apply();
145    }
146
147    public boolean getEnableStrictMode() {
148        return mSharedPreferences.getBoolean(ENABLE_STRICT_MODE, false);
149    }
150
151    /**
152     * Generate a new "device UID".  This is local to Email app only, to prevent possibility
153     * of correlation with any other user activities in any other apps.
154     * @return a persistent, unique ID
155     */
156    public synchronized String getDeviceUID() {
157         String result = mSharedPreferences.getString(DEVICE_UID, null);
158         if (result == null) {
159             result = UUID.randomUUID().toString();
160             mSharedPreferences.edit().putString(DEVICE_UID, result).apply();
161         }
162         return result;
163    }
164
165    public int getOneTimeInitializationProgress() {
166        return mSharedPreferences.getInt(ONE_TIME_INITIALIZATION_PROGRESS, 0);
167    }
168
169    public void setOneTimeInitializationProgress(int progress) {
170        mSharedPreferences.edit().putInt(ONE_TIME_INITIALIZATION_PROGRESS, progress).apply();
171    }
172
173    public int getAutoAdvanceDirection() {
174        return mSharedPreferences.getInt(AUTO_ADVANCE_DIRECTION, AUTO_ADVANCE_DEFAULT);
175    }
176
177    public void setAutoAdvanceDirection(int direction) {
178        mSharedPreferences.edit().putInt(AUTO_ADVANCE_DIRECTION, direction).apply();
179    }
180
181    public int getTextZoom() {
182        return mSharedPreferences.getInt(TEXT_ZOOM, TEXT_ZOOM_DEFAULT);
183    }
184
185    public void setTextZoom(int zoom) {
186        mSharedPreferences.edit().putInt(TEXT_ZOOM, zoom).apply();
187    }
188
189    public boolean getBackgroundAttachments() {
190        return mSharedPreferences.getBoolean(BACKGROUND_ATTACHMENTS, false);
191    }
192
193    public void setBackgroundAttachments(boolean allowed) {
194        mSharedPreferences.edit().putBoolean(BACKGROUND_ATTACHMENTS, allowed).apply();
195    }
196
197    /**
198     * Determines whether or not a sender should be trusted and images should automatically be
199     * shown for messages by that sender.
200     */
201    public boolean shouldShowImagesFor(String email) {
202        if (mTrustedSenders == null) {
203            try {
204                mTrustedSenders = parseEmailSet(mSharedPreferences.getString(TRUSTED_SENDERS, ""));
205            } catch (JSONException e) {
206                // Something went wrong, and the data is corrupt. Just clear it to be safe.
207                Log.w(Logging.LOG_TAG, "Trusted sender set corrupted. Clearing");
208                mSharedPreferences.edit().putString(TRUSTED_SENDERS, "").apply();
209                mTrustedSenders = new HashSet<String>();
210            }
211        }
212        return mTrustedSenders.contains(email);
213    }
214
215    /**
216     * Marks a sender as trusted so that images from that sender will automatically be shown.
217     */
218    public void setSenderAsTrusted(String email) {
219        if (!mTrustedSenders.contains(email)) {
220            mTrustedSenders.add(email);
221            mSharedPreferences
222                    .edit()
223                    .putString(TRUSTED_SENDERS, packEmailSet(mTrustedSenders))
224                    .apply();
225        }
226    }
227
228    /**
229     * Clears all trusted senders asynchronously.
230     */
231    public void clearTrustedSenders() {
232        mTrustedSenders = new HashSet<String>();
233        mSharedPreferences
234                .edit()
235                .putString(TRUSTED_SENDERS, packEmailSet(mTrustedSenders))
236                .apply();
237    }
238
239    HashSet<String> parseEmailSet(String serialized) throws JSONException {
240        HashSet<String> result = new HashSet<String>();
241        if (!TextUtils.isEmpty(serialized)) {
242            JSONArray arr = new JSONArray(serialized);
243            for (int i = 0, len = arr.length(); i < len; i++) {
244                result.add((String) arr.get(i));
245            }
246        }
247        return result;
248    }
249
250    String packEmailSet(HashSet<String> set) {
251        return new JSONArray(set).toString();
252    }
253
254    public void clear() {
255        mSharedPreferences.edit().clear().apply();
256    }
257
258    public void dump() {
259        if (Logging.LOGD) {
260            for (String key : mSharedPreferences.getAll().keySet()) {
261                Log.v(Logging.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key));
262            }
263        }
264    }
265}
266