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;
|
2010-03-03 23:00:30 -05:00
|
|
|
import java.util.List;
|
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-06 17:09:48 -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-06 17:09:48 -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;
|
2010-03-03 23:00:30 -05:00
|
|
|
private List<Account> accounts;
|
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-06 17:09:48 -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-06 17:09:48 -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-06 17:09:48 -05:00
|
|
|
private synchronized void loadAccounts() {
|
2010-03-03 23:00:30 -05:00
|
|
|
String accountUuids = getPreferences().getString("accountUuids", null);
|
2011-02-06 17:09:48 -05:00
|
|
|
if ((accountUuids != null) && (accountUuids.length() != 0)) {
|
2010-03-03 23:00:30 -05:00
|
|
|
String[] uuids = accountUuids.split(",");
|
|
|
|
accounts = new ArrayList<Account>(uuids.length);
|
2011-02-06 17:09:48 -05:00
|
|
|
for (String uuid : uuids) {
|
2010-07-18 21:52:18 -04:00
|
|
|
accounts.add(new Account(this, uuid));
|
2010-03-03 23:00:30 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-03-03 23:00:30 -05:00
|
|
|
accounts = new ArrayList<Account>();
|
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-06 17:09:48 -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-06 17:09:48 -05:00
|
|
|
if ((newAccount != null) && newAccount.getAccountNumber() != -1) {
|
2010-04-03 19:44:26 -04:00
|
|
|
accounts.add(newAccount);
|
|
|
|
newAccount = null;
|
|
|
|
}
|
|
|
|
|
2010-08-02 07:55:31 -04:00
|
|
|
return accounts.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-06 17:09:48 -05:00
|
|
|
public synchronized Collection<Account> getAvailableAccounts() {
|
|
|
|
if (accounts == null) {
|
2010-11-13 16:40:56 -05:00
|
|
|
loadAccounts();
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if ((newAccount != null) && newAccount.getAccountNumber() != -1) {
|
2010-11-13 16:40:56 -05:00
|
|
|
accounts.add(newAccount);
|
|
|
|
newAccount = null;
|
|
|
|
}
|
|
|
|
Collection<Account> retval = new ArrayList<Account>(accounts.size());
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.isAvailable(mContext)) {
|
2010-11-13 16:40:56 -05:00
|
|
|
retval.add(account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -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
|
|
|
}
|
2010-03-03 23:00:30 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.getUuid().equals(uuid)) {
|
2010-03-03 23:00:30 -05:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if ((newAccount != null) && newAccount.getUuid().equals(uuid)) {
|
2010-04-03 19:44:26 -04:00
|
|
|
return newAccount;
|
|
|
|
}
|
|
|
|
|
2010-03-03 23:00:30 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public synchronized Account newAccount() {
|
2010-04-03 19:44:26 -04:00
|
|
|
newAccount = new Account(K9.app);
|
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-06 17:09:48 -05:00
|
|
|
public synchronized void deleteAccount(Account account) {
|
2010-03-03 23:00:30 -05:00
|
|
|
accounts.remove(account);
|
|
|
|
account.delete(this);
|
2010-04-03 19:44:26 -04:00
|
|
|
|
2011-02-06 17:09:48 -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-06 17:09:48 -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-06 17:09:48 -05:00
|
|
|
if (defaultAccount == null) {
|
2010-11-13 16:40:56 -05:00
|
|
|
Collection<Account> accounts = getAvailableAccounts();
|
2011-02-06 17:09:48 -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-06 17:09:48 -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-06 17:09:48 -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-06 17:09:48 -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
|
|
|
}
|