Access filesDir lazily (initialize on the background thread)

This commit is contained in:
Sergey Solovyev 2017-05-26 19:03:31 +02:00
parent a8821e75bf
commit adf764cf13
5 changed files with 32 additions and 18 deletions

View File

@ -123,7 +123,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
@Override @Override
@NonNull @NonNull
protected File getEntitiesFile() { protected File getEntitiesFile() {
return new File(filesDir, "variables.json"); return new File(filesDir.get(), "variables.json");
} }
@Nullable @Nullable

View File

@ -57,6 +57,8 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import dagger.Lazy;
public abstract class BaseEntitiesRegistry<T extends MathEntity> implements EntitiesRegistry<T> { public abstract class BaseEntitiesRegistry<T extends MathEntity> implements EntitiesRegistry<T> {
@Nonnull @Nonnull
@ -82,7 +84,7 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
public Executor backgroundThread; public Executor backgroundThread;
@Inject @Inject
@Named(AppModule.DIR_FILES) @Named(AppModule.DIR_FILES)
public File filesDir; public Lazy<File> filesDir;
@Nonnull @Nonnull
private final Map<String, Integer> descriptions = new HashMap<>(); private final Map<String, Integer> descriptions = new HashMap<>();

View File

@ -199,7 +199,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
@Override @Override
@NonNull @NonNull
protected File getEntitiesFile() { protected File getEntitiesFile() {
return new File(filesDir, "functions.json"); return new File(filesDir.get(), "functions.json");
} }
@Override @Override

View File

@ -22,6 +22,8 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import static android.text.TextUtils.isEmpty;
import android.app.Application; import android.app.Application;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
@ -61,7 +63,7 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import static android.text.TextUtils.isEmpty; import dagger.Lazy;
@Singleton @Singleton
public class History { public class History {
@ -102,7 +104,7 @@ public class History {
Executor backgroundThread; Executor backgroundThread;
@Inject @Inject
@Named(AppModule.DIR_FILES) @Named(AppModule.DIR_FILES)
File filesDir; Lazy<File> filesDir;
@Nullable @Nullable
static List<HistoryState> convertOldHistory(@NonNull String xml) throws Exception { static List<HistoryState> convertOldHistory(@NonNull String xml) throws Exception {
@ -190,12 +192,12 @@ public class History {
@NonNull @NonNull
File getSavedHistoryFile() { File getSavedHistoryFile() {
return new File(filesDir, "history-saved.json"); return new File(filesDir.get(), "history-saved.json");
} }
@NonNull @NonNull
File getRecentHistoryFile() { File getRecentHistoryFile() {
return new File(filesDir, "history-recent.json"); return new File(filesDir.get(), "history-recent.json");
} }
private void migrateOldHistory() { private void migrateOldHistory() {

View File

@ -4,22 +4,31 @@ import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus; 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.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 org.solovyev.android.io.FileSystem;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.File;
import java.io.IOException; import dagger.Lazy;
import java.util.concurrent.Executor; import jscl.math.Expression;
import jscl.math.Generic;
import jscl.math.JsclInteger;
import jscl.text.ParseException;
@Singleton @Singleton
public class Memory { public class Memory {
@ -29,7 +38,7 @@ public class Memory {
@NonNull @NonNull
private final FileSystem fileSystem; private final FileSystem fileSystem;
@NonNull @NonNull
private final File filesDir; private final Lazy<File> filesDir;
@NonNull @NonNull
private final WriteTask writeTask = new WriteTask(); private final WriteTask writeTask = new WriteTask();
@NonNull @NonNull
@ -50,7 +59,8 @@ public class Memory {
private boolean loaded; private boolean loaded;
@Inject @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<File> filesDir, @NonNull Handler handler) {
this.fileSystem = fileSystem; this.fileSystem = fileSystem;
this.filesDir = filesDir; this.filesDir = filesDir;
this.handler = handler; this.handler = handler;
@ -187,7 +197,7 @@ public class Memory {
@Nonnull @Nonnull
private File getFile() { private File getFile() {
return new File(filesDir, "memory.txt"); return new File(filesDir.get(), "memory.txt");
} }
public void requestValue() { public void requestValue() {