From 5da6034c76e3ee64988a115640ecb947f5f45607 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Fri, 26 May 2017 18:53:18 +0200 Subject: [PATCH] Start loading UI preferences as soon as possible --- .../calculator/CalculatorApplication.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) 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 f23a8082..fe4d67f8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -105,11 +105,15 @@ public class CalculatorApplication extends android.app.Application implements Sh public void onCreate() { timer.reset(); - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - final Languages languages = new Languages(this, preferences); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + // start loading UI preferences as soon as possible in order for them to be loaded by the + // time they are queried + final SharedPreferences uiPrefs = AppModule.provideUiPreferences(this); + + final Languages languages = new Languages(this, prefs); timer.addSplit("languages"); - onPreCreate(preferences, languages); + onPreCreate(prefs, uiPrefs, languages); timer.addSplit("onPreCreate"); super.onCreate(); @@ -118,7 +122,7 @@ public class CalculatorApplication extends android.app.Application implements Sh initDagger(languages); timer.addSplit("initDagger"); - onPostCreate(preferences, languages); + onPostCreate(prefs, languages); timer.addSplit("onPostCreate"); timer.dumpToLog(); } @@ -132,11 +136,11 @@ public class CalculatorApplication extends android.app.Application implements Sh history.init(initThread); } - private void onPostCreate(@Nonnull final SharedPreferences preferences, @Nonnull Languages languages) { + private void onPostCreate(@Nonnull final SharedPreferences prefs, @Nonnull Languages languages) { App.init(this); languages.init(); - preferences.registerOnSharedPreferenceChangeListener(this); + prefs.registerOnSharedPreferenceChangeListener(this); languages.updateContextLocale(this, true); calculator.init(initThread); @@ -147,7 +151,7 @@ public class CalculatorApplication extends android.app.Application implements Sh warmUpEngine(); // delayed GA reporting in order to avoid initialization of GA on the main // application thread and to postpone it as much as possible - ga.get().reportInitially(preferences); + ga.get().reportInitially(prefs); } }); } @@ -163,7 +167,8 @@ public class CalculatorApplication extends android.app.Application implements Sh } } - private void onPreCreate(@Nonnull SharedPreferences preferences, @Nonnull Languages languages) { + private void onPreCreate(@Nonnull SharedPreferences prefs, @NonNull SharedPreferences uiPrefs, + @Nonnull Languages languages) { // first we need to setup crash handler and memory leak analyzer if (AcraErrorReporter.ENABLED) { ACRA.init(this, new ACRAConfiguration() @@ -175,11 +180,11 @@ 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)); + Preferences.init(this, prefs); + UiPreferences.init(prefs, uiPrefs); // 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(prefs); setTheme(theme.theme); final Language language = languages.getCurrent();