Skip to content

Commit

Permalink
add generic memory pressure listener interface
Browse files Browse the repository at this point in the history
Reviewed By: lexs

Differential Revision: D2989071

fb-gh-sync-id: 375c7551bd4b281096850732432f016c2684add0
shipit-source-id: 375c7551bd4b281096850732432f016c2684add0
  • Loading branch information
foghina authored and Facebook Github Bot 6 committed Mar 3, 2016
1 parent 9a3f11d commit c027f05
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Expand Up @@ -2,7 +2,9 @@

package com.facebook.react;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import android.annotation.TargetApi;
import android.app.Activity;
Expand All @@ -11,9 +13,8 @@
import android.content.res.Configuration;
import android.os.Build;

import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.MemoryPressure;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.MemoryPressureListener;

import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
Expand All @@ -27,7 +28,8 @@ public class MemoryPressureRouter {
// am start -a "com.facebook.catalyst.ACTION_TRIM_MEMORY" --activity-single-top -n <activity>
private static final String ACTION_TRIM_MEMORY ="com.facebook.catalyst.ACTION_TRIM_MEMORY";

private @Nullable CatalystInstance mCatalystInstance;
private final Set<MemoryPressureListener> mListeners =
Collections.synchronizedSet(new LinkedHashSet<MemoryPressureListener>());
private final ComponentCallbacks2 mCallbacks = new ComponentCallbacks2() {
@Override
public void onTrimMemory(int level) {
Expand Down Expand Up @@ -60,12 +62,18 @@ public static boolean handleDebugIntent(Activity activity, String action) {
context.getApplicationContext().registerComponentCallbacks(mCallbacks);
}

public void onNewReactContextCreated(ReactContext reactContext) {
mCatalystInstance = reactContext.getCatalystInstance();
/**
* Add a listener to be notified of memory pressure events.
*/
public void addMemoryPressureListener(MemoryPressureListener listener) {
mListeners.add(listener);
}

public void onReactInstanceDestroyed() {
mCatalystInstance = null;
/**
* Remove a listener previously added with {@link #addMemoryPressureListener}.
*/
public void removeMemoryPressureListener(MemoryPressureListener listener) {
mListeners.remove(listener);
}

public void destroy(Context context) {
Expand All @@ -81,8 +89,12 @@ private void trimMemory(int level) {
}

private void dispatchMemoryPressure(MemoryPressure level) {
if (mCatalystInstance != null) {
mCatalystInstance.handleMemoryPressure(level);
// copy listeners array to avoid ConcurrentModificationException if any of the listeners remove
// themselves in handleMemoryPressure()
MemoryPressureListener[] listeners =
mListeners.toArray(new MemoryPressureListener[mListeners.size()]);
for (MemoryPressureListener listener : listeners) {
listener.handleMemoryPressure(level);
}
}

Expand Down
Expand Up @@ -715,7 +715,7 @@ private void setupReactContext(ReactApplicationContext reactContext) {

catalystInstance.initialize();
mDevSupportManager.onNewReactContextCreated(reactContext);
mMemoryPressureRouter.onNewReactContextCreated(reactContext);
mMemoryPressureRouter.addMemoryPressureListener(catalystInstance);
moveReactContextToCurrentLifecycleState();

for (ReactRootView rootView : mAttachedRootViews) {
Expand Down Expand Up @@ -772,7 +772,7 @@ private void tearDownReactContext(ReactContext reactContext) {
}
reactContext.destroy();
mDevSupportManager.onReactInstanceDestroyed(reactContext);
mMemoryPressureRouter.onReactInstanceDestroyed();
mMemoryPressureRouter.removeMemoryPressureListener(reactContext.getCatalystInstance());
}

/**
Expand Down
Expand Up @@ -21,7 +21,7 @@
* Java APIs be invokable from JavaScript as well.
*/
@DoNotStrip
public interface CatalystInstance {
public interface CatalystInstance extends MemoryPressureListener {
void runJSBundle();
// This is called from java code, so it won't be stripped anyway, but proguard will rename it,
// which this prevents.
Expand All @@ -48,8 +48,6 @@ public interface CatalystInstance {
<T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface);
Collection<NativeModule> getNativeModules();

void handleMemoryPressure(MemoryPressure level);

/**
* Adds a idle listener for this Catalyst instance. The listener will receive notifications
* whenever the bridge transitions from idle to busy and vice-versa, where the busy state is
Expand Down
@@ -0,0 +1,15 @@
// Copyright 2004-present Facebook. All Rights Reserved.

package com.facebook.react.bridge;

/**
* Listener interface for memory pressure events.
*/
public interface MemoryPressureListener {

/**
* Called when the system generates a memory warning.
*/
void handleMemoryPressure(MemoryPressure level);

}

0 comments on commit c027f05

Please sign in to comment.