2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email;
|
|
|
|
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.Logging;
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2010-02-01 23:53:46 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
public class Preferences {
|
2009-08-14 00:43:25 +00:00
|
|
|
|
|
|
|
// Preferences file
|
|
|
|
private static final String PREFERENCES_FILE = "AndroidMail.Main";
|
|
|
|
|
|
|
|
// Preferences field names
|
|
|
|
private static final String ACCOUNT_UUIDS = "accountUuids";
|
|
|
|
private static final String DEFAULT_ACCOUNT_UUID = "defaultAccountUuid";
|
|
|
|
private static final String ENABLE_DEBUG_LOGGING = "enableDebugLogging";
|
|
|
|
private static final String ENABLE_EXCHANGE_LOGGING = "enableExchangeLogging";
|
|
|
|
private static final String ENABLE_EXCHANGE_FILE_LOGGING = "enableExchangeFileLogging";
|
2010-11-01 23:15:15 +00:00
|
|
|
private static final String INHIBIT_GRAPHICS_ACCELERATION = "inhibitGraphicsAcceleration";
|
2011-01-27 22:31:26 +00:00
|
|
|
private static final String FORCE_ONE_MINUTE_REFRESH = "forceOneMinuteRefresh";
|
2010-02-01 23:53:46 +00:00
|
|
|
private static final String DEVICE_UID = "deviceUID";
|
2010-01-28 18:07:51 +00:00
|
|
|
private static final String ONE_TIME_INITIALIZATION_PROGRESS = "oneTimeInitializationProgress";
|
2010-09-09 22:34:19 +00:00
|
|
|
private static final String AUTO_ADVANCE_DIRECTION = "autoAdvance";
|
2010-11-02 21:57:22 +00:00
|
|
|
private static final String TEXT_ZOOM = "textZoom";
|
2010-12-08 20:28:02 +00:00
|
|
|
private static final String BACKGROUND_ATTACHMENTS = "backgroundAttachments";
|
2010-09-09 22:34:19 +00:00
|
|
|
|
|
|
|
public static final int AUTO_ADVANCE_NEWER = 0;
|
|
|
|
public static final int AUTO_ADVANCE_OLDER = 1;
|
|
|
|
public static final int AUTO_ADVANCE_MESSAGE_LIST = 2;
|
|
|
|
// "move to older" was the behavior on older versions.
|
|
|
|
public static final int AUTO_ADVANCE_DEFAULT = AUTO_ADVANCE_OLDER;
|
2009-08-14 00:43:25 +00:00
|
|
|
|
2011-01-12 02:39:23 +00:00
|
|
|
// The following constants are used as offsets into TEXT_ZOOM_ARRAY (below)
|
2010-11-02 21:57:22 +00:00
|
|
|
public static final int TEXT_ZOOM_TINY = 0;
|
|
|
|
public static final int TEXT_ZOOM_SMALL = 1;
|
|
|
|
public static final int TEXT_ZOOM_NORMAL = 2;
|
|
|
|
public static final int TEXT_ZOOM_LARGE = 3;
|
|
|
|
public static final int TEXT_ZOOM_HUGE = 4;
|
2011-01-12 02:39:23 +00:00
|
|
|
// "normal" will be the default
|
|
|
|
public static final int TEXT_ZOOM_DEFAULT = TEXT_ZOOM_NORMAL;
|
2010-11-02 21:57:22 +00:00
|
|
|
|
2010-05-10 21:20:16 +00:00
|
|
|
private static Preferences sPreferences;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2010-05-10 21:20:16 +00:00
|
|
|
final SharedPreferences mSharedPreferences;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
private Preferences(Context context) {
|
2009-08-14 00:43:25 +00:00
|
|
|
mSharedPreferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO need to think about what happens if this gets GCed along with the
|
|
|
|
* Activity that initialized it. Do we lose ability to read Preferences in
|
|
|
|
* further Activities? Maybe this should be stored in the Application
|
|
|
|
* context.
|
|
|
|
*/
|
|
|
|
public static synchronized Preferences getPreferences(Context context) {
|
2010-05-10 21:20:16 +00:00
|
|
|
if (sPreferences == null) {
|
|
|
|
sPreferences = new Preferences(context);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
2010-05-10 21:20:16 +00:00
|
|
|
return sPreferences;
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of the accounts on the system. If no accounts are
|
|
|
|
* registered the method returns an empty array.
|
|
|
|
*/
|
|
|
|
public Account[] getAccounts() {
|
2009-08-14 00:43:25 +00:00
|
|
|
String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
|
2009-03-04 03:32:22 +00:00
|
|
|
if (accountUuids == null || accountUuids.length() == 0) {
|
|
|
|
return new Account[] {};
|
|
|
|
}
|
|
|
|
String[] uuids = accountUuids.split(",");
|
|
|
|
Account[] accounts = new Account[uuids.length];
|
|
|
|
for (int i = 0, length = uuids.length; i < length; i++) {
|
|
|
|
accounts[i] = new Account(this, uuids[i]);
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an account object by Uri, or return null if no account exists
|
|
|
|
* TODO: Merge hardcoded strings with the same strings in Account.java
|
|
|
|
*/
|
|
|
|
public Account getAccountByContentUri(Uri uri) {
|
|
|
|
if (!"content".equals(uri.getScheme()) || !"accounts".equals(uri.getAuthority())) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String uuid = uri.getPath().substring(1);
|
|
|
|
if (uuid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2009-08-14 00:43:25 +00:00
|
|
|
String accountUuids = mSharedPreferences.getString(ACCOUNT_UUIDS, null);
|
2009-03-04 03:32:22 +00:00
|
|
|
if (accountUuids == null || accountUuids.length() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String[] uuids = accountUuids.split(",");
|
|
|
|
for (int i = 0, length = uuids.length; i < length; i++) {
|
|
|
|
if (uuid.equals(uuids[i])) {
|
|
|
|
return new Account(this, uuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Account marked as default. If no account is marked as default
|
|
|
|
* the first account in the list is marked as default and then returned. If
|
|
|
|
* there are no accounts on the system the method returns null.
|
|
|
|
*/
|
|
|
|
public Account getDefaultAccount() {
|
2009-08-14 00:43:25 +00:00
|
|
|
String defaultAccountUuid = mSharedPreferences.getString(DEFAULT_ACCOUNT_UUID, null);
|
2009-03-04 03:32:22 +00:00
|
|
|
Account defaultAccount = null;
|
|
|
|
Account[] accounts = getAccounts();
|
|
|
|
if (defaultAccountUuid != null) {
|
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.getUuid().equals(defaultAccountUuid)) {
|
|
|
|
defaultAccount = account;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultAccount == null) {
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
defaultAccount = accounts[0];
|
|
|
|
setDefaultAccount(defaultAccount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDefaultAccount(Account account) {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putString(DEFAULT_ACCOUNT_UUID, account.getUuid()).apply();
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setEnableDebugLogging(boolean value) {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putBoolean(ENABLE_DEBUG_LOGGING, value).apply();
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
2009-08-14 00:43:25 +00:00
|
|
|
public boolean getEnableDebugLogging() {
|
|
|
|
return mSharedPreferences.getBoolean(ENABLE_DEBUG_LOGGING, false);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 04:58:54 +00:00
|
|
|
public void setEnableExchangeLogging(boolean value) {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_LOGGING, value).apply();
|
2009-08-09 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getEnableExchangeLogging() {
|
2009-08-14 00:43:25 +00:00
|
|
|
return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_LOGGING, false);
|
2009-08-09 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setEnableExchangeFileLogging(boolean value) {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putBoolean(ENABLE_EXCHANGE_FILE_LOGGING, value).apply();
|
2009-08-09 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getEnableExchangeFileLogging() {
|
2009-08-14 00:43:25 +00:00
|
|
|
return mSharedPreferences.getBoolean(ENABLE_EXCHANGE_FILE_LOGGING, false);
|
2009-08-09 04:58:54 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 23:15:15 +00:00
|
|
|
public void setInhibitGraphicsAcceleration(boolean value) {
|
|
|
|
mSharedPreferences.edit().putBoolean(INHIBIT_GRAPHICS_ACCELERATION, value).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getInhibitGraphicsAcceleration() {
|
|
|
|
return mSharedPreferences.getBoolean(INHIBIT_GRAPHICS_ACCELERATION, false);
|
|
|
|
}
|
|
|
|
|
2011-01-27 22:31:26 +00:00
|
|
|
public void setForceOneMinuteRefresh(boolean value) {
|
|
|
|
mSharedPreferences.edit().putBoolean(FORCE_ONE_MINUTE_REFRESH, value).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getForceOneMinuteRefresh() {
|
|
|
|
return mSharedPreferences.getBoolean(FORCE_ONE_MINUTE_REFRESH, false);
|
|
|
|
}
|
|
|
|
|
2010-02-01 23:53:46 +00:00
|
|
|
/**
|
|
|
|
* Generate a new "device UID". This is local to Email app only, to prevent possibility
|
|
|
|
* of correlation with any other user activities in any other apps.
|
|
|
|
* @return a persistent, unique ID
|
|
|
|
*/
|
|
|
|
public synchronized String getDeviceUID() {
|
|
|
|
String result = mSharedPreferences.getString(DEVICE_UID, null);
|
|
|
|
if (result == null) {
|
|
|
|
result = UUID.randomUUID().toString();
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putString(DEVICE_UID, result).apply();
|
2010-02-01 23:53:46 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-01-28 18:07:51 +00:00
|
|
|
public int getOneTimeInitializationProgress() {
|
|
|
|
return mSharedPreferences.getInt(ONE_TIME_INITIALIZATION_PROGRESS, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOneTimeInitializationProgress(int progress) {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putInt(ONE_TIME_INITIALIZATION_PROGRESS, progress).apply();
|
2010-01-28 18:07:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-09 22:34:19 +00:00
|
|
|
public int getAutoAdvanceDirection() {
|
|
|
|
return mSharedPreferences.getInt(AUTO_ADVANCE_DIRECTION, AUTO_ADVANCE_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAutoAdvanceDirection(int direction) {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().putInt(AUTO_ADVANCE_DIRECTION, direction).apply();
|
2010-09-09 22:34:19 +00:00
|
|
|
}
|
|
|
|
|
2010-11-02 21:57:22 +00:00
|
|
|
public int getTextZoom() {
|
|
|
|
return mSharedPreferences.getInt(TEXT_ZOOM, TEXT_ZOOM_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTextZoom(int zoom) {
|
|
|
|
mSharedPreferences.edit().putInt(TEXT_ZOOM, zoom).apply();
|
|
|
|
}
|
|
|
|
|
2010-12-08 20:28:02 +00:00
|
|
|
public boolean getBackgroundAttachments() {
|
|
|
|
return mSharedPreferences.getBoolean(BACKGROUND_ATTACHMENTS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBackgroundAttachments(boolean allowed) {
|
|
|
|
mSharedPreferences.edit().putBoolean(BACKGROUND_ATTACHMENTS, allowed).apply();
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
public void save() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
2010-11-01 15:51:26 +00:00
|
|
|
mSharedPreferences.edit().clear().apply();
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void dump() {
|
2009-07-16 21:09:15 +00:00
|
|
|
if (Email.LOGD) {
|
2009-03-04 03:32:22 +00:00
|
|
|
for (String key : mSharedPreferences.getAll().keySet()) {
|
2011-02-11 23:05:17 +00:00
|
|
|
Log.v(Logging.LOG_TAG, key + " = " + mSharedPreferences.getAll().get(key));
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|