From 30eed6af8c97e05d4226da28ace089dca339c62e Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 7 Feb 2016 19:39:40 +0100 Subject: [PATCH] Avoid nullable filesDir --- .../android/calculator/AppModule.java | 28 +++++++++++++++++++ .../android/calculator/VariablesRegistry.java | 2 +- .../entities/BaseEntitiesRegistry.java | 3 ++ .../functions/FunctionsRegistry.java | 2 +- .../android/calculator/history/History.java | 7 +++-- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/AppModule.java b/app/src/main/java/org/solovyev/android/calculator/AppModule.java index 10a40c91..4cdb7465 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppModule.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppModule.java @@ -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 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 d6feb80a..50c1304d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java @@ -129,7 +129,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry { @Override @NonNull protected File getEntitiesFile() { - return new File(application.getFilesDir(), "variables.json"); + return new File(filesDir, "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 e10501a7..e2264e3b 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 @@ -75,6 +75,9 @@ public abstract class BaseEntitiesRegistry implements Enti @Inject @Named(AppModule.THREAD_BACKGROUND) public Executor backgroundThread; + @Inject + @Named(AppModule.DIR_FILES) + public File filesDir; // synchronized on lock private boolean initialized; 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 a288fb87..aad286e8 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 @@ -134,7 +134,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry { @Override @NonNull protected File getEntitiesFile() { - return new File(application.getFilesDir(), "functions.json"); + return new File(filesDir, "functions.json"); } @Nonnull 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 4f232793..11cc7029 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 @@ -86,6 +86,9 @@ public class History { @Inject @Named(AppModule.THREAD_BACKGROUND) Executor backgroundThread; + @Inject + @Named(AppModule.DIR_FILES) + File filesDir; @Nullable static List 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() {