Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 01274278 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

synchronized Preferences with a lock object

parent e7d6f85e
Loading
Loading
Loading
Loading
+91 −66
Original line number Diff line number Diff line
@@ -10,8 +10,10 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

import android.content.Context;
import android.support.annotation.GuardedBy;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
@@ -46,16 +48,22 @@ public class Preferences {
    }

    private Storage storage;
    @GuardedBy("accountLock")
    private Map<String, Account> accounts = null;
    @GuardedBy("accountLock")
    private List<Account> accountsInOrder = null;
    @GuardedBy("accountLock")
    private Account newAccount;
    private final ArrayList<AccountsChangeListener> accountsChangeListeners = new ArrayList<>();

    private final List<AccountsChangeListener> accountsChangeListeners = new CopyOnWriteArrayList<>();
    private final Context context;
    private final LocalStoreProvider localStoreProvider;
    private final CoreResourceProvider resourceProvider;
    private final LocalKeyStoreManager localKeyStoreManager;
    private final StoragePersister storagePersister;

    private final Object accountLock = new Object();

    private Preferences(Context context, CoreResourceProvider resourceProvider,
            StoragePersister storagePersister, LocalStoreProvider localStoreProvider,
            LocalKeyStoreManager localKeyStoreManager,
@@ -85,11 +93,14 @@ public class Preferences {

    @RestrictTo(Scope.TESTS)
    public void clearAccounts() {
        synchronized (accountLock) {
            accounts = new HashMap<>();
            accountsInOrder = new LinkedList<>();
        }
    }

    public synchronized void loadAccounts() {
    public void loadAccounts() {
        synchronized (accountLock) {
            accounts = new HashMap<>();
            accountsInOrder = new LinkedList<>();
            String accountUuids = getStorage().getString("accountUuids", null);
@@ -110,6 +121,7 @@ public class Preferences {
                newAccount = null;
            }
        }
    }

    /**
     * Returns an array of the accounts on the system. If no accounts are
@@ -117,13 +129,15 @@ public class Preferences {
     *
     * @return all accounts
     */
    public synchronized List<Account> getAccounts() {
    public List<Account> getAccounts() {
        synchronized (accountLock) {
            if (accounts == null) {
                loadAccounts();
            }

            return Collections.unmodifiableList(new ArrayList<>(accountsInOrder));
        }
    }

    /**
     * Returns an array of the accounts on the system. If no accounts are
@@ -131,27 +145,30 @@ public class Preferences {
     *
     * @return all accounts with {@link Account#isAvailable(Context)}
     */
    public synchronized Collection<Account> getAvailableAccounts() {
    public Collection<Account> getAvailableAccounts() {
        List<Account> allAccounts = getAccounts();
        Collection<Account> retval = new ArrayList<>(accounts.size());
        Collection<Account> result = new ArrayList<>(allAccounts.size());
        for (Account account : allAccounts) {
            if (account.isEnabled() && account.isAvailable(context)) {
                retval.add(account);
                result.add(account);
            }
        }

        return retval;
        return result;
    }

    public synchronized Account getAccount(String uuid) {
    public Account getAccount(String uuid) {
        synchronized (accountLock) {
            if (accounts == null) {
                loadAccounts();
            }

            return accounts.get(uuid);
        }
    }

    public synchronized Account newAccount() {
    public Account newAccount() {
        synchronized (accountLock) {
            String accountUuid = UUID.randomUUID().toString();
            newAccount = new Account(accountUuid);
            accountPreferenceSerializer.loadDefaults(newAccount);
@@ -160,8 +177,10 @@ public class Preferences {

            return newAccount;
        }
    }

    public synchronized void deleteAccount(Account account) {
    public void deleteAccount(Account account) {
        synchronized (accountLock) {
            if (accounts != null) {
                accounts.remove(account.getUuid());
            }
@@ -184,6 +203,7 @@ public class Preferences {
            if (newAccount == account) {
                newAccount = null;
            }
        }

        notifyListeners();
    }
@@ -194,8 +214,11 @@ public class Preferences {
     * there are no accounts on the system the method returns null.
     */
    public Account getDefaultAccount() {
        Account defaultAccount;
        synchronized (accountLock) {
            String defaultAccountUuid = getStorage().getString("defaultAccountUuid", null);
        Account defaultAccount = getAccount(defaultAccountUuid);
            defaultAccount = getAccount(defaultAccountUuid);
        }

        if (defaultAccount == null) {
            Collection<Account> accounts = getAvailableAccounts();
@@ -288,10 +311,12 @@ public class Preferences {
    }

    public void move(Account account, boolean mUp) {
        synchronized (accountLock) {
            StorageEditor storageEditor = createStorageEditor();
            accountPreferenceSerializer.move(storageEditor, account, storage, mUp);
            storageEditor.commit();
            loadAccounts();
        }

        notifyListeners();
    }