From 874a0c3c5f4df0ecf9e169f60575a374e0cf210e Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 30 May 2017 16:49:15 -0500 Subject: [PATCH] Added Dagger singleton Attempting to fix casting exceptions in threaded sync adapter and NPE in locale --- .../InjectingAbstractThreadedSyncAdapter.java | 4 +- src/main/java/org/tasks/injection/Dagger.java | 45 +++++++++++++++++++ .../tasks/injection/InjectingApplication.java | 4 +- src/main/java/org/tasks/locale/Locale.java | 15 ++++--- 4 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/tasks/injection/Dagger.java diff --git a/src/googleplay/java/org/tasks/injection/InjectingAbstractThreadedSyncAdapter.java b/src/googleplay/java/org/tasks/injection/InjectingAbstractThreadedSyncAdapter.java index 61e86dc92..15e8529bc 100644 --- a/src/googleplay/java/org/tasks/injection/InjectingAbstractThreadedSyncAdapter.java +++ b/src/googleplay/java/org/tasks/injection/InjectingAbstractThreadedSyncAdapter.java @@ -11,8 +11,8 @@ public abstract class InjectingAbstractThreadedSyncAdapter extends AbstractThrea } private void inject(Context context) { - inject(((InjectingApplication) context.getApplicationContext()) - .getComponent() + inject(Dagger.get(context) + .getApplicationComponent() .plus(new SyncAdapterModule())); } diff --git a/src/main/java/org/tasks/injection/Dagger.java b/src/main/java/org/tasks/injection/Dagger.java new file mode 100644 index 000000000..cf7369ac0 --- /dev/null +++ b/src/main/java/org/tasks/injection/Dagger.java @@ -0,0 +1,45 @@ +package org.tasks.injection; + +import android.content.Context; + +import org.tasks.locale.Locale; + +import timber.log.Timber; + +class Dagger { + + private static final Object lock = new Object(); + + private static Dagger instance; + + public static Dagger get(Context context) { + if (instance == null) { + synchronized (lock) { + if (instance == null) { + instance = new Dagger(context); + } + } + } + return instance; + } + + private ApplicationComponent applicationComponent; + + private Dagger(Context context) { + Context localeContext = context.getApplicationContext(); + try { + localeContext = Locale.getInstance(localeContext) + .createConfigurationContext(localeContext); + } catch (Exception e) { + Timber.e(e.getMessage(), e); + } + + applicationComponent = DaggerApplicationComponent.builder() + .applicationModule(new ApplicationModule(localeContext)) + .build(); + } + + ApplicationComponent getApplicationComponent() { + return applicationComponent; + } +} diff --git a/src/main/java/org/tasks/injection/InjectingApplication.java b/src/main/java/org/tasks/injection/InjectingApplication.java index f2af6fb9c..df5f01cbd 100644 --- a/src/main/java/org/tasks/injection/InjectingApplication.java +++ b/src/main/java/org/tasks/injection/InjectingApplication.java @@ -15,9 +15,7 @@ public abstract class InjectingApplication extends BaseApplication { Context context = Locale.getInstance(this).createConfigurationContext(getApplicationContext()); - applicationComponent = DaggerApplicationComponent.builder() - .applicationModule(new ApplicationModule(context)) - .build(); + applicationComponent = Dagger.get(context).getApplicationComponent(); inject(applicationComponent); } diff --git a/src/main/java/org/tasks/locale/Locale.java b/src/main/java/org/tasks/locale/Locale.java index a1b26e0b7..d8e1918c3 100644 --- a/src/main/java/org/tasks/locale/Locale.java +++ b/src/main/java/org/tasks/locale/Locale.java @@ -28,11 +28,16 @@ public class Locale { public static Locale getInstance(Context context) { if (INSTANCE == null) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String language = prefs.getString(context.getString(R.string.p_language), null); - int directionOverride = Integer.parseInt(prefs.getString(context.getString(R.string.p_layout_direction), "-1")); - INSTANCE = new Locale(DEFAULT.getLocale(), language, directionOverride); - java.util.Locale.setDefault(INSTANCE.getLocale()); + synchronized (DEFAULT) { + if (INSTANCE == null) { + Context applicationContext = context.getApplicationContext(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext); + String language = prefs.getString(applicationContext.getString(R.string.p_language), null); + int directionOverride = Integer.parseInt(prefs.getString(applicationContext.getString(R.string.p_layout_direction), "-1")); + INSTANCE = new Locale(DEFAULT.getLocale(), language, directionOverride); + java.util.Locale.setDefault(INSTANCE.getLocale()); + } + } } return getInstance();