UiPreferences migrated

This commit is contained in:
serso 2016-03-01 09:23:46 +01:00
parent 52d5e8f3fb
commit 6f685f4a2a
5 changed files with 93 additions and 37 deletions

View File

@ -9,20 +9,21 @@ import android.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.GeneratedHandlerFinder; import com.squareup.otto.GeneratedHandlerFinder;
import dagger.Module;
import dagger.Provides;
import jscl.JsclMathEngine;
import org.solovyev.android.UiThreadExecutor; 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.Plot;
import org.solovyev.android.plotter.Plotter; 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.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -30,6 +31,15 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger; 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 @Module
public class AppModule { public class AppModule {
@ -93,6 +103,11 @@ public class AppModule {
@Singleton @Singleton
@Named(PREFS_UI) @Named(PREFS_UI)
SharedPreferences provideUiPreferences() { SharedPreferences provideUiPreferences() {
return provideUiPreferences(application);
}
@NonNull
public static SharedPreferences provideUiPreferences(@NonNull Application application) {
return application.getSharedPreferences("ui", Context.MODE_PRIVATE); return application.getSharedPreferences("ui", Context.MODE_PRIVATE);
} }

View File

@ -27,8 +27,9 @@ import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.util.TimingLogger; import android.util.TimingLogger;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import jscl.MathEngine;
import org.acra.ACRA; import org.acra.ACRA;
import org.acra.ACRAConfiguration; import org.acra.ACRAConfiguration;
import org.acra.sender.HttpSender; 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.android.calculator.language.Languages;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import java.util.Locale;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; 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 { 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 // then we should set default preferences
Preferences.init(this, preferences); Preferences.init(this, preferences);
UiPreferences.init(preferences, AppModule.provideUiPreferences(this));
// and change application's theme/language is needed // and change application's theme/language is needed
final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(preferences); final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(preferences);

View File

@ -35,8 +35,7 @@ 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;
@ -47,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;
@ -63,29 +70,21 @@ import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
public final class Preferences { public final class Preferences {
public static final Preference<Integer> version = IntegerPreference.of("version", 2); private static final Preference<Integer> version = IntegerPreference.of("version", 2);
private Preferences() { private Preferences() {
throw new AssertionError(); throw new AssertionError();
} }
static void init(@Nonnull Application application, @Nonnull SharedPreferences preferences) { static void init(@Nonnull Application application, @Nonnull SharedPreferences preferences) {
int oldVersion; final int currentVersion = getVersion(preferences);
if (version.isSet(preferences)) { if (currentVersion == 0) {
oldVersion = version.getPreference(preferences);
} else if (Deleted.appVersion.isSet(preferences)) {
oldVersion = 1;
} else {
oldVersion = 0;
}
if (oldVersion == 0) {
final SharedPreferences.Editor editor = preferences.edit(); final SharedPreferences.Editor editor = preferences.edit();
setInitialDefaultValues(application, preferences, editor); setInitialDefaultValues(application, preferences, editor);
editor.apply(); editor.apply();
} else if (oldVersion == 1) { } else if (currentVersion == 1) {
final SharedPreferences.Editor editor = preferences.edit(); final SharedPreferences.Editor editor = preferences.edit();
if (!Gui.vibrateOnKeypress.isSet(preferences)) { if (!Gui.vibrateOnKeypress.isSet(preferences)) {
//noinspection deprecation
Gui.vibrateOnKeypress.putPreference(editor, Deleted.hapticFeedback.getPreference(preferences) > 0); Gui.vibrateOnKeypress.putPreference(editor, Deleted.hapticFeedback.getPreference(preferences) > 0);
} }
migratePreference(preferences, editor, Gui.highlightText, Deleted.colorDisplay); 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.showReleaseNotes, Deleted.showReleaseNotes);
migratePreference(preferences, editor, Gui.showEqualsButton, Deleted.showEqualsButton); migratePreference(preferences, editor, Gui.showEqualsButton, Deleted.showEqualsButton);
migratePreference(preferences, editor, Gui.rotateScreen, Deleted.autoOrientation); 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); final Gui.Layout layout = Deleted.layout.getPreference(preferences);
if (layout == Gui.Layout.main_cellphone) { if (layout == Gui.Layout.main_cellphone) {
Gui.layout.putDefault(editor); 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 <T> void migratePreference(@Nonnull SharedPreferences preferences, @NonNull SharedPreferences.Editor editor, @NonNull Preference<T> to, @NonNull Preference<T> from) { private static <T> void migratePreference(@Nonnull SharedPreferences preferences, @NonNull SharedPreferences.Editor editor, @NonNull Preference<T> to, @NonNull Preference<T> from) {
if (!to.isSet(preferences)) { if (!to.isSet(preferences)) {
to.putPreference(editor, from.getPreferenceNoError(preferences)); to.putPreference(editor, from.getPreferenceNoError(preferences));
@ -419,9 +424,10 @@ public final class Preferences {
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 class Deleted {
static final Preference<Integer> appVersion = IntegerPreference.of("application.version", DEF_VALUE); static final Preference<Integer> appVersion = IntegerPreference.of("application.version", DEF_VALUE);
static final Preference<Boolean> feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false); static final Preference<Boolean> feedbackWindowShown = BooleanPreference.of("feedback_window_shown", false);
static final Preference<Integer> appOpenedCounter = IntegerPreference.of("app_opened_counter", 0);
static final Preference<Long> hapticFeedback = NumberToStringPreference.of("hapticFeedback", 60L, Long.class); 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> colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true);
static final Preference<Boolean> preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true); static final Preference<Boolean> preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true);
@ -431,6 +437,5 @@ public final class Preferences {
static final Preference<Boolean> usePrevAsBack = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev", false); static final Preference<Boolean> usePrevAsBack = BooleanPreference.of("org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev", false);
static final Preference<Boolean> showEqualsButton = BooleanPreference.of("showEqualsButton", true); static final Preference<Boolean> showEqualsButton = BooleanPreference.of("showEqualsButton", true);
static final Preference<Boolean> autoOrientation = BooleanPreference.of("autoOrientation", true); static final Preference<Boolean> autoOrientation = BooleanPreference.of("autoOrientation", true);
static final Preference<Integer> appOpenedCounter = IntegerPreference.of("app_opened_counter", 0);
} }
} }

View File

@ -43,7 +43,7 @@ public class StartupHelper {
if (!App.isMonkeyRunner(activity)) { if (!App.isMonkeyRunner(activity)) {
handleOnMainActivityOpened(activity, editor, opened == null ? 0 : opened); handleOnMainActivityOpened(activity, editor, opened == null ? 0 : opened);
} }
UiPreferences.version.putPreference(editor, Android.getAppVersionCode(activity)); UiPreferences.appVersion.putPreference(editor, Android.getAppVersionCode(activity));
editor.apply(); editor.apply();
} }
@ -56,13 +56,13 @@ public class StartupHelper {
return; return;
} }
if (!UiPreferences.version.isSet(preferences)) { if (!UiPreferences.appVersion.isSet(uiPreferences)) {
// new start // new start
startWizard(wizards, activity); startWizard(wizards, activity);
return; return;
} }
final Integer savedVersion = UiPreferences.version.getPreference(uiPreferences); final Integer savedVersion = UiPreferences.appVersion.getPreference(uiPreferences);
if (savedVersion < currentVersion) { if (savedVersion < currentVersion) {
if (Preferences.Gui.showReleaseNotes.getPreference(preferences) && hasReleaseNotes(activity, savedVersion + 1)) { if (Preferences.Gui.showReleaseNotes.getPreference(preferences) && hasReleaseNotes(activity, savedVersion + 1)) {
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();

View File

@ -1,15 +1,46 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import javax.annotation.Nonnull;
final class UiPreferences { final class UiPreferences {
@NonNull @NonNull
public static final Preference<Integer> opened = IntegerPreference.of("opened", 0); public static final Preference<Integer> opened = IntegerPreference.of("opened", 0);
@NonNull @NonNull
public static final Preference<Integer> version = IntegerPreference.of("version", IntegerPreference.DEF_VALUE); public static final Preference<Integer> version = IntegerPreference.of("version", 1);
@NonNull
public static final Preference<Integer> appVersion = IntegerPreference.of("appVersion", IntegerPreference.DEF_VALUE);
@NonNull @NonNull
public static final Preference<Boolean> rateUsShown = BooleanPreference.of("rateUsShown", false); public static final Preference<Boolean> 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 <T> void migratePreference(@NonNull SharedPreferences uiPreferences, @Nonnull SharedPreferences preferences, @NonNull SharedPreferences.Editor uiEditor, @NonNull Preference<T> uiPreference, @NonNull Preference<T> 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;
}
} }