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

Commit 9cd9eb90 authored by cketti's avatar cketti
Browse files

Move chip color selection code from Account to AccountCreator

This will allow us to avoid using Robolectric with a lot of tests that
mock the Account class. Until now we had to use Robolectric because of
the static array initializer calling Color.parseColor().
parent 4678e467
Loading
Loading
Loading
Loading
+0 −36
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;

import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
@@ -110,18 +109,6 @@ public class Account implements BaseAccount, StoreConfig {
    public static final String IDENTITY_EMAIL_KEY = "email";
    public static final String IDENTITY_DESCRIPTION_KEY = "description";

    /*
     * https://developer.android.com/design/style/color.html
     * Note: Order does matter, it's the order in which they will be picked.
     */
    private static final Integer[] PREDEFINED_COLORS = new Integer[] {
            Color.parseColor("#0099CC"),    // blue
            Color.parseColor("#669900"),    // green
            Color.parseColor("#FF8800"),    // orange
            Color.parseColor("#CC0000"),    // red
            Color.parseColor("#9933CC")     // purple
    };

    public enum SortType {
        SORT_DATE(R.string.sort_earliest_first, R.string.sort_latest_first, false),
        SORT_ARRIVAL(R.string.sort_earliest_first, R.string.sort_latest_first, false),
@@ -296,7 +283,6 @@ public class Account implements BaseAccount, StoreConfig {
        autoExpandFolder = INBOX;
        inboxFolder = INBOX;
        maxPushFolders = 10;
        chipColor = pickColor(context);
        goToUnreadMessageSearch = false;
        subscribedFoldersOnly = false;
        maximumPolledMessageAge = -1;
@@ -339,28 +325,6 @@ public class Account implements BaseAccount, StoreConfig {
        cacheChips();
    }

    /*
     * Pick a nice Android guidelines color if we haven't used them all yet.
     */
    private int pickColor(Context context) {
        List<Account> accounts = Preferences.getPreferences(context).getAccounts();

        List<Integer> availableColors = new ArrayList<>(PREDEFINED_COLORS.length);
        Collections.addAll(availableColors, PREDEFINED_COLORS);

        for (Account account : accounts) {
            Integer color = account.getChipColor();
            if (availableColors.contains(color)) {
                availableColors.remove(color);
                if (availableColors.isEmpty()) {
                    break;
                }
            }
        }

        return (availableColors.isEmpty()) ? ColorPicker.getRandomColor() : availableColors.get(0);
    }

    protected Account(Preferences preferences, String uuid) {
        this.accountUuid = uuid;
        loadAccount(preferences);
+44 −0
Original line number Diff line number Diff line
package com.fsck.k9.account;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Color;

import com.fsck.k9.Account;
import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.Preferences;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings.Type;
import com.larswerkman.colorpicker.ColorPicker;


/**
@@ -12,6 +22,18 @@ import com.fsck.k9.mail.ServerSettings.Type;
 * TODO Move much of the code from com.fsck.k9.activity.setup.* into here
 */
public class AccountCreator {
    /*
     * https://developer.android.com/design/style/color.html
     * Note: Order does matter, it's the order in which they will be picked.
     */
    private static final Integer[] PREDEFINED_COLORS = new Integer[] {
            Color.parseColor("#0099CC"),    // blue
            Color.parseColor("#669900"),    // green
            Color.parseColor("#FF8800"),    // orange
            Color.parseColor("#CC0000"),    // red
            Color.parseColor("#9933CC")     // purple
    };


    public static DeletePolicy getDefaultDeletePolicy(Type type) {
        switch (type) {
@@ -45,4 +67,26 @@ public class AccountCreator {

        throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType);
    }

    /*
     * Pick a nice Android guidelines color if we haven't used them all yet.
     */
    public static int pickColor(Context context) {
        List<Account> accounts = Preferences.getPreferences(context).getAccounts();

        List<Integer> availableColors = new ArrayList<>(PREDEFINED_COLORS.length);
        Collections.addAll(availableColors, PREDEFINED_COLORS);

        for (Account account : accounts) {
            Integer color = account.getChipColor();
            if (availableColors.contains(color)) {
                availableColors.remove(color);
                if (availableColors.isEmpty()) {
                    break;
                }
            }
        }

        return (availableColors.isEmpty()) ? ColorPicker.getRandomColor() : availableColors.get(0);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -314,6 +314,7 @@ public class AccountSetupBasics extends K9Activity
            }
            if (mAccount == null) {
                mAccount = Preferences.getPreferences(this).newAccount();
                mAccount.setChipColor(AccountCreator.pickColor(this));
            }
            mAccount.setName(getOwnerName());
            mAccount.setEmail(email);
@@ -400,6 +401,7 @@ public class AccountSetupBasics extends K9Activity

        if (mAccount == null) {
            mAccount = Preferences.getPreferences(this).newAccount();
            mAccount.setChipColor(AccountCreator.pickColor(this));
        }
        mAccount.setName(getOwnerName());
        mAccount.setEmail(email);
+3 −0
Original line number Diff line number Diff line
@@ -5,10 +5,13 @@ import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings.Type;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.assertEquals;


@RunWith(RobolectricTestRunner.class)
public class AccountCreatorTest {

    @Test