Skip to content

Commit

Permalink
refactor MemoryPressureListener to use Android levels
Browse files Browse the repository at this point in the history
Reviewed By: bnham

Differential Revision: D5603426

fbshipit-source-id: 6d09a56544c27e46f4b9ef491798720e37214e47
  • Loading branch information
cwdick authored and facebook-github-bot committed Aug 12, 2017
1 parent 8b2975a commit 37754c5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -80,13 +41,7 @@ public void removeMemoryPressureListener(MemoryPressureListener listener) {

@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
Expand All @@ -97,7 +52,7 @@ public void onConfigurationChanged(Configuration newConfig) {
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 =
Expand All @@ -106,8 +61,4 @@ private void dispatchMemoryPressure(MemoryPressure level) {
listener.handleMemoryPressure(level);
}
}

private static void simulateTrimMemory(Application application, int level) {
application.onTrimMemory(level);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +24,12 @@
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
Expand Down Expand Up @@ -396,11 +393,11 @@ public Collection<NativeModule> getNativeModules() {
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public interface MemoryPressureListener {
/**
* Called when the system generates a memory warning.
*/
void handleMemoryPressure(MemoryPressure level);
void handleMemoryPressure(int level);

}

0 comments on commit 37754c5

Please sign in to comment.