diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index dfb0976ebd71..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; @@ -84,9 +85,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; @@ -572,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 @@ -596,6 +599,11 @@ public void onTrimMemory(final int level) { } else { background = false; } + + //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 && mBitmapCache != null) { + mBitmapCache.evictAll(); + } } diff --git a/src/org/wordpress/android/util/BitmapLruCache.java b/src/org/wordpress/android/util/BitmapLruCache.java index b986b74d5160..fa18b33faf7c 100644 --- a/src/org/wordpress/android/util/BitmapLruCache.java +++ b/src/org/wordpress/android/util/BitmapLruCache.java @@ -14,8 +14,10 @@ public BitmapLruCache(int maxSize) { @Override protected int sizeOf(String key, Bitmap value) { + // The cache size will be measured in kilobytes rather than + // number of items. int bytes = (value.getRowBytes() * value.getHeight()); - return (bytes / 1024); + return (bytes / 1024); //value.getByteCount() introduced in HONEYCOMB_MR1 or higher. } @Override @@ -27,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