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. 4 5 package org.chromium.base; 6 7 import android.app.Activity; 8 import android.content.ComponentCallbacks2; 9 import android.content.Context; 10 import android.content.res.Configuration; 11 12 13 /** 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. 17 */ 18 public class MemoryPressureListener { 19 /** 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. 22 */ 23 private static final String ACTION_LOW_MEMORY = "org.chromium.base.ACTION_LOW_MEMORY"; 24 25 /** 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. 28 */ 29 private static final String ACTION_TRIM_MEMORY = "org.chromium.base.ACTION_TRIM_MEMORY"; 30 31 /** 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 34 */ 35 private static final String ACTION_TRIM_MEMORY_RUNNING_CRITICAL = 36 "org.chromium.base.ACTION_TRIM_MEMORY_RUNNING_CRITICAL"; 37 38 /** 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 41 */ 42 private static final String ACTION_TRIM_MEMORY_MODERATE = 43 "org.chromium.base.ACTION_TRIM_MEMORY_MODERATE"; 44 45 @CalledByNative registerSystemCallback(Context context)46 private static void registerSystemCallback(Context context) { 47 context.registerComponentCallbacks( 48 new ComponentCallbacks2() { 49 @Override 50 public void onTrimMemory(int level) { 51 maybeNotifyMemoryPresure(level); 52 } 53 54 @Override 55 public void onLowMemory() { 56 nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL); 57 } 58 59 @Override 60 public void onConfigurationChanged(Configuration configuration) { 61 } 62 }); 63 } 64 65 /** 66 * Used by applications to simulate a memory pressure signal. By throwing certain intent 67 * actions. 68 */ handleDebugIntent(Activity activity, String action)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); 79 } else { 80 return false; 81 } 82 83 return true; 84 } 85 maybeNotifyMemoryPresure(int level)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); 94 } 95 } 96 simulateLowMemoryPressureSignal(Activity activity)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(); 103 } 104 simulateTrimMemoryPressureSignal(Activity activity, int level)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); 111 } 112 nativeOnMemoryPressure(int memoryPressureType)113 private static native void nativeOnMemoryPressure(int memoryPressureType); 114 } 115