clean up Preferences

This commit is contained in:
Vincent Breitmoser 2017-03-27 11:38:55 +02:00
parent f67a5f53a6
commit 910ee75798

View file

@ -31,26 +31,26 @@ public class Preferences {
} }
private Storage mStorage; private Storage storage;
private Map<String, Account> accounts = null; private Map<String, Account> accounts = null;
private List<Account> accountsInOrder = null; private List<Account> accountsInOrder = null;
private Account newAccount; private Account newAccount;
private Context mContext; private Context context;
private Preferences(Context context) { private Preferences(Context context) {
mStorage = Storage.getStorage(context); storage = Storage.getStorage(context);
mContext = context; this.context = context;
if (mStorage.isEmpty()) { if (storage.isEmpty()) {
Timber.i("Preferences storage is zero-size, importing from Android-style preferences"); Timber.i("Preferences storage is zero-size, importing from Android-style preferences");
StorageEditor editor = mStorage.edit(); StorageEditor editor = storage.edit();
editor.copy(context.getSharedPreferences("AndroidMail.Main", Context.MODE_PRIVATE)); editor.copy(context.getSharedPreferences("AndroidMail.Main", Context.MODE_PRIVATE));
editor.commit(); editor.commit();
} }
} }
public synchronized void loadAccounts() { public synchronized void loadAccounts() {
accounts = new HashMap<String, Account>(); accounts = new HashMap<>();
accountsInOrder = new LinkedList<Account>(); accountsInOrder = new LinkedList<>();
String accountUuids = getStorage().getString("accountUuids", null); String accountUuids = getStorage().getString("accountUuids", null);
if ((accountUuids != null) && (accountUuids.length() != 0)) { if ((accountUuids != null) && (accountUuids.length() != 0)) {
String[] uuids = accountUuids.split(","); String[] uuids = accountUuids.split(",");
@ -80,7 +80,7 @@ public class Preferences {
loadAccounts(); loadAccounts();
} }
return Collections.unmodifiableList(new ArrayList<Account>(accountsInOrder)); return Collections.unmodifiableList(new ArrayList<>(accountsInOrder));
} }
/** /**
@ -91,9 +91,9 @@ public class Preferences {
*/ */
public synchronized Collection<Account> getAvailableAccounts() { public synchronized Collection<Account> getAvailableAccounts() {
List<Account> allAccounts = getAccounts(); List<Account> allAccounts = getAccounts();
Collection<Account> retval = new ArrayList<Account>(accounts.size()); Collection<Account> retval = new ArrayList<>(accounts.size());
for (Account account : allAccounts) { for (Account account : allAccounts) {
if (account.isEnabled() && account.isAvailable(mContext)) { if (account.isEnabled() && account.isAvailable(context)) {
retval.add(account); retval.add(account);
} }
} }
@ -105,13 +105,12 @@ public class Preferences {
if (accounts == null) { if (accounts == null) {
loadAccounts(); loadAccounts();
} }
Account account = accounts.get(uuid);
return account; return accounts.get(uuid);
} }
public synchronized Account newAccount() { public synchronized Account newAccount() {
newAccount = new Account(mContext); newAccount = new Account(context);
accounts.put(newAccount.getUuid(), newAccount); accounts.put(newAccount.getUuid(), newAccount);
accountsInOrder.add(newAccount); accountsInOrder.add(newAccount);
@ -165,10 +164,10 @@ public class Preferences {
} }
public Storage getStorage() { public Storage getStorage() {
return mStorage; return storage;
} }
public static <T extends Enum<T>> T getEnumStringPref(Storage storage, String key, T defaultEnum) { static <T extends Enum<T>> T getEnumStringPref(Storage storage, String key, T defaultEnum) {
String stringPref = storage.getString(key, null); String stringPref = storage.getString(key, null);
if (stringPref == null) { if (stringPref == null) {