diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/util/AbsLogger.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/util/AbsLogger.java index 121aea8d1..fe5a20264 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/util/AbsLogger.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/util/AbsLogger.java @@ -57,6 +57,10 @@ public abstract class AbsLogger { error(null, throwable); } + public static void logIfFalse(boolean expression, String message) { + if (!expression) error(message); + } + protected abstract void logImpl(@Nullable String message, @Nullable Throwable throwable); protected abstract void errorImpl(@Nullable String message, @Nullable Throwable throwable); diff --git a/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java b/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java index 8e32e1846..193a0b91f 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java +++ b/twidere/src/main/java/org/mariotaku/twidere/app/TwidereApplication.java @@ -49,7 +49,6 @@ import org.mariotaku.twidere.util.DebugModeUtils; import org.mariotaku.twidere.util.MathUtils; import org.mariotaku.twidere.util.StrictModeUtils; import org.mariotaku.twidere.util.TwidereLogger; -import org.mariotaku.twidere.util.UserColorNameManager; import org.mariotaku.twidere.util.Utils; import org.mariotaku.twidere.util.content.TwidereSQLiteOpenHelper; import org.mariotaku.twidere.util.dagger.ApplicationModule; @@ -60,7 +59,6 @@ import org.mariotaku.twidere.util.net.TwidereNetwork; import java.io.File; import java.io.IOException; -import static org.mariotaku.twidere.util.Utils.getBestCacheDir; import static org.mariotaku.twidere.util.Utils.getInternalCacheDir; import static org.mariotaku.twidere.util.Utils.initAccountColor; import static org.mariotaku.twidere.util.Utils.startRefreshServiceIfNeeded; @@ -229,14 +227,16 @@ public class TwidereApplication extends MultiDexApplication implements Constants } private DiskCache createDiskCache(final String dirName) { - final File cacheDir = getBestCacheDir(this, dirName); + final File cacheDir = Utils.getExternalCacheDir(this, dirName); final File fallbackCacheDir = getInternalCacheDir(this, dirName); final URLFileNameGenerator fileNameGenerator = new URLFileNameGenerator(); final SharedPreferences preferences = getSharedPreferences(); final int cacheSize = MathUtils.clamp(preferences.getInt(KEY_CACHE_SIZE_LIMIT, 300), 100, 500); try { - return new LruDiskCache(cacheDir, fallbackCacheDir, fileNameGenerator, - cacheSize * 1024 * 1024, 0); + final int cacheMaxSizeBytes = cacheSize * 1024 * 1024; + if (cacheDir != null) + return new LruDiskCache(cacheDir, fallbackCacheDir, fileNameGenerator, cacheMaxSizeBytes, 0); + return new LruDiskCache(fallbackCacheDir, null, fileNameGenerator, cacheMaxSizeBytes, 0); } catch (IOException e) { return new ReadOnlyDiskLRUNameCache(cacheDir, fallbackCacheDir, fileNameGenerator); } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/LoganSquareWrapper.java b/twidere/src/main/java/org/mariotaku/twidere/util/LoganSquareWrapper.java index 57897e470..9428e4c51 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/LoganSquareWrapper.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/LoganSquareWrapper.java @@ -55,12 +55,12 @@ public class LoganSquareWrapper extends LoganSquare { public static File getSerializationFile(final Context context, final Object... args) throws IOException { if (context == null || args == null || args.length == 0) return null; - final File cache_dir = Utils.getBestCacheDir(context, JSON_CACHE_DIR); - if (!cache_dir.exists()) { - cache_dir.mkdirs(); + final File cacheDir = Utils.getBestCacheDir(context, JSON_CACHE_DIR); + if (!cacheDir.exists()) { + AbsLogger.logIfFalse(cacheDir.mkdirs(), "Unable to create cache dir"); } final String filename = Utils.encodeQueryParams(TwidereArrayUtils.toString(args, '.', false)); - return new File(cache_dir, filename + ".json"); + return new File(cacheDir, filename + ".json"); } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java b/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java index be14c8572..bda465a5f 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java @@ -1876,6 +1876,16 @@ public final class Utils implements Constants { return new File(context.getCacheDir(), cacheDirName); } + @Nullable + public static File getExternalCacheDir(final Context context, final String cacheDirName) { + if (context == null) throw new NullPointerException(); + final File externalCacheDir = context.getExternalCacheDir(); + if (externalCacheDir == null) return null; + final File cacheDir = new File(externalCacheDir, cacheDirName); + if (cacheDir.isDirectory() || cacheDir.mkdirs()) return cacheDir; + return new File(context.getCacheDir(), cacheDirName); + } + public static CharSequence getKeywordBoldedText(final CharSequence orig, final String... keywords) { return getKeywordHighlightedText(orig, new StyleSpan(Typeface.BOLD), keywords); }