1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Rework code for predefinied account colors

This commit is contained in:
cketti 2013-02-26 18:29:12 +01:00
parent f7bef247f5
commit 556503318c
4 changed files with 19 additions and 76 deletions

View File

@ -11,7 +11,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -91,18 +90,16 @@ public class Account implements BaseAccount {
public static final String IDENTITY_EMAIL_KEY = "email"; public static final String IDENTITY_EMAIL_KEY = "email";
public static final String IDENTITY_DESCRIPTION_KEY = "description"; public static final String IDENTITY_DESCRIPTION_KEY = "description";
private static final String EMPTY = "empty";
/* /*
http://developer.android.com/design/style/color.html * http://developer.android.com/design/style/color.html
Note: Order does matter, it's the order in which they will be picked. * Note: Order does matter, it's the order in which they will be picked.
*/ */
public static final Integer[] PREDEFINED_COLORS = new Integer[] { public static final Integer[] PREDEFINED_COLORS = new Integer[] {
Color.parseColor("#0099CC"), // blue Color.parseColor("#0099CC"), // blue
Color.parseColor("#669900"), // green Color.parseColor("#669900"), // green
Color.parseColor("#FF8800"), // orange Color.parseColor("#FF8800"), // orange
Color.parseColor("#CC0000"), // red Color.parseColor("#CC0000"), // red
Color.parseColor("#9933CC") // purple Color.parseColor("#9933CC") // purple
}; };
public enum SortType { public enum SortType {
@ -345,59 +342,22 @@ public class Account implements BaseAccount {
* Pick a nice Android guidelines color if we haven't used them all yet. * Pick a nice Android guidelines color if we haven't used them all yet.
*/ */
private int pickColor(Context context) { private int pickColor(Context context) {
SharedPreferences prefs = Preferences.getPreferences(context).getPreferences(); Account[] accounts = Preferences.getPreferences(context).getAccounts();
String availableColors = prefs.getString("availableColors","");
if (!availableColors.equals(EMPTY)) { List<Integer> availableColors = new ArrayList<Integer>(PREDEFINED_COLORS.length);
Collections.addAll(availableColors, PREDEFINED_COLORS);
// first run for (Account account : accounts) {
if (availableColors.isEmpty()) { Integer color = account.getChipColor();
availableColors = Utility.combine(Account.PREDEFINED_COLORS, ','); if (availableColors.contains(color)) {
} availableColors.remove(color);
if (availableColors.isEmpty()) {
String[] colors = availableColors.split(","); break;
}
// 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<Integer> newAvailableColors = new ArrayList<Integer>();
HashSet<Integer> oldAvailableColors = new HashSet<Integer>();
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) { return (availableColors.isEmpty()) ? ColorPicker.getRandomColor() : availableColors.get(0);
if (color == currentColor || oldAvailableColors.contains(color)) {
newAvailableColors.add(color);
}
}
return Utility.combine(newAvailableColors.toArray(), ',');
} }
protected Account(Preferences preferences, String uuid) { protected Account(Preferences preferences, String uuid) {
@ -566,11 +526,6 @@ public class Account implements BaseAccount {
editor.putString("accountUuids", accountUuids); 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 + ".storeUri");
editor.remove(mUuid + ".localStoreUri"); editor.remove(mUuid + ".localStoreUri");
editor.remove(mUuid + ".transportUri"); editor.remove(mUuid + ".transportUri");
@ -884,15 +839,7 @@ public class Account implements BaseAccount {
} }
public synchronized void setChipColor(Context context, int color) { public synchronized void setChipColor(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; mChipColor = color;
cacheChips(); cacheChips();
} }

View File

@ -884,7 +884,7 @@ public class AccountSettings extends K9PreferenceActivity {
dialog = new ColorPickerDialog(this, dialog = new ColorPickerDialog(this,
new ColorPickerDialog.OnColorChangedListener() { new ColorPickerDialog.OnColorChangedListener() {
public void colorChanged(int color) { public void colorChanged(int color) {
mAccount.setChipColor(AccountSettings.this, color); mAccount.setChipColor(color);
} }
}, },
mAccount.getChipColor()); mAccount.getChipColor());

View File

@ -21,7 +21,6 @@ import com.fsck.k9.K9.SplitViewMode;
import com.fsck.k9.K9.Theme; import com.fsck.k9.K9.Theme;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.Account.SortType; import com.fsck.k9.Account.SortType;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.preferences.Settings.*; import com.fsck.k9.preferences.Settings.*;
public class GlobalSettings { public class GlobalSettings {
@ -222,9 +221,6 @@ public class GlobalSettings {
s.put("showContactPicture", Settings.versions( s.put("showContactPicture", Settings.versions(
new V(25, new BooleanSetting(true)) 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); SETTINGS = Collections.unmodifiableMap(s);

View File

@ -35,7 +35,7 @@ public class Settings {
* *
* @see SettingsExporter * @see SettingsExporter
*/ */
public static final int VERSION = 27; public static final int VERSION = 26;
public static Map<String, Object> validate(int version, Map<String, public static Map<String, Object> validate(int version, Map<String,
TreeMap<Integer, SettingsDescription>> settings, TreeMap<Integer, SettingsDescription>> settings,