1c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn/* 2c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Copyright (C) 2006 The Android Open Source Project 3c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * 4c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License"); 5c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * you may not use this file except in compliance with the License. 6c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * You may obtain a copy of the License at 7c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * 8c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * http://www.apache.org/licenses/LICENSE-2.0 9c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * 10c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Unless required by applicable law or agreed to in writing, software 11c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS, 12c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * See the License for the specific language governing permissions and 14c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * limitations under the License. 15c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 16c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 17c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackbornpackage android.content; 18c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 19c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn/** 20c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Extended {@link ComponentCallbacks} interface with a new callback for 21c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * finer-grained memory management. 22c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 23c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackbornpublic interface ComponentCallbacks2 extends ComponentCallbacks { 24c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 25c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn /** 26c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Level for {@link #onTrimMemory(int)}: the process is nearing the end 27c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * of the background LRU list, and if more memory isn't found soon it will 28c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * be killed. 29c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 30c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn static final int TRIM_MEMORY_COMPLETE = 80; 31c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 32c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn /** 33c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Level for {@link #onTrimMemory(int)}: the process is around the middle 34c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * of the background LRU list; freeing memory can help the system keep 35c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * other processes running later in the list for better overall performance. 36c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 37c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn static final int TRIM_MEMORY_MODERATE = 60; 38c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 39c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn /** 40c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Level for {@link #onTrimMemory(int)}: the process has gone on to the 41c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * LRU list. This is a good opportunity to clean up resources that can 42c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * efficiently and quickly be re-built if the user returns to the app. 43c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 44c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn static final int TRIM_MEMORY_BACKGROUND = 40; 45c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 46c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn /** 47c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Level for {@link #onTrimMemory(int)}: the process had been showing 48c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * a user interface, and is no longer doing so. Large allocations with 49c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * the UI should be released at this point to allow memory to be better 50c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * managed. 51c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 52c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn static final int TRIM_MEMORY_UI_HIDDEN = 20; 53c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn 54c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn /** 5527ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * Level for {@link #onTrimMemory(int)}: the process is not an expendable 5627ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * background process, but the device is running extremely low on memory 5727ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * and is about to not be able to keep any background processes running. 5827ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * Your running process should free up as many non-critical resources as it 5927ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * can to allow that memory to be used elsewhere. The next thing that 6027ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * will happen after this is {@link #onLowMemory()} called to report that 6127ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * nothing at all can be kept in the background, a situation that can start 6227ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * to notably impact the user. 6327ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn */ 6427ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn static final int TRIM_MEMORY_RUNNING_CRITICAL = 15; 6527ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn 6627ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn /** 6727ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * Level for {@link #onTrimMemory(int)}: the process is not an expendable 6827ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * background process, but the device is running low on memory. 6927ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * Your running process should free up unneeded resources to allow that 7027ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * memory to be used elsewhere. 7127ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn */ 7227ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn static final int TRIM_MEMORY_RUNNING_LOW = 10; 7327ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn 7427ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn 7527ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn /** 7627ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * Level for {@link #onTrimMemory(int)}: the process is not an expendable 7727ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * background process, but the device is running moderately low on memory. 7827ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * Your running process may want to release some unneeded resources for 7927ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * use elsewhere. 8027ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn */ 8127ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn static final int TRIM_MEMORY_RUNNING_MODERATE = 5; 8227ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn 8327ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn /** 84c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * Called when the operating system has determined that it is a good 85c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * time for a process to trim unneeded memory from its process. This will 86c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * happen for example when it goes in the background and there is not enough 8727ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * memory to keep as many background processes running as desired. You 8827ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * should never compare to exact values of the level, since new intermediate 8927ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * values may be added -- you will typically want to compare if the value 9027ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * is greater or equal to a level you are interested in. 91c5bf7584422adca286c1f27a073df925c06f068dDianne Hackborn * 92c5bf7584422adca286c1f27a073df925c06f068dDianne Hackborn * <p>To retrieve the processes current trim level at any point, you can 93c5bf7584422adca286c1f27a073df925c06f068dDianne Hackborn * use {@link android.app.ActivityManager#getMyMemoryState 94c5bf7584422adca286c1f27a073df925c06f068dDianne Hackborn * ActivityManager.getMyMemoryState(RunningAppProcessInfo)}. 95c5bf7584422adca286c1f27a073df925c06f068dDianne Hackborn * 96c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * @param level The context of the trim, giving a hint of the amount of 97c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * trimming the application may like to perform. May be 98c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn * {@link #TRIM_MEMORY_COMPLETE}, {@link #TRIM_MEMORY_MODERATE}, 9927ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * {@link #TRIM_MEMORY_BACKGROUND}, {@link #TRIM_MEMORY_UI_HIDDEN}, 10027ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * {@link #TRIM_MEMORY_RUNNING_CRITICAL}, {@link #TRIM_MEMORY_RUNNING_LOW}, 10127ff913d56de8400083a13fc572e2812b32c890cDianne Hackborn * or {@link #TRIM_MEMORY_RUNNING_MODERATE}. 102c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn */ 103c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn void onTrimMemory(int level); 104c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn} 105