1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-24 08:38:51 -05:00

Changed internal representation of the selected theme in the database

This commit is contained in:
cketti 2012-03-29 06:33:01 +02:00
parent 1266c3c73e
commit d584492a6d
6 changed files with 56 additions and 22 deletions

View File

@ -38,6 +38,9 @@ import com.fsck.k9.service.ShutdownReceiver;
import com.fsck.k9.service.StorageGoneReceiver;
public class K9 extends Application {
public static final int THEME_LIGHT = 0;
public static final int THEME_DARK = 1;
/**
* Components that are interested in knowing when the K9 instance is
* available and ready (Android invokes Application.onCreate() after other
@ -72,7 +75,7 @@ public class K9 extends Application {
}
private static String language = "";
private static int theme = android.R.style.Theme_Light;
private static int theme = THEME_LIGHT;
private static final FontSizes fontSizes = new FontSizes();
@ -604,7 +607,17 @@ public class K9 extends Application {
}
K9.setK9Language(sprefs.getString("language", ""));
K9.setK9Theme(sprefs.getInt("theme", android.R.style.Theme_Light));
int theme = sprefs.getInt("theme", THEME_LIGHT);
// We used to save the resource ID of the theme. So convert that to the new format if
// necessary.
if (theme == THEME_DARK || theme == android.R.style.Theme) {
theme = THEME_DARK;
} else {
theme = THEME_LIGHT;
}
K9.setK9Theme(theme);
}
private void maybeSetupStrictMode() {
@ -663,6 +676,14 @@ public class K9 extends Application {
language = nlanguage;
}
public static int getK9ThemeResourceId(int theme) {
return (theme == THEME_LIGHT) ? android.R.style.Theme_Light : android.R.style.Theme;
}
public static int getK9ThemeResourceId() {
return getK9ThemeResourceId(theme);
}
public static int getK9Theme() {
return theme;
}

View File

@ -29,7 +29,7 @@ public class K9Activity extends Activity {
public void onCreate(Bundle icicle, boolean useTheme) {
setLanguage(this, K9.getK9Language());
if (useTheme) {
setTheme(K9.getK9Theme());
setTheme(K9.getK9ThemeResourceId());
}
super.onCreate(icicle);
setupFormats();

View File

@ -13,7 +13,7 @@ public class K9ListActivity extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
K9Activity.setLanguage(this, K9.getK9Language());
setTheme(K9.getK9Theme());
setTheme(K9.getK9ThemeResourceId());
super.onCreate(icicle);
setupFormats();
}

View File

@ -148,7 +148,7 @@ public class Prefs extends K9PreferenceActivity {
entryVector.toArray(EMPTY_CHAR_SEQUENCE_ARRAY),
entryValueVector.toArray(EMPTY_CHAR_SEQUENCE_ARRAY));
final String theme = (K9.getK9Theme() == android.R.style.Theme) ? "dark" : "light";
final String theme = (K9.getK9Theme() == K9.THEME_DARK) ? "dark" : "light";
mTheme = setupListPreference(PREFERENCE_THEME, theme);
findPreference(PREFERENCE_FONT_SIZE).setOnPreferenceClickListener(
@ -348,7 +348,7 @@ public class Prefs extends K9PreferenceActivity {
SharedPreferences preferences = Preferences.getPreferences(this).getPreferences();
K9.setK9Language(mLanguage.getValue());
K9.setK9Theme(mTheme.getValue().equals("dark") ? android.R.style.Theme : android.R.style.Theme_Light);
K9.setK9Theme(mTheme.getValue().equals("dark") ? K9.THEME_DARK : K9.THEME_LIGHT);
K9.setAnimations(mAnimations.isChecked());
K9.setGesturesEnabled(mGestures.isChecked());
K9.setCompactLayouts(compactLayouts.isChecked());

View File

@ -181,7 +181,7 @@ public class GlobalSettings {
new V(1, new BooleanSetting(false))
));
s.put("theme", Settings.versions(
new V(1, new ThemeSetting(android.R.style.Theme_Light))
new V(1, new ThemeSetting(K9.THEME_LIGHT))
));
s.put("useGalleryBugWorkaround", Settings.versions(
new V(1, new GalleryBugWorkaroundSetting())
@ -291,34 +291,47 @@ public class GlobalSettings {
/**
* The theme setting.
*/
public static class ThemeSetting extends PseudoEnumSetting<Integer> {
private final Map<Integer, String> mMapping;
public static class ThemeSetting extends SettingsDescription {
private static final String THEME_LIGHT = "light";
private static final String THEME_DARK = "dark";
public ThemeSetting(int defaultValue) {
super(defaultValue);
Map<Integer, String> mapping = new HashMap<Integer, String>();
mapping.put(android.R.style.Theme_Light, "light");
mapping.put(android.R.style.Theme, "dark");
mMapping = Collections.unmodifiableMap(mapping);
}
@Override
protected Map<Integer, String> getMapping() {
return mMapping;
}
@Override
public Object fromString(String value) throws InvalidSettingValueException {
try {
Integer theme = Integer.parseInt(value);
if (mMapping.containsKey(theme)) {
return theme;
if (theme == K9.THEME_LIGHT ||
// We used to store the resource ID of the theme in the preference storage,
// but don't use the database upgrade mechanism to update the values. So
// we have to deal with the old format here.
theme == android.R.style.Theme_Light) {
return K9.THEME_LIGHT;
} else if (theme == K9.THEME_DARK || theme == android.R.style.Theme) {
return K9.THEME_DARK;
}
} catch (NumberFormatException e) { /* do nothing */ }
throw new InvalidSettingValueException();
}
@Override
public Object fromPrettyString(String value) throws InvalidSettingValueException {
if (THEME_LIGHT.equals(value)) {
return K9.THEME_LIGHT;
} else if (THEME_DARK.equals(value)) {
return K9.THEME_DARK;
}
throw new InvalidSettingValueException();
}
@Override
public String toPrettyString(Object value) {
return (((Integer)value).intValue() == K9.THEME_LIGHT) ? THEME_LIGHT : THEME_DARK;
}
}
/**

View File

@ -123,7 +123,7 @@ public class RemoteControlService extends CoreService {
String theme = intent.getStringExtra(K9_THEME);
if (theme != null) {
K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? android.R.style.Theme : android.R.style.Theme_Light);
K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? K9.THEME_DARK : K9.THEME_LIGHT);
}
SharedPreferences sPrefs = preferences.getPreferences();