From 19920e0cdf8e109a06c54a78f4e5fc96bdb382bd Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 11 Dec 2013 13:14:28 +0100 Subject: [PATCH 1/5] Use getByteCount in HONEYCOMB_MR1 or higher API --- src/org/wordpress/android/WordPress.java | 5 +++-- .../wordpress/android/util/BitmapLruCache.java | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index dfb0976ebd71..a66998189c1b 100644 --- a/src/org/wordpress/android/WordPress.java +++ b/src/org/wordpress/android/WordPress.java @@ -84,9 +84,10 @@ public class WordPress extends Application { public static BitmapLruCache getBitmapCache() { if (mBitmapCache == null) { - // see http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html + // The cache size will be measured in kilobytes rather than + // number of items. See http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); - int cacheSize = maxMemory / 16; + int cacheSize = maxMemory / 16; //Use 1/16th of the available memory for this memory cache. mBitmapCache = new BitmapLruCache(cacheSize); } return mBitmapCache; diff --git a/src/org/wordpress/android/util/BitmapLruCache.java b/src/org/wordpress/android/util/BitmapLruCache.java index b986b74d5160..c6c00d385a53 100644 --- a/src/org/wordpress/android/util/BitmapLruCache.java +++ b/src/org/wordpress/android/util/BitmapLruCache.java @@ -2,6 +2,7 @@ package org.wordpress.android.util; import android.graphics.Bitmap; +import android.os.Build; import android.support.v4.util.LruCache; import com.android.volley.toolbox.ImageLoader.ImageCache; @@ -14,8 +15,20 @@ public BitmapLruCache(int maxSize) { @Override protected int sizeOf(String key, Bitmap value) { - int bytes = (value.getRowBytes() * value.getHeight()); - return (bytes / 1024); + // The cache size will be measured in kilobytes rather than + // number of items. + + int bytes = 0; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { + bytes = (value.getRowBytes() * value.getHeight()); + } else { + bytes = value.getByteCount(); + } + + if (bytes!=0) + bytes = bytes / 1024; + + return bytes; } @Override From 7f40fe43e70fd2a14f3cad2e3139623dbc7888c2 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 11 Dec 2013 13:29:28 +0100 Subject: [PATCH 2/5] Trim memory cache when the device is running in low-memory --- src/org/wordpress/android/WordPress.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index a66998189c1b..c752e8a7e4ff 100644 --- a/src/org/wordpress/android/WordPress.java +++ b/src/org/wordpress/android/WordPress.java @@ -597,6 +597,13 @@ public void onTrimMemory(final int level) { } else { background = false; } + + //Levels that we need to consider are TRIM_MEMORY_UI_HIDDEN = 20; - TRIM_MEMORY_RUNNING_CRITICAL = 15; - TRIM_MEMORY_RUNNING_LOW = 10; + if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { + int maxMemory = (int) (Runtime.getRuntime().freeMemory() / 1024); + int cacheSize = maxMemory / 16; + mBitmapCache.trimToSize(cacheSize); + } } From 4f06a04c9a6e82897c077772efe649f35f2004a3 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 11 Dec 2013 15:43:15 +0100 Subject: [PATCH 3/5] Use 'evictAll' to clear the cache, instead of resizing it. --- src/org/wordpress/android/WordPress.java | 6 ++---- .../wordpress/android/util/BitmapLruCache.java | 17 +++-------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index c752e8a7e4ff..928ccb4bb9cb 100644 --- a/src/org/wordpress/android/WordPress.java +++ b/src/org/wordpress/android/WordPress.java @@ -598,11 +598,9 @@ public void onTrimMemory(final int level) { background = false; } - //Levels that we need to consider are TRIM_MEMORY_UI_HIDDEN = 20; - TRIM_MEMORY_RUNNING_CRITICAL = 15; - TRIM_MEMORY_RUNNING_LOW = 10; + //Levels that we need to consider are TRIM_MEMORY_RUNNING_CRITICAL = 15; - TRIM_MEMORY_RUNNING_LOW = 10; - TRIM_MEMORY_RUNNING_MODERATE = 5; if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { - int maxMemory = (int) (Runtime.getRuntime().freeMemory() / 1024); - int cacheSize = maxMemory / 16; - mBitmapCache.trimToSize(cacheSize); + mBitmapCache.evictAll(); } } diff --git a/src/org/wordpress/android/util/BitmapLruCache.java b/src/org/wordpress/android/util/BitmapLruCache.java index c6c00d385a53..fa18b33faf7c 100644 --- a/src/org/wordpress/android/util/BitmapLruCache.java +++ b/src/org/wordpress/android/util/BitmapLruCache.java @@ -2,7 +2,6 @@ package org.wordpress.android.util; import android.graphics.Bitmap; -import android.os.Build; import android.support.v4.util.LruCache; import com.android.volley.toolbox.ImageLoader.ImageCache; @@ -17,18 +16,8 @@ public BitmapLruCache(int maxSize) { protected int sizeOf(String key, Bitmap value) { // The cache size will be measured in kilobytes rather than // number of items. - - int bytes = 0; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { - bytes = (value.getRowBytes() * value.getHeight()); - } else { - bytes = value.getByteCount(); - } - - if (bytes!=0) - bytes = bytes / 1024; - - return bytes; + int bytes = (value.getRowBytes() * value.getHeight()); + return (bytes / 1024); //value.getByteCount() introduced in HONEYCOMB_MR1 or higher. } @Override @@ -40,4 +29,4 @@ public Bitmap getBitmap(String key) { public void putBitmap(String key, Bitmap bitmap) { this.put(key, bitmap); } -} +} \ No newline at end of file From 92b47285b919a9b6ae40582eb2a855ab61d1fe73 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 11 Dec 2013 15:52:00 +0100 Subject: [PATCH 4/5] Add a null check on mBitmapCache. It should never be 'null' but better to check it. --- src/org/wordpress/android/WordPress.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index 928ccb4bb9cb..c9bc15cb2322 100644 --- a/src/org/wordpress/android/WordPress.java +++ b/src/org/wordpress/android/WordPress.java @@ -599,7 +599,7 @@ public void onTrimMemory(final int level) { } //Levels that we need to consider are TRIM_MEMORY_RUNNING_CRITICAL = 15; - TRIM_MEMORY_RUNNING_LOW = 10; - TRIM_MEMORY_RUNNING_MODERATE = 5; - if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { + if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN && mBitmapCache != null) { mBitmapCache.evictAll(); } From fb6a4843e661d9fd549de92dbe0ca509fa192825 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 11 Dec 2013 17:32:49 +0100 Subject: [PATCH 5/5] Add 'targetApi' annotation to prevent lint warning on PushNotificationBackendMonitor --- src/org/wordpress/android/WordPress.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index c9bc15cb2322..c1b88fdabcec 100644 --- a/src/org/wordpress/android/WordPress.java +++ b/src/org/wordpress/android/WordPress.java @@ -1,6 +1,7 @@ package org.wordpress.android; import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.app.Activity; import android.app.Application; import android.content.ComponentCallbacks2; @@ -573,6 +574,7 @@ public HttpResponse performRequest(Request request, Map heade * This class also uses ActivityLifecycleCallbacks and a timer used as guard, to make sure to detect the send to background event and not other events. * */ + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private class PushNotificationsBackendMonitor implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 { private final int DEFAULT_TIMEOUT = 2 * 60; //2 minutes