From 14ff42daf7a924c54bbbf1d9e30b595f27f4e1cc Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 26 Jul 2017 10:24:58 +0200 Subject: [PATCH] Call methods of GA on the main thread to avoid ConcurrentModificationException --- .../calculator/CalculatorApplication.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 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 93d692db..4c16fb43 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -23,6 +23,7 @@ package org.solovyev.android.calculator; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Handler; import android.preference.PreferenceManager; import android.support.annotation.NonNull; @@ -52,6 +53,27 @@ import jscl.MathEngine; public class CalculatorApplication extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener { + // delayed GA reporting in order to avoid initialization of GA on the main + // application thread and to postpone it as much as possible + private class GaInitializer extends AsyncTask { + @NonNull + private final SharedPreferences prefs; + + GaInitializer(@NonNull SharedPreferences prefs) { + this.prefs = prefs; + } + + @Override + protected Ga doInBackground(Void... params) { + return ga.get(); + } + + @Override + protected void onPostExecute(@NonNull Ga ga) { + ga.reportInitially(prefs); + } + } + @Inject @Named(AppModule.THREAD_INIT) Executor initThread; @@ -149,11 +171,9 @@ public class CalculatorApplication extends android.app.Application implements Sh @Override public void run() { 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(prefs); } }); + new GaInitializer(prefs).executeOnExecutor(initThread); } private void warmUpEngine() {