improved on storage running low

This commit is contained in:
Mariotaku Lee 2017-03-06 16:36:25 +08:00
parent 4580ff8aa5
commit a4a6237aeb
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
6 changed files with 46 additions and 38 deletions

View File

@ -171,8 +171,8 @@ dependencies {
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
compile 'jp.wasabeef:glide-transformations:2.0.1'
compile 'com.github.mariotaku.MediaViewerLibrary:base:0.9.22'
compile 'com.github.mariotaku.MediaViewerLibrary:subsample-image-view:0.9.22'
compile 'com.github.mariotaku.MediaViewerLibrary:base:0.9.23'
compile 'com.github.mariotaku.MediaViewerLibrary:subsample-image-view:0.9.23'
compile 'com.github.mariotaku:SQLiteQB:0.9.10'
compile "com.github.mariotaku.ObjectCursor:core:${libVersions['ObjectCursor']}"
compile 'com.github.mariotaku:MultiValueSwitch:0.9.7'
@ -182,7 +182,7 @@ dependencies {
compile "com.github.mariotaku.CommonsLibrary:text:${libVersions['MariotakuCommons']}"
compile "com.github.mariotaku.CommonsLibrary:text-kotlin:${libVersions['MariotakuCommons']}"
compile 'com.github.mariotaku:KPreferences:0.9.5'
compile 'com.github.mariotaku:Chameleon:0.9.15'
compile 'com.github.mariotaku:Chameleon:0.9.16'
compile "org.jetbrains.kotlin:kotlin-stdlib:${libVersions['Kotlin']}"
compile 'nl.komponents.kovenant:kovenant:3.3.0'

View File

@ -462,13 +462,18 @@ public final class Utils implements Constants {
}
@Nullable
public static File getExternalCacheDir(final Context context, final String cacheDirName) {
public static File getExternalCacheDir(final Context context, final String cacheDirName,
final long sizeInBytes) {
if (context == null) throw new NullPointerException();
final File externalCacheDir = context.getExternalCacheDir();
if (externalCacheDir == null) return null;
final File cacheDir = new File(externalCacheDir, cacheDirName);
if (sizeInBytes > 0 && externalCacheDir.getFreeSpace() < sizeInBytes / 10) {
// Less then 10% space available
return null;
}
if (cacheDir.isDirectory() || cacheDir.mkdirs()) return cacheDir;
return new File(context.getCacheDir(), cacheDirName);
return null;
}
public static String getLocalizedNumber(final Locale locale, final Number number) {
@ -493,14 +498,6 @@ public final class Utils implements Constants {
return list.toArray(new String[list.size()]);
}
public static String getNormalTwitterProfileImage(final String url) {
return getTwitterProfileImageOfSize(url, "normal");
}
public static Uri getNotificationUri(final int tableId, final Uri def) {
return def;
}
public static String getOriginalTwitterProfileImage(final String url) {
if (url == null) return null;
final Matcher matcher = PATTERN_TWITTER_PROFILE_IMAGES.matcher(url);
@ -613,8 +610,7 @@ public final class Utils implements Constants {
}
public static String getTwitterProfileImageOfSize(final String url, final String size) {
if (url == null) return null;
public static String getTwitterProfileImageOfSize(@NonNull final String url, @NonNull final String size) {
final Matcher matcher = PATTERN_TWITTER_PROFILE_IMAGES.matcher(url);
if (matcher.matches()) {
return matcher.replaceFirst("$1$2/profile_images/$3/$4_" + size + "$6");

View File

@ -39,7 +39,13 @@ fun RequestManager.loadProfileImage(
@ImageShapeStyle style: Int = ImageShapeStyle.SHAPE_CIRCLE,
size: String? = null
): DrawableRequestBuilder<String?> {
return configureLoadProfileImage(context, style) { load(Utils.getTwitterProfileImageOfSize(url, size)) }
return configureLoadProfileImage(context, style) {
if (url == null || size == null) {
return@configureLoadProfileImage load(url)
} else {
return@configureLoadProfileImage load(Utils.getTwitterProfileImageOfSize(url, size))
}
}
}
fun RequestManager.loadProfileImage(context: Context, resourceId: Int,

View File

@ -57,27 +57,32 @@ class DiskLRUFileCache(val cacheDir: File) : FileCache {
cache?.remove(hash(key))
}
@Throws(IOException::class)
override fun save(key: String, stream: InputStream, extra: ByteArray?, listener: FileCache.CopyListener?) {
val editor = cache?.edit(hash(key)) ?: return
val hashedKey = hash(key)
val editor = cache?.edit(hashedKey) ?: throw IOException("Unable to open cache for $key")
editor.getFile(0).outputStream().use {
var bytesCopied: Int = 0
val buffer = ByteArray(8192)
var bytes = stream.read(buffer)
while (bytes >= 0) {
it.write(buffer, 0, bytes)
bytesCopied += bytes
if (listener != null && !listener.onCopied(bytesCopied)) {
editor.abort()
return
try {
editor.getFile(0).outputStream().use {
var bytesCopied: Int = 0
val buffer = ByteArray(8192)
var bytes = stream.read(buffer)
while (bytes >= 0) {
it.write(buffer, 0, bytes)
bytesCopied += bytes
if (listener != null && !listener.onCopied(bytesCopied)) {
return
}
bytes = stream.read(buffer)
}
bytes = stream.read(buffer)
}
if (extra != null) {
editor.getFile(1).writeBytes(extra)
}
editor.commit()
} finally {
editor.abortUnlessCommitted()
}
if (extra != null) {
editor.getFile(1).writeBytes(extra)
}
editor.commit()
}
private fun hash(key: String): String {

View File

@ -311,7 +311,7 @@ class ApplicationModule(private val application: Application) {
fun cache(preferences: SharedPreferencesWrapper): Cache {
val cacheSizeMB = preferences.getInt(KEY_CACHE_SIZE_LIMIT, 300).coerceIn(100..500)
// Convert to bytes
return Cache(getCacheDir("network"), cacheSizeMB * 1048576L)
return Cache(getCacheDir("network", cacheSizeMB * 1048576L), cacheSizeMB * 1048576L)
}
@Provides
@ -323,17 +323,17 @@ class ApplicationModule(private val application: Application) {
@Provides
@Singleton
fun jsonCache(): JsonCache {
return JsonCache(getCacheDir("json"))
return JsonCache(getCacheDir("json", 100 * 1048576L))
}
@Provides
@Singleton
fun fileCache(): FileCache {
return DiskLRUFileCache(getCacheDir("media"))
return DiskLRUFileCache(getCacheDir("media", 100 * 1048576L))
}
private fun getCacheDir(dirName: String): File {
return Utils.getExternalCacheDir(application, dirName) ?:
private fun getCacheDir(dirName: String, sizeInBytes: Long): File {
return Utils.getExternalCacheDir(application, dirName, sizeInBytes) ?:
Utils.getInternalCacheDir(application, dirName)
}

View File

@ -36,7 +36,8 @@
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
android:layout_gravity="center"
android:background="@android:color/black"/>
</com.commonsware.cwac.layouts.AspectLockedFrameLayout>