From 6f685f4a2a4350dd8975b43ea4c586b2df490bd4 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 1 Mar 2016 09:23:46 +0100 Subject: [PATCH] UiPreferences migrated --- .../android/calculator/AppModule.java | 31 +++++++++--- .../calculator/CalculatorApplication.java | 11 +++-- .../android/calculator/Preferences.java | 49 ++++++++++--------- .../android/calculator/StartupHelper.java | 6 +-- .../android/calculator/UiPreferences.java | 33 ++++++++++++- 5 files changed, 93 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/AppModule.java b/app/src/main/java/org/solovyev/android/calculator/AppModule.java index b7776f19..952e981c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppModule.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppModule.java @@ -9,20 +9,21 @@ import android.os.Looper; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.util.Log; + import com.squareup.otto.Bus; import com.squareup.otto.GeneratedHandlerFinder; -import dagger.Module; -import dagger.Provides; -import jscl.JsclMathEngine; + import org.solovyev.android.UiThreadExecutor; -import org.solovyev.android.checkout.*; +import org.solovyev.android.checkout.Billing; +import org.solovyev.android.checkout.Checkout; +import org.solovyev.android.checkout.Inventory; +import org.solovyev.android.checkout.ProductTypes; +import org.solovyev.android.checkout.Products; +import org.solovyev.android.checkout.RobotmediaDatabase; +import org.solovyev.android.checkout.RobotmediaInventory; import org.solovyev.android.plotter.Plot; import org.solovyev.android.plotter.Plotter; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.inject.Named; -import javax.inject.Singleton; import java.io.File; import java.util.Collections; import java.util.concurrent.Executor; @@ -30,6 +31,15 @@ import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Named; +import javax.inject.Singleton; + +import dagger.Module; +import dagger.Provides; +import jscl.JsclMathEngine; + @Module public class AppModule { @@ -93,6 +103,11 @@ public class AppModule { @Singleton @Named(PREFS_UI) SharedPreferences provideUiPreferences() { + return provideUiPreferences(application); + } + + @NonNull + public static SharedPreferences provideUiPreferences(@NonNull Application application) { return application.getSharedPreferences("ui", Context.MODE_PRIVATE); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 4992b2a7..316576da 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -27,8 +27,9 @@ import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; import android.util.TimingLogger; + import com.squareup.otto.Bus; -import jscl.MathEngine; + import org.acra.ACRA; import org.acra.ACRAConfiguration; import org.acra.sender.HttpSender; @@ -39,11 +40,14 @@ import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Languages; import org.solovyev.common.msg.MessageType; +import java.util.Locale; +import java.util.concurrent.Executor; + import javax.annotation.Nonnull; import javax.inject.Inject; import javax.inject.Named; -import java.util.Locale; -import java.util.concurrent.Executor; + +import jscl.MathEngine; public class CalculatorApplication extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -171,6 +175,7 @@ public class CalculatorApplication extends android.app.Application implements Sh // then we should set default preferences Preferences.init(this, preferences); + UiPreferences.init(preferences, AppModule.provideUiPreferences(this)); // and change application's theme/language is needed final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(preferences); 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 1c97399f..bdd9fff0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Preferences.java +++ b/app/src/main/java/org/solovyev/android/calculator/Preferences.java @@ -35,8 +35,7 @@ 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; @@ -47,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; @@ -63,29 +70,21 @@ import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE; public final class Preferences { - public static final Preference version = IntegerPreference.of("version", 2); + private static final Preference version = IntegerPreference.of("version", 2); private Preferences() { throw new AssertionError(); } static void init(@Nonnull Application application, @Nonnull SharedPreferences preferences) { - int oldVersion; - if (version.isSet(preferences)) { - oldVersion = version.getPreference(preferences); - } else if (Deleted.appVersion.isSet(preferences)) { - oldVersion = 1; - } else { - oldVersion = 0; - } - if (oldVersion == 0) { + final int currentVersion = getVersion(preferences); + if (currentVersion == 0) { final SharedPreferences.Editor editor = preferences.edit(); setInitialDefaultValues(application, preferences, editor); editor.apply(); - } else if (oldVersion == 1) { + } else if (currentVersion == 1) { final SharedPreferences.Editor editor = preferences.edit(); if (!Gui.vibrateOnKeypress.isSet(preferences)) { - //noinspection deprecation Gui.vibrateOnKeypress.putPreference(editor, Deleted.hapticFeedback.getPreference(preferences) > 0); } migratePreference(preferences, editor, Gui.highlightText, Deleted.colorDisplay); @@ -96,9 +95,6 @@ public final class Preferences { migratePreference(preferences, editor, Gui.showReleaseNotes, Deleted.showReleaseNotes); migratePreference(preferences, editor, Gui.showEqualsButton, Deleted.showEqualsButton); migratePreference(preferences, editor, Gui.rotateScreen, Deleted.autoOrientation); - migratePreference(preferences, editor, UiPreferences.rateUsShown, Deleted.feedbackWindowShown); - migratePreference(preferences, editor, UiPreferences.opened, Deleted.appOpenedCounter); - migratePreference(preferences, editor, UiPreferences.version, Deleted.appVersion); final Gui.Layout layout = Deleted.layout.getPreference(preferences); if (layout == Gui.Layout.main_cellphone) { Gui.layout.putDefault(editor); @@ -112,6 +108,15 @@ public final class Preferences { } } + private static int getVersion(@Nonnull SharedPreferences preferences) { + if (version.isSet(preferences)) { + return version.getPreference(preferences); + } else if (Deleted.appVersion.isSet(preferences)) { + return 1; + } + return 0; + } + 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)); @@ -419,9 +424,10 @@ public final class Preferences { public static final Preference plotImag = BooleanPreference.of("graph_plot_imag", false); } - private static class Deleted { + static class Deleted { static final Preference appVersion = IntegerPreference.of("application.version", DEF_VALUE); static final Preference feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false); + static final Preference appOpenedCounter = IntegerPreference.of("app_opened_counter", 0); 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); @@ -431,6 +437,5 @@ public final class Preferences { static final Preference usePrevAsBack = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev", false); static final Preference showEqualsButton = BooleanPreference.of("showEqualsButton", true); static final Preference autoOrientation = BooleanPreference.of("autoOrientation", true); - static final Preference appOpenedCounter = IntegerPreference.of("app_opened_counter", 0); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/StartupHelper.java b/app/src/main/java/org/solovyev/android/calculator/StartupHelper.java index 6a7df705..23b8a6e5 100644 --- a/app/src/main/java/org/solovyev/android/calculator/StartupHelper.java +++ b/app/src/main/java/org/solovyev/android/calculator/StartupHelper.java @@ -43,7 +43,7 @@ public class StartupHelper { if (!App.isMonkeyRunner(activity)) { handleOnMainActivityOpened(activity, editor, opened == null ? 0 : opened); } - UiPreferences.version.putPreference(editor, Android.getAppVersionCode(activity)); + UiPreferences.appVersion.putPreference(editor, Android.getAppVersionCode(activity)); editor.apply(); } @@ -56,13 +56,13 @@ public class StartupHelper { return; } - if (!UiPreferences.version.isSet(preferences)) { + if (!UiPreferences.appVersion.isSet(uiPreferences)) { // new start startWizard(wizards, activity); return; } - final Integer savedVersion = UiPreferences.version.getPreference(uiPreferences); + final Integer savedVersion = UiPreferences.appVersion.getPreference(uiPreferences); if (savedVersion < currentVersion) { if (Preferences.Gui.showReleaseNotes.getPreference(preferences) && hasReleaseNotes(activity, savedVersion + 1)) { final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java b/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java index a9344595..00a24233 100644 --- a/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java +++ b/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java @@ -1,15 +1,46 @@ package org.solovyev.android.calculator; +import android.content.SharedPreferences; import android.support.annotation.NonNull; + import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.Preference; +import javax.annotation.Nonnull; + final class UiPreferences { @NonNull public static final Preference opened = IntegerPreference.of("opened", 0); @NonNull - public static final Preference version = IntegerPreference.of("version", IntegerPreference.DEF_VALUE); + public static final Preference version = IntegerPreference.of("version", 1); + @NonNull + public static final Preference appVersion = IntegerPreference.of("appVersion", IntegerPreference.DEF_VALUE); @NonNull public static final Preference rateUsShown = BooleanPreference.of("rateUsShown", false); + + public static void init(@NonNull SharedPreferences preferences, @NonNull SharedPreferences uiPreferences) { + final int currentVersion = getVersion(uiPreferences); + if (currentVersion == 0) { + final SharedPreferences.Editor editor = uiPreferences.edit(); + migratePreference(uiPreferences, preferences, editor, UiPreferences.rateUsShown, Preferences.Deleted.feedbackWindowShown); + migratePreference(uiPreferences, preferences, editor, UiPreferences.opened, Preferences.Deleted.appOpenedCounter); + migratePreference(uiPreferences, preferences, editor, UiPreferences.appVersion, Preferences.Deleted.appVersion); + version.putDefault(editor); + editor.apply(); + } + } + + private static void migratePreference(@NonNull SharedPreferences uiPreferences, @Nonnull SharedPreferences preferences, @NonNull SharedPreferences.Editor uiEditor, @NonNull Preference uiPreference, @NonNull Preference preference) { + if (!uiPreference.isSet(uiPreferences)) { + uiPreference.putPreference(uiEditor, preference.getPreferenceNoError(preferences)); + } + } + + private static int getVersion(@NonNull SharedPreferences uiPreferences) { + if (version.isSet(uiPreferences)) { + return version.getPreference(uiPreferences); + } + return 0; + } }