Preferences.java revision bd29c3090305ce415fb29ba7af339a1359c746d3
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 DEVICE_UID = "deviceUID";
38    private static final String ONE_TIME_INITIALIZATION_PROGRESS = "oneTimeInitializationProgress";
39    private static final String AUTO_ADVANCE_DIRECTION = "autoAdvance";
40
41    public static final int AUTO_ADVANCE_NEWER = 0;
42    public static final int AUTO_ADVANCE_OLDER = 1;
43    public static final int AUTO_ADVANCE_MESSAGE_LIST = 2;
44    // "move to older" was the behavior on older versions.
45    public static final int AUTO_ADVANCE_DEFAULT = AUTO_ADVANCE_OLDER;
46
47    private static Preferences sPreferences;
48
49    final SharedPreferences mSharedPreferences;
50
51    private Preferences(Context context) {
52        mSharedPreferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
53    }
54
55    /**
56     * TODO need to think about what happens if this gets GCed along with the
57     * Activity that initialized it. Do we lose ability to read Preferences in
58     * further Activities? Maybe this should be stored in the Application
59     * context.
60     */
61    public static synchronized Preferences getPreferences(Context context) {
62        if (sPreferences == null) {
63            sPreferences = new Preferences(context);
64        }
65        return sPreferences;
66    }
67
68    /**
69     * Returns an array of the accounts on the system. If no accounts are
70     * registered the method returns an empty array.
71     */
72    public Account[] getAccounts() {
73        String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
74        if (accountUuids == null || accountUuids.length() == 0) {
75            return new Account[] {};
76        }
77        String[] uuids = accountUuids.split(",");
78        Account[] accounts = new Account[uuids.length];
79        for (int i = 0, length = uuids.length; i < length; i++) {
80            accounts[i] = new Account(this, uuids[i]);
81        }
82        return accounts;
83    }
84
85    /**
86     * Get an account object by Uri, or return null if no account exists
87     * TODO: Merge hardcoded strings with the same strings in Account.java
88     */
89    public Account getAccountByContentUri(Uri uri) {
90        if (!"content".equals(uri.getScheme()) || !"accounts".equals(uri.getAuthority())) {
91            return null;
92        }
93        String uuid = uri.getPath().substring(1);
94        if (uuid == null) {
95            return null;
96        }
97        String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
98        if (accountUuids == null || accountUuids.length() == 0) {
99            return null;
100        }
101        String[] uuids = accountUuids.split(",");
102        for (int i = 0, length = uuids.length; i < length; i++) {
103            if (uuid.equals(uuids[i])) {
104                return new Account(this, uuid);
105            }
106        }
107        return null;
108    }
109
110    /**
111     * Returns the Account marked as default. If no account is marked as default
112     * the first account in the list is marked as default and then returned. If
113     * there are no accounts on the system the method returns null.
114     */
115    public Account getDefaultAccount() {
116        String defaultAccountUuid = mSharedPreferences.getString(DEFAULT_ACCOUNT_UUID, null);
117        Account defaultAccount = null;
118        Account[] accounts = getAccounts();
119        if (defaultAccountUuid != null) {
120            for (Account account : accounts) {
121                if (account.getUuid().equals(defaultAccountUuid)) {
122                    defaultAccount = account;
123                    break;
124                }
125            }
126        }
127
128        if (defaultAccount == null) {
129            if (accounts.length > 0) {
130                defaultAccount = accounts[0];
131                setDefaultAccount(defaultAccount);
132            }
133        }
134
135        return defaultAccount;
136    }
137
138    public void setDefaultAccount(Account account) {
139        mSharedPreferences.edit().putString(DEFAULT_ACCOUNT_UUID, account.getUuid()).apply();
140    }
141
142    public void setEnableDebugLogging(boolean value) {
143        mSharedPreferences.edit().putBoolean(ENABLE_DEBUG_LOGGING, value).apply();
144    }
145
146    public boolean getEnableDebugLogging() {
147        return mSharedPreferences.getBoolean(ENABLE_DEBUG_LOGGING, false);
148    }
149
150    public void setEnableExchangeLogging(boolean value) {
151        mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_LOGGING, value).apply();
152    }
153
154    public boolean getEnableExchangeLogging() {
155        return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_LOGGING, false);
156    }
157
158    public void setEnableExchangeFileLogging(boolean value) {
159        mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_FILE_LOGGING, value).apply();
160    }
161
162    public boolean getEnableExchangeFileLogging() {
163        return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_FILE_LOGGING, false);
164    }
165
166    /**
167     * Generate a new "device UID".  This is local to Email app only, to prevent possibility
168     * of correlation with any other user activities in any other apps.
169     * @return a persistent, unique ID
170     */
171    public synchronized String getDeviceUID() {
172         String result = mSharedPreferences.getString(DEVICE_UID, null);
173         if (result == null) {
174             result = UUID.randomUUID().toString();
175             mSharedPreferences.edit().putString(DEVICE_UID, result).apply();
176         }
177         return result;
178    }
179
180    public int getOneTimeInitializationProgress() {
181        return mSharedPreferences.getInt(ONE_TIME_INITIALIZATION_PROGRESS, 0);
182    }
183
184    public void setOneTimeInitializationProgress(int progress) {
185        mSharedPreferences.edit().putInt(ONE_TIME_INITIALIZATION_PROGRESS, progress).apply();
186    }
187
188    public int getAutoAdvanceDirection() {
189        return mSharedPreferences.getInt(AUTO_ADVANCE_DIRECTION, AUTO_ADVANCE_DEFAULT);
190    }
191
192    public void setAutoAdvanceDirection(int direction) {
193        mSharedPreferences.edit().putInt(AUTO_ADVANCE_DIRECTION, direction).apply();
194    }
195
196    public void save() {
197    }
198
199    public void clear() {
200        mSharedPreferences.edit().clear().apply();
201    }
202
203    public void dump() {
204        if (Email.LOGD) {
205            for (String key : mSharedPreferences.getAll().keySet()) {
206                Log.v(Email.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key));
207            }
208        }
209    }
210}
211