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