1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.base;
7 import android.app.Activity;
8 import android.content.ComponentCallbacks2;
9 import android.content.Context;
10 import android.content.res.Configuration;
14 * This is an internal implementation of the C++ counterpart.
15 * It registers a ComponentCallbacks2 with the system, and dispatches into
16 * native for levels that are considered actionable.
18 public class MemoryPressureListener {
20 * Sending an intent with this action to Chrome will cause it to issue a call to onLowMemory
21 * thus simulating a low memory situations.
23 private static final String ACTION_LOW_MEMORY = "org.chromium.base.ACTION_LOW_MEMORY";
26 * Sending an intent with this action to Chrome will cause it to issue a call to onTrimMemory
27 * thus simulating a low memory situations.
29 private static final String ACTION_TRIM_MEMORY = "org.chromium.base.ACTION_TRIM_MEMORY";
32 * Sending an intent with this action to Chrome will cause it to issue a call to onTrimMemory
33 * with notification level TRIM_MEMORY_RUNNING_CRITICAL thus simulating a low memory situation
35 private static final String ACTION_TRIM_MEMORY_RUNNING_CRITICAL =
36 "org.chromium.base.ACTION_TRIM_MEMORY_RUNNING_CRITICAL";
39 * Sending an intent with this action to Chrome will cause it to issue a call to onTrimMemory
40 * with notification level TRIM_MEMORY_MODERATE thus simulating a low memory situation
42 private static final String ACTION_TRIM_MEMORY_MODERATE =
43 "org.chromium.base.ACTION_TRIM_MEMORY_MODERATE";
46 private static void registerSystemCallback(Context context) {
47 context.registerComponentCallbacks(
48 new ComponentCallbacks2() {
50 public void onTrimMemory(int level) {
51 maybeNotifyMemoryPresure(level);
55 public void onLowMemory() {
56 nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL);
60 public void onConfigurationChanged(Configuration configuration) {
66 * Used by applications to simulate a memory pressure signal. By throwing certain intent
69 public static boolean handleDebugIntent(Activity activity, String action) {
70 if (ACTION_LOW_MEMORY.equals(action)) {
71 simulateLowMemoryPressureSignal(activity);
72 } else if (ACTION_TRIM_MEMORY.equals(action)) {
73 simulateTrimMemoryPressureSignal(activity, ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
74 } else if (ACTION_TRIM_MEMORY_RUNNING_CRITICAL.equals(action)) {
75 simulateTrimMemoryPressureSignal(activity, ComponentCallbacks2.TRIM_MEMORY_MODERATE);
76 } else if (ACTION_TRIM_MEMORY_MODERATE.equals(action)) {
77 simulateTrimMemoryPressureSignal(activity,
78 ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL);
86 public static void maybeNotifyMemoryPresure(int level) {
87 if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
88 nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL);
89 } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND ||
90 level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
91 // Don't notifiy on TRIM_MEMORY_UI_HIDDEN, since this class only
92 // dispatches actionable memory pressure signals to native.
93 nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_MODERATE);
97 private static void simulateLowMemoryPressureSignal(Activity activity) {
98 // The Application and the Activity each have a list of callbacks they notify when this
99 // method is called. Notifying these will simulate the event at the App/Activity level
100 // as well as trigger the listener bound from native in this process.
101 activity.getApplication().onLowMemory();
102 activity.onLowMemory();
105 private static void simulateTrimMemoryPressureSignal(Activity activity, int level) {
106 // The Application and the Activity each have a list of callbacks they notify when this
107 // method is called. Notifying these will simulate the event at the App/Activity level
108 // as well as trigger the listener bound from native in this process.
109 activity.getApplication().onTrimMemory(level);
110 activity.onTrimMemory(level);
113 private static native void nativeOnMemoryPressure(int memoryPressureType);