Simplify Catalyst handleMemoryPressure

Reviewed By: cwdick

Differential Revision: D5200555

fbshipit-source-id: 86f12acca33ece265d3482ba52de9afcc83173cd
This commit is contained in:
Pieter De Baets
2017-06-26 05:48:40 -07:00
committed by Facebook Github Bot
parent e2dff82160
commit 83faa4b608
12 changed files with 56 additions and 109 deletions

View File

@@ -25,32 +25,18 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
/**
* Translates and routes memory pressure events to the current catalyst instance.
*/
public class MemoryPressureRouter {
public class MemoryPressureRouter implements ComponentCallbacks2 {
// Trigger this by sending an intent to your activity with adb shell:
// am broadcast -a com.facebook.catalyst.ACTION_TRIM_MEMORY_MODERATE
// am broadcast -a com.facebook.react.ACTION_TRIM_MEMORY_MODERATE
private static final String ACTION_TRIM_MEMORY_UI_HIDDEN =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_UI_HIDDEN";
"com.facebook.react.ACTION_TRIM_MEMORY_UI_HIDDEN";
private static final String ACTION_TRIM_MEMORY_MODERATE =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_MODERATE";
"com.facebook.react.ACTION_TRIM_MEMORY_MODERATE";
private static final String ACTION_TRIM_MEMORY_CRITICAL =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_CRITICAL";
"com.facebook.react.ACTION_TRIM_MEMORY_CRITICAL";
private final Set<MemoryPressureListener> mListeners =
Collections.synchronizedSet(new LinkedHashSet<MemoryPressureListener>());
private final ComponentCallbacks2 mCallbacks = new ComponentCallbacks2() {
@Override
public void onTrimMemory(int level) {
trimMemory(level);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
}
@Override
public void onLowMemory() {
}
};
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean handleDebugIntent(Application application, String action) {
@@ -71,7 +57,11 @@ public class MemoryPressureRouter {
}
MemoryPressureRouter(Context context) {
context.getApplicationContext().registerComponentCallbacks(mCallbacks);
context.getApplicationContext().registerComponentCallbacks(this);
}
public void destroy(Context context) {
context.getApplicationContext().unregisterComponentCallbacks(this);
}
/**
@@ -88,11 +78,8 @@ public class MemoryPressureRouter {
mListeners.remove(listener);
}
public void destroy(Context context) {
context.getApplicationContext().unregisterComponentCallbacks(mCallbacks);
}
private void trimMemory(int level) {
@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) {
@@ -102,6 +89,14 @@ public class MemoryPressureRouter {
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
}
@Override
public void onLowMemory() {
}
private void dispatchMemoryPressure(MemoryPressure level) {
// copy listeners array to avoid ConcurrentModificationException if any of the listeners remove
// themselves in handleMemoryPressure()

View File

@@ -393,26 +393,14 @@ public class CatalystInstanceImpl implements CatalystInstance {
return mNativeModuleRegistry.getAllModules();
}
private native void handleMemoryPressureUiHidden();
private native void handleMemoryPressureModerate();
private native void handleMemoryPressureCritical();
private native void jniHandleMemoryPressure(int level);
@Override
public void handleMemoryPressure(MemoryPressure level) {
if (mDestroyed) {
return;
}
switch (level) {
case UI_HIDDEN:
handleMemoryPressureUiHidden();
break;
case MODERATE:
handleMemoryPressureModerate();
break;
case CRITICAL:
handleMemoryPressureCritical();
break;
}
jniHandleMemoryPressure(level.ordinal());
}
/**