diff --git a/src/com/fsck/k9/Account.java b/src/com/fsck/k9/Account.java index d7a49bde7..3a8c18492 100644 --- a/src/com/fsck/k9/Account.java +++ b/src/com/fsck/k9/Account.java @@ -10,12 +10,15 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.HashMap; +import java.util.HashSet; import java.util.concurrent.ConcurrentHashMap; import android.content.ContentResolver; import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; +import android.graphics.Color; import android.net.ConnectivityManager; import android.net.Uri; import android.util.Log; @@ -41,8 +44,6 @@ import com.fsck.k9.search.SearchSpecification.Searchfield; import com.fsck.k9.view.ColorChip; import com.larswerkman.colorpicker.ColorPicker; -import java.util.HashMap; - /** * Account stores all of the settings for a single account defined by the user. It is able to save * and delete itself given a Preferences to work with. Each account is defined by a UUID. @@ -90,6 +91,20 @@ public class Account implements BaseAccount { public static final String IDENTITY_EMAIL_KEY = "email"; public static final String IDENTITY_DESCRIPTION_KEY = "description"; + private static final String EMPTY = "empty"; + + /* + http://developer.android.com/design/style/color.html + Note: Order does matter, it's the order in which they will be picked. + */ + public 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), @@ -280,7 +295,7 @@ public class Account implements BaseAccount { mAutoExpandFolderName = INBOX; mInboxFolderName = INBOX; mMaxPushFolders = 10; - mChipColor = ColorPicker.getRandomColor(); + mChipColor = pickColor(context); goToUnreadMessageSearch = false; mNotificationShowsUnreadCount = true; subscribedFoldersOnly = false; @@ -326,6 +341,65 @@ public class Account implements BaseAccount { cacheChips(); } + /* + * Pick a nice Android guidelines color if we haven't used them all yet. + */ + private int pickColor(Context context) { + SharedPreferences prefs = Preferences.getPreferences(context).getPreferences(); + String availableColors = prefs.getString("availableColors",""); + + if (!availableColors.equals(EMPTY)) { + + // first run + if (availableColors.isEmpty()) { + availableColors = Utility.combine(Account.PREDEFINED_COLORS, ','); + } + + String[] colors = availableColors.split(","); + + // determine remaining colors + String remainingColors = ""; + if (colors.length > 1) { + remainingColors = Utility.combine(Arrays.copyOfRange(colors, 1, colors.length), ','); + } else { + remainingColors = EMPTY; + } + + // save remaining colors + SharedPreferences.Editor editor = prefs.edit(); + editor.putString("availableColors", remainingColors); + editor.commit(); + + return Integer.parseInt(colors[0]); + } else { + return ColorPicker.getRandomColor(); + } + } + + /* + Put the account color back in circulation if it's a predefined one, + we also want to maintain the order of preference. + */ + private String calculateAvailableColors(Preferences preferences, int currentColor) { + ArrayList newAvailableColors = new ArrayList(); + HashSet oldAvailableColors = new HashSet(); + String oldColors = preferences.getPreferences().getString("availableColors", ""); + + if (!oldColors.equals(EMPTY)) { + for (String color : oldColors.split(",")) { + oldAvailableColors.add(Integer.parseInt(color)); + } + } + + for (Integer color : PREDEFINED_COLORS) { + if (color == currentColor || oldAvailableColors.contains(color)) { + newAvailableColors.add(color); + } + } + + return Utility.combine(newAvailableColors.toArray(), ','); + } + protected Account(Preferences preferences, String uuid) { this.mUuid = uuid; loadAccount(preferences); @@ -492,6 +566,11 @@ public class Account implements BaseAccount { editor.putString("accountUuids", accountUuids); } + // Put color back in circulation if necesarry + if (Arrays.asList(PREDEFINED_COLORS).contains(mChipColor)) { + editor.putString("availableColors", calculateAvailableColors(preferences, mChipColor)); + } + editor.remove(mUuid + ".storeUri"); editor.remove(mUuid + ".localStoreUri"); editor.remove(mUuid + ".transportUri"); @@ -805,10 +884,17 @@ public class Account implements BaseAccount { } - public synchronized void setChipColor(int color) { + public synchronized void setChipColor(Context context, int color) { + // release current color if predefined one + if (Arrays.asList(PREDEFINED_COLORS).contains(mChipColor)) { + String availableColors = calculateAvailableColors(Preferences.getPreferences(context), color); + SharedPreferences.Editor editor = Preferences.getPreferences(context).getPreferences().edit(); + editor.putString("availableColors", availableColors); + editor.commit(); + } + mChipColor = color; cacheChips(); - } public synchronized void cacheChips() { diff --git a/src/com/fsck/k9/preferences/GlobalSettings.java b/src/com/fsck/k9/preferences/GlobalSettings.java index 5357149ae..d51942020 100644 --- a/src/com/fsck/k9/preferences/GlobalSettings.java +++ b/src/com/fsck/k9/preferences/GlobalSettings.java @@ -21,6 +21,7 @@ import com.fsck.k9.K9.SplitViewMode; import com.fsck.k9.K9.Theme; import com.fsck.k9.R; import com.fsck.k9.Account.SortType; +import com.fsck.k9.helper.Utility; import com.fsck.k9.preferences.Settings.*; public class GlobalSettings { @@ -221,6 +222,9 @@ public class GlobalSettings { s.put("showContactPicture", Settings.versions( new V(25, new BooleanSetting(true)) )); + s.put("availableColors", Settings.versions( + new V(26, new StringSetting(Utility.combine(Account.PREDEFINED_COLORS, ','))) + )); SETTINGS = Collections.unmodifiableMap(s); diff --git a/src/com/fsck/k9/preferences/Settings.java b/src/com/fsck/k9/preferences/Settings.java index b56f2d87e..e52872f20 100644 --- a/src/com/fsck/k9/preferences/Settings.java +++ b/src/com/fsck/k9/preferences/Settings.java @@ -35,7 +35,7 @@ public class Settings { * * @see SettingsExporter */ - public static final int VERSION = 26; + public static final int VERSION = 27; public static Map validate(int version, Map> settings,