Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/425 free memory cache when the device running on low-memory #427

Merged
merged 5 commits into from
Dec 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/org/wordpress/android/WordPress.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -572,6 +574,7 @@ public HttpResponse performRequest(Request<?> request, Map<String, String> 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
Expand All @@ -596,6 +599,11 @@ public void onTrimMemory(final int level) {
} else {
background = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this cause background to be set to false when the level is TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or TRIM_MEMORY_COMPLETE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we want background = true only when 'level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN'-

}

//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();
}

}

Expand Down
6 changes: 4 additions & 2 deletions src/org/wordpress/android/util/BitmapLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,4 +29,4 @@ public Bitmap getBitmap(String key) {
public void putBitmap(String key, Bitmap bitmap) {
this.put(key, bitmap);
}
}
}