Preferences.java revision 52618afa81eb47b9efd4b199b4ae2abbbab3402b
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 com.android.emailcommon.Logging;
20
21import android.content.Context;
22import android.content.SharedPreferences;
23import android.net.Uri;
24import android.util.Log;
25
26import java.util.UUID;
27
28public class Preferences {
29
30    // Preferences file
31    private static final String PREFERENCES_FILE = "AndroidMail.Main";
32
33    // Preferences field names
34    private static final String ACCOUNT_UUIDS = "accountUuids";
35    private static final String DEFAULT_ACCOUNT_UUID = "defaultAccountUuid";
36    private static final String ENABLE_DEBUG_LOGGING = "enableDebugLogging";
37    private static final String ENABLE_EXCHANGE_LOGGING = "enableExchangeLogging";
38    private static final String ENABLE_EXCHANGE_FILE_LOGGING = "enableExchangeFileLogging";
39    private static final String INHIBIT_GRAPHICS_ACCELERATION = "inhibitGraphicsAcceleration";
40    private static final String FORCE_ONE_MINUTE_REFRESH = "forceOneMinuteRefresh";
41    private static final String ENABLE_STRICT_MODE = "enableStrictMode";
42    private static final String DEVICE_UID = "deviceUID";
43    private static final String ONE_TIME_INITIALIZATION_PROGRESS = "oneTimeInitializationProgress";
44    private static final String AUTO_ADVANCE_DIRECTION = "autoAdvance";
45    private static final String TEXT_ZOOM = "textZoom";
46    private static final String BACKGROUND_ATTACHMENTS = "backgroundAttachments";
47
48    public static final int AUTO_ADVANCE_NEWER = 0;
49    public static final int AUTO_ADVANCE_OLDER = 1;
50    public static final int AUTO_ADVANCE_MESSAGE_LIST = 2;
51    // "move to older" was the behavior on older versions.
52    private static final int AUTO_ADVANCE_DEFAULT = AUTO_ADVANCE_OLDER;
53
54    // The following constants are used as offsets into TEXT_ZOOM_ARRAY (below)
55    public static final int TEXT_ZOOM_TINY = 0;
56    public static final int TEXT_ZOOM_SMALL = 1;
57    public static final int TEXT_ZOOM_NORMAL = 2;
58    public static final int TEXT_ZOOM_LARGE = 3;
59    public static final int TEXT_ZOOM_HUGE = 4;
60    // "normal" will be the default
61    public static final int TEXT_ZOOM_DEFAULT = TEXT_ZOOM_NORMAL;
62
63    private static Preferences sPreferences;
64
65    final SharedPreferences mSharedPreferences;
66
67    private Preferences(Context context) {
68        mSharedPreferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
69    }
70
71    /**
72     * TODO need to think about what happens if this gets GCed along with the
73     * Activity that initialized it. Do we lose ability to read Preferences in
74     * further Activities? Maybe this should be stored in the Application
75     * context.
76     */
77    public static synchronized Preferences getPreferences(Context context) {
78        if (sPreferences == null) {
79            sPreferences = new Preferences(context);
80        }
81        return sPreferences;
82    }
83
84    /**
85     * Returns an array of the accounts on the system. If no accounts are
86     * registered the method returns an empty array.
87     */
88    public Account[] getAccounts() {
89        String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
90        if (accountUuids == null || accountUuids.length() == 0) {
91            return new Account[] {};
92        }
93        String[] uuids = accountUuids.split(",");
94        Account[] accounts = new Account[uuids.length];
95        for (int i = 0, length = uuids.length; i < length; i++) {
96            accounts[i] = new Account(this, uuids[i]);
97        }
98        return accounts;
99    }
100
101    /**
102     * Get an account object by Uri, or return null if no account exists
103     * TODO: Merge hardcoded strings with the same strings in Account.java
104     */
105    public Account getAccountByContentUri(Uri uri) {
106        if (!"content".equals(uri.getScheme()) || !"accounts".equals(uri.getAuthority())) {
107            return null;
108        }
109        String uuid = uri.getPath().substring(1);
110        if (uuid == null) {
111            return null;
112        }
113        String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
114        if (accountUuids == null || accountUuids.length() == 0) {
115            return null;
116        }
117        String[] uuids = accountUuids.split(",");
118        for (int i = 0, length = uuids.length; i < length; i++) {
119            if (uuid.equals(uuids[i])) {
120                return new Account(this, uuid);
121            }
122        }
123        return null;
124    }
125
126    /**
127     * Returns the Account marked as default. If no account is marked as default
128     * the first account in the list is marked as default and then returned. If
129     * there are no accounts on the system the method returns null.
130     */
131    public Account getDefaultAccount() {
132        String defaultAccountUuid = mSharedPreferences.getString(DEFAULT_ACCOUNT_UUID, null);
133        Account defaultAccount = null;
134        Account[] accounts = getAccounts();
135        if (defaultAccountUuid != null) {
136            for (Account account : accounts) {
137                if (account.getUuid().equals(defaultAccountUuid)) {
138                    defaultAccount = account;
139                    break;
140                }
141            }
142        }
143
144        if (defaultAccount == null) {
145            if (accounts.length > 0) {
146                defaultAccount = accounts[0];
147                setDefaultAccount(defaultAccount);
148            }
149        }
150
151        return defaultAccount;
152    }
153
154    public void setDefaultAccount(Account account) {
155        mSharedPreferences.edit().putString(DEFAULT_ACCOUNT_UUID, account.getUuid()).apply();
156    }
157
158    public void setEnableDebugLogging(boolean value) {
159        mSharedPreferences.edit().putBoolean(ENABLE_DEBUG_LOGGING, value).apply();
160    }
161
162    public boolean getEnableDebugLogging() {
163        return mSharedPreferences.getBoolean(ENABLE_DEBUG_LOGGING, false);
164    }
165
166    public void setEnableExchangeLogging(boolean value) {
167        mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_LOGGING, value).apply();
168    }
169
170    public boolean getEnableExchangeLogging() {
171        return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_LOGGING, false);
172    }
173
174    public void setEnableExchangeFileLogging(boolean value) {
175        mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_FILE_LOGGING, value).apply();
176    }
177
178    public boolean getEnableExchangeFileLogging() {
179        return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_FILE_LOGGING, false);
180    }
181
182    public void setInhibitGraphicsAcceleration(boolean value) {
183        mSharedPreferences.edit().putBoolean(INHIBIT_GRAPHICS_ACCELERATION, value).apply();
184    }
185
186    public boolean getInhibitGraphicsAcceleration() {
187        return mSharedPreferences.getBoolean(INHIBIT_GRAPHICS_ACCELERATION, false);
188    }
189
190    public void setForceOneMinuteRefresh(boolean value) {
191        mSharedPreferences.edit().putBoolean(FORCE_ONE_MINUTE_REFRESH, value).apply();
192    }
193
194    public boolean getForceOneMinuteRefresh() {
195        return mSharedPreferences.getBoolean(FORCE_ONE_MINUTE_REFRESH, false);
196    }
197
198    public void setEnableStrictMode(boolean value) {
199        mSharedPreferences.edit().putBoolean(ENABLE_STRICT_MODE, value).apply();
200    }
201
202    public boolean getEnableStrictMode() {
203        return mSharedPreferences.getBoolean(ENABLE_STRICT_MODE, false);
204    }
205
206    /**
207     * Generate a new "device UID".  This is local to Email app only, to prevent possibility
208     * of correlation with any other user activities in any other apps.
209     * @return a persistent, unique ID
210     */
211    public synchronized String getDeviceUID() {
212         String result = mSharedPreferences.getString(DEVICE_UID, null);
213         if (result == null) {
214             result = UUID.randomUUID().toString();
215             mSharedPreferences.edit().putString(DEVICE_UID, result).apply();
216         }
217         return result;
218    }
219
220    public int getOneTimeInitializationProgress() {
221        return mSharedPreferences.getInt(ONE_TIME_INITIALIZATION_PROGRESS, 0);
222    }
223
224    public void setOneTimeInitializationProgress(int progress) {
225        mSharedPreferences.edit().putInt(ONE_TIME_INITIALIZATION_PROGRESS, progress).apply();
226    }
227
228    public int getAutoAdvanceDirection() {
229        return mSharedPreferences.getInt(AUTO_ADVANCE_DIRECTION, AUTO_ADVANCE_DEFAULT);
230    }
231
232    public void setAutoAdvanceDirection(int direction) {
233        mSharedPreferences.edit().putInt(AUTO_ADVANCE_DIRECTION, direction).apply();
234    }
235
236    public int getTextZoom() {
237        return mSharedPreferences.getInt(TEXT_ZOOM, TEXT_ZOOM_DEFAULT);
238    }
239
240    public void setTextZoom(int zoom) {
241        mSharedPreferences.edit().putInt(TEXT_ZOOM, zoom).apply();
242    }
243
244    public boolean getBackgroundAttachments() {
245        return mSharedPreferences.getBoolean(BACKGROUND_ATTACHMENTS, false);
246    }
247
248    public void setBackgroundAttachments(boolean allowed) {
249        mSharedPreferences.edit().putBoolean(BACKGROUND_ATTACHMENTS, allowed).apply();
250    }
251
252    public void save() {
253    }
254
255    public void clear() {
256        mSharedPreferences.edit().clear().apply();
257    }
258
259    public void dump() {
260        if (Email.LOGD) {
261            for (String key : mSharedPreferences.getAll().keySet()) {
262                Log.v(Logging.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key));
263            }
264        }
265    }
266}
267