From a4a6237aebcc40bc502aed9802d9051de1666a79 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 6 Mar 2017 16:36:25 +0800 Subject: [PATCH] improved on storage running low --- twidere/build.gradle | 6 +-- .../org/mariotaku/twidere/util/Utils.java | 20 ++++------ .../twidere/extension/GlideExtensions.kt | 8 +++- .../twidere/util/cache/DiskLRUFileCache.kt | 37 +++++++++++-------- .../twidere/util/dagger/ApplicationModule.kt | 10 ++--- ...layout_media_viewer_texture_video_view.xml | 3 +- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/twidere/build.gradle b/twidere/build.gradle index 46a95d9ee..b79f972c1 100644 --- a/twidere/build.gradle +++ b/twidere/build.gradle @@ -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' 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 d5ba4feda..f58560be0 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java @@ -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"); diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/GlideExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/GlideExtensions.kt index 062d889c7..26413fbad 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/GlideExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/GlideExtensions.kt @@ -39,7 +39,13 @@ fun RequestManager.loadProfileImage( @ImageShapeStyle style: Int = ImageShapeStyle.SHAPE_CIRCLE, size: String? = null ): DrawableRequestBuilder { - 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, diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/cache/DiskLRUFileCache.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/cache/DiskLRUFileCache.kt index e1c396ccc..fc299d1f7 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/cache/DiskLRUFileCache.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/cache/DiskLRUFileCache.kt @@ -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 { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt index 3b25849d3..afe0d2c82 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt @@ -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) } diff --git a/twidere/src/main/res/layout/layout_media_viewer_texture_video_view.xml b/twidere/src/main/res/layout/layout_media_viewer_texture_video_view.xml index d50a8fe1d..5dd22d071 100644 --- a/twidere/src/main/res/layout/layout_media_viewer_texture_video_view.xml +++ b/twidere/src/main/res/layout/layout_media_viewer_texture_video_view.xml @@ -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"/>