Migrate preferences

This commit is contained in:
serso 2016-02-29 17:45:47 +01:00
parent 8b36232e07
commit 1ac6b6dfb0
6 changed files with 60 additions and 52 deletions

View File

@ -22,15 +22,6 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
import static org.solovyev.android.calculator.Preferences.Gui.preventScreenFromFading;
import static org.solovyev.android.calculator.release.ReleaseNotes.hasReleaseNotes;
import static org.solovyev.android.wizard.WizardUi.continueWizard;
import static org.solovyev.android.wizard.WizardUi.createLaunchIntent;
import static org.solovyev.android.wizard.WizardUi.startWizard;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
@ -62,13 +53,22 @@ import org.solovyev.android.wizard.Wizard;
import org.solovyev.android.wizard.Wizards; import org.solovyev.android.wizard.Wizards;
import org.solovyev.common.Objects; import org.solovyev.common.Objects;
import butterknife.Bind;
import butterknife.ButterKnife;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import butterknife.Bind;
import butterknife.ButterKnife;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
import static org.solovyev.android.calculator.Preferences.Gui.keepScreenOn;
import static org.solovyev.android.calculator.release.ReleaseNotes.hasReleaseNotes;
import static org.solovyev.android.wizard.WizardUi.continueWizard;
import static org.solovyev.android.wizard.WizardUi.createLaunchIntent;
import static org.solovyev.android.wizard.WizardUi.startWizard;
public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener, Toolbar.OnMenuItemClickListener { public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener, Toolbar.OnMenuItemClickListener {
@Inject @Inject
@ -236,7 +236,7 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
} }
final Window window = getWindow(); final Window window = getWindow();
if (preventScreenFromFading.getPreference(preferences)) { if (keepScreenOn.getPreference(preferences)) {
window.addFlags(FLAG_KEEP_SCREEN_ON); window.addFlags(FLAG_KEEP_SCREEN_ON);
} else { } else {
window.clearFlags(FLAG_KEEP_SCREEN_ON); window.clearFlags(FLAG_KEEP_SCREEN_ON);

View File

@ -31,11 +31,11 @@ import android.graphics.Color;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.ColorRes; import android.support.annotation.ColorRes;
import android.support.annotation.LayoutRes; import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes; import android.support.annotation.StyleRes;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.about.AboutActivity; import org.solovyev.android.calculator.about.AboutActivity;
import org.solovyev.android.calculator.functions.FunctionsActivity; import org.solovyev.android.calculator.functions.FunctionsActivity;
@ -46,15 +46,23 @@ import org.solovyev.android.calculator.operators.OperatorsActivity;
import org.solovyev.android.calculator.preferences.PreferencesActivity; import org.solovyev.android.calculator.preferences.PreferencesActivity;
import org.solovyev.android.calculator.variables.VariablesActivity; import org.solovyev.android.calculator.variables.VariablesActivity;
import org.solovyev.android.calculator.wizard.WizardActivity; import org.solovyev.android.calculator.wizard.WizardActivity;
import org.solovyev.android.prefs.*; import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.NumberToStringPreference;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.AngleUnit;
import jscl.NumeralBase;
import static org.solovyev.android.Android.isPhoneModel; import static org.solovyev.android.Android.isPhoneModel;
import static org.solovyev.android.DeviceModel.samsung_galaxy_s; import static org.solovyev.android.DeviceModel.samsung_galaxy_s;
import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2; import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2;
@ -79,9 +87,13 @@ public final class Preferences {
final SharedPreferences.Editor editor = preferences.edit(); final SharedPreferences.Editor editor = preferences.edit();
if (!Gui.vibrateOnKeypress.isSet(preferences)) { if (!Gui.vibrateOnKeypress.isSet(preferences)) {
//noinspection deprecation //noinspection deprecation
Gui.vibrateOnKeypress.putPreference(editor, Gui.hapticFeedback.getPreference(preferences) > 0); Gui.vibrateOnKeypress.putPreference(editor, Deleted.hapticFeedback.getPreference(preferences) > 0);
} }
final Gui.Layout layout = Gui.layout.getPreference(preferences); migratePreference(preferences, editor, Gui.highlightText, Deleted.colorDisplay);
migratePreference(preferences, editor, Gui.keepScreenOn, Deleted.preventScreenFromFading);
migratePreference(preferences, editor, Gui.theme, Deleted.oldTheme);
migratePreference(preferences, editor, Gui.layout, Deleted.oldLayout);
final Gui.Layout layout = Deleted.oldLayout.getPreference(preferences);
if (layout == Gui.Layout.main_cellphone) { if (layout == Gui.Layout.main_cellphone) {
Gui.layout.putDefault(editor); Gui.layout.putDefault(editor);
} else if (layout == Gui.Layout.main_calculator_mobile) { } else if (layout == Gui.Layout.main_calculator_mobile) {
@ -93,6 +105,12 @@ public final class Preferences {
} }
} }
private static <T> void migratePreference(@Nonnull SharedPreferences preferences, @NonNull SharedPreferences.Editor editor, @NonNull Preference<T> to, @NonNull Preference<T> from) {
if (!to.isSet(preferences)) {
to.putPreference(editor, from.getPreferenceNoError(preferences));
}
}
private static void setInitialDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) { private static void setInitialDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull SharedPreferences.Editor editor) {
if (!Engine.Preferences.groupingSeparator.isSet(preferences)) { if (!Engine.Preferences.groupingSeparator.isSet(preferences)) {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
@ -128,7 +146,7 @@ public final class Preferences {
Gui.showEqualsButton.tryPutDefault(preferences, editor); Gui.showEqualsButton.tryPutDefault(preferences, editor);
Gui.autoOrientation.tryPutDefault(preferences, editor); Gui.autoOrientation.tryPutDefault(preferences, editor);
Gui.hideNumeralBaseDigits.tryPutDefault(preferences, editor); Gui.hideNumeralBaseDigits.tryPutDefault(preferences, editor);
Gui.preventScreenFromFading.tryPutDefault(preferences, editor); Gui.keepScreenOn.tryPutDefault(preferences, editor);
Gui.language.tryPutDefault(preferences, editor); Gui.language.tryPutDefault(preferences, editor);
Graph.plotImag.tryPutDefault(preferences, editor); Graph.plotImag.tryPutDefault(preferences, editor);
@ -267,10 +285,8 @@ public final class Preferences {
public static class Gui { public static class Gui {
public static final Preference<Theme> theme = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.material_theme, Theme.class); public static final Preference<Theme> theme = StringPreference.ofEnum("gui.theme", Theme.material_theme, Theme.class);
public static final Preference<Layout> layout = StringPreference.ofEnum( public static final Preference<Layout> layout = StringPreference.ofEnum("gui.layout", Layout.simple, Layout.class);
"org.solovyev.android.calculator.CalculatorActivity_calc_layout", Layout.simple,
Layout.class);
public static final Preference<String> language = StringPreference.of("gui.language", Languages.SYSTEM_LANGUAGE_CODE); public static final Preference<String> language = StringPreference.of("gui.language", Languages.SYSTEM_LANGUAGE_CODE);
public static final Preference<Boolean> feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false); public static final Preference<Boolean> feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false);
public static final Preference<Boolean> showReleaseNotes = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_show_release_notes", true); public static final Preference<Boolean> showReleaseNotes = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_show_release_notes", true);
@ -278,11 +294,9 @@ public final class Preferences {
public static final Preference<Boolean> showEqualsButton = BooleanPreference.of("showEqualsButton", true); public static final Preference<Boolean> showEqualsButton = BooleanPreference.of("showEqualsButton", true);
public static final Preference<Boolean> autoOrientation = BooleanPreference.of("autoOrientation", true); public static final Preference<Boolean> autoOrientation = BooleanPreference.of("autoOrientation", true);
public static final Preference<Boolean> hideNumeralBaseDigits = BooleanPreference.of("hideNumeralBaseDigits", true); public static final Preference<Boolean> hideNumeralBaseDigits = BooleanPreference.of("hideNumeralBaseDigits", true);
public static final Preference<Boolean> preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true); public static final Preference<Boolean> keepScreenOn = BooleanPreference.of("gui.keepScreenOn", true);
public static final Preference<Boolean> colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true); public static final Preference<Boolean> highlightText = BooleanPreference.of("gui.highlightText", true);
public static final Preference<Boolean> vibrateOnKeypress = BooleanPreference.of("gui.vibrateOnKeypress", true); public static final Preference<Boolean> vibrateOnKeypress = BooleanPreference.of("gui.vibrateOnKeypress", true);
@Deprecated
public static final Preference<Long> hapticFeedback = NumberToStringPreference.of("hapticFeedback", 60L, Long.class);
@Nonnull @Nonnull
public static Theme getTheme(@Nonnull SharedPreferences preferences) { public static Theme getTheme(@Nonnull SharedPreferences preferences) {
@ -400,4 +414,12 @@ public final class Preferences {
public static class Graph { public static class Graph {
public static final Preference<Boolean> plotImag = BooleanPreference.of("graph_plot_imag", false); public static final Preference<Boolean> plotImag = BooleanPreference.of("graph_plot_imag", false);
} }
private static class Deleted {
static final Preference<Long> hapticFeedback = NumberToStringPreference.of("hapticFeedback", 60L, Long.class);
static final Preference<Boolean> colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true);
static final Preference<Boolean> preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true);
static final Preference<Gui.Theme> oldTheme = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Gui.Theme.material_theme, Gui.Theme.class);
static final Preference<Gui.Layout> oldLayout = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_layout", Gui.Layout.simple, Gui.Layout.class);
}
} }

View File

@ -22,8 +22,6 @@
package org.solovyev.android.calculator.floating; package org.solovyev.android.calculator.floating;
import static org.solovyev.android.calculator.App.cast;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
@ -50,6 +48,8 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public class FloatingCalculatorService extends Service implements FloatingViewListener, SharedPreferences.OnSharedPreferenceChangeListener { public class FloatingCalculatorService extends Service implements FloatingViewListener, SharedPreferences.OnSharedPreferenceChangeListener {
private static final String SHOW_WINDOW_ACTION = "org.solovyev.android.calculator.onscreen.SHOW_WINDOW"; private static final String SHOW_WINDOW_ACTION = "org.solovyev.android.calculator.onscreen.SHOW_WINDOW";

View File

@ -12,7 +12,6 @@ import org.solovyev.android.calculator.text.TextProcessorEditorResult;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static org.solovyev.android.calculator.Preferences.Gui.colorDisplay;
import static org.solovyev.android.calculator.Preferences.Gui.theme; import static org.solovyev.android.calculator.Preferences.Gui.theme;
public final class EditorTextProcessor implements TextProcessor<TextProcessorEditorResult, String>, SharedPreferences.OnSharedPreferenceChangeListener { public final class EditorTextProcessor implements TextProcessor<TextProcessorEditorResult, String>, SharedPreferences.OnSharedPreferenceChangeListener {
@ -30,7 +29,7 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
this.preferences = preferences; this.preferences = preferences;
this.engine = engine; this.engine = engine;
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, colorDisplay.getKey()); onSharedPreferenceChanged(preferences, Preferences.Gui.highlightText.getKey());
} }
@Nonnull @Nonnull
@ -57,8 +56,8 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (colorDisplay.isSameKey(key)) { if (Preferences.Gui.highlightText.isSameKey(key)) {
setHighlightText(colorDisplay.getPreference(preferences)); setHighlightText(Preferences.Gui.highlightText.getPreference(preferences));
} else if (theme.isSameKey(key)) { } else if (theme.isSameKey(key)) {
final int color = getTextColor(preferences); final int color = getTextColor(preferences);
textHighlighter = new TextHighlighter(color, true, engine); textHighlighter = new TextHighlighter(color, true, engine);

View File

@ -21,12 +21,6 @@
--> -->
<resources> <resources>
<string name="p_calc_color_display_key" translatable="false">org.solovyev.android.calculator.CalculatorModel_color_display</string>
<string name="p_calc_color_display" translatable="false">true</string>
<string name="p_calc_haptic_feedback_key" translatable="false">org.solovyev.android.calculator.CalculatorModel_haptic_feedback</string>
<string name="p_calc_haptic_feedback" translatable="false">false</string>
<string name="p_calc_use_back_button_as_prev_key" translatable="false"> <string name="p_calc_use_back_button_as_prev_key" translatable="false">
org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev
</string> </string>
@ -38,12 +32,6 @@
<string name="p_calc_history" translatable="false">org.solovyev.android.calculator.CalculatorModel_history</string> <string name="p_calc_history" translatable="false">org.solovyev.android.calculator.CalculatorModel_history</string>
<string name="p_calc_theme_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>
<string name="p_calc_theme" translatable="false">default_theme</string>
<string name="p_calc_layout_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_calc_layout</string>
<string name="p_calc_layout" translatable="false">main_calculator</string>
<string name="p_calc_ad_free" translatable="false">false</string> <string name="p_calc_ad_free" translatable="false">false</string>
</resources> </resources>

View File

@ -31,8 +31,7 @@
m:materialColor="@color/material_text_selector" /> m:materialColor="@color/material_text_selector" />
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:defaultValue="true" a:key="gui.highlightText"
a:key="@string/p_calc_color_display_key"
a:summary="@string/c_calc_color_display_summary" a:summary="@string/c_calc_color_display_summary"
a:title="@string/c_calc_color_display_title" /> a:title="@string/c_calc_color_display_title" />
@ -50,13 +49,13 @@
<ListPreference <ListPreference
a:entries="@array/p_theme_names" a:entries="@array/p_theme_names"
a:entryValues="@array/p_theme_values" a:entryValues="@array/p_theme_values"
a:key="@string/p_calc_theme_key" a:key="gui.theme"
a:title="@string/cpp_theme" /> a:title="@string/cpp_theme" />
<ListPreference <ListPreference
a:entries="@array/p_layout_names" a:entries="@array/p_layout_names"
a:entryValues="@array/p_layout_values" a:entryValues="@array/p_layout_values"
a:key="@string/p_calc_layout_key" a:key="gui.layout"
a:summary="@string/c_calc_layout_summary" a:summary="@string/c_calc_layout_summary"
a:title="@string/c_calc_layout" /> a:title="@string/c_calc_layout" />
@ -73,7 +72,7 @@
a:title="@string/c_show_equals_button_title" /> a:title="@string/c_show_equals_button_title" />
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="preventScreenFromFading" a:key="gui.keepScreenOn"
a:title="@string/cpp_prefs_keep_screen_on" /> a:title="@string/cpp_prefs_keep_screen_on" />
</PreferenceScreen> </PreferenceScreen>