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-09-05 13:38:03 -04:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
2010-03-03 23:00:30 -05:00
|
|
|
import java.util.List;
|
2011-09-05 13:38:03 -04:00
|
|
|
import java.util.Map;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.Log;
|
2013-07-23 02:17:11 -04:00
|
|
|
|
2013-08-29 20:28:57 -04:00
|
|
|
import com.fsck.k9.mail.Store;
|
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) {
|
2012-05-16 16:22:39 -04:00
|
|
|
Context appContext = context.getApplicationContext();
|
2011-02-26 19:39:06 -05:00
|
|
|
if (preferences == null) {
|
2012-05-16 16:22:39 -04:00
|
|
|
preferences = new Preferences(appContext);
|
2010-03-03 23:00:30 -05:00
|
|
|
}
|
|
|
|
return preferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-11 10:33:54 -04:00
|
|
|
private Storage mStorage;
|
2011-09-05 13:38:03 -04: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-04-12 21:44:43 -04:00
|
|
|
public synchronized void loadAccounts() {
|
2011-09-05 13:38:03 -04:00
|
|
|
accounts = 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-04-12 21:44:43 -04:00
|
|
|
Account newAccount = new Account(this, uuid);
|
|
|
|
accounts.put(uuid, newAccount);
|
|
|
|
accountsInOrder.add(newAccount);
|
2010-03-03 23:00:30 -05:00
|
|
|
}
|
|
|
|
}
|
2011-09-05 13:38:03 -04:00
|
|
|
if ((newAccount != null) && newAccount.getAccountNumber() != -1) {
|
2011-04-12 21:44:43 -04:00
|
|
|
accounts.put(newAccount.getUuid(), newAccount);
|
2011-09-05 13:38:03 -04:00
|
|
|
accountsInOrder.add(newAccount);
|
|
|
|
newAccount = null;
|
|
|
|
}
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
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-09-05 13:38:03 -04: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-09-05 13:38:03 -04:00
|
|
|
Account[] allAccounts = getAccounts();
|
2010-11-13 16:40:56 -05:00
|
|
|
Collection<Account> retval = new ArrayList<Account>(accounts.size());
|
2011-09-05 13:38:03 -04:00
|
|
|
for (Account account : allAccounts) {
|
2011-10-17 23:09:20 -04:00
|
|
|
if (account.isEnabled() && 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-09-05 13:38:03 -04:00
|
|
|
Account account = accounts.get(uuid);
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-09-05 13:38:03 -04: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-09-05 13:38:03 -04: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-10-16 22:34:26 -04:00
|
|
|
if (accounts != null) {
|
|
|
|
accounts.remove(account.getUuid());
|
|
|
|
}
|
|
|
|
if (accountsInOrder != null) {
|
|
|
|
accountsInOrder.remove(account);
|
|
|
|
}
|
2011-10-11 02:53:51 -04:00
|
|
|
|
2013-08-29 20:42:46 -04:00
|
|
|
Store.removeAccount(account);
|
2013-07-23 02:08:20 -04:00
|
|
|
|
2013-12-03 19:20:20 -05:00
|
|
|
account.deleteCertificates();
|
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-10-06 12:28:14 -04:00
|
|
|
if (!accounts.isEmpty()) {
|
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 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
|
|
|
}
|