127b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel/*
227b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * Copyright (C) 2006 The Android Open Source Project
327b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *
427b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * Licensed under the Apache License, Version 2.0 (the "License");
527b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * you may not use this file except in compliance with the License.
627b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * You may obtain a copy of the License at
727b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *
827b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *      http://www.apache.org/licenses/LICENSE-2.0
927b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *
1027b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * Unless required by applicable law or agreed to in writing, software
1127b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * distributed under the License is distributed on an "AS IS" BASIS,
1227b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1327b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * See the License for the specific language governing permissions and
1427b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * limitations under the License.
1527b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel */
1627b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel
1727b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamelpackage android.content;
1827b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel
1927b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel/**
2027b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * Extended {@link ComponentCallbacks} interface with a new callback for
2127b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * finer-grained memory management. This interface is available in all application components
2227b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * ({@link android.app.Activity}, {@link android.app.Service},
2327b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * {@link ContentProvider}, and {@link android.app.Application}).
2427b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *
2527b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * <p>You should implement {@link #onTrimMemory} to incrementally release memory based on current
2627b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * system constraints. Using this callback to release your resources helps provide a more
2727b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * responsive system overall, but also directly benefits the user experience for
28f5c5d22c471f399f215662a8e471bf02b5b6bcfaDianne Hackborn * your app by allowing the system to keep your process alive longer. That is,
2927b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * if you <em>don't</em> trim your resources based on memory levels defined by this callback,
3027b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * the system is more likely to kill your process while it is cached in the least-recently used
3127b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * (LRU) list, thus requiring your app to restart and restore all state when the user returns to it.
3227b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *
33d49258fed489bcd72a2b2a6ce3a3c38d4b2531e6Dianne Hackborn * <p>The values provided by {@link #onTrimMemory} do not represent a single linear progression of
3427b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * memory limits, but provide you different types of clues about memory availability:</p>
3527b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * <ul>
367299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * <li>When your app is running:
377299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn *  <ol>
387299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn *  <li>{@link #TRIM_MEMORY_RUNNING_MODERATE} <br>The device is beginning to run low on memory.
397299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * Your app is running and not killable.
407299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn *  <li>{@link #TRIM_MEMORY_RUNNING_LOW} <br>The device is running much lower on memory.
4127b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * Your app is running and not killable, but please release unused resources to improve system
4227b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * performance (which directly impacts your app's performance).
4327b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *  <li>{@link #TRIM_MEMORY_RUNNING_CRITICAL} <br>The device is running extremely low on memory.
4427b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * Your app is not yet considered a killable process, but the system will begin killing
4527b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * background processes if apps do not release resources, so you should release non-critical
4627b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * resources now to prevent performance degradation.
4727b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *  </ol>
4827b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * </li>
4927b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * <li>When your app's visibility changes:
5027b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *  <ol>
5127b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *  <li>{@link #TRIM_MEMORY_UI_HIDDEN} <br>Your app's UI is no longer visible, so this is a good
5227b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * time to release large resources that are used only by your UI.
5327b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel *  </ol>
5427b28b3f62bd3b54fa13acd5d035940b9be464f3Tobias Haamel * </li>
55 * <li>When your app's process resides in the background LRU list:
56 *  <ol>
57 *  <li>{@link #TRIM_MEMORY_BACKGROUND} <br>The system is running low on memory and your process is
58 * near the beginning of the LRU list. Although your app process is not at a high risk of being
59 * killed, the system may already be killing processes in the LRU list, so you should release
60 * resources that are easy to recover so your process will remain in the list and resume
61 * quickly when the user returns to your app.
62 *  <li>{@link #TRIM_MEMORY_MODERATE} <br>The system is running low on memory and your process is
63 * near the middle of the LRU list. If the system becomes further constrained for memory, there's a
64 * chance your process will be killed.
65 *  <li>{@link #TRIM_MEMORY_COMPLETE} <br>The system is running low on memory and your process is
66 * one of the first to be killed if the system does not recover memory now. You should release
67 * absolutely everything that's not critical to resuming your app state.
68 *   <p>To support API levels lower than 14, you can use the {@link #onLowMemory} method as a
69 * fallback that's roughly equivalent to the {@link ComponentCallbacks2#TRIM_MEMORY_COMPLETE} level.
70 *  </li>
71 *  </ol>
72 * <p class="note"><strong>Note:</strong> When the system begins
73 * killing processes in the LRU list, although it primarily works bottom-up, it does give some
74 * consideration to which processes are consuming more memory and will thus provide more gains in
75 * memory if killed. So the less memory you consume while in the LRU list overall, the better
76 * your chances are to remain in the list and be able to quickly resume.</p>
77 * </li>
78 * </ul>
79 * <p>More information about the different stages of a process lifecycle (such as what it means
80 * to be placed in the background LRU list) is provided in the <a
81 * href="{@docRoot}guide/components/processes-and-threads.html#Lifecycle">Processes and Threads</a>
82 * document.
83 */
84public interface ComponentCallbacks2 extends ComponentCallbacks {
85
86    /**
87     * Level for {@link #onTrimMemory(int)}: the process is nearing the end
88     * of the background LRU list, and if more memory isn't found soon it will
89     * be killed.
90     */
91    static final int TRIM_MEMORY_COMPLETE = 80;
92
93    /**
94     * Level for {@link #onTrimMemory(int)}: the process is around the middle
95     * of the background LRU list; freeing memory can help the system keep
96     * other processes running later in the list for better overall performance.
97     */
98    static final int TRIM_MEMORY_MODERATE = 60;
99
100    /**
101     * Level for {@link #onTrimMemory(int)}: the process has gone on to the
102     * LRU list.  This is a good opportunity to clean up resources that can
103     * efficiently and quickly be re-built if the user returns to the app.
104     */
105    static final int TRIM_MEMORY_BACKGROUND = 40;
106
107    /**
108     * Level for {@link #onTrimMemory(int)}: the process had been showing
109     * a user interface, and is no longer doing so.  Large allocations with
110     * the UI should be released at this point to allow memory to be better
111     * managed.
112     */
113    static final int TRIM_MEMORY_UI_HIDDEN = 20;
114
115    /**
116     * Level for {@link #onTrimMemory(int)}: the process is not an expendable
117     * background process, but the device is running extremely low on memory
118     * and is about to not be able to keep any background processes running.
119     * Your running process should free up as many non-critical resources as it
120     * can to allow that memory to be used elsewhere.  The next thing that
121     * will happen after this is {@link #onLowMemory()} called to report that
122     * nothing at all can be kept in the background, a situation that can start
123     * to notably impact the user.
124     */
125    static final int TRIM_MEMORY_RUNNING_CRITICAL = 15;
126
127    /**
128     * Level for {@link #onTrimMemory(int)}: the process is not an expendable
129     * background process, but the device is running low on memory.
130     * Your running process should free up unneeded resources to allow that
131     * memory to be used elsewhere.
132     */
133    static final int TRIM_MEMORY_RUNNING_LOW = 10;
134
135
136    /**
137     * Level for {@link #onTrimMemory(int)}: the process is not an expendable
138     * background process, but the device is running moderately low on memory.
139     * Your running process may want to release some unneeded resources for
140     * use elsewhere.
141     */
142    static final int TRIM_MEMORY_RUNNING_MODERATE = 5;
143
144    /**
145     * Called when the operating system has determined that it is a good
146     * time for a process to trim unneeded memory from its process.  This will
147     * happen for example when it goes in the background and there is not enough
148     * memory to keep as many background processes running as desired.  You
149     * should never compare to exact values of the level, since new intermediate
150     * values may be added -- you will typically want to compare if the value
151     * is greater or equal to a level you are interested in.
152     *
153     * <p>To retrieve the processes current trim level at any point, you can
154     * use {@link android.app.ActivityManager#getMyMemoryState
155     * ActivityManager.getMyMemoryState(RunningAppProcessInfo)}.
156     *
157     * @param level The context of the trim, giving a hint of the amount of
158     * trimming the application may like to perform.  May be
159     * {@link #TRIM_MEMORY_COMPLETE}, {@link #TRIM_MEMORY_MODERATE},
160     * {@link #TRIM_MEMORY_BACKGROUND}, {@link #TRIM_MEMORY_UI_HIDDEN},
161     * {@link #TRIM_MEMORY_RUNNING_CRITICAL}, {@link #TRIM_MEMORY_RUNNING_LOW},
162     * or {@link #TRIM_MEMORY_RUNNING_MODERATE}.
163     */
164    void onTrimMemory(int level);
165}
166