Preferences.java revision 687f9962d7095e18ef994cd0e64337f02ed1a5bd
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 java.util.Arrays; 20 21import android.content.Context; 22import android.content.SharedPreferences; 23import android.net.Uri; 24import android.util.Config; 25import android.util.Log; 26 27public class Preferences { 28 private static Preferences preferences; 29 30 SharedPreferences mSharedPreferences; 31 32 private Preferences(Context context) { 33 mSharedPreferences = context.getSharedPreferences("AndroidMail.Main", Context.MODE_PRIVATE); 34 } 35 36 /** 37 * TODO need to think about what happens if this gets GCed along with the 38 * Activity that initialized it. Do we lose ability to read Preferences in 39 * further Activities? Maybe this should be stored in the Application 40 * context. 41 * 42 * @return 43 */ 44 public static synchronized Preferences getPreferences(Context context) { 45 if (preferences == null) { 46 preferences = new Preferences(context); 47 } 48 return preferences; 49 } 50 51 /** 52 * Returns an array of the accounts on the system. If no accounts are 53 * registered the method returns an empty array. 54 * 55 * @return 56 */ 57 public Account[] getAccounts() { 58 String accountUuids = mSharedPreferences.getString("accountUuids", null); 59 if (accountUuids == null || accountUuids.length() == 0) { 60 return new Account[] {}; 61 } 62 String[] uuids = accountUuids.split(","); 63 Account[] accounts = new Account[uuids.length]; 64 for (int i = 0, length = uuids.length; i < length; i++) { 65 accounts[i] = new Account(this, uuids[i]); 66 } 67 return accounts; 68 } 69 70 public Account getAccountByContentUri(Uri uri) { 71 return new Account(this, uri.getPath().substring(1)); 72 } 73 74 /** 75 * Returns the Account marked as default. If no account is marked as default 76 * the first account in the list is marked as default and then returned. If 77 * there are no accounts on the system the method returns null. 78 * 79 * @return 80 */ 81 public Account getDefaultAccount() { 82 String defaultAccountUuid = mSharedPreferences.getString("defaultAccountUuid", null); 83 Account defaultAccount = null; 84 Account[] accounts = getAccounts(); 85 if (defaultAccountUuid != null) { 86 for (Account account : accounts) { 87 if (account.getUuid().equals(defaultAccountUuid)) { 88 defaultAccount = account; 89 break; 90 } 91 } 92 } 93 94 if (defaultAccount == null) { 95 if (accounts.length > 0) { 96 defaultAccount = accounts[0]; 97 setDefaultAccount(defaultAccount); 98 } 99 } 100 101 return defaultAccount; 102 } 103 104 public void setDefaultAccount(Account account) { 105 mSharedPreferences.edit().putString("defaultAccountUuid", account.getUuid()).commit(); 106 } 107 108 public void setEnableDebugLogging(boolean value) { 109 mSharedPreferences.edit().putBoolean("enableDebugLogging", value).commit(); 110 } 111 112 public boolean geteEnableDebugLogging() { 113 return mSharedPreferences.getBoolean("enableDebugLogging", false); 114 } 115 116 public void setEnableSensitiveLogging(boolean value) { 117 mSharedPreferences.edit().putBoolean("enableSensitiveLogging", value).commit(); 118 } 119 120 public boolean getEnableSensitiveLogging() { 121 return mSharedPreferences.getBoolean("enableSensitiveLogging", false); 122 } 123 124 public void save() { 125 } 126 127 public void clear() { 128 mSharedPreferences.edit().clear().commit(); 129 } 130 131 public void dump() { 132 if (Config.LOGV) { 133 for (String key : mSharedPreferences.getAll().keySet()) { 134 Log.v(Email.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key)); 135 } 136 } 137 } 138} 139