Avoid nullable filesDir

This commit is contained in:
serso 2016-02-07 19:39:40 +01:00
parent 8c1143cb8f
commit 30eed6af8c
5 changed files with 38 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.Log;
import com.squareup.otto.Bus;
import com.squareup.otto.GeneratedHandlerFinder;
import dagger.Module;
@ -19,6 +20,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@ -34,6 +36,7 @@ public class AppModule {
public static final String THREAD_UI = "thread-ui";
// multiple threads
public static final String THREAD_BACKGROUND = "thread-background";
public static final String DIR_FILES = "dir-files";
@NonNull
private final Application application;
@ -146,6 +149,31 @@ public class AppModule {
return Typeface.createFromAsset(application.getAssets(), "fonts/Roboto-Regular.ttf");
}
@Singleton
@Provides
@Named(DIR_FILES)
File provideFilesDir(@Named(THREAD_INIT) Executor initThread) {
final File filesDir = makeFilesDir();
initThread.execute(new Runnable() {
@Override
public void run() {
if (!filesDir.exists() && !filesDir.mkdirs()) {
Log.e(App.TAG, "Can't create files dirs");
}
}
});
return filesDir;
}
@Nonnull
private File makeFilesDir() {
final File filesDir = application.getFilesDir();
if (filesDir == null) {
return new File(application.getApplicationInfo().dataDir, "files");
}
return filesDir;
}
private static class AppBus extends Bus {
@NonNull

View File

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

View File

@ -75,6 +75,9 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
@Inject
@Named(AppModule.THREAD_BACKGROUND)
public Executor backgroundThread;
@Inject
@Named(AppModule.DIR_FILES)
public File filesDir;
// synchronized on lock
private boolean initialized;

View File

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

View File

@ -86,6 +86,9 @@ public class History {
@Inject
@Named(AppModule.THREAD_BACKGROUND)
Executor backgroundThread;
@Inject
@Named(AppModule.DIR_FILES)
File filesDir;
@Nullable
static List<HistoryState> convertOldHistory(@NonNull String xml) {
@ -170,12 +173,12 @@ public class History {
@NonNull
private File getSavedHistoryFile() {
return new File(application.getFilesDir(), "history-saved.json");
return new File(filesDir, "history-saved.json");
}
@NonNull
private File getRecentHistoryFile() {
return new File(application.getFilesDir(), "history-recent.json");
return new File(filesDir, "history-recent.json");
}
private void migrateOldHistory() {