From adf764cf13ef4f6b3c7fdca68507255dcd94a378 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Fri, 26 May 2017 19:03:31 +0200 Subject: [PATCH] Access filesDir lazily (initialize on the background thread) --- .../android/calculator/VariablesRegistry.java | 2 +- .../entities/BaseEntitiesRegistry.java | 4 ++- .../functions/FunctionsRegistry.java | 2 +- .../android/calculator/history/History.java | 10 +++--- .../android/calculator/memory/Memory.java | 32 ++++++++++++------- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java index 382a2f53..19063c67 100644 --- a/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java @@ -123,7 +123,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry { @Override @NonNull protected File getEntitiesFile() { - return new File(filesDir, "variables.json"); + return new File(filesDir.get(), "variables.json"); } @Nullable diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesRegistry.java index fdbaeb1d..01af496f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesRegistry.java @@ -57,6 +57,8 @@ import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Named; +import dagger.Lazy; + public abstract class BaseEntitiesRegistry implements EntitiesRegistry { @Nonnull @@ -82,7 +84,7 @@ public abstract class BaseEntitiesRegistry implements Enti public Executor backgroundThread; @Inject @Named(AppModule.DIR_FILES) - public File filesDir; + public Lazy filesDir; @Nonnull private final Map descriptions = new HashMap<>(); diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java index 46ae2d95..5800fc1d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsRegistry.java @@ -199,7 +199,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry { @Override @NonNull protected File getEntitiesFile() { - return new File(filesDir, "functions.json"); + return new File(filesDir.get(), "functions.json"); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/History.java index bb7ef9cf..76d2b2c7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/History.java @@ -22,6 +22,8 @@ package org.solovyev.android.calculator.history; +import static android.text.TextUtils.isEmpty; + import android.app.Application; import android.content.SharedPreferences; import android.os.Handler; @@ -61,7 +63,7 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; -import static android.text.TextUtils.isEmpty; +import dagger.Lazy; @Singleton public class History { @@ -102,7 +104,7 @@ public class History { Executor backgroundThread; @Inject @Named(AppModule.DIR_FILES) - File filesDir; + Lazy filesDir; @Nullable static List convertOldHistory(@NonNull String xml) throws Exception { @@ -190,12 +192,12 @@ public class History { @NonNull File getSavedHistoryFile() { - return new File(filesDir, "history-saved.json"); + return new File(filesDir.get(), "history-saved.json"); } @NonNull File getRecentHistoryFile() { - return new File(filesDir, "history-recent.json"); + return new File(filesDir.get(), "history-recent.json"); } private void migrateOldHistory() { diff --git a/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java b/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java index b38c14d1..197c0ed1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java +++ b/app/src/main/java/org/solovyev/android/calculator/memory/Memory.java @@ -4,22 +4,31 @@ import android.os.Handler; import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; + import com.squareup.otto.Bus; -import jscl.math.Expression; -import jscl.math.Generic; -import jscl.math.JsclInteger; -import jscl.text.ParseException; + import org.solovyev.android.Check; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.AppModule; +import org.solovyev.android.calculator.Notifier; +import org.solovyev.android.calculator.Runnables; +import org.solovyev.android.calculator.ToJsclTextProcessor; import org.solovyev.android.io.FileSystem; +import java.io.File; +import java.io.IOException; +import java.util.concurrent.Executor; + import javax.annotation.Nonnull; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; -import java.io.File; -import java.io.IOException; -import java.util.concurrent.Executor; + +import dagger.Lazy; +import jscl.math.Expression; +import jscl.math.Generic; +import jscl.math.JsclInteger; +import jscl.text.ParseException; @Singleton public class Memory { @@ -29,7 +38,7 @@ public class Memory { @NonNull private final FileSystem fileSystem; @NonNull - private final File filesDir; + private final Lazy filesDir; @NonNull private final WriteTask writeTask = new WriteTask(); @NonNull @@ -50,7 +59,8 @@ public class Memory { private boolean loaded; @Inject - public Memory(@NonNull @Named(AppModule.THREAD_INIT) Executor initThread, @NonNull FileSystem fileSystem, @NonNull @Named(AppModule.DIR_FILES) File filesDir, @NonNull Handler handler) { + public Memory(@NonNull @Named(AppModule.THREAD_INIT) Executor initThread, @NonNull FileSystem fileSystem, @NonNull @Named(AppModule.DIR_FILES) + Lazy filesDir, @NonNull Handler handler) { this.fileSystem = fileSystem; this.filesDir = filesDir; this.handler = handler; @@ -187,7 +197,7 @@ public class Memory { @Nonnull private File getFile() { - return new File(filesDir, "memory.txt"); + return new File(filesDir.get(), "memory.txt"); } public void requestValue() {