From 1ac6b6dfb099ab3cb5a068e32ffeb404d38d7809 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 29 Feb 2016 17:45:47 +0100 Subject: [PATCH] Migrate preferences --- .../calculator/CalculatorActivity.java | 26 ++++----- .../android/calculator/Preferences.java | 54 +++++++++++++------ .../floating/FloatingCalculatorService.java | 4 +- .../calculator/view/EditorTextProcessor.java | 7 ++- app/src/main/res/values/preferences.xml | 12 ----- .../main/res/xml/preferences_appearance.xml | 9 ++-- 6 files changed, 60 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index afc0ba7c..258e401c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -22,15 +22,6 @@ 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.SharedPreferences; import android.content.res.Resources; @@ -62,13 +53,22 @@ import org.solovyev.android.wizard.Wizard; import org.solovyev.android.wizard.Wizards; import org.solovyev.common.Objects; -import butterknife.Bind; -import butterknife.ButterKnife; - import javax.annotation.Nonnull; import javax.annotation.Nullable; 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 { @Inject @@ -236,7 +236,7 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference } final Window window = getWindow(); - if (preventScreenFromFading.getPreference(preferences)) { + if (keepScreenOn.getPreference(preferences)) { window.addFlags(FLAG_KEEP_SCREEN_ON); } else { window.clearFlags(FLAG_KEEP_SCREEN_ON); diff --git a/app/src/main/java/org/solovyev/android/calculator/Preferences.java b/app/src/main/java/org/solovyev/android/calculator/Preferences.java index f9706664..b6b41890 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Preferences.java +++ b/app/src/main/java/org/solovyev/android/calculator/Preferences.java @@ -31,11 +31,11 @@ import android.graphics.Color; import android.provider.Settings; import android.support.annotation.ColorRes; import android.support.annotation.LayoutRes; +import android.support.annotation.NonNull; import android.support.annotation.StyleRes; import android.util.SparseArray; import android.view.ContextThemeWrapper; -import jscl.AngleUnit; -import jscl.NumeralBase; + import org.solovyev.android.Check; import org.solovyev.android.calculator.about.AboutActivity; 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.variables.VariablesActivity; 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.util.EnumMap; import java.util.Locale; 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.DeviceModel.samsung_galaxy_s; import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2; @@ -79,9 +87,13 @@ public final class Preferences { final SharedPreferences.Editor editor = preferences.edit(); if (!Gui.vibrateOnKeypress.isSet(preferences)) { //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) { Gui.layout.putDefault(editor); } else if (layout == Gui.Layout.main_calculator_mobile) { @@ -93,6 +105,12 @@ public final class Preferences { } } + private static void migratePreference(@Nonnull SharedPreferences preferences, @NonNull SharedPreferences.Editor editor, @NonNull Preference to, @NonNull Preference 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) { if (!Engine.Preferences.groupingSeparator.isSet(preferences)) { final Locale locale = Locale.getDefault(); @@ -128,7 +146,7 @@ public final class Preferences { Gui.showEqualsButton.tryPutDefault(preferences, editor); Gui.autoOrientation.tryPutDefault(preferences, editor); Gui.hideNumeralBaseDigits.tryPutDefault(preferences, editor); - Gui.preventScreenFromFading.tryPutDefault(preferences, editor); + Gui.keepScreenOn.tryPutDefault(preferences, editor); Gui.language.tryPutDefault(preferences, editor); Graph.plotImag.tryPutDefault(preferences, editor); @@ -267,10 +285,8 @@ public final class Preferences { public static class Gui { - public static final Preference theme = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.material_theme, Theme.class); - public static final Preference layout = StringPreference.ofEnum( - "org.solovyev.android.calculator.CalculatorActivity_calc_layout", Layout.simple, - Layout.class); + public static final Preference theme = StringPreference.ofEnum("gui.theme", Theme.material_theme, Theme.class); + public static final Preference layout = StringPreference.ofEnum("gui.layout", Layout.simple, Layout.class); public static final Preference language = StringPreference.of("gui.language", Languages.SYSTEM_LANGUAGE_CODE); public static final Preference feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false); public static final Preference showReleaseNotes = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_show_release_notes", true); @@ -278,11 +294,9 @@ public final class Preferences { public static final Preference showEqualsButton = BooleanPreference.of("showEqualsButton", true); public static final Preference autoOrientation = BooleanPreference.of("autoOrientation", true); public static final Preference hideNumeralBaseDigits = BooleanPreference.of("hideNumeralBaseDigits", true); - public static final Preference preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true); - public static final Preference colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true); + public static final Preference keepScreenOn = BooleanPreference.of("gui.keepScreenOn", true); + public static final Preference highlightText = BooleanPreference.of("gui.highlightText", true); public static final Preference vibrateOnKeypress = BooleanPreference.of("gui.vibrateOnKeypress", true); - @Deprecated - public static final Preference hapticFeedback = NumberToStringPreference.of("hapticFeedback", 60L, Long.class); @Nonnull public static Theme getTheme(@Nonnull SharedPreferences preferences) { @@ -400,4 +414,12 @@ public final class Preferences { public static class Graph { public static final Preference plotImag = BooleanPreference.of("graph_plot_imag", false); } + + private static class Deleted { + static final Preference hapticFeedback = NumberToStringPreference.of("hapticFeedback", 60L, Long.class); + static final Preference colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true); + static final Preference preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true); + static final Preference oldTheme = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Gui.Theme.material_theme, Gui.Theme.class); + static final Preference oldLayout = StringPreference.ofEnum("org.solovyev.android.calculator.CalculatorActivity_calc_layout", Gui.Layout.simple, Gui.Layout.class); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorService.java b/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorService.java index 3389decd..0f3d7280 100644 --- a/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorService.java +++ b/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorService.java @@ -22,8 +22,6 @@ package org.solovyev.android.calculator.floating; -import static org.solovyev.android.calculator.App.cast; - import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -50,6 +48,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; +import static org.solovyev.android.calculator.App.cast; + public class FloatingCalculatorService extends Service implements FloatingViewListener, SharedPreferences.OnSharedPreferenceChangeListener { private static final String SHOW_WINDOW_ACTION = "org.solovyev.android.calculator.onscreen.SHOW_WINDOW"; diff --git a/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java index 82ccee12..c2c47911 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/EditorTextProcessor.java @@ -12,7 +12,6 @@ import org.solovyev.android.calculator.text.TextProcessorEditorResult; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import static org.solovyev.android.calculator.Preferences.Gui.colorDisplay; import static org.solovyev.android.calculator.Preferences.Gui.theme; public final class EditorTextProcessor implements TextProcessor, SharedPreferences.OnSharedPreferenceChangeListener { @@ -30,7 +29,7 @@ public final class EditorTextProcessor implements TextProcessor - org.solovyev.android.calculator.CalculatorModel_color_display - true - - org.solovyev.android.calculator.CalculatorModel_haptic_feedback - false - org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev @@ -38,12 +32,6 @@ org.solovyev.android.calculator.CalculatorModel_history - org.solovyev.android.calculator.CalculatorActivity_calc_theme - default_theme - - org.solovyev.android.calculator.CalculatorActivity_calc_layout - main_calculator - false \ No newline at end of file diff --git a/app/src/main/res/xml/preferences_appearance.xml b/app/src/main/res/xml/preferences_appearance.xml index f102bcaa..2834ff59 100644 --- a/app/src/main/res/xml/preferences_appearance.xml +++ b/app/src/main/res/xml/preferences_appearance.xml @@ -31,8 +31,7 @@ m:materialColor="@color/material_text_selector" /> @@ -50,13 +49,13 @@ @@ -73,7 +72,7 @@ a:title="@string/c_show_equals_button_title" /> \ No newline at end of file