improved cache dir

This commit is contained in:
Mariotaku Lee 2015-10-09 17:47:49 +08:00
parent d9c47eaf88
commit 7b958d9e0e
4 changed files with 23 additions and 9 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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);
}