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:26:04 -07:00 committed by Facebook Github Bot
parent 8b2975ad7b
commit 37754c5c83
3 changed files with 14 additions and 66 deletions

View File

@ -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);
}
}

View File

@ -9,17 +9,8 @@
package com.facebook.react.bridge;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import android.content.res.AssetManager;
import android.util.Log;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.jni.HybridData;
@ -33,6 +24,12 @@ import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.TraceListener;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
/**
* This provides an implementation of the public CatalystInstance instance. It is public because
@ -396,11 +393,11 @@ public class CatalystInstanceImpl implements CatalystInstance {
private native void jniHandleMemoryPressure(int level);
@Override
public void handleMemoryPressure(MemoryPressure level) {
public void handleMemoryPressure(int level) {
if (mDestroyed) {
return;
}
jniHandleMemoryPressure(level.ordinal());
jniHandleMemoryPressure(level);
}
/**

View File

@ -10,6 +10,6 @@ public interface MemoryPressureListener {
/**
* Called when the system generates a memory warning.
*/
void handleMemoryPressure(MemoryPressure level);
void handleMemoryPressure(int level);
}