Preferences.java revision 24c068b912efd0b027f7842c4b98fa1482dba33c
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 public static final float[] TEXT_ZOOM_ARRAY = new float[] {0.8f, 0.9f, 1.0f, 1.2f, 1.5f}; 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 /** 188 * Generate a new "device UID". This is local to Email app only, to prevent possibility 189 * of correlation with any other user activities in any other apps. 190 * @return a persistent, unique ID 191 */ 192 public synchronized String getDeviceUID() { 193 String result = mSharedPreferences.getString(DEVICE_UID, null); 194 if (result == null) { 195 result = UUID.randomUUID().toString(); 196 mSharedPreferences.edit().putString(DEVICE_UID, result).apply(); 197 } 198 return result; 199 } 200 201 public int getOneTimeInitializationProgress() { 202 return mSharedPreferences.getInt(ONE_TIME_INITIALIZATION_PROGRESS, 0); 203 } 204 205 public void setOneTimeInitializationProgress(int progress) { 206 mSharedPreferences.edit().putInt(ONE_TIME_INITIALIZATION_PROGRESS, progress).apply(); 207 } 208 209 public int getAutoAdvanceDirection() { 210 return mSharedPreferences.getInt(AUTO_ADVANCE_DIRECTION, AUTO_ADVANCE_DEFAULT); 211 } 212 213 public void setAutoAdvanceDirection(int direction) { 214 mSharedPreferences.edit().putInt(AUTO_ADVANCE_DIRECTION, direction).apply(); 215 } 216 217 public int getTextZoom() { 218 return mSharedPreferences.getInt(TEXT_ZOOM, TEXT_ZOOM_DEFAULT); 219 } 220 221 public void setTextZoom(int zoom) { 222 mSharedPreferences.edit().putInt(TEXT_ZOOM, zoom).apply(); 223 } 224 225 public boolean getBackgroundAttachments() { 226 return mSharedPreferences.getBoolean(BACKGROUND_ATTACHMENTS, false); 227 } 228 229 public void setBackgroundAttachments(boolean allowed) { 230 mSharedPreferences.edit().putBoolean(BACKGROUND_ATTACHMENTS, allowed).apply(); 231 } 232 233 public void save() { 234 } 235 236 public void clear() { 237 mSharedPreferences.edit().clear().apply(); 238 } 239 240 public void dump() { 241 if (Email.LOGD) { 242 for (String key : mSharedPreferences.getAll().keySet()) { 243 Log.v(Email.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key)); 244 } 245 } 246 } 247} 248