rename AccountManager to AccountPreferenceSerializer, move more things out of Account
This commit is contained in:
parent
912b5d70a4
commit
cd01aec9d8
5 changed files with 60 additions and 75 deletions
|
@ -467,32 +467,6 @@ public class Account implements BaseAccount, StoreConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int findNewAccountNumber(List<Integer> accountNumbers) {
|
|
||||||
int newAccountNumber = -1;
|
|
||||||
Collections.sort(accountNumbers);
|
|
||||||
for (int accountNumber : accountNumbers) {
|
|
||||||
if (accountNumber > newAccountNumber + 1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
newAccountNumber = accountNumber;
|
|
||||||
}
|
|
||||||
newAccountNumber++;
|
|
||||||
return newAccountNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<Integer> getExistingAccountNumbers(Preferences preferences) {
|
|
||||||
List<Account> accounts = preferences.getAccounts();
|
|
||||||
List<Integer> accountNumbers = new ArrayList<>(accounts.size());
|
|
||||||
for (Account a : accounts) {
|
|
||||||
accountNumbers.add(a.getAccountNumber());
|
|
||||||
}
|
|
||||||
return accountNumbers;
|
|
||||||
}
|
|
||||||
public static int generateAccountNumber(Preferences preferences) {
|
|
||||||
List<Integer> accountNumbers = getExistingAccountNumbers(preferences);
|
|
||||||
return findNewAccountNumber(accountNumbers);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void move(Preferences preferences, boolean moveUp) {
|
public void move(Preferences preferences, boolean moveUp) {
|
||||||
String[] uuids = preferences.getStorage().getString("accountUuids", "").split(",");
|
String[] uuids = preferences.getStorage().getString("accountUuids", "").split(",");
|
||||||
StorageEditor editor = preferences.getStorage().edit();
|
StorageEditor editor = preferences.getStorage().edit();
|
||||||
|
@ -526,7 +500,7 @@ public class Account implements BaseAccount, StoreConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void save() {
|
public synchronized void save() {
|
||||||
DI.get(AccountManager.class).save(this);
|
DI.get(Preferences.class).saveAccount(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetVisibleLimits() {
|
private void resetVisibleLimits() {
|
||||||
|
|
|
@ -7,44 +7,14 @@ import com.fsck.k9.preferences.Storage
|
||||||
import com.fsck.k9.preferences.StorageEditor
|
import com.fsck.k9.preferences.StorageEditor
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class AccountManager(
|
class AccountPreferenceSerializer {
|
||||||
private val preferences: Preferences,
|
|
||||||
private val localKeyStoreManager: LocalKeyStoreManager
|
|
||||||
) {
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun save(account: Account) {
|
fun save(storage: Storage, editor: StorageEditor, account: Account) {
|
||||||
val accountUuid = account.uuid
|
val accountUuid = account.uuid
|
||||||
|
|
||||||
val editor = preferences.storage.edit()
|
if (!storage.getString("accountUuids", "").contains(account.uuid)) {
|
||||||
|
var accountUuids = storage.getString("accountUuids", "")
|
||||||
if (!preferences.storage.getString("accountUuids", "").contains(accountUuid)) {
|
accountUuids += (if (accountUuids.isNotEmpty()) "," else "") + account.uuid
|
||||||
/*
|
|
||||||
* When the account is first created we assign it a unique account number. The
|
|
||||||
* account number will be unique to that account for the lifetime of the account.
|
|
||||||
* So, we get all the existing account numbers, sort them ascending, loop through
|
|
||||||
* the list and check if the number is greater than 1 + the previous number. If so
|
|
||||||
* we use the previous number + 1 as the account number. This refills gaps.
|
|
||||||
* accountNumber starts as -1 on a newly created account. It must be -1 for this
|
|
||||||
* algorithm to work.
|
|
||||||
*
|
|
||||||
* I bet there is a much smarter way to do this. Anyone like to suggest it?
|
|
||||||
*/
|
|
||||||
val accounts = preferences.accounts
|
|
||||||
val accountNumbers = IntArray(accounts.size)
|
|
||||||
for (i in accounts.indices) {
|
|
||||||
accountNumbers[i] = accounts[i].accountNumber
|
|
||||||
}
|
|
||||||
Arrays.sort(accountNumbers)
|
|
||||||
for (accountNumber in accountNumbers) {
|
|
||||||
if (accountNumber > account.accountNumber + 1) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
account.accountNumber = accountNumber
|
|
||||||
}
|
|
||||||
account.accountNumber += 1
|
|
||||||
|
|
||||||
var accountUuids = preferences.storage.getString("accountUuids", "")
|
|
||||||
accountUuids += (if (accountUuids.isNotEmpty()) "," else "") + accountUuid
|
|
||||||
editor.putString("accountUuids", accountUuids)
|
editor.putString("accountUuids", accountUuids)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,20 +110,18 @@ class AccountManager(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveIdentities(account, preferences.storage, editor)
|
saveIdentities(account, storage, editor)
|
||||||
|
|
||||||
editor.commit()
|
editor.commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun delete(account: Account) {
|
fun delete(storage: Storage, account: Account) {
|
||||||
localKeyStoreManager.deleteCertificates(account)
|
|
||||||
|
|
||||||
val accountUuid = account.uuid
|
val accountUuid = account.uuid
|
||||||
|
|
||||||
// Get the list of account UUIDs
|
// Get the list of account UUIDs
|
||||||
val uuids = preferences.storage.getString("accountUuids", "").split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
val uuids = storage.getString("accountUuids", "").split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||||
|
|
||||||
// Create a list of all account UUIDs excluding this account
|
// Create a list of all account UUIDs excluding this account
|
||||||
val newUuids = ArrayList<String>(uuids.size)
|
val newUuids = ArrayList<String>(uuids.size)
|
||||||
|
@ -163,7 +131,7 @@ class AccountManager(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val editor = preferences.storage.edit()
|
val editor = storage.edit()
|
||||||
|
|
||||||
// Only change the 'accountUuids' value if this account's UUID was listed before
|
// Only change the 'accountUuids' value if this account's UUID was listed before
|
||||||
if (newUuids.size < uuids.size) {
|
if (newUuids.size < uuids.size) {
|
||||||
|
@ -254,7 +222,7 @@ class AccountManager(
|
||||||
for (type in NetworkType.values()) {
|
for (type in NetworkType.values()) {
|
||||||
editor.remove(accountUuid + ".useCompression." + type.name)
|
editor.remove(accountUuid + ".useCompression." + type.name)
|
||||||
}
|
}
|
||||||
deleteIdentities(account, preferences.storage, editor)
|
deleteIdentities(account, storage, editor)
|
||||||
// TODO: Remove preference settings that may exist for individual folders in the account.
|
// TODO: Remove preference settings that may exist for individual folders in the account.
|
||||||
editor.commit()
|
editor.commit()
|
||||||
}
|
}
|
|
@ -26,8 +26,9 @@ public class Preferences {
|
||||||
public static synchronized Preferences getPreferences(Context context) {
|
public static synchronized Preferences getPreferences(Context context) {
|
||||||
Context appContext = context.getApplicationContext();
|
Context appContext = context.getApplicationContext();
|
||||||
CoreResourceProvider resourceProvider = DI.get(CoreResourceProvider.class);
|
CoreResourceProvider resourceProvider = DI.get(CoreResourceProvider.class);
|
||||||
|
LocalKeyStoreManager localKeyStoreManager = DI.get(LocalKeyStoreManager.class);
|
||||||
if (preferences == null) {
|
if (preferences == null) {
|
||||||
preferences = new Preferences(appContext, resourceProvider);
|
preferences = new Preferences(appContext, resourceProvider, localKeyStoreManager);
|
||||||
}
|
}
|
||||||
return preferences;
|
return preferences;
|
||||||
}
|
}
|
||||||
|
@ -38,11 +39,13 @@ public class Preferences {
|
||||||
private Account newAccount;
|
private Account newAccount;
|
||||||
private Context context;
|
private Context context;
|
||||||
private final CoreResourceProvider resourceProvider;
|
private final CoreResourceProvider resourceProvider;
|
||||||
|
private final LocalKeyStoreManager localKeyStoreManager;
|
||||||
|
|
||||||
private Preferences(Context context, CoreResourceProvider resourceProvider) {
|
private Preferences(Context context, CoreResourceProvider resourceProvider, LocalKeyStoreManager localKeyStoreManager) {
|
||||||
storage = Storage.getStorage(context);
|
storage = Storage.getStorage(context);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.resourceProvider = resourceProvider;
|
this.resourceProvider = resourceProvider;
|
||||||
|
this.localKeyStoreManager = localKeyStoreManager;
|
||||||
if (storage.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 = storage.edit();
|
StorageEditor editor = storage.edit();
|
||||||
|
@ -135,7 +138,8 @@ public class Preferences {
|
||||||
}
|
}
|
||||||
LocalStore.removeAccount(account);
|
LocalStore.removeAccount(account);
|
||||||
|
|
||||||
DI.get(AccountManager.class).delete(account);
|
DI.get(AccountPreferenceSerializer.class).delete(storage, account);
|
||||||
|
localKeyStoreManager.deleteCertificates(account);
|
||||||
|
|
||||||
if (newAccount == account) {
|
if (newAccount == account) {
|
||||||
newAccount = null;
|
newAccount = null;
|
||||||
|
@ -190,4 +194,43 @@ public class Preferences {
|
||||||
private BackendManager getBackendManager() {
|
private BackendManager getBackendManager() {
|
||||||
return DI.get(BackendManager.class);
|
return DI.get(BackendManager.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveAccount(Account account) {
|
||||||
|
StorageEditor editor = storage.edit();
|
||||||
|
|
||||||
|
if (!accounts.containsKey(account.getUuid())) {
|
||||||
|
int accountNumber = generateAccountNumber();
|
||||||
|
account.setAccountNumber(accountNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
DI.get(AccountPreferenceSerializer.class).save(storage, editor, account);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int generateAccountNumber() {
|
||||||
|
List<Integer> accountNumbers = getExistingAccountNumbers();
|
||||||
|
return findNewAccountNumber(accountNumbers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Integer> getExistingAccountNumbers() {
|
||||||
|
List<Account> accounts = getAccounts();
|
||||||
|
List<Integer> accountNumbers = new ArrayList<>(accounts.size());
|
||||||
|
for (Account a : accounts) {
|
||||||
|
accountNumbers.add(a.getAccountNumber());
|
||||||
|
}
|
||||||
|
return accountNumbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int findNewAccountNumber(List<Integer> accountNumbers) {
|
||||||
|
int newAccountNumber = -1;
|
||||||
|
Collections.sort(accountNumbers);
|
||||||
|
for (int accountNumber : accountNumbers) {
|
||||||
|
if (accountNumber > newAccountNumber + 1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
newAccountNumber = accountNumber;
|
||||||
|
}
|
||||||
|
newAccountNumber++;
|
||||||
|
return newAccountNumber;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package com.fsck.k9.notification
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.support.v4.app.NotificationManagerCompat
|
import android.support.v4.app.NotificationManagerCompat
|
||||||
import com.fsck.k9.AccountManager
|
import com.fsck.k9.AccountPreferenceSerializer
|
||||||
import com.fsck.k9.LocalKeyStoreManager
|
import com.fsck.k9.LocalKeyStoreManager
|
||||||
import com.fsck.k9.mail.ssl.LocalKeyStore
|
import com.fsck.k9.mail.ssl.LocalKeyStore
|
||||||
import org.koin.dsl.module.applicationContext
|
import org.koin.dsl.module.applicationContext
|
||||||
|
@ -21,7 +21,7 @@ val coreNotificationModule = applicationContext {
|
||||||
get()
|
get()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
bean { AccountManager(get(), get()) }
|
bean { AccountPreferenceSerializer() }
|
||||||
bean { LocalKeyStore.getInstance() }
|
bean { LocalKeyStore.getInstance() }
|
||||||
bean { LocalKeyStoreManager(get()) }
|
bean { LocalKeyStoreManager(get()) }
|
||||||
bean { CertificateErrorNotifications(get(), get(), get()) }
|
bean { CertificateErrorNotifications(get(), get(), get()) }
|
||||||
|
|
|
@ -417,7 +417,7 @@ public class SettingsImporter {
|
||||||
|
|
||||||
// If it's a new account generate and write a new "accountNumber"
|
// If it's a new account generate and write a new "accountNumber"
|
||||||
if (!mergeImportedAccount) {
|
if (!mergeImportedAccount) {
|
||||||
int newAccountNumber = Account.generateAccountNumber(prefs);
|
int newAccountNumber = prefs.generateAccountNumber();
|
||||||
putString(editor, accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
|
putString(editor, accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue