From fd16aad6052ec4d97cfefad64e60f4ef7b2adb81 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 29 Feb 2016 20:50:47 +0100 Subject: [PATCH] Preferences refactor --- .../android/calculator/AppModule.java | 34 ++++--- .../calculator/CalculatorActivity.java | 85 +---------------- .../calculator/CalculatorApplication.java | 2 +- .../solovyev/android/calculator/Engine.java | 2 +- .../android/calculator/Preferences.java | 32 +++++-- .../android/calculator/StartupHelper.java | 91 +++++++++++++++++++ .../android/calculator/UiPreferences.java | 15 +++ 7 files changed, 151 insertions(+), 110 deletions(-) create mode 100644 app/src/main/java/org/solovyev/android/calculator/StartupHelper.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/UiPreferences.java 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 90ed2d01..b7776f19 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppModule.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppModule.java @@ -9,25 +9,20 @@ 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 org.solovyev.android.UiThreadExecutor; -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 dagger.Module; import dagger.Provides; import jscl.JsclMathEngine; +import org.solovyev.android.UiThreadExecutor; +import org.solovyev.android.checkout.*; +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; @@ -35,11 +30,6 @@ 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; - @Module public class AppModule { @@ -52,6 +42,7 @@ public class AppModule { public static final String DIR_FILES = "dir-files"; public static final String PREFS_FLOATING = "prefs-floating"; public static final String PREFS_TABS = "prefs-tabs"; + public static final String PREFS_UI = "prefs-ui"; @NonNull private final Application application; @@ -98,6 +89,13 @@ public class AppModule { return application.getSharedPreferences("tabs", Context.MODE_PRIVATE); } + @Provides + @Singleton + @Named(PREFS_UI) + SharedPreferences provideUiPreferences() { + return application.getSharedPreferences("ui", Context.MODE_PRIVATE); + } + @Provides @Singleton @Named(THREAD_INIT) 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 bce4ee96..9effe829 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -22,32 +22,22 @@ package org.solovyev.android.calculator; -import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; import android.support.v7.widget.Toolbar; -import android.text.method.LinkMovementMethod; import android.view.*; import android.widget.FrameLayout; -import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; import org.solovyev.android.Activities; -import org.solovyev.android.Android; import org.solovyev.android.calculator.converter.ConverterFragment; import org.solovyev.android.calculator.history.History; import org.solovyev.android.calculator.keyboard.PartialKeyboardUi; -import org.solovyev.android.calculator.wizard.CalculatorWizards; -import org.solovyev.android.prefs.Preference; -import org.solovyev.android.wizard.Wizard; -import org.solovyev.android.wizard.Wizards; -import org.solovyev.common.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -57,8 +47,6 @@ 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.*; public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener, Toolbar.OnMenuItemClickListener { @@ -74,6 +62,8 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference History history; @Inject ActivityLauncher launcher; + @Inject + StartupHelper startupHelper; @Nullable @Bind(R.id.partial_keyboard) View partialKeyboard; @@ -90,75 +80,6 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference super(R.layout.activity_main); } - private static void firstTimeInit(@Nonnull SharedPreferences preferences, @Nonnull Context context) { - final SharedPreferences.Editor editor = preferences.edit(); - final Integer appOpenedCounter = Preferences.appOpenedCounter.getPreference(preferences); - Preferences.appOpenedCounter.putPreference(editor, appOpenedCounter == null ? 1 : appOpenedCounter + 1); - - final Integer savedVersion = Preferences.appVersion.getPreference(preferences); - - final int appVersion = Android.getAppVersionCode(context); - - Preferences.appVersion.putPreference(editor, appVersion); - editor.apply(); - - if (!App.isMonkeyRunner(context)) { - - boolean dialogShown = false; - final Wizards wizards = App.getWizards(); - final Wizard wizard = wizards.getWizard(CalculatorWizards.FIRST_TIME_WIZARD); - if (wizard.isStarted() && !wizard.isFinished()) { - continueWizard(wizards, wizard.getName(), context); - dialogShown = true; - } else { - if (Objects.areEqual(savedVersion, Preferences.appVersion.getDefaultValue())) { - // new start - startWizard(wizards, context); - dialogShown = true; - } else { - if (savedVersion < appVersion) { - final boolean showReleaseNotes = Preferences.Gui.showReleaseNotes.getPreference(preferences); - if (showReleaseNotes && hasReleaseNotes(context, savedVersion + 1)) { - final Bundle bundle = new Bundle(); - bundle.putInt(CalculatorWizards.RELEASE_NOTES_VERSION, savedVersion); - context.startActivity(createLaunchIntent(wizards, CalculatorWizards.RELEASE_NOTES, context, bundle)); - dialogShown = true; - } - } - } - } - - //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); - if (!dialogShown) { - if (appOpenedCounter != null && appOpenedCounter > 30) { - dialogShown = showSpecialWindow(preferences, Preferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context); - } - } - } - } - - private static boolean showSpecialWindow(@Nonnull SharedPreferences preferences, @Nonnull Preference specialWindowShownPref, int layoutId, int textViewId, @Nonnull Context context) { - boolean result = false; - - final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences); - if (specialWindowShown != null && !specialWindowShown) { - final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); - final View view = layoutInflater.inflate(layoutId, null); - - final TextView feedbackTextView = (TextView) view.findViewById(textViewId); - feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); - - final AlertDialog.Builder builder = new AlertDialog.Builder(context, App.getTheme().alertDialogTheme).setView(view); - builder.setPositiveButton(android.R.string.ok, null); - builder.create().show(); - - result = true; - specialWindowShownPref.putPreference(preferences, true); - } - - return result; - } - @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -183,7 +104,7 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference useBackAsPrev = Preferences.Gui.useBackAsPrevious.getPreference(preferences); if (savedInstanceState == null) { - firstTimeInit(preferences, this); + startupHelper.onMainActivityOpened(this); } toggleOrientationChange(); 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 edef91f9..4992b2a7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -170,7 +170,7 @@ public class CalculatorApplication extends android.app.Application implements Sh } // then we should set default preferences - Preferences.setDefaultValues(this, preferences); + Preferences.init(this, preferences); // 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/Engine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java index c1bf6352..958c743e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -161,7 +161,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene } else { oldVersion = 0; } - final int newVersion = Preferences.version.getPreference(preferences); + final int newVersion = Preferences.version.getDefaultValue(); if (oldVersion == newVersion) { return; } 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 92c9569a..1c97399f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Preferences.java +++ b/app/src/main/java/org/solovyev/android/calculator/Preferences.java @@ -63,19 +63,26 @@ import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE; public final class Preferences { - public static final Preference appVersion = IntegerPreference.of("application.version", DEF_VALUE); - public static final Preference appOpenedCounter = IntegerPreference.of("app_opened_counter", 0); + public static final Preference version = IntegerPreference.of("version", 2); + private Preferences() { throw new AssertionError(); } - static void setDefaultValues(@Nonnull Application application, @Nonnull SharedPreferences preferences) { - final int version = Preferences.appVersion.getPreference(preferences); - if (version == DEF_VALUE) { + 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 SharedPreferences.Editor editor = preferences.edit(); setInitialDefaultValues(application, preferences, editor); editor.apply(); - } else if (version <= 143) { + } else if (oldVersion == 1) { final SharedPreferences.Editor editor = preferences.edit(); if (!Gui.vibrateOnKeypress.isSet(preferences)) { //noinspection deprecation @@ -89,6 +96,9 @@ 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); @@ -97,6 +107,7 @@ public final class Preferences { } else if (layout == Gui.Layout.simple_mobile) { Gui.layout.putPreference(editor, Gui.Layout.simple); } + version.putDefault(editor); editor.apply(); } } @@ -136,7 +147,6 @@ public final class Preferences { Gui.theme.tryPutDefault(preferences, editor); Gui.layout.tryPutDefault(preferences, editor); - Gui.feedbackWindowShown.tryPutDefault(preferences, editor); Gui.showReleaseNotes.tryPutDefault(preferences, editor); Gui.useBackAsPrevious.tryPutDefault(preferences, editor); Gui.showEqualsButton.tryPutDefault(preferences, editor); @@ -154,6 +164,7 @@ public final class Preferences { Onscreen.theme.tryPutDefault(preferences, editor); Widget.theme.tryPutDefault(preferences, editor); + version.putDefault(editor); final ContentResolver cr = application.getContentResolver(); if (cr != null) { @@ -275,12 +286,14 @@ public final class Preferences { public static final Preference preferredAngleUnits = StringPreference.ofEnum("preferred_angle_units", Engine.Preferences.angleUnit.getDefaultValue(), AngleUnit.class); } + public static class App { + } + public static class Gui { 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("gui.showReleaseNotes", true); public static final Preference useBackAsPrevious = BooleanPreference.of("gui.useBackAsPrevious", false); public static final Preference showEqualsButton = BooleanPreference.of("gui.showEqualsButton", true); @@ -407,6 +420,8 @@ public final class Preferences { } private 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 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); @@ -416,5 +431,6 @@ 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 new file mode 100644 index 00000000..b00e17de --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/StartupHelper.java @@ -0,0 +1,91 @@ +package org.solovyev.android.calculator; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.text.method.LinkMovementMethod; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; +import org.solovyev.android.Android; +import org.solovyev.android.calculator.wizard.CalculatorWizards; +import org.solovyev.android.wizard.Wizard; +import org.solovyev.android.wizard.Wizards; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import static org.solovyev.android.calculator.release.ReleaseNotes.hasReleaseNotes; +import static org.solovyev.android.wizard.WizardUi.*; + +@Singleton +public class StartupHelper { + + @Named(AppModule.PREFS_UI) + @Inject + SharedPreferences uiPreferences; + @Inject + SharedPreferences preferences; + + @Inject + public StartupHelper() { + } + + public void onMainActivityOpened(@NonNull AppCompatActivity activity) { + final SharedPreferences.Editor editor = uiPreferences.edit(); + final Integer opened = UiPreferences.opened.getPreference(uiPreferences); + UiPreferences.opened.putPreference(editor, opened == null ? 1 : opened + 1); + + if (!App.isMonkeyRunner(activity)) { + handleOnMainActivityOpened(activity, editor, opened == null ? 0 : opened); + } + + editor.apply(); + } + + private void handleOnMainActivityOpened(@NonNull AppCompatActivity activity, @NonNull SharedPreferences.Editor editor, int opened) { + final int currentVersion = Android.getAppVersionCode(activity); + final Wizards wizards = App.getWizards(); + final Wizard wizard = wizards.getWizard(CalculatorWizards.FIRST_TIME_WIZARD); + if (wizard.isStarted() && !wizard.isFinished()) { + UiPreferences.version.putPreference(editor, currentVersion); + continueWizard(wizards, wizard.getName(), activity); + return; + } + + if (!UiPreferences.version.isSet(preferences)) { + // new start + startWizard(wizards, activity); + UiPreferences.version.putPreference(editor, currentVersion); + return; + } + + final Integer savedVersion = UiPreferences.version.getPreference(uiPreferences); + if (savedVersion < currentVersion) { + UiPreferences.version.putPreference(editor, currentVersion); + if (Preferences.Gui.showReleaseNotes.getPreference(preferences) && hasReleaseNotes(activity, savedVersion + 1)) { + final Bundle bundle = new Bundle(); + bundle.putInt(CalculatorWizards.RELEASE_NOTES_VERSION, savedVersion); + activity.startActivity(createLaunchIntent(wizards, CalculatorWizards.RELEASE_NOTES, activity, bundle)); + return; + } + } + + if (opened > 30 && !UiPreferences.rateUsShown.getPreference(uiPreferences)) { + final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + final View view = layoutInflater.inflate(R.layout.feedback, null); + + final TextView feedbackTextView = (TextView) view.findViewById(R.id.feedbackText); + feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); + + final AlertDialog.Builder builder = new AlertDialog.Builder(activity, App.getTheme().alertDialogTheme).setView(view); + builder.setPositiveButton(android.R.string.ok, null); + builder.create().show(); + UiPreferences.rateUsShown.putPreference(editor, true); + } + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java b/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java new file mode 100644 index 00000000..a9344595 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/UiPreferences.java @@ -0,0 +1,15 @@ +package org.solovyev.android.calculator; + +import android.support.annotation.NonNull; +import org.solovyev.android.prefs.BooleanPreference; +import org.solovyev.android.prefs.IntegerPreference; +import org.solovyev.android.prefs.Preference; + +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); + @NonNull + public static final Preference rateUsShown = BooleanPreference.of("rateUsShown", false); +}