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