improved cache dir
This commit is contained in:
parent
d9c47eaf88
commit
7b958d9e0e
|
@ -57,6 +57,10 @@ public abstract class AbsLogger {
|
||||||
error(null, throwable);
|
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 logImpl(@Nullable String message, @Nullable Throwable throwable);
|
||||||
|
|
||||||
protected abstract void errorImpl(@Nullable String message, @Nullable Throwable throwable);
|
protected abstract void errorImpl(@Nullable String message, @Nullable Throwable throwable);
|
||||||
|
|
|
@ -49,7 +49,6 @@ import org.mariotaku.twidere.util.DebugModeUtils;
|
||||||
import org.mariotaku.twidere.util.MathUtils;
|
import org.mariotaku.twidere.util.MathUtils;
|
||||||
import org.mariotaku.twidere.util.StrictModeUtils;
|
import org.mariotaku.twidere.util.StrictModeUtils;
|
||||||
import org.mariotaku.twidere.util.TwidereLogger;
|
import org.mariotaku.twidere.util.TwidereLogger;
|
||||||
import org.mariotaku.twidere.util.UserColorNameManager;
|
|
||||||
import org.mariotaku.twidere.util.Utils;
|
import org.mariotaku.twidere.util.Utils;
|
||||||
import org.mariotaku.twidere.util.content.TwidereSQLiteOpenHelper;
|
import org.mariotaku.twidere.util.content.TwidereSQLiteOpenHelper;
|
||||||
import org.mariotaku.twidere.util.dagger.ApplicationModule;
|
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.File;
|
||||||
import java.io.IOException;
|
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.getInternalCacheDir;
|
||||||
import static org.mariotaku.twidere.util.Utils.initAccountColor;
|
import static org.mariotaku.twidere.util.Utils.initAccountColor;
|
||||||
import static org.mariotaku.twidere.util.Utils.startRefreshServiceIfNeeded;
|
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) {
|
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 File fallbackCacheDir = getInternalCacheDir(this, dirName);
|
||||||
final URLFileNameGenerator fileNameGenerator = new URLFileNameGenerator();
|
final URLFileNameGenerator fileNameGenerator = new URLFileNameGenerator();
|
||||||
final SharedPreferences preferences = getSharedPreferences();
|
final SharedPreferences preferences = getSharedPreferences();
|
||||||
final int cacheSize = MathUtils.clamp(preferences.getInt(KEY_CACHE_SIZE_LIMIT, 300), 100, 500);
|
final int cacheSize = MathUtils.clamp(preferences.getInt(KEY_CACHE_SIZE_LIMIT, 300), 100, 500);
|
||||||
try {
|
try {
|
||||||
return new LruDiskCache(cacheDir, fallbackCacheDir, fileNameGenerator,
|
final int cacheMaxSizeBytes = cacheSize * 1024 * 1024;
|
||||||
cacheSize * 1024 * 1024, 0);
|
if (cacheDir != null)
|
||||||
|
return new LruDiskCache(cacheDir, fallbackCacheDir, fileNameGenerator, cacheMaxSizeBytes, 0);
|
||||||
|
return new LruDiskCache(fallbackCacheDir, null, fileNameGenerator, cacheMaxSizeBytes, 0);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return new ReadOnlyDiskLRUNameCache(cacheDir, fallbackCacheDir, fileNameGenerator);
|
return new ReadOnlyDiskLRUNameCache(cacheDir, fallbackCacheDir, fileNameGenerator);
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,12 +55,12 @@ public class LoganSquareWrapper extends LoganSquare {
|
||||||
|
|
||||||
public static File getSerializationFile(final Context context, final Object... args) throws IOException {
|
public static File getSerializationFile(final Context context, final Object... args) throws IOException {
|
||||||
if (context == null || args == null || args.length == 0) return null;
|
if (context == null || args == null || args.length == 0) return null;
|
||||||
final File cache_dir = Utils.getBestCacheDir(context, JSON_CACHE_DIR);
|
final File cacheDir = Utils.getBestCacheDir(context, JSON_CACHE_DIR);
|
||||||
if (!cache_dir.exists()) {
|
if (!cacheDir.exists()) {
|
||||||
cache_dir.mkdirs();
|
AbsLogger.logIfFalse(cacheDir.mkdirs(), "Unable to create cache dir");
|
||||||
}
|
}
|
||||||
final String filename = Utils.encodeQueryParams(TwidereArrayUtils.toString(args, '.', false));
|
final String filename = Utils.encodeQueryParams(TwidereArrayUtils.toString(args, '.', false));
|
||||||
return new File(cache_dir, filename + ".json");
|
return new File(cacheDir, filename + ".json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1876,6 +1876,16 @@ public final class Utils implements Constants {
|
||||||
return new File(context.getCacheDir(), cacheDirName);
|
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) {
|
public static CharSequence getKeywordBoldedText(final CharSequence orig, final String... keywords) {
|
||||||
return getKeywordHighlightedText(orig, new StyleSpan(Typeface.BOLD), keywords);
|
return getKeywordHighlightedText(orig, new StyleSpan(Typeface.BOLD), keywords);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue