2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2010-03-03 23:00:30 -05:00
|
|
|
import java.util.ArrayList;
|
2010-11-13 16:40:56 -05:00
|
|
|
import java.util.Collection;
|
2011-02-26 12:31:56 -05:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
2010-03-03 23:00:30 -05:00
|
|
|
import java.util.List;
|
2011-02-26 12:31:56 -05:00
|
|
|
import java.util.Map;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.Config;
|
|
|
|
import android.util.Log;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.preferences.Editor;
|
|
|
|
import com.fsck.k9.preferences.Storage;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public class Preferences {
|
2010-08-02 07:55:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Immutable empty {@link Account} array
|
|
|
|
*/
|
|
|
|
private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[0];
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
private static Preferences preferences;
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public static synchronized Preferences getPreferences(Context context) {
|
|
|
|
if (preferences == null) {
|
2010-03-03 23:00:30 -05:00
|
|
|
preferences = new Preferences(context);
|
|
|
|
}
|
|
|
|
return preferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-11 10:33:54 -04:00
|
|
|
private Storage mStorage;
|
2011-02-26 12:31:56 -05:00
|
|
|
private Map<String, Account> accounts = null;
|
|
|
|
private List<Account> accountsInOrder = null;
|
2010-04-03 19:44:26 -04:00
|
|
|
private Account newAccount;
|
2010-11-13 16:40:56 -05:00
|
|
|
private Context mContext;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
private Preferences(Context context) {
|
2009-11-24 19:40:29 -05:00
|
|
|
mStorage = Storage.getStorage(context);
|
2010-11-13 16:40:56 -05:00
|
|
|
mContext = context;
|
2011-02-26 19:39:06 -05:00
|
|
|
if (mStorage.size() == 0) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.i(K9.LOG_TAG, "Preferences storage is zero-size, importing from Android-style preferences");
|
2009-11-24 19:40:29 -05:00
|
|
|
Editor editor = mStorage.edit();
|
|
|
|
editor.copy(context.getSharedPreferences("AndroidMail.Main", Context.MODE_PRIVATE));
|
|
|
|
editor.commit();
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
private synchronized void loadAccounts() {
|
2011-02-26 12:31:56 -05:00
|
|
|
accounts = new HashMap<String, Account>();
|
|
|
|
refreshAccounts();
|
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
|
|
|
|
public synchronized void refreshAccounts() {
|
2011-02-26 12:31:56 -05:00
|
|
|
Map<String, Account> newAccountMap = new HashMap<String, Account>();
|
|
|
|
accountsInOrder = new LinkedList<Account>();
|
2010-03-03 23:00:30 -05:00
|
|
|
String accountUuids = getPreferences().getString("accountUuids", null);
|
2011-02-26 19:39:06 -05:00
|
|
|
if ((accountUuids != null) && (accountUuids.length() != 0)) {
|
2010-03-03 23:00:30 -05:00
|
|
|
String[] uuids = accountUuids.split(",");
|
2011-02-26 19:39:06 -05:00
|
|
|
for (String uuid : uuids) {
|
2011-02-26 12:31:56 -05:00
|
|
|
Account account = accounts.get(uuid);
|
2011-02-26 19:39:06 -05:00
|
|
|
if (account != null) {
|
2011-02-26 12:31:56 -05:00
|
|
|
newAccountMap.put(uuid, account);
|
|
|
|
accountsInOrder.add(account);
|
2011-02-26 19:39:06 -05:00
|
|
|
} else {
|
2011-02-26 12:31:56 -05:00
|
|
|
Account newAccount = new Account(this, uuid);
|
|
|
|
newAccountMap.put(uuid, newAccount);
|
|
|
|
accountsInOrder.add(newAccount);
|
|
|
|
}
|
2010-03-03 23:00:30 -05:00
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
if ((newAccount != null) && newAccount.getAccountNumber() != -1) {
|
2011-02-26 12:31:56 -05:00
|
|
|
newAccountMap.put(newAccount.getUuid(), newAccount);
|
|
|
|
accountsInOrder.add(newAccount);
|
|
|
|
newAccount = null;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
accounts = newAccountMap;
|
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
|
|
|
* Returns an array of the accounts on the system. If no accounts are
|
|
|
|
* registered the method returns an empty array.
|
2010-11-13 16:40:56 -05:00
|
|
|
* @return all accounts
|
2008-11-01 17:32:06 -04:00
|
|
|
*/
|
2011-02-26 19:39:06 -05:00
|
|
|
public synchronized Account[] getAccounts() {
|
|
|
|
if (accounts == null) {
|
2010-03-03 23:00:30 -05:00
|
|
|
loadAccounts();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2010-03-03 23:00:30 -05:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
return accountsInOrder.toArray(EMPTY_ACCOUNT_ARRAY);
|
2010-03-03 23:00:30 -05:00
|
|
|
}
|
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
/**
|
|
|
|
* Returns an array of the accounts on the system. If no accounts are
|
|
|
|
* registered the method returns an empty array.
|
|
|
|
* @return all accounts with {@link Account#isAvailable(Context)}
|
|
|
|
*/
|
2011-02-26 19:39:06 -05:00
|
|
|
public synchronized Collection<Account> getAvailableAccounts() {
|
2011-02-26 12:31:56 -05:00
|
|
|
Account[] allAccounts = getAccounts();
|
2010-11-13 16:40:56 -05:00
|
|
|
Collection<Account> retval = new ArrayList<Account>(accounts.size());
|
2011-02-26 19:39:06 -05:00
|
|
|
for (Account account : allAccounts) {
|
|
|
|
if (account.isAvailable(mContext)) {
|
2010-11-13 16:40:56 -05:00
|
|
|
retval.add(account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public synchronized Account getAccount(String uuid) {
|
|
|
|
if (accounts == null) {
|
2010-03-03 23:00:30 -05:00
|
|
|
loadAccounts();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2011-02-26 12:31:56 -05:00
|
|
|
Account account = accounts.get(uuid);
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
return account;
|
2010-03-03 23:00:30 -05:00
|
|
|
}
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public synchronized Account newAccount() {
|
2010-04-03 19:44:26 -04:00
|
|
|
newAccount = new Account(K9.app);
|
2011-02-26 12:31:56 -05:00
|
|
|
accounts.put(newAccount.getUuid(), newAccount);
|
|
|
|
accountsInOrder.add(newAccount);
|
2010-03-03 23:00:30 -05:00
|
|
|
|
2010-04-03 19:44:26 -04:00
|
|
|
return newAccount;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public synchronized void deleteAccount(Account account) {
|
2011-02-26 12:31:56 -05:00
|
|
|
accounts.remove(account.getUuid());
|
|
|
|
accountsInOrder.remove(account);
|
2010-03-03 23:00:30 -05:00
|
|
|
account.delete(this);
|
2010-04-03 19:44:26 -04:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
if (newAccount == account) {
|
2010-04-03 19:44:26 -04:00
|
|
|
newAccount = null;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-02-26 19:39:06 -05:00
|
|
|
public Account getDefaultAccount() {
|
2009-04-11 10:33:54 -04:00
|
|
|
String defaultAccountUuid = getPreferences().getString("defaultAccountUuid", null);
|
2010-03-03 23:00:30 -05:00
|
|
|
Account defaultAccount = getAccount(defaultAccountUuid);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
if (defaultAccount == null) {
|
2010-11-13 16:40:56 -05:00
|
|
|
Collection<Account> accounts = getAvailableAccounts();
|
2011-02-26 19:39:06 -05:00
|
|
|
if (accounts.size() > 0) {
|
2010-11-13 16:40:56 -05:00
|
|
|
defaultAccount = accounts.iterator().next();
|
2008-11-01 17:32:06 -04:00
|
|
|
setDefaultAccount(defaultAccount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultAccount;
|
|
|
|
}
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public void setDefaultAccount(Account account) {
|
2009-04-11 10:33:54 -04:00
|
|
|
getPreferences().edit().putString("defaultAccountUuid", account.getUuid()).commit();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public void dump() {
|
|
|
|
if (Config.LOGV) {
|
|
|
|
for (String key : getPreferences().getAll().keySet()) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.v(K9.LOG_TAG, key + " = " + getPreferences().getAll().get(key));
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-11 10:33:54 -04:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public SharedPreferences getPreferences() {
|
2009-11-24 19:40:29 -05:00
|
|
|
return mStorage;
|
2009-04-11 10:33:54 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|