This source file includes following definitions.
- registerSystemCallback
- handleDebugIntent
- maybeNotifyMemoryPresure
- simulateLowMemoryPressureSignal
- simulateTrimMemoryPressureSignal
- nativeOnMemoryPressure
package org.chromium.base;
import android.app.Activity;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
public class MemoryPressureListener {
private static final String ACTION_LOW_MEMORY = "org.chromium.base.ACTION_LOW_MEMORY";
private static final String ACTION_TRIM_MEMORY = "org.chromium.base.ACTION_TRIM_MEMORY";
private static final String ACTION_TRIM_MEMORY_RUNNING_CRITICAL =
"org.chromium.base.ACTION_TRIM_MEMORY_RUNNING_CRITICAL";
private static final String ACTION_TRIM_MEMORY_MODERATE =
"org.chromium.base.ACTION_TRIM_MEMORY_MODERATE";
@CalledByNative
private static void registerSystemCallback(Context context) {
context.registerComponentCallbacks(
new ComponentCallbacks2() {
@Override
public void onTrimMemory(int level) {
maybeNotifyMemoryPresure(level);
}
@Override
public void onLowMemory() {
nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL);
}
@Override
public void onConfigurationChanged(Configuration configuration) {
}
});
}
public static boolean handleDebugIntent(Activity activity, String action) {
if (ACTION_LOW_MEMORY.equals(action)) {
simulateLowMemoryPressureSignal(activity);
} else if (ACTION_TRIM_MEMORY.equals(action)) {
simulateTrimMemoryPressureSignal(activity, ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
} else if (ACTION_TRIM_MEMORY_RUNNING_CRITICAL.equals(action)) {
simulateTrimMemoryPressureSignal(activity, ComponentCallbacks2.TRIM_MEMORY_MODERATE);
} else if (ACTION_TRIM_MEMORY_MODERATE.equals(action)) {
simulateTrimMemoryPressureSignal(activity,
ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL);
} else {
return false;
}
return true;
}
public static void maybeNotifyMemoryPresure(int level) {
if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL);
} else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND ||
level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_MODERATE);
}
}
private static void simulateLowMemoryPressureSignal(Activity activity) {
activity.getApplication().onLowMemory();
activity.onLowMemory();
}
private static void simulateTrimMemoryPressureSignal(Activity activity, int level) {
activity.getApplication().onTrimMemory(level);
activity.onTrimMemory(level);
}
private static native void nativeOnMemoryPressure(int memoryPressureType);
}