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.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.GeneratedHandlerFinder; import com.squareup.otto.GeneratedHandlerFinder;
import dagger.Module; import dagger.Module;
@ -19,6 +20,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -34,6 +36,7 @@ public class AppModule {
public static final String THREAD_UI = "thread-ui"; public static final String THREAD_UI = "thread-ui";
// multiple threads // multiple threads
public static final String THREAD_BACKGROUND = "thread-background"; public static final String THREAD_BACKGROUND = "thread-background";
public static final String DIR_FILES = "dir-files";
@NonNull @NonNull
private final Application application; private final Application application;
@ -146,6 +149,31 @@ public class AppModule {
return Typeface.createFromAsset(application.getAssets(), "fonts/Roboto-Regular.ttf"); 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 { private static class AppBus extends Bus {
@NonNull @NonNull

View File

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

View File

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

View File

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

View File

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