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
@NonNull
protected File getEntitiesFile() {
return new File(filesDir, "variables.json");
return new File(filesDir.get(), "variables.json");
}
@Nullable

View File

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

View File

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

View File

@ -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<File> filesDir;
@Nullable
static List<HistoryState> 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() {

View File

@ -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<File> 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<File> 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() {