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