refactor MemoryPressureListener to use Android levels

Reviewed By: bnham

Differential Revision: D5603426

fbshipit-source-id: 6d09a56544c27e46f4b9ef491798720e37214e47
This commit is contained in:
Charles Dick
2017-08-12 06:31:02 -07:00
committed by Facebook Github Bot
parent 8b2975ad7b
commit 37754c5c83
3 changed files with 14 additions and 66 deletions
@@ -2,60 +2,21 @@
package com.facebook.react;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import com.facebook.react.bridge.MemoryPressure;
import com.facebook.react.bridge.MemoryPressureListener;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Translates and routes memory pressure events to the current catalyst instance.
*/
public class MemoryPressureRouter implements ComponentCallbacks2 {
// Trigger this by sending an intent to your activity with adb shell:
// am broadcast -a com.facebook.react.ACTION_TRIM_MEMORY_MODERATE
private static final String ACTION_TRIM_MEMORY_UI_HIDDEN =
"com.facebook.react.ACTION_TRIM_MEMORY_UI_HIDDEN";
private static final String ACTION_TRIM_MEMORY_MODERATE =
"com.facebook.react.ACTION_TRIM_MEMORY_MODERATE";
private static final String ACTION_TRIM_MEMORY_CRITICAL =
"com.facebook.react.ACTION_TRIM_MEMORY_CRITICAL";
private final Set<MemoryPressureListener> mListeners =
Collections.synchronizedSet(new LinkedHashSet<MemoryPressureListener>());
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean handleDebugIntent(Application application, String action) {
switch (action) {
case ACTION_TRIM_MEMORY_UI_HIDDEN:
simulateTrimMemory(application, ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
break;
case ACTION_TRIM_MEMORY_MODERATE:
simulateTrimMemory(application, TRIM_MEMORY_MODERATE);
break;
case ACTION_TRIM_MEMORY_CRITICAL:
simulateTrimMemory(application, TRIM_MEMORY_COMPLETE);
default:
return false;
}
return true;
}
MemoryPressureRouter(Context context) {
context.getApplicationContext().registerComponentCallbacks(this);
}
@@ -80,13 +41,7 @@ public class MemoryPressureRouter implements ComponentCallbacks2 {
@Override
public void onTrimMemory(int level) {
if (level >= TRIM_MEMORY_COMPLETE) {
dispatchMemoryPressure(MemoryPressure.CRITICAL);
} else if (level >= TRIM_MEMORY_BACKGROUND || level == TRIM_MEMORY_RUNNING_CRITICAL) {
dispatchMemoryPressure(MemoryPressure.MODERATE);
} else if (level == TRIM_MEMORY_UI_HIDDEN) {
dispatchMemoryPressure(MemoryPressure.UI_HIDDEN);
}
dispatchMemoryPressure(level);
}
@Override
@@ -97,7 +52,7 @@ public class MemoryPressureRouter implements ComponentCallbacks2 {
public void onLowMemory() {
}
private void dispatchMemoryPressure(MemoryPressure level) {
private void dispatchMemoryPressure(int level) {
// copy listeners array to avoid ConcurrentModificationException if any of the listeners remove
// themselves in handleMemoryPressure()
MemoryPressureListener[] listeners =
@@ -106,8 +61,4 @@ public class MemoryPressureRouter implements ComponentCallbacks2 {
listener.handleMemoryPressure(level);
}
}
private static void simulateTrimMemory(Application application, int level) {
application.onTrimMemory(level);
}
}