• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.email;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.net.Uri;
22 import android.util.Log;
23 
24 public class Preferences {
25 
26     // Preferences file
27     private static final String PREFERENCES_FILE = "AndroidMail.Main";
28 
29     // Preferences field names
30     private static final String ACCOUNT_UUIDS = "accountUuids";
31     private static final String DEFAULT_ACCOUNT_UUID = "defaultAccountUuid";
32     private static final String ENABLE_DEBUG_LOGGING = "enableDebugLogging";
33     private static final String ENABLE_SENSITIVE_LOGGING = "enableSensitiveLogging";
34     private static final String ENABLE_EXCHANGE_LOGGING = "enableExchangeLogging";
35     private static final String ENABLE_EXCHANGE_FILE_LOGGING = "enableExchangeFileLogging";
36 
37     private static Preferences preferences;
38 
39     SharedPreferences mSharedPreferences;
40 
Preferences(Context context)41     private Preferences(Context context) {
42         mSharedPreferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
43     }
44 
45     /**
46      * TODO need to think about what happens if this gets GCed along with the
47      * Activity that initialized it. Do we lose ability to read Preferences in
48      * further Activities? Maybe this should be stored in the Application
49      * context.
50      */
getPreferences(Context context)51     public static synchronized Preferences getPreferences(Context context) {
52         if (preferences == null) {
53             preferences = new Preferences(context);
54         }
55         return preferences;
56     }
57 
58     /**
59      * Returns an array of the accounts on the system. If no accounts are
60      * registered the method returns an empty array.
61      */
getAccounts()62     public Account[] getAccounts() {
63         String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
64         if (accountUuids == null || accountUuids.length() == 0) {
65             return new Account[] {};
66         }
67         String[] uuids = accountUuids.split(",");
68         Account[] accounts = new Account[uuids.length];
69         for (int i = 0, length = uuids.length; i < length; i++) {
70             accounts[i] = new Account(this, uuids[i]);
71         }
72         return accounts;
73     }
74 
75     /**
76      * Get an account object by Uri, or return null if no account exists
77      * TODO: Merge hardcoded strings with the same strings in Account.java
78      */
getAccountByContentUri(Uri uri)79     public Account getAccountByContentUri(Uri uri) {
80         if (!"content".equals(uri.getScheme()) || !"accounts".equals(uri.getAuthority())) {
81             return null;
82         }
83         String uuid = uri.getPath().substring(1);
84         if (uuid == null) {
85             return null;
86         }
87         String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
88         if (accountUuids == null || accountUuids.length() == 0) {
89             return null;
90         }
91         String[] uuids = accountUuids.split(",");
92         for (int i = 0, length = uuids.length; i < length; i++) {
93             if (uuid.equals(uuids[i])) {
94                 return new Account(this, uuid);
95             }
96         }
97         return null;
98     }
99 
100     /**
101      * Returns the Account marked as default. If no account is marked as default
102      * the first account in the list is marked as default and then returned. If
103      * there are no accounts on the system the method returns null.
104      */
getDefaultAccount()105     public Account getDefaultAccount() {
106         String defaultAccountUuid = mSharedPreferences.getString(DEFAULT_ACCOUNT_UUID, null);
107         Account defaultAccount = null;
108         Account[] accounts = getAccounts();
109         if (defaultAccountUuid != null) {
110             for (Account account : accounts) {
111                 if (account.getUuid().equals(defaultAccountUuid)) {
112                     defaultAccount = account;
113                     break;
114                 }
115             }
116         }
117 
118         if (defaultAccount == null) {
119             if (accounts.length > 0) {
120                 defaultAccount = accounts[0];
121                 setDefaultAccount(defaultAccount);
122             }
123         }
124 
125         return defaultAccount;
126     }
127 
setDefaultAccount(Account account)128     public void setDefaultAccount(Account account) {
129         mSharedPreferences.edit().putString(DEFAULT_ACCOUNT_UUID, account.getUuid()).commit();
130     }
131 
setEnableDebugLogging(boolean value)132     public void setEnableDebugLogging(boolean value) {
133         mSharedPreferences.edit().putBoolean(ENABLE_DEBUG_LOGGING, value).commit();
134     }
135 
getEnableDebugLogging()136     public boolean getEnableDebugLogging() {
137         return mSharedPreferences.getBoolean(ENABLE_DEBUG_LOGGING, false);
138     }
139 
setEnableSensitiveLogging(boolean value)140     public void setEnableSensitiveLogging(boolean value) {
141         mSharedPreferences.edit().putBoolean(ENABLE_SENSITIVE_LOGGING, value).commit();
142     }
143 
getEnableSensitiveLogging()144     public boolean getEnableSensitiveLogging() {
145         return mSharedPreferences.getBoolean(ENABLE_SENSITIVE_LOGGING, false);
146     }
147 
setEnableExchangeLogging(boolean value)148     public void setEnableExchangeLogging(boolean value) {
149         mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_LOGGING, value).commit();
150     }
151 
getEnableExchangeLogging()152     public boolean getEnableExchangeLogging() {
153         return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_LOGGING, false);
154     }
155 
setEnableExchangeFileLogging(boolean value)156     public void setEnableExchangeFileLogging(boolean value) {
157         mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_FILE_LOGGING, value).commit();
158     }
159 
getEnableExchangeFileLogging()160     public boolean getEnableExchangeFileLogging() {
161         return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_FILE_LOGGING, false);
162     }
163 
save()164     public void save() {
165     }
166 
clear()167     public void clear() {
168         mSharedPreferences.edit().clear().commit();
169     }
170 
dump()171     public void dump() {
172         if (Email.LOGD) {
173             for (String key : mSharedPreferences.getAll().keySet()) {
174                 Log.v(Email.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key));
175             }
176         }
177     }
178 }
179