blob: b38133901b30504a1b166b66fc9275b83618da05 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Winson Chung1af8eda2016-02-05 17:55:56 +000019import static java.lang.Character.MIN_VALUE;
20
Tor Norbyec615c6f2015-03-02 10:11:44 -080021import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
23import android.annotation.IdRes;
24import android.annotation.IntDef;
25import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070026import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070027import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070028import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070029import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070030import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070031import android.annotation.SystemApi;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070032import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040033import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070034import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070035import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050039import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070040import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070041import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070042import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.SharedPreferences;
44import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080045import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070046import android.content.pm.PackageManager;
47import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.res.Configuration;
49import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070050import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.database.Cursor;
52import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080054import android.graphics.Color;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.graphics.drawable.Drawable;
Clara Bayarrifcd7e802016-03-10 12:58:18 +000056import android.hardware.input.InputManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070058import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070060import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070061import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080065import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070066import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000067import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060069import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080070import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080071import android.os.SystemProperties;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070072import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000077import android.transition.Scene;
78import android.transition.TransitionManager;
79import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.util.EventLog;
82import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070083import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070084import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000086import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070087import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070089import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070091import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +000092import android.view.DragEvent;
Clara Bayarrifcd7e802016-03-10 12:58:18 +000093import android.view.InputDevice;
94import android.view.KeyCharacterMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +010096import android.view.KeyboardShortcutGroup;
97import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.LayoutInflater;
99import android.view.Menu;
100import android.view.MenuInflater;
101import android.view.MenuItem;
102import android.view.MotionEvent;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700103import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700105import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700107import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000109import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000111import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700113import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700114import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800116import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000117import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100118
Winson Chung1af8eda2016-02-05 17:55:56 +0000119import com.android.internal.app.IVoiceInteractor;
120import com.android.internal.app.ToolbarActionBar;
121import com.android.internal.app.WindowDecorActionBar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100122import com.android.internal.policy.PhoneWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Dianne Hackborn625ac272010-09-17 18:29:22 -0700124import java.io.FileDescriptor;
125import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700126import java.lang.annotation.Retention;
127import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700128import java.util.ArrayList;
129import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700130import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132/**
133 * An activity is a single, focused thing that the user can do. Almost all
134 * activities interact with the user, so the Activity class takes care of
135 * creating a window for you in which you can place your UI with
136 * {@link #setContentView}. While activities are often presented to the user
137 * as full-screen windows, they can also be used in other ways: as floating
138 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
139 * or embedded inside of another activity (using {@link ActivityGroup}).
140 *
141 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * <ul>
144 * <li> {@link #onCreate} is where you initialize your activity. Most
145 * importantly, here you will usually call {@link #setContentView(int)}
146 * with a layout resource defining your UI, and using {@link #findViewById}
147 * to retrieve the widgets in that UI that you need to interact with
148 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * <li> {@link #onPause} is where you deal with the user leaving your
151 * activity. Most importantly, any changes made by the user should at this
152 * point be committed (usually to the
153 * {@link android.content.ContentProvider} holding the data).
154 * </ul>
155 *
156 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
157 * activity classes must have a corresponding
158 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
159 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * <p>Topics covered here:
162 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700163 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
165 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
166 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
167 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
168 * <li><a href="#Permissions">Permissions</a>
169 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
170 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700171 *
172 * <div class="special reference">
173 * <h3>Developer Guides</h3>
174 * <p>The Activity class is an important part of an application's overall lifecycle,
175 * and the way activities are launched and put together is a fundamental
176 * part of the platform's application model. For a detailed perspective on the structure of an
177 * Android application and how activities behave, please read the
178 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700179 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700180 * developer guides.</p>
181 *
182 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700183 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700184 * developer guide.</p>
185 * </div>
186 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700187 * <a name="Fragments"></a>
188 * <h3>Fragments</h3>
189 *
190 * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
191 * implementations can make use of the {@link Fragment} class to better
192 * modularize their code, build more sophisticated user interfaces for larger
193 * screens, and help scale their application between small and large screens.
194 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <a name="ActivityLifecycle"></a>
196 * <h3>Activity Lifecycle</h3>
197 *
198 * <p>Activities in the system are managed as an <em>activity stack</em>.
199 * When a new activity is started, it is placed on the top of the stack
200 * and becomes the running activity -- the previous activity always remains
201 * below it in the stack, and will not come to the foreground again until
202 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <p>An activity has essentially four states:</p>
205 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700206 * <li> If an activity is in the foreground of the screen (at the top of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * the stack),
208 * it is <em>active</em> or <em>running</em>. </li>
209 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700210 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * is <em>paused</em>. A paused activity is completely alive (it
212 * maintains all state and member information and remains attached to
213 * the window manager), but can be killed by the system in extreme
214 * low memory situations.
215 * <li>If an activity is completely obscured by another activity,
216 * it is <em>stopped</em>. It still retains all state and member information,
217 * however, it is no longer visible to the user so its window is hidden
218 * and it will often be killed by the system when memory is needed
219 * elsewhere.</li>
220 * <li>If an activity is paused or stopped, the system can drop the activity
221 * from memory by either asking it to finish, or simply killing its
222 * process. When it is displayed again to the user, it must be
223 * completely restarted and restored to its previous state.</li>
224 * </ul>
225 *
226 * <p>The following diagram shows the important state paths of an Activity.
227 * The square rectangles represent callback methods you can implement to
228 * perform operations when the Activity moves between states. The colored
229 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <p><img src="../../../images/activity_lifecycle.png"
232 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * <p>There are three key loops you may be interested in monitoring within your
235 * activity:
RoboErik55011652014-07-09 15:05:53 -0700236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <ul>
238 * <li>The <b>entire lifetime</b> of an activity happens between the first call
239 * to {@link android.app.Activity#onCreate} through to a single final call
240 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
241 * of "global" state in onCreate(), and release all remaining resources in
242 * onDestroy(). For example, if it has a thread running in the background
243 * to download data from the network, it may create that thread in onCreate()
244 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <li>The <b>visible lifetime</b> of an activity happens between a call to
247 * {@link android.app.Activity#onStart} until a corresponding call to
248 * {@link android.app.Activity#onStop}. During this time the user can see the
249 * activity on-screen, though it may not be in the foreground and interacting
250 * with the user. Between these two methods you can maintain resources that
251 * are needed to show the activity to the user. For example, you can register
252 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900253 * that impact your UI, and unregister it in onStop() when the user no
254 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * can be called multiple times, as the activity becomes visible and hidden
256 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
259 * {@link android.app.Activity#onResume} until a corresponding call to
260 * {@link android.app.Activity#onPause}. During this time the activity is
261 * in front of all other activities and interacting with the user. An activity
262 * can frequently go between the resumed and paused states -- for example when
263 * the device goes to sleep, when an activity result is delivered, when a new
264 * intent is delivered -- so the code in these methods should be fairly
265 * lightweight.
266 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * <p>The entire lifecycle of an activity is defined by the following
269 * Activity methods. All of these are hooks that you can override
270 * to do appropriate work when the activity changes state. All
271 * activities will implement {@link android.app.Activity#onCreate}
272 * to do their initial setup; many will also implement
273 * {@link android.app.Activity#onPause} to commit changes to data and
274 * otherwise prepare to stop interacting with the user. You should always
275 * call up to your superclass when implementing these methods.</p>
276 *
277 * </p>
278 * <pre class="prettyprint">
279 * public class Activity extends ApplicationContext {
280 * protected void onCreate(Bundle savedInstanceState);
281 *
282 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * protected void onRestart();
285 *
286 * protected void onResume();
287 *
288 * protected void onPause();
289 *
290 * protected void onStop();
291 *
292 * protected void onDestroy();
293 * }
294 * </pre>
295 *
296 * <p>In general the movement through an activity's lifecycle looks like
297 * this:</p>
298 *
299 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
300 * <colgroup align="left" span="3" />
301 * <colgroup align="left" />
302 * <colgroup align="center" />
303 * <colgroup align="center" />
304 *
305 * <thead>
306 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
307 * </thead>
308 *
309 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700310 * <tr><td colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 * <td>Called when the activity is first created.
312 * This is where you should do all of your normal static set up:
313 * create views, bind data to lists, etc. This method also
314 * provides you with a Bundle containing the activity's previously
315 * frozen state, if there was one.
316 * <p>Always followed by <code>onStart()</code>.</td>
317 * <td align="center">No</td>
318 * <td align="center"><code>onStart()</code></td>
319 * </tr>
320 *
321 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700322 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 * <td>Called after your activity has been stopped, prior to it being
324 * started again.
325 * <p>Always followed by <code>onStart()</code></td>
326 * <td align="center">No</td>
327 * <td align="center"><code>onStart()</code></td>
328 * </tr>
329 *
smain@google.com10e29b82016-06-23 12:09:30 -0700330 * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * <td>Called when the activity is becoming visible to the user.
332 * <p>Followed by <code>onResume()</code> if the activity comes
333 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
334 * <td align="center">No</td>
335 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
336 * </tr>
337 *
338 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700339 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 * <td>Called when the activity will start
341 * interacting with the user. At this point your activity is at
342 * the top of the activity stack, with user input going to it.
343 * <p>Always followed by <code>onPause()</code>.</td>
344 * <td align="center">No</td>
345 * <td align="center"><code>onPause()</code></td>
346 * </tr>
347 *
smain@google.com10e29b82016-06-23 12:09:30 -0700348 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * <td>Called when the system is about to start resuming a previous
350 * activity. This is typically used to commit unsaved changes to
351 * persistent data, stop animations and other things that may be consuming
352 * CPU, etc. Implementations of this method must be very quick because
353 * the next activity will not be resumed until this method returns.
354 * <p>Followed by either <code>onResume()</code> if the activity
355 * returns back to the front, or <code>onStop()</code> if it becomes
356 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800357 * <td align="center"><font color="#800000"><strong>Pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB}</strong></font></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * <td align="center"><code>onResume()</code> or<br>
359 * <code>onStop()</code></td>
360 * </tr>
361 *
smain@google.com10e29b82016-06-23 12:09:30 -0700362 * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 * <td>Called when the activity is no longer visible to the user, because
364 * another activity has been resumed and is covering this one. This
365 * may happen either because a new activity is being started, an existing
366 * one is being brought in front of this one, or this one is being
367 * destroyed.
368 * <p>Followed by either <code>onRestart()</code> if
369 * this activity is coming back to interact with the user, or
370 * <code>onDestroy()</code> if this activity is going away.</td>
371 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
372 * <td align="center"><code>onRestart()</code> or<br>
373 * <code>onDestroy()</code></td>
374 * </tr>
375 *
smain@google.com10e29b82016-06-23 12:09:30 -0700376 * <tr><td colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * <td>The final call you receive before your
378 * activity is destroyed. This can happen either because the
379 * activity is finishing (someone called {@link Activity#finish} on
380 * it, or because the system is temporarily destroying this
381 * instance of the activity to save space. You can distinguish
382 * between these two scenarios with the {@link
383 * Activity#isFinishing} method.</td>
384 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
385 * <td align="center"><em>nothing</em></td>
386 * </tr>
387 * </tbody>
388 * </table>
389 *
390 * <p>Note the "Killable" column in the above table -- for those methods that
391 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800392 * activity may be killed by the system <em>at any time</em> without another line
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * of its code being executed. Because of this, you should use the
394 * {@link #onPause} method to write any persistent data (such as user edits)
395 * to storage. In addition, the method
396 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
397 * in such a background state, allowing you to save away any dynamic instance
398 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700399 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
401 * section for more information on how the lifecycle of a process is tied
402 * to the activities it is hosting. Note that it is important to save
403 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800404 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * be called in every situation as described in its documentation.</p>
406 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800407 * <p class="note">Be aware that these semantics will change slightly between
408 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
409 * vs. those targeting prior platforms. Starting with Honeycomb, an application
410 * is not in the killable state until its {@link #onStop} has returned. This
411 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
412 * safely called after {@link #onPause()} and allows and application to safely
413 * wait until {@link #onStop()} to save persistent state.</p>
414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * <p>For those methods that are not marked as being killable, the activity's
416 * process will not be killed by the system starting from the time the method
417 * is called and continuing after it returns. Thus an activity is in the killable
418 * state, for example, between after <code>onPause()</code> to the start of
419 * <code>onResume()</code>.</p>
420 *
421 * <a name="ConfigurationChanges"></a>
422 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * <p>If the configuration of the device (as defined by the
425 * {@link Configuration Resources.Configuration} class) changes,
426 * then anything displaying a user interface will need to update to match that
427 * configuration. Because Activity is the primary mechanism for interacting
428 * with the user, it includes special support for handling configuration
429 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * <p>Unless you specify otherwise, a configuration change (such as a change
432 * in screen orientation, language, input devices, etc) will cause your
433 * current activity to be <em>destroyed</em>, going through the normal activity
434 * lifecycle process of {@link #onPause},
435 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
436 * had been in the foreground or visible to the user, once {@link #onDestroy} is
437 * called in that instance then a new instance of the activity will be
438 * created, with whatever savedInstanceState the previous instance had generated
439 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700440 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * <p>This is done because any application resource,
442 * including layout files, can change based on any configuration value. Thus
443 * the only safe way to handle a configuration change is to re-retrieve all
444 * resources, including layouts, drawables, and strings. Because activities
445 * must already know how to save their state and re-create themselves from
446 * that state, this is a convenient way to have an activity restart itself
447 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * <p>In some special cases, you may want to bypass restarting of your
450 * activity based on one or more types of configuration changes. This is
451 * done with the {@link android.R.attr#configChanges android:configChanges}
452 * attribute in its manifest. For any types of configuration changes you say
453 * that you handle there, you will receive a call to your current activity's
454 * {@link #onConfigurationChanged} method instead of being restarted. If
455 * a configuration change involves any that you do not handle, however, the
456 * activity will still be restarted and {@link #onConfigurationChanged}
457 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * <a name="StartingActivities"></a>
460 * <h3>Starting Activities and Getting Results</h3>
461 *
462 * <p>The {@link android.app.Activity#startActivity}
463 * method is used to start a
464 * new activity, which will be placed at the top of the activity stack. It
465 * takes a single argument, an {@link android.content.Intent Intent},
466 * which describes the activity
467 * to be executed.</p>
468 *
469 * <p>Sometimes you want to get a result back from an activity when it
470 * ends. For example, you may start an activity that lets the user pick
471 * a person in a list of contacts; when it ends, it returns the person
472 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700473 * {@link android.app.Activity#startActivityForResult(Intent, int)}
474 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700476 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 *
478 * <p>When an activity exits, it can call
479 * {@link android.app.Activity#setResult(int)}
480 * to return data back to its parent. It must always supply a result code,
481 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
482 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
483 * return back an Intent containing any additional data it wants. All of this
484 * information appears back on the
485 * parent's <code>Activity.onActivityResult()</code>, along with the integer
486 * identifier it originally supplied.</p>
487 *
488 * <p>If a child activity fails for any reason (such as crashing), the parent
489 * activity will receive a result with the code RESULT_CANCELED.</p>
490 *
491 * <pre class="prettyprint">
492 * public class MyActivity extends Activity {
493 * ...
494 *
495 * static final int PICK_CONTACT_REQUEST = 0;
496 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700497 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
499 * // When the user center presses, let them pick a contact.
500 * startActivityForResult(
501 * new Intent(Intent.ACTION_PICK,
502 * new Uri("content://contacts")),
503 * PICK_CONTACT_REQUEST);
504 * return true;
505 * }
506 * return false;
507 * }
508 *
509 * protected void onActivityResult(int requestCode, int resultCode,
510 * Intent data) {
511 * if (requestCode == PICK_CONTACT_REQUEST) {
512 * if (resultCode == RESULT_OK) {
513 * // A contact was picked. Here we will just display it
514 * // to the user.
515 * startActivity(new Intent(Intent.ACTION_VIEW, data));
516 * }
517 * }
518 * }
519 * }
520 * </pre>
521 *
522 * <a name="SavingPersistentState"></a>
523 * <h3>Saving Persistent State</h3>
524 *
525 * <p>There are generally two kinds of persistent state than an activity
526 * will deal with: shared document-like data (typically stored in a SQLite
527 * database using a {@linkplain android.content.ContentProvider content provider})
528 * and internal state such as user preferences.</p>
529 *
530 * <p>For content provider data, we suggest that activities use a
531 * "edit in place" user model. That is, any edits a user makes are effectively
532 * made immediately without requiring an additional confirmation step.
533 * Supporting this model is generally a simple matter of following two rules:</p>
534 *
535 * <ul>
536 * <li> <p>When creating a new document, the backing database entry or file for
537 * it is created immediately. For example, if the user chooses to write
538 * a new e-mail, a new entry for that e-mail is created as soon as they
539 * start entering data, so that if they go to any other activity after
540 * that point this e-mail will now appear in the list of drafts.</p>
541 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
542 * commit to the backing content provider or file any changes the user
543 * has made. This ensures that those changes will be seen by any other
544 * activity that is about to run. You will probably want to commit
545 * your data even more aggressively at key times during your
546 * activity's lifecycle: for example before starting a new
547 * activity, before finishing your own activity, when the user
548 * switches between input fields, etc.</p>
549 * </ul>
550 *
551 * <p>This model is designed to prevent data loss when a user is navigating
552 * between activities, and allows the system to safely kill an activity (because
553 * system resources are needed somewhere else) at any time after it has been
554 * paused. Note this implies
555 * that the user pressing BACK from your activity does <em>not</em>
556 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800557 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
559 *
560 * <p>See the {@linkplain android.content.ContentProvider content package} for
561 * more information about content providers. These are a key aspect of how
562 * different activities invoke and propagate data between themselves.</p>
563 *
564 * <p>The Activity class also provides an API for managing internal persistent state
565 * associated with an activity. This can be used, for example, to remember
566 * the user's preferred initial display in a calendar (day view or week view)
567 * or the user's default home page in a web browser.</p>
568 *
569 * <p>Activity persistent state is managed
570 * with the method {@link #getPreferences},
571 * allowing you to retrieve and
572 * modify a set of name/value pairs associated with the activity. To use
573 * preferences that are shared across multiple application components
574 * (activities, receivers, services, providers), you can use the underlying
575 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
576 * to retrieve a preferences
577 * object stored under a specific name.
578 * (Note that it is not possible to share settings data across application
579 * packages -- for that you will need a content provider.)</p>
580 *
581 * <p>Here is an excerpt from a calendar activity that stores the user's
582 * preferred view mode in its persistent settings:</p>
583 *
584 * <pre class="prettyprint">
585 * public class CalendarActivity extends Activity {
586 * ...
587 *
588 * static final int DAY_VIEW_MODE = 0;
589 * static final int WEEK_VIEW_MODE = 1;
590 *
591 * private SharedPreferences mPrefs;
592 * private int mCurViewMode;
593 *
594 * protected void onCreate(Bundle savedInstanceState) {
595 * super.onCreate(savedInstanceState);
596 *
597 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700598 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * }
600 *
601 * protected void onPause() {
602 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 * SharedPreferences.Editor ed = mPrefs.edit();
605 * ed.putInt("view_mode", mCurViewMode);
606 * ed.commit();
607 * }
608 * }
609 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * <a name="Permissions"></a>
612 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * <p>The ability to start a particular Activity can be enforced when it is
615 * declared in its
616 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
617 * tag. By doing so, other applications will need to declare a corresponding
618 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
619 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800620 *
621 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
622 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
623 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
624 * Activity access to the specific URIs in the Intent. Access will remain
625 * until the Activity has finished (it will remain across the hosting
626 * process being killed and other temporary destruction). As of
627 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
628 * was already created and a new Intent is being delivered to
629 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
630 * to the existing ones it holds.
631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
633 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * <a name="ProcessLifecycle"></a>
636 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 * <p>The Android system attempts to keep application process around for as
639 * long as possible, but eventually will need to remove old processes when
640 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
641 * Lifecycle</a>, the decision about which process to remove is intimately
642 * tied to the state of the user's interaction with it. In general, there
643 * are four states a process can be in based on the activities running in it,
644 * listed here in order of importance. The system will kill less important
645 * processes (the last ones) before it resorts to killing more important
646 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700647 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * <ol>
649 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
650 * that the user is currently interacting with) is considered the most important.
651 * Its process will only be killed as a last resort, if it uses more memory
652 * than is available on the device. Generally at this point the device has
653 * reached a memory paging state, so this is required in order to keep the user
654 * interface responsive.
655 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
656 * but not in the foreground, such as one sitting behind a foreground dialog)
657 * is considered extremely important and will not be killed unless that is
658 * required to keep the foreground activity running.
659 * <li> <p>A <b>background activity</b> (an activity that is not visible to
660 * the user and has been paused) is no longer critical, so the system may
661 * safely kill its process to reclaim memory for other foreground or
662 * visible processes. If its process needs to be killed, when the user navigates
663 * back to the activity (making it visible on the screen again), its
664 * {@link #onCreate} method will be called with the savedInstanceState it had previously
665 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
666 * state as the user last left it.
667 * <li> <p>An <b>empty process</b> is one hosting no activities or other
668 * application components (such as {@link Service} or
669 * {@link android.content.BroadcastReceiver} classes). These are killed very
670 * quickly by the system as memory becomes low. For this reason, any
671 * background operation you do outside of an activity must be executed in the
672 * context of an activity BroadcastReceiver or Service to ensure that the system
673 * knows it needs to keep your process around.
674 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * <p>Sometimes an Activity may need to do a long-running operation that exists
677 * independently of the activity lifecycle itself. An example may be a camera
678 * application that allows you to upload a picture to a web site. The upload
679 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700680 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * should start a {@link Service} in which the upload takes place. This allows
682 * the system to properly prioritize your process (considering it to be more
683 * important than other non-visible applications) for the duration of the
684 * upload, independent of whether the original activity is paused, stopped,
685 * or finished.
686 */
687public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700688 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700690 OnCreateContextMenuListener, ComponentCallbacks2,
Skuhnece2faa52015-08-11 10:36:38 -0700691 Window.OnWindowDismissedCallback, WindowControllerCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700693 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694
695 /** Standard activity result: operation canceled. */
696 public static final int RESULT_CANCELED = 0;
697 /** Standard activity result: operation succeeded. */
698 public static final int RESULT_OK = -1;
699 /** Start of user-defined activity results. */
700 public static final int RESULT_FIRST_USER = 1;
701
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700702 /** @hide Task isn't finished when activity is finished */
703 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700704 /**
705 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
706 * past behavior the task is also removed from recents.
707 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700708 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700709 /**
710 * @hide Task is finished along with the finishing activity, but it is not removed from
711 * recents.
712 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700713 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
714
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700715 static final String FRAGMENTS_TAG = "android:fragments";
716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
718 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
719 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
720 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800721 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700722 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
723 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
Svetoslav970b59c2015-06-09 16:05:21 -0700725 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
726
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100727 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100728
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800729 private static class ManagedDialog {
730 Dialog mDialog;
731 Bundle mArgs;
732 }
733 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
735 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
736 private Instrumentation mInstrumentation;
737 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700738 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 /*package*/ String mEmbeddedID;
740 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700741 /*package*/ Intent mIntent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800742 /*package*/ String mReferrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 private ComponentName mComponent;
744 /*package*/ ActivityInfo mActivityInfo;
745 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 Activity mParent;
747 boolean mCalled;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600748 /*package*/ boolean mResumed;
Andrii Kulian58178f22016-03-16 13:44:56 -0700749 /*package*/ boolean mStopped;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 boolean mFinished;
751 boolean mStartedActivity;
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700752 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700753 private boolean mDoReportFullyDrawn = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700754 /** true if the activity is going through a transient pause */
755 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500756 /** true if the activity is being destroyed in order to recreate it with a new configuration */
757 /*package*/ boolean mChangingConfigurations = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 /*package*/ int mConfigChangeFlags;
759 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100760 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700761 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700763 static final class NonConfigurationInstances {
764 Object activity;
765 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800766 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700767 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700768 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700769 }
770 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 private Window mWindow;
773
774 private WindowManager mWindowManager;
775 /*package*/ View mDecor = null;
776 /*package*/ boolean mWindowAdded = false;
777 /*package*/ boolean mVisibleFromServer = false;
778 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700779 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700780 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781
Dianne Hackborn91097de2014-04-04 18:02:06 -0700782 private VoiceInteractor mVoiceInteractor;
783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 private CharSequence mTitle;
785 private int mTitleColor = 0;
786
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700787 // we must have a handler before the FragmentController is constructed
788 final Handler mHandler = new Handler();
789 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700790
Jose Lima4b6c6692014-08-12 17:41:12 -0700791 // Most recent call to requestVisibleBehind().
792 boolean mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 private static final class ManagedCursor {
795 ManagedCursor(Cursor cursor) {
796 mCursor = cursor;
797 mReleased = false;
798 mUpdated = false;
799 }
800
801 private final Cursor mCursor;
802 private boolean mReleased;
803 private boolean mUpdated;
804 }
805 private final ArrayList<ManagedCursor> mManagedCursors =
806 new ArrayList<ManagedCursor>();
807
RoboErik55011652014-07-09 15:05:53 -0700808 // protected by synchronized (this)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 int mResultCode = RESULT_CANCELED;
810 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700811
Craig Mautner5eda9b32013-07-02 11:58:16 -0700812 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700813 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700815 private SearchEvent mSearchEvent;
816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000818 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
821 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700822
Winsonb6403152016-02-23 13:32:09 -0800823 private ActivityManager.TaskDescription mTaskDescription =
824 new ActivityManager.TaskDescription();
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
827
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700828 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700829 private final Object mInstanceTracker = StrictMode.trackActivity(this);
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700832
George Mount1fecfb22014-06-18 14:55:55 -0700833 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700834 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
835 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
Svetoslavffb32b12015-10-15 16:54:00 -0700837 private boolean mHasCurrentPermissionsRequest;
George Mount5beb26172015-12-15 13:36:01 -0800838 private boolean mEatKeyUpEvent;
Svetoslavffb32b12015-10-15 16:54:00 -0700839
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800840 private static native String getDlWarning();
841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 /** Return the intent that started this activity. */
843 public Intent getIntent() {
844 return mIntent;
845 }
846
RoboErik55011652014-07-09 15:05:53 -0700847 /**
848 * Change the intent returned by {@link #getIntent}. This holds a
849 * reference to the given intent; it does not copy it. Often used in
850 * conjunction with {@link #onNewIntent}.
851 *
852 * @param newIntent The new Intent object to return from getIntent
853 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 * @see #getIntent
855 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700856 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 public void setIntent(Intent newIntent) {
858 mIntent = newIntent;
859 }
860
861 /** Return the application that owns this activity. */
862 public final Application getApplication() {
863 return mApplication;
864 }
865
866 /** Is this activity embedded inside of another activity? */
867 public final boolean isChild() {
868 return mParent != null;
869 }
RoboErik55011652014-07-09 15:05:53 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 /** Return the parent activity if this view is an embedded child. */
872 public final Activity getParent() {
873 return mParent;
874 }
875
876 /** Retrieve the window manager for showing custom windows. */
877 public WindowManager getWindowManager() {
878 return mWindowManager;
879 }
880
881 /**
882 * Retrieve the current {@link android.view.Window} for the activity.
883 * This can be used to directly access parts of the Window API that
884 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700885 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 * @return Window The current window, or null if the activity is not
887 * visual.
888 */
889 public Window getWindow() {
890 return mWindow;
891 }
892
893 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800894 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700895 */
896 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700897 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700898 }
RoboErik55011652014-07-09 15:05:53 -0700899
Dianne Hackbornc8017682010-07-06 13:34:38 -0700900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * Calls {@link android.view.Window#getCurrentFocus} on the
902 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700905 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 * @see #getWindow
907 * @see android.view.Window#getCurrentFocus
908 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700909 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 public View getCurrentFocus() {
911 return mWindow != null ? mWindow.getCurrentFocus() : null;
912 }
913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 /**
915 * Called when the activity is starting. This is where most initialization
916 * should go: calling {@link #setContentView(int)} to inflate the
917 * activity's UI, using {@link #findViewById} to programmatically interact
918 * with widgets in the UI, calling
919 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
920 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 * <p>You can call {@link #finish} from within this function, in
923 * which case onDestroy() will be immediately called without any of the rest
924 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
925 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700926 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 * <p><em>Derived classes must call through to the super class's
928 * implementation of this method. If they do not, an exception will be
929 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700930 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 * @param savedInstanceState If the activity is being re-initialized after
932 * previously being shut down then this Bundle contains the data it most
933 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700934 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 * @see #onStart
936 * @see #onSaveInstanceState
937 * @see #onRestoreInstanceState
938 * @see #onPostCreate
939 */
Tor Norbye83c68962015-03-10 20:55:31 -0700940 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -0800941 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -0700942 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700943 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700944 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700945 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700946 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700947 if (mActivityInfo.parentActivityName != null) {
948 if (mActionBar == null) {
949 mEnableDefaultActionBarUp = true;
950 } else {
951 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
952 }
953 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700954 if (savedInstanceState != null) {
955 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
956 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
957 ? mLastNonConfigurationInstances.fragments : null);
958 }
959 mFragments.dispatchCreate();
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700960 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -0700961 if (mVoiceInteractor != null) {
962 mVoiceInteractor.attachActivity(this);
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 mCalled = true;
965 }
966
967 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700968 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -0700969 * the attribute {@link android.R.attr#persistableMode} set to
970 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -0700971 *
972 * @param savedInstanceState if the activity is being re-initialized after
973 * previously being shut down then this Bundle contains the data it most
974 * recently supplied in {@link #onSaveInstanceState}.
975 * <b><i>Note: Otherwise it is null.</i></b>
976 * @param persistentState if the activity is being re-initialized after
977 * previously being shut down or powered off then this Bundle contains the data it most
978 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
979 * <b><i>Note: Otherwise it is null.</i></b>
980 *
981 * @see #onCreate(android.os.Bundle)
982 * @see #onStart
983 * @see #onSaveInstanceState
984 * @see #onRestoreInstanceState
985 * @see #onPostCreate
986 */
Craig Mautner7a629c22014-09-04 14:57:04 -0700987 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -0700988 @Nullable PersistableBundle persistentState) {
989 onCreate(savedInstanceState);
990 }
991
992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 * The hook for {@link ActivityThread} to restore the state of this activity.
994 *
995 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
996 * {@link #restoreManagedDialogs(android.os.Bundle)}.
997 *
998 * @param savedInstanceState contains the saved state
999 */
1000 final void performRestoreInstanceState(Bundle savedInstanceState) {
1001 onRestoreInstanceState(savedInstanceState);
1002 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004
1005 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001006 * The hook for {@link ActivityThread} to restore the state of this activity.
1007 *
1008 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1009 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1010 *
1011 * @param savedInstanceState contains the saved state
1012 * @param persistentState contains the persistable saved state
1013 */
1014 final void performRestoreInstanceState(Bundle savedInstanceState,
1015 PersistableBundle persistentState) {
1016 onRestoreInstanceState(savedInstanceState, persistentState);
1017 if (savedInstanceState != null) {
1018 restoreManagedDialogs(savedInstanceState);
1019 }
1020 }
1021
1022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * This method is called after {@link #onStart} when the activity is
1024 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001025 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 * to restore their state, but it is sometimes convenient to do it here
1027 * after all of the initialization has been done or to allow subclasses to
1028 * decide whether to use your default implementation. The default
1029 * implementation of this method performs a restore of any view state that
1030 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001031 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * <p>This method is called between {@link #onStart} and
1033 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001036 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 * @see #onCreate
1038 * @see #onPostCreate
1039 * @see #onResume
1040 * @see #onSaveInstanceState
1041 */
1042 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1043 if (mWindow != null) {
1044 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1045 if (windowState != null) {
1046 mWindow.restoreHierarchyState(windowState);
1047 }
1048 }
1049 }
Craig Mautnera0026042014-04-23 11:45:37 -07001050
1051 /**
1052 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001053 * created with the attribute {@link android.R.attr#persistableMode} set to
1054 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1055 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001056 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1057 *
1058 * <p>This method is called between {@link #onStart} and
1059 * {@link #onPostCreate}.
1060 *
1061 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1062 *
1063 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1064 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1065 *
1066 * @see #onRestoreInstanceState(Bundle)
1067 * @see #onCreate
1068 * @see #onPostCreate
1069 * @see #onResume
1070 * @see #onSaveInstanceState
1071 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001072 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001073 PersistableBundle persistentState) {
1074 if (savedInstanceState != null) {
1075 onRestoreInstanceState(savedInstanceState);
1076 }
1077 }
1078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 /**
1080 * Restore the state of any saved managed dialogs.
1081 *
1082 * @param savedInstanceState The bundle to restore from.
1083 */
1084 private void restoreManagedDialogs(Bundle savedInstanceState) {
1085 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1086 if (b == null) {
1087 return;
1088 }
1089
1090 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1091 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001092 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 for (int i = 0; i < numDialogs; i++) {
1094 final Integer dialogId = ids[i];
1095 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1096 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001097 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1098 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001099 final ManagedDialog md = new ManagedDialog();
1100 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1101 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1102 if (md.mDialog != null) {
1103 mManagedDialogs.put(dialogId, md);
1104 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1105 md.mDialog.onRestoreInstanceState(dialogState);
1106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108 }
1109 }
1110
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001111 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1112 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001113 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001114 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001115 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001116 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001117 return dialog;
1118 }
1119
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001120 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 return SAVED_DIALOG_KEY_PREFIX + key;
1122 }
1123
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001124 private static String savedDialogArgsKeyFor(int key) {
1125 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127
1128 /**
1129 * Called when activity start-up is complete (after {@link #onStart}
1130 * and {@link #onRestoreInstanceState} have been called). Applications will
1131 * generally not implement this method; it is intended for system
1132 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 * <p><em>Derived classes must call through to the super class's
1135 * implementation of this method. If they do not, an exception will be
1136 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 * @param savedInstanceState If the activity is being re-initialized after
1139 * previously being shut down then this Bundle contains the data it most
1140 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1141 * @see #onCreate
1142 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001143 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001144 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 if (!isChild()) {
1146 mTitleReady = true;
1147 onTitleChanged(getTitle(), getTitleColor());
1148 }
Winsonb6403152016-02-23 13:32:09 -08001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 mCalled = true;
1151 }
1152
1153 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001154 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001155 * created with the attribute {@link android.R.attr#persistableMode} set to
1156 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001157 *
1158 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1159 * @param persistentState The data caming from the PersistableBundle first
1160 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1161 *
1162 * @see #onCreate
1163 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001164 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001165 @Nullable PersistableBundle persistentState) {
1166 onPostCreate(savedInstanceState);
1167 }
1168
1169 /**
RoboErik55011652014-07-09 15:05:53 -07001170 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1171 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001172 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 *
1174 * <p><em>Derived classes must call through to the super class's
1175 * implementation of this method. If they do not, an exception will be
1176 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001177 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 * @see #onCreate
1179 * @see #onStop
1180 * @see #onResume
1181 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001182 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001184 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001186
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001187 mFragments.doLoaderStart();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001188
1189 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191
1192 /**
1193 * Called after {@link #onStop} when the current activity is being
1194 * re-displayed to the user (the user has navigated back to it). It will
1195 * be followed by {@link #onStart} and then {@link #onResume}.
1196 *
1197 * <p>For activities that are using raw {@link Cursor} objects (instead of
1198 * creating them through
1199 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1200 * this is usually the place
1201 * where the cursor should be requeried (because you had deactivated it in
1202 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 * <p><em>Derived classes must call through to the super class's
1205 * implementation of this method. If they do not, an exception will be
1206 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 * @see #onStop
1209 * @see #onStart
1210 * @see #onResume
1211 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001212 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 protected void onRestart() {
1214 mCalled = true;
1215 }
1216
1217 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001218 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1219 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1220 * to give the activity a hint that its state is no longer saved -- it will generally
1221 * be called after {@link #onSaveInstanceState} and prior to the activity being
1222 * resumed/started again.
1223 */
1224 public void onStateNotSaved() {
1225 }
1226
1227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1229 * {@link #onPause}, for your activity to start interacting with the user.
1230 * This is a good place to begin animations, open exclusive-access devices
1231 * (such as the camera), etc.
1232 *
1233 * <p>Keep in mind that onResume is not the best indicator that your activity
1234 * is visible to the user; a system window such as the keyguard may be in
1235 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1236 * activity is visible to the user (for example, to resume a game).
1237 *
1238 * <p><em>Derived classes must call through to the super class's
1239 * implementation of this method. If they do not, an exception will be
1240 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 * @see #onRestoreInstanceState
1243 * @see #onRestart
1244 * @see #onPostResume
1245 * @see #onPause
1246 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001247 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001249 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001250 getApplication().dispatchActivityResumed(this);
George Mountbdc4d8d2016-03-23 13:10:13 -07001251 mActivityTransitionState.onResume(this, isTopOfTask());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 mCalled = true;
1253 }
1254
1255 /**
1256 * Called when activity resume is complete (after {@link #onResume} has
1257 * been called). Applications will generally not implement this method;
1258 * it is intended for system classes to do final setup after application
1259 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001260 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 * <p><em>Derived classes must call through to the super class's
1262 * implementation of this method. If they do not, an exception will be
1263 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 * @see #onResume
1266 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001267 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 protected void onPostResume() {
1269 final Window win = getWindow();
1270 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001271 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 mCalled = true;
1273 }
1274
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001275 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001276 if (mVoiceInteractor != null) {
1277 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1278 activeRequest.cancel();
1279 activeRequest.clear();
1280 }
1281 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001282 if (voiceInteractor == null) {
1283 mVoiceInteractor = null;
1284 } else {
1285 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1286 Looper.myLooper());
1287 }
1288 }
1289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001291 * Check whether this activity is running as part of a voice interaction with the user.
1292 * If true, it should perform its interaction with the user through the
1293 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1294 */
1295 public boolean isVoiceInteraction() {
1296 return mVoiceInteractor != null;
1297 }
1298
1299 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001300 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1301 * of a voice interaction. That is, returns true if this activity was directly
1302 * started by the voice interaction service as the initiation of a voice interaction.
1303 * Otherwise, for example if it was started by another activity while under voice
1304 * interaction, returns false.
1305 */
1306 public boolean isVoiceInteractionRoot() {
1307 try {
1308 return mVoiceInteractor != null
1309 && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken);
1310 } catch (RemoteException e) {
1311 }
1312 return false;
1313 }
1314
1315 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001316 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1317 * interact with this activity.
1318 */
1319 public VoiceInteractor getVoiceInteractor() {
1320 return mVoiceInteractor;
1321 }
1322
1323 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001324 * Queries whether the currently enabled voice interaction service supports returning
1325 * a voice interactor for use by the activity. This is valid only for the duration of the
1326 * activity.
1327 *
1328 * @return whether the current voice interaction service supports local voice interaction
1329 */
1330 public boolean isLocalVoiceInteractionSupported() {
1331 try {
1332 return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction();
1333 } catch (RemoteException re) {
1334 }
1335 return false;
1336 }
1337
1338 /**
1339 * Starts a local voice interaction session. When ready,
1340 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1341 * to the registered voice interaction service.
1342 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1343 */
1344 public void startLocalVoiceInteraction(Bundle privateOptions) {
1345 try {
1346 ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions);
1347 } catch (RemoteException re) {
1348 }
1349 }
1350
1351 /**
1352 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1353 * voice interaction session being started. You can now retrieve a voice interactor using
1354 * {@link #getVoiceInteractor()}.
1355 */
1356 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001357 }
1358
1359 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001360 * Callback to indicate that the local voice interaction has stopped either
1361 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1362 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1363 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001364 */
1365 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001366 }
1367
1368 /**
1369 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001370 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1371 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001372 */
1373 public void stopLocalVoiceInteraction() {
1374 try {
1375 ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken);
1376 } catch (RemoteException re) {
1377 }
1378 }
1379
1380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 * This is called for activities that set launchMode to "singleTop" in
1382 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1383 * flag when calling {@link #startActivity}. In either case, when the
1384 * activity is re-launched while at the top of the activity stack instead
1385 * of a new instance of the activity being started, onNewIntent() will be
1386 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001387 * re-launch it.
1388 *
1389 * <p>An activity will always be paused before receiving a new intent, so
1390 * you can count on {@link #onResume} being called after this method.
1391 *
1392 * <p>Note that {@link #getIntent} still returns the original Intent. You
1393 * can use {@link #setIntent} to update it to this new Intent.
1394 *
1395 * @param intent The new intent that was started for the activity.
1396 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001398 * @see #setIntent
1399 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 */
1401 protected void onNewIntent(Intent intent) {
1402 }
1403
1404 /**
1405 * The hook for {@link ActivityThread} to save the state of this activity.
1406 *
1407 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1408 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1409 *
1410 * @param outState The bundle to save the state to.
1411 */
1412 final void performSaveInstanceState(Bundle outState) {
1413 onSaveInstanceState(outState);
1414 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001415 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001416 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001417 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419
1420 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001421 * The hook for {@link ActivityThread} to save the state of this activity.
1422 *
1423 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1424 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1425 *
1426 * @param outState The bundle to save the state to.
1427 * @param outPersistentState The bundle to save persistent state to.
1428 */
1429 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1430 onSaveInstanceState(outState, outPersistentState);
1431 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001432 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001433 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1434 ", " + outPersistentState);
1435 }
1436
1437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 * Called to retrieve per-instance state from an activity before being killed
1439 * so that the state can be restored in {@link #onCreate} or
1440 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1441 * will be passed to both).
1442 *
1443 * <p>This method is called before an activity may be killed so that when it
1444 * comes back some time in the future it can restore its state. For example,
1445 * if activity B is launched in front of activity A, and at some point activity
1446 * A is killed to reclaim resources, activity A will have a chance to save the
1447 * current state of its user interface via this method so that when the user
1448 * returns to activity A, the state of the user interface can be restored
1449 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1450 *
1451 * <p>Do not confuse this method with activity lifecycle callbacks such as
1452 * {@link #onPause}, which is always called when an activity is being placed
1453 * in the background or on its way to destruction, or {@link #onStop} which
1454 * is called before destruction. One example of when {@link #onPause} and
1455 * {@link #onStop} is called and not this method is when a user navigates back
1456 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1457 * on B because that particular instance will never be restored, so the
1458 * system avoids calling it. An example when {@link #onPause} is called and
1459 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1460 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1461 * killed during the lifetime of B since the state of the user interface of
1462 * A will stay intact.
1463 *
1464 * <p>The default implementation takes care of most of the UI per-instance
1465 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1466 * view in the hierarchy that has an id, and by saving the id of the currently
1467 * focused view (all of which is restored by the default implementation of
1468 * {@link #onRestoreInstanceState}). If you override this method to save additional
1469 * information not captured by each individual view, you will likely want to
1470 * call through to the default implementation, otherwise be prepared to save
1471 * all of the state of each view yourself.
1472 *
1473 * <p>If called, this method will occur before {@link #onStop}. There are
1474 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 * @see #onCreate
1479 * @see #onRestoreInstanceState
1480 * @see #onPause
1481 */
1482 protected void onSaveInstanceState(Bundle outState) {
1483 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001484 Parcelable p = mFragments.saveAllState();
1485 if (p != null) {
1486 outState.putParcelable(FRAGMENTS_TAG, p);
1487 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001488 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490
1491 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001492 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001493 * created with the attribute {@link android.R.attr#persistableMode} set to
1494 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1495 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1496 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001497 *
1498 * @param outState Bundle in which to place your saved state.
1499 * @param outPersistentState State which will be saved across reboots.
1500 *
1501 * @see #onSaveInstanceState(Bundle)
1502 * @see #onCreate
1503 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1504 * @see #onPause
1505 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001506 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001507 onSaveInstanceState(outState);
1508 }
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * Save the state of any managed dialogs.
1512 *
1513 * @param outState place to store the saved state.
1514 */
1515 private void saveManagedDialogs(Bundle outState) {
1516 if (mManagedDialogs == null) {
1517 return;
1518 }
1519
1520 final int numDialogs = mManagedDialogs.size();
1521 if (numDialogs == 0) {
1522 return;
1523 }
1524
1525 Bundle dialogState = new Bundle();
1526
1527 int[] ids = new int[mManagedDialogs.size()];
1528
1529 // save each dialog's bundle, gather the ids
1530 for (int i = 0; i < numDialogs; i++) {
1531 final int key = mManagedDialogs.keyAt(i);
1532 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001533 final ManagedDialog md = mManagedDialogs.valueAt(i);
1534 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1535 if (md.mArgs != null) {
1536 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539
1540 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1541 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1542 }
1543
1544
1545 /**
1546 * Called as part of the activity lifecycle when an activity is going into
1547 * the background, but has not (yet) been killed. The counterpart to
1548 * {@link #onResume}.
1549 *
1550 * <p>When activity B is launched in front of activity A, this callback will
1551 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1552 * so be sure to not do anything lengthy here.
1553 *
1554 * <p>This callback is mostly used for saving any persistent state the
1555 * activity is editing, to present a "edit in place" model to the user and
1556 * making sure nothing is lost if there are not enough resources to start
1557 * the new activity without first killing this one. This is also a good
1558 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001559 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * as fast as possible, or to close resources that are exclusive access
1561 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001562 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 * <p>In situations where the system needs more memory it may kill paused
1564 * processes to reclaim resources. Because of this, you should be sure
1565 * that all of your state is saved by the time you return from
1566 * this function. In general {@link #onSaveInstanceState} is used to save
1567 * per-instance state in the activity and this method is used to store
1568 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001569 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 * <p>After receiving this call you will usually receive a following call
1571 * to {@link #onStop} (after the next activity has been resumed and
1572 * displayed), however in some cases there will be a direct call back to
1573 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001574 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 * <p><em>Derived classes must call through to the super class's
1576 * implementation of this method. If they do not, an exception will be
1577 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001578 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 * @see #onResume
1580 * @see #onSaveInstanceState
1581 * @see #onStop
1582 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001583 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001585 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001586 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 mCalled = true;
1588 }
1589
1590 /**
1591 * Called as part of the activity lifecycle when an activity is about to go
1592 * into the background as the result of user choice. For example, when the
1593 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1594 * when an incoming phone call causes the in-call Activity to be automatically
1595 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1596 * the activity being interrupted. In cases when it is invoked, this method
1597 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 * <p>This callback and {@link #onUserInteraction} are intended to help
1600 * activities manage status bar notifications intelligently; specifically,
1601 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001602 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 * @see #onUserInteraction()
1604 */
1605 protected void onUserLeaveHint() {
1606 }
RoboErik55011652014-07-09 15:05:53 -07001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 /**
1609 * Generate a new thumbnail for this activity. This method is called before
1610 * pausing the activity, and should draw into <var>outBitmap</var> the
1611 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1612 * can use the given <var>canvas</var>, which is configured to draw into the
1613 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001614 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001615 * <p>The default implementation returns fails and does not draw a thumbnail;
1616 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 * @param outBitmap The bitmap to contain the thumbnail.
1619 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001620 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 * @return Return true if you have drawn into the bitmap; otherwise after
1622 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 * @see #onCreateDescription
1625 * @see #onSaveInstanceState
1626 * @see #onPause
1627 */
1628 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001629 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
1631
1632 /**
1633 * Generate a new description for this activity. This method is called
1634 * before pausing the activity and can, if desired, return some textual
1635 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001636 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 * <p>The default implementation returns null, which will cause you to
1638 * inherit the description from the previous activity. If all activities
1639 * return null, generally the label of the top activity will be used as the
1640 * description.
RoboErik55011652014-07-09 15:05:53 -07001641 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 * @return A description of what the user is doing. It should be short and
1643 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 * @see #onCreateThumbnail
1646 * @see #onSaveInstanceState
1647 * @see #onPause
1648 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001649 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 public CharSequence onCreateDescription() {
1651 return null;
1652 }
1653
1654 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001655 * This is called when the user is requesting an assist, to build a full
1656 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1657 * application. You can override this method to place into the bundle anything
1658 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001659 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001660 *
1661 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001662 * been registered with {@link Application#registerOnProvideAssistDataListener
1663 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001664 */
1665 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001666 }
1667
1668 /**
1669 * This is called when the user is requesting an assist, to provide references
1670 * to content related to the current activity. Before being called, the
1671 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1672 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1673 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1674 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1675 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1676 *
1677 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1678 * context of the activity, and fill in its ClipData with additional content of
1679 * interest that the user is currently viewing. For example, an image gallery application
1680 * that has launched in to an activity allowing the user to swipe through pictures should
1681 * modify the intent to reference the current image they are looking it; such an
1682 * application when showing a list of pictures should add a ClipData that has
1683 * references to all of the pictures currently visible on screen.</p>
1684 *
1685 * @param outContent The assist content to return.
1686 */
1687 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001688 }
1689
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001690 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01001691 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001692 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
1693 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01001694 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001695 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001696 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1697 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01001698 }
1699
1700 /**
1701 * Dismiss the Keyboard Shortcuts screen.
1702 */
1703 public final void dismissKeyboardShortcutsHelper() {
1704 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001705 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1706 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001707 }
1708
Clara Bayarri75e09792015-07-29 16:20:40 +01001709 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001710 public void onProvideKeyboardShortcuts(
1711 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01001712 if (menu == null) {
1713 return;
1714 }
1715 KeyboardShortcutGroup group = null;
1716 int menuSize = menu.size();
1717 for (int i = 0; i < menuSize; ++i) {
1718 final MenuItem item = menu.getItem(i);
1719 final CharSequence title = item.getTitle();
1720 final char alphaShortcut = item.getAlphabeticShortcut();
1721 if (title != null && alphaShortcut != MIN_VALUE) {
1722 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001723 final int resource = mApplication.getApplicationInfo().labelRes;
1724 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001725 }
1726 group.addItem(new KeyboardShortcutInfo(
1727 title, alphaShortcut, KeyEvent.META_CTRL_ON));
1728 }
1729 }
1730 if (group != null) {
1731 data.add(group);
1732 }
1733 }
1734
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001735 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001736 * Ask to have the current assistant shown to the user. This only works if the calling
1737 * activity is the current foreground activity. It is the same as calling
1738 * {@link android.service.voice.VoiceInteractionService#showSession
1739 * VoiceInteractionService.showSession} and requesting all of the possible context.
1740 * The receiver will always see
1741 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1742 * @return Returns true if the assistant was successfully invoked, else false. For example
1743 * false will be returned if the caller is not the current top activity.
1744 */
1745 public boolean showAssist(Bundle args) {
1746 try {
1747 return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args);
1748 } catch (RemoteException e) {
1749 }
1750 return false;
1751 }
1752
1753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 * Called when you are no longer visible to the user. You will next
1755 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1756 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 * <p><em>Derived classes must call through to the super class's
1759 * implementation of this method. If they do not, an exception will be
1760 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 * @see #onRestart
1763 * @see #onResume
1764 * @see #onSaveInstanceState
1765 * @see #onDestroy
1766 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001767 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001769 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001770 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001771 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001772 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001773 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 mCalled = true;
1775 }
1776
1777 /**
1778 * Perform any final cleanup before an activity is destroyed. This can
1779 * happen either because the activity is finishing (someone called
1780 * {@link #finish} on it, or because the system is temporarily destroying
1781 * this instance of the activity to save space. You can distinguish
1782 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001783 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 * <p><em>Note: do not count on this method being called as a place for
1785 * saving data! For example, if an activity is editing data in a content
1786 * provider, those edits should be committed in either {@link #onPause} or
1787 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1788 * free resources like threads that are associated with an activity, so
1789 * that a destroyed activity does not leave such things around while the
1790 * rest of its application is still running. There are situations where
1791 * the system will simply kill the activity's hosting process without
1792 * calling this method (or any others) in it, so it should not be used to
1793 * do things that are intended to remain around after the process goes
1794 * away.
RoboErik55011652014-07-09 15:05:53 -07001795 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 * <p><em>Derived classes must call through to the super class's
1797 * implementation of this method. If they do not, an exception will be
1798 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 * @see #onPause
1801 * @see #onStop
1802 * @see #finish
1803 * @see #isFinishing
1804 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001805 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001807 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 mCalled = true;
1809
1810 // dismiss any dialogs we are managing.
1811 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 final int numDialogs = mManagedDialogs.size();
1813 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001814 final ManagedDialog md = mManagedDialogs.valueAt(i);
1815 if (md.mDialog.isShowing()) {
1816 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001819 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821
1822 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001823 synchronized (mManagedCursors) {
1824 int numCursors = mManagedCursors.size();
1825 for (int i = 0; i < numCursors; i++) {
1826 ManagedCursor c = mManagedCursors.get(i);
1827 if (c != null) {
1828 c.mCursor.close();
1829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001831 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
Amith Yamasani49860442010-03-17 20:54:10 -07001833
1834 // Close any open search dialog
1835 if (mSearchManager != null) {
1836 mSearchManager.stopSearch();
1837 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001838
Chris Banes21b25772016-01-04 20:41:59 +00001839 if (mActionBar != null) {
1840 mActionBar.onDestroy();
1841 }
1842
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001843 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845
1846 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001847 * Report to the system that your app is now fully drawn, purely for diagnostic
1848 * purposes (calling it does not impact the visible behavior of the activity).
1849 * This is only used to help instrument application launch times, so that the
1850 * app can report when it is fully in a usable state; without this, the only thing
1851 * the system itself can determine is the point at which the activity's window
1852 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001853 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001854 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001855 * entirely drawn your UI and populated with all of the significant data. You
1856 * can safely call this method any time after first launch as well, in which case
1857 * it will simply be ignored.
1858 */
1859 public void reportFullyDrawn() {
1860 if (mDoReportFullyDrawn) {
1861 mDoReportFullyDrawn = false;
1862 try {
1863 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1864 } catch (RemoteException e) {
1865 }
1866 }
1867 }
1868
1869 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08001870 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
1871 * visa-versa.
1872 * @see android.R.attr#resizeableActivity
1873 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001874 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001875 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001876 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07001877 // Left deliberately empty. There should be no side effects if a direct
1878 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001879 }
1880
1881 /**
1882 * Returns true if the activity is currently in multi-window mode.
1883 * @see android.R.attr#resizeableActivity
1884 *
1885 * @return True if the activity is in multi-window mode.
1886 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001887 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001888 try {
Andrii Kulian933076d2016-03-29 17:04:42 -07001889 return ActivityManagerNative.getDefault().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001890 } catch (RemoteException e) {
1891 }
1892 return false;
1893 }
1894
1895 /**
1896 * Called by the system when the activity changes to and from picture-in-picture mode.
1897 * @see android.R.attr#supportsPictureInPicture
1898 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001899 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001900 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001901 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07001902 // Left deliberately empty. There should be no side effects if a direct
1903 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001904 }
1905
1906 /**
1907 * Returns true if the activity is currently in picture-in-picture mode.
1908 * @see android.R.attr#supportsPictureInPicture
1909 *
1910 * @return True if the activity is in picture-in-picture mode.
1911 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001912 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001913 try {
Andrii Kulian933076d2016-03-29 17:04:42 -07001914 return ActivityManagerNative.getDefault().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001915 } catch (RemoteException e) {
1916 }
1917 return false;
1918 }
1919
1920 /**
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001921 * Puts the activity in picture-in-picture mode.
1922 * @see android.R.attr#supportsPictureInPicture
1923 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001924 public void enterPictureInPictureMode() {
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001925 try {
Andrii Kulian933076d2016-03-29 17:04:42 -07001926 ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001927 } catch (RemoteException e) {
1928 }
1929 }
1930
1931 /**
Winson Chung84a38342016-11-08 16:15:10 -08001932 * Puts the activity in picture-in-picture mode with a given aspect ratio.
1933 * @see android.R.attr#supportsPictureInPicture
1934 *
1935 * @param aspectRatio the new aspect ratio of the picture-in-picture.
1936 */
1937 public void enterPictureInPictureMode(float aspectRatio) {
1938 try {
1939 ActivityManagerNative.getDefault().enterPictureInPictureModeWithAspectRatio(mToken,
1940 aspectRatio);
1941 } catch (RemoteException e) {
1942 }
1943 }
1944
1945 /**
1946 * Updates the aspect ratio of the current picture-in-picture activity.
1947 *
1948 * @param aspectRatio the new aspect ratio of the picture-in-picture.
1949 */
1950 public void setPictureInPictureAspectRatio(float aspectRatio) {
1951 try {
1952 ActivityManagerNative.getDefault().setPictureInPictureAspectRatio(mToken, aspectRatio);
1953 } catch (RemoteException e) {
1954 }
1955 }
1956
1957 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 * Called by the system when the device configuration changes while your
1959 * activity is running. Note that this will <em>only</em> be called if
1960 * you have selected configurations you would like to handle with the
1961 * {@link android.R.attr#configChanges} attribute in your manifest. If
1962 * any configuration change occurs that is not selected to be reported
1963 * by that attribute, then instead of reporting it the system will stop
1964 * and restart the activity (to have it launched with the new
1965 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 * <p>At the time that this function has been called, your Resources
1968 * object will have been updated to return resource values matching the
1969 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001970 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 * @param newConfig The new device configuration.
1972 */
1973 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001974 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001976
Dianne Hackborn9d071802010-12-08 14:49:15 -08001977 mFragments.dispatchConfigurationChanged(newConfig);
1978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 if (mWindow != null) {
1980 // Pass the configuration changed event to the window
1981 mWindow.onConfigurationChanged(newConfig);
1982 }
Adam Powell45c0b192011-07-28 15:11:57 -07001983
1984 if (mActionBar != null) {
1985 // Do this last; the action bar will need to access
1986 // view changes from above.
1987 mActionBar.onConfigurationChanged(newConfig);
1988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
RoboErik55011652014-07-09 15:05:53 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 /**
1992 * If this activity is being destroyed because it can not handle a
1993 * configuration parameter being changed (and thus its
1994 * {@link #onConfigurationChanged(Configuration)} method is
1995 * <em>not</em> being called), then you can use this method to discover
1996 * the set of changes that have occurred while in the process of being
1997 * destroyed. Note that there is no guarantee that these will be
1998 * accurate (other changes could have happened at any time), so you should
1999 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002000 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 * @return Returns a bit field of the configuration parameters that are
2002 * changing, as defined by the {@link android.content.res.Configuration}
2003 * class.
2004 */
2005 public int getChangingConfigurations() {
2006 return mConfigChangeFlags;
2007 }
RoboErik55011652014-07-09 15:05:53 -07002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 /**
2010 * Retrieve the non-configuration instance data that was previously
2011 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2012 * be available from the initial {@link #onCreate} and
2013 * {@link #onStart} calls to the new instance, allowing you to extract
2014 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 * <p>Note that the data you retrieve here should <em>only</em> be used
2017 * as an optimization for handling configuration changes. You should always
2018 * be able to handle getting a null pointer back, and an activity must
2019 * still be able to restore itself to its previous state (through the
2020 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2021 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002022 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002023 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002024 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002025 * available on older platforms through the Android support libraries.
2026 *
2027 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002029 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002031 return mLastNonConfigurationInstances != null
2032 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
RoboErik55011652014-07-09 15:05:53 -07002034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 /**
2036 * Called by the system, as part of destroying an
2037 * activity due to a configuration change, when it is known that a new
2038 * instance will immediately be created for the new configuration. You
2039 * can return any object you like here, including the activity instance
2040 * itself, which can later be retrieved by calling
2041 * {@link #getLastNonConfigurationInstance()} in the new activity
2042 * instance.
RoboErik55011652014-07-09 15:05:53 -07002043 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002044 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2045 * or later, consider instead using a {@link Fragment} with
2046 * {@link Fragment#setRetainInstance(boolean)
2047 * Fragment.setRetainInstance(boolean}.</em>
2048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 * <p>This function is called purely as an optimization, and you must
2050 * not rely on it being called. When it is called, a number of guarantees
2051 * will be made to help optimize configuration switching:
2052 * <ul>
2053 * <li> The function will be called between {@link #onStop} and
2054 * {@link #onDestroy}.
2055 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002056 * created after this one's {@link #onDestroy()} is called. In particular,
2057 * <em>no</em> messages will be dispatched during this time (when the returned
2058 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 * <li> The object you return here will <em>always</em> be available from
2060 * the {@link #getLastNonConfigurationInstance()} method of the following
2061 * activity instance as described there.
2062 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002063 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 * <p>These guarantees are designed so that an activity can use this API
2065 * to propagate extensive state from the old to new activity instance, from
2066 * loaded bitmaps, to network connections, to evenly actively running
2067 * threads. Note that you should <em>not</em> propagate any data that
2068 * may change based on the configuration, including any data loaded from
2069 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002070 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002071 * <p>The guarantee of no message handling during the switch to the next
2072 * activity simplifies use with active objects. For example if your retained
2073 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2074 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2075 * not be called from the call here until you execute the next instance's
2076 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2077 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2078 * running in a separate thread.)
2079 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002080 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002081 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002082 * available on older platforms through the Android support libraries.
2083 *
2084 * @return any Object holding the desired state to propagate to the
2085 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 */
2087 public Object onRetainNonConfigurationInstance() {
2088 return null;
2089 }
RoboErik55011652014-07-09 15:05:53 -07002090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 /**
2092 * Retrieve the non-configuration instance data that was previously
2093 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2094 * be available from the initial {@link #onCreate} and
2095 * {@link #onStart} calls to the new instance, allowing you to extract
2096 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002097 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 * <p>Note that the data you retrieve here should <em>only</em> be used
2099 * as an optimization for handling configuration changes. You should always
2100 * be able to handle getting a null pointer back, and an activity must
2101 * still be able to restore itself to its previous state (through the
2102 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2103 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 * @return Returns the object previously returned by
2106 * {@link #onRetainNonConfigurationChildInstances()}
2107 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002108 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002109 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2110 return mLastNonConfigurationInstances != null
2111 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
RoboErik55011652014-07-09 15:05:53 -07002113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 /**
2115 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2116 * it should return either a mapping from child activity id strings to arbitrary objects,
2117 * or null. This method is intended to be used by Activity framework subclasses that control a
2118 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2119 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2120 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002121 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2123 return null;
2124 }
RoboErik55011652014-07-09 15:05:53 -07002125
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002126 NonConfigurationInstances retainNonConfigurationInstances() {
2127 Object activity = onRetainNonConfigurationInstance();
2128 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002129 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002130
2131 // We're already stopped but we've been asked to retain.
2132 // Our fragments are taken care of but we need to mark the loaders for retention.
2133 // In order to do this correctly we need to restart the loaders first before
2134 // handing them off to the next activity.
2135 mFragments.doLoaderStart();
2136 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002137 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002138
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002139 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002140 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002141 return null;
2142 }
RoboErik55011652014-07-09 15:05:53 -07002143
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002144 NonConfigurationInstances nci = new NonConfigurationInstances();
2145 nci.activity = activity;
2146 nci.children = children;
2147 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002148 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002149 if (mVoiceInteractor != null) {
2150 mVoiceInteractor.retainInstance();
2151 nci.voiceInteractor = mVoiceInteractor;
2152 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002153 return nci;
2154 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002157 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002159 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002161
2162 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002163 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002164 mCalled = true;
2165 mFragments.dispatchTrimMemory(level);
2166 }
2167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002169 * Return the FragmentManager for interacting with fragments associated
2170 * with this activity.
2171 */
2172 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002173 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002174 }
RoboErik55011652014-07-09 15:05:53 -07002175
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002176 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002177 * Called when a Fragment is being attached to this activity, immediately
2178 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2179 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
2180 */
2181 public void onAttachFragment(Fragment fragment) {
2182 }
RoboErik55011652014-07-09 15:05:53 -07002183
Dianne Hackbornc8017682010-07-06 13:34:38 -07002184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 * Wrapper around
2186 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2187 * that gives the resulting {@link Cursor} to call
2188 * {@link #startManagingCursor} so that the activity will manage its
2189 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002190 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002191 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2192 * or later, consider instead using {@link LoaderManager} instead, available
2193 * via {@link #getLoaderManager()}.</em>
2194 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002195 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2196 * this method, because the activity will do that for you at the appropriate time. However, if
2197 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2198 * not</em> automatically close the cursor and, in that case, you must call
2199 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 * @param uri The URI of the content provider to query.
2202 * @param projection List of columns to return.
2203 * @param selection SQL WHERE clause.
2204 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002205 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2209 * @see #startManagingCursor
2210 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002211 *
2212 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 */
Jason parks6ed50de2010-08-25 10:18:50 -05002214 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002215 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2216 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2218 if (c != null) {
2219 startManagingCursor(c);
2220 }
2221 return c;
2222 }
2223
2224 /**
2225 * Wrapper around
2226 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2227 * that gives the resulting {@link Cursor} to call
2228 * {@link #startManagingCursor} so that the activity will manage its
2229 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002230 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002231 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2232 * or later, consider instead using {@link LoaderManager} instead, available
2233 * via {@link #getLoaderManager()}.</em>
2234 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002235 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2236 * this method, because the activity will do that for you at the appropriate time. However, if
2237 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2238 * not</em> automatically close the cursor and, in that case, you must call
2239 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 * @param uri The URI of the content provider to query.
2242 * @param projection List of columns to return.
2243 * @param selection SQL WHERE clause.
2244 * @param selectionArgs The arguments to selection, if any ?s are pesent
2245 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2250 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002251 *
2252 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 */
Jason parks6ed50de2010-08-25 10:18:50 -05002254 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002255 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2256 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2258 if (c != null) {
2259 startManagingCursor(c);
2260 }
2261 return c;
2262 }
2263
2264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 * This method allows the activity to take care of managing the given
2266 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2267 * That is, when the activity is stopped it will automatically call
2268 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2269 * it will call {@link Cursor#requery} for you. When the activity is
2270 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002271 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002272 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2273 * or later, consider instead using {@link LoaderManager} instead, available
2274 * via {@link #getLoaderManager()}.</em>
2275 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002276 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2277 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2278 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2279 * <em>will not</em> automatically close the cursor and, in that case, you must call
2280 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002281 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2285 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002286 *
2287 * @deprecated Use the new {@link android.content.CursorLoader} class with
2288 * {@link LoaderManager} instead; this is also
2289 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 */
Jason parks6ed50de2010-08-25 10:18:50 -05002291 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 public void startManagingCursor(Cursor c) {
2293 synchronized (mManagedCursors) {
2294 mManagedCursors.add(new ManagedCursor(c));
2295 }
2296 }
2297
2298 /**
2299 * Given a Cursor that was previously given to
2300 * {@link #startManagingCursor}, stop the activity's management of that
2301 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002302 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002303 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002304 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002305 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002310 *
2311 * @deprecated Use the new {@link android.content.CursorLoader} class with
2312 * {@link LoaderManager} instead; this is also
2313 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 */
Jason parks6ed50de2010-08-25 10:18:50 -05002315 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 public void stopManagingCursor(Cursor c) {
2317 synchronized (mManagedCursors) {
2318 final int N = mManagedCursors.size();
2319 for (int i=0; i<N; i++) {
2320 ManagedCursor mc = mManagedCursors.get(i);
2321 if (mc.mCursor == c) {
2322 mManagedCursors.remove(i);
2323 break;
2324 }
2325 }
2326 }
2327 }
2328
2329 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002330 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2331 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002332 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002334 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337
2338 /**
2339 * Finds a view that was identified by the id attribute from the XML that
2340 * was processed in {@link #onCreate}.
2341 *
2342 * @return The view if found or null otherwise.
2343 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002344 @Nullable
Tor Norbye7b9c9122013-05-30 16:48:33 -07002345 public View findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 return getWindow().findViewById(id);
2347 }
RoboErik55011652014-07-09 15:05:53 -07002348
Adam Powell33b97432010-04-20 10:01:14 -07002349 /**
2350 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002351 *
Adam Powell33b97432010-04-20 10:01:14 -07002352 * @return The Activity's ActionBar, or null if it does not have one.
2353 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002354 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002355 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002356 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002357 return mActionBar;
2358 }
Adam Powelle43340c2014-03-17 19:10:43 -07002359
2360 /**
2361 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2362 * Activity window.
2363 *
2364 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2365 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2366 * a traditional window decor action bar. The toolbar's menu will be populated with the
2367 * Activity's options menu and the navigation button will be wired through the standard
2368 * {@link android.R.id#home home} menu select action.</p>
2369 *
2370 * <p>In order to use a Toolbar within the Activity's window content the application
2371 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2372 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002373 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002374 */
Adam Powell37780142014-06-01 13:31:00 -07002375 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002376 final ActionBar ab = getActionBar();
2377 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002378 throw new IllegalStateException("This Activity already has an action bar supplied " +
2379 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2380 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2381 }
Chris Banes21b25772016-01-04 20:41:59 +00002382
2383 // If we reach here then we're setting a new action bar
2384 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002385 mMenuInflater = null;
2386
Chris Banes21b25772016-01-04 20:41:59 +00002387 // If we have an action bar currently, destroy it
2388 if (ab != null) {
2389 ab.onDestroy();
2390 }
2391
Chris Banesc7d6c322016-01-27 14:09:16 +00002392 if (toolbar != null) {
2393 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2394 mActionBar = tbab;
2395 mWindow.setCallback(tbab.getWrappedWindowCallback());
2396 } else {
2397 mActionBar = null;
2398 // Re-set the original window callback since we may have already set a Toolbar wrapper
2399 mWindow.setCallback(this);
2400 }
2401
2402 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002403 }
RoboErik55011652014-07-09 15:05:53 -07002404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 /**
Adam Powell33b97432010-04-20 10:01:14 -07002406 * Creates a new ActionBar, locates the inflated ActionBarView,
2407 * initializes the ActionBar with the view, and sets mActionBar.
2408 */
Adam Powelle43340c2014-03-17 19:10:43 -07002409 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002410 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002411
2412 // Initializing the window decor can change window feature flags.
2413 // Make sure that we have the correct set before performing the test below.
2414 window.getDecorView();
2415
Adam Powell9b4c8042010-08-10 15:36:44 -07002416 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002417 return;
2418 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002419
Adam Powelle43340c2014-03-17 19:10:43 -07002420 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002421 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002422
2423 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2424 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002425 }
RoboErik55011652014-07-09 15:05:53 -07002426
Adam Powell33b97432010-04-20 10:01:14 -07002427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 * Set the activity content from a layout resource. The resource will be
2429 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002432 *
Romain Guy482b34a62011-01-20 10:59:28 -08002433 * @see #setContentView(android.view.View)
2434 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002436 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002438 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
2440
2441 /**
2442 * Set the activity content to an explicit view. This view is placed
2443 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002444 * view hierarchy. When calling this method, the layout parameters of the
2445 * specified view are ignored. Both the width and the height of the view are
2446 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2447 * your own layout parameters, invoke
2448 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2449 * instead.
RoboErik55011652014-07-09 15:05:53 -07002450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002452 *
2453 * @see #setContentView(int)
2454 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 */
2456 public void setContentView(View view) {
2457 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002458 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 }
2460
2461 /**
2462 * Set the activity content to an explicit view. This view is placed
2463 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002464 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 * @param view The desired content to display.
2467 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002468 *
2469 * @see #setContentView(android.view.View)
2470 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 */
2472 public void setContentView(View view, ViewGroup.LayoutParams params) {
2473 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002474 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
2476
2477 /**
2478 * Add an additional content view to the activity. Added after any existing
2479 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002480 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 * @param view The desired content to display.
2482 * @param params Layout parameters for the view.
2483 */
2484 public void addContentView(View view, ViewGroup.LayoutParams params) {
2485 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002486 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
2488
2489 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002490 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2491 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2492 *
2493 * <p>This method will return non-null after content has been initialized (e.g. by using
2494 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2495 *
2496 * @return This window's content TransitionManager or null if none is set.
2497 */
2498 public TransitionManager getContentTransitionManager() {
2499 return getWindow().getTransitionManager();
2500 }
2501
2502 /**
2503 * Set the {@link TransitionManager} to use for default transitions in this window.
2504 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2505 *
2506 * @param tm The TransitionManager to use for scene changes.
2507 */
2508 public void setContentTransitionManager(TransitionManager tm) {
2509 getWindow().setTransitionManager(tm);
2510 }
2511
2512 /**
2513 * Retrieve the {@link Scene} representing this window's current content.
2514 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2515 *
2516 * <p>This method will return null if the current content is not represented by a Scene.</p>
2517 *
2518 * @return Current Scene being shown or null
2519 */
2520 public Scene getContentScene() {
2521 return getWindow().getContentScene();
2522 }
2523
2524 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002525 * Sets whether this activity is finished when touched outside its window's
2526 * bounds.
2527 */
2528 public void setFinishOnTouchOutside(boolean finish) {
2529 mWindow.setCloseOnTouchOutside(finish);
2530 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002531
2532 /** @hide */
2533 @IntDef({
2534 DEFAULT_KEYS_DISABLE,
2535 DEFAULT_KEYS_DIALER,
2536 DEFAULT_KEYS_SHORTCUT,
2537 DEFAULT_KEYS_SEARCH_LOCAL,
2538 DEFAULT_KEYS_SEARCH_GLOBAL})
2539 @Retention(RetentionPolicy.SOURCE)
2540 @interface DefaultKeyMode {}
2541
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2544 * keys.
RoboErik55011652014-07-09 15:05:53 -07002545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 * @see #setDefaultKeyMode
2547 */
2548 static public final int DEFAULT_KEYS_DISABLE = 0;
2549 /**
2550 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2551 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 * @see #setDefaultKeyMode
2554 */
2555 static public final int DEFAULT_KEYS_DIALER = 1;
2556 /**
2557 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2558 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002559 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 * @see #setDefaultKeyMode
2563 */
2564 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2565 /**
2566 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2567 * will start an application-defined search. (If the application or activity does not
2568 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002569 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 * @see #setDefaultKeyMode
2573 */
2574 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2575
2576 /**
2577 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2578 * will start a global search (typically web search, but some platforms may define alternate
2579 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 * @see #setDefaultKeyMode
2584 */
2585 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2586
2587 /**
2588 * Select the default key handling for this activity. This controls what
2589 * will happen to key events that are not otherwise handled. The default
2590 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2591 * floor. Other modes allow you to launch the dialer
2592 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2593 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002594 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002596 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * <p>Note that the mode selected here does not impact the default
2598 * handling of system keys, such as the "back" and "menu" keys, and your
2599 * activity and its views always get a first chance to receive and handle
2600 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002601 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 * @see #DEFAULT_KEYS_DISABLE
2605 * @see #DEFAULT_KEYS_DIALER
2606 * @see #DEFAULT_KEYS_SHORTCUT
2607 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2608 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2609 * @see #onKeyDown
2610 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002611 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 // Some modes use a SpannableStringBuilder to track & dispatch input events
2615 // This list must remain in sync with the switch in onKeyDown()
2616 switch (mode) {
2617 case DEFAULT_KEYS_DISABLE:
2618 case DEFAULT_KEYS_SHORTCUT:
2619 mDefaultKeySsb = null; // not used in these modes
2620 break;
2621 case DEFAULT_KEYS_DIALER:
2622 case DEFAULT_KEYS_SEARCH_LOCAL:
2623 case DEFAULT_KEYS_SEARCH_GLOBAL:
2624 mDefaultKeySsb = new SpannableStringBuilder();
2625 Selection.setSelection(mDefaultKeySsb,0);
2626 break;
2627 default:
2628 throw new IllegalArgumentException();
2629 }
2630 }
2631
2632 /**
2633 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002634 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 * is inside a TextView will not trigger the event (unless it is a navigation
2636 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 * <p>If the focused view didn't want this event, this method is called.
2639 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002640 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2641 * by calling {@link #onBackPressed()}, though the behavior varies based
2642 * on the application compatibility mode: for
2643 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2644 * it will set up the dispatch to call {@link #onKeyUp} where the action
2645 * will be performed; for earlier applications, it will perform the
2646 * action immediately in on-down, as those versions of the platform
2647 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002648 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002649 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002650 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002651 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002653 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * this event and it should continue to be propagated.
2655 * @see #onKeyUp
2656 * @see android.view.KeyEvent
2657 */
2658 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002659 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002660 if (getApplicationInfo().targetSdkVersion
2661 >= Build.VERSION_CODES.ECLAIR) {
2662 event.startTracking();
2663 } else {
2664 onBackPressed();
2665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 return true;
2667 }
RoboErik55011652014-07-09 15:05:53 -07002668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2670 return false;
2671 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002672 Window w = getWindow();
2673 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2674 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2675 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002676 return true;
2677 }
2678 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 } else {
2680 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2681 boolean clearSpannable = false;
2682 boolean handled;
2683 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2684 clearSpannable = true;
2685 handled = false;
2686 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002687 handled = TextKeyListener.getInstance().onKeyDown(
2688 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 if (handled && mDefaultKeySsb.length() > 0) {
2690 // something useable has been typed - dispatch it now.
2691
2692 final String str = mDefaultKeySsb.toString();
2693 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 switch (mDefaultKeyMode) {
2696 case DEFAULT_KEYS_DIALER:
2697 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2698 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002699 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 break;
2701 case DEFAULT_KEYS_SEARCH_LOCAL:
2702 startSearch(str, false, null, false);
2703 break;
2704 case DEFAULT_KEYS_SEARCH_GLOBAL:
2705 startSearch(str, false, null, true);
2706 break;
2707 }
2708 }
2709 }
2710 if (clearSpannable) {
2711 mDefaultKeySsb.clear();
2712 mDefaultKeySsb.clearSpans();
2713 Selection.setSelection(mDefaultKeySsb,0);
2714 }
2715 return handled;
2716 }
2717 }
2718
2719 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002720 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2721 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2722 * the event).
2723 */
2724 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2725 return false;
2726 }
2727
2728 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002730 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 * is inside a TextView will not trigger the event (unless it is a navigation
2732 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002733 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002734 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2735 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002738 * further, or <code>false</code> to indicate that you have not handled
2739 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 * @see #onKeyDown
2741 * @see KeyEvent
2742 */
2743 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002744 if (getApplicationInfo().targetSdkVersion
2745 >= Build.VERSION_CODES.ECLAIR) {
2746 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2747 && !event.isCanceled()) {
2748 onBackPressed();
2749 return true;
2750 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 return false;
2753 }
2754
2755 /**
2756 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2757 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2758 * the event).
2759 */
2760 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2761 return false;
2762 }
RoboErik55011652014-07-09 15:05:53 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002765 * Called when the activity has detected the user's press of the back
2766 * key. The default implementation simply finishes the current activity,
2767 * but you can override this to do whatever you want.
2768 */
2769 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002770 if (mActionBar != null && mActionBar.collapseActionView()) {
2771 return;
2772 }
2773
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002774 if (!mFragments.getFragmentManager().popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002775 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002776 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002777 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002778
2779 /**
2780 * Called when a key shortcut event is not handled by any of the views in the Activity.
2781 * Override this method to implement global key shortcuts for the Activity.
2782 * Key shortcuts can also be implemented by setting the
2783 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2784 *
2785 * @param keyCode The value in event.getKeyCode().
2786 * @param event Description of the key event.
2787 * @return True if the key shortcut was handled.
2788 */
2789 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07002790 // Let the Action Bar have a chance at handling the shortcut.
2791 ActionBar actionBar = getActionBar();
2792 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08002793 }
2794
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 * Called when a touch screen event was not handled by any of the views
2797 * under it. This is most useful to process touch events that happen
2798 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 * @return Return true if you have consumed the event, false if you haven't.
2803 * The default implementation always returns false.
2804 */
2805 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002806 if (mWindow.shouldCloseOnTouch(this, event)) {
2807 finish();
2808 return true;
2809 }
RoboErik55011652014-07-09 15:05:53 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 return false;
2812 }
RoboErik55011652014-07-09 15:05:53 -07002813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 /**
2815 * Called when the trackball was moved and not handled by any of the
2816 * views inside of the activity. So, for example, if the trackball moves
2817 * while focus is on a button, you will receive a call here because
2818 * buttons do not normally do anything with trackball events. The call
2819 * here happens <em>before</em> trackball movements are converted to
2820 * DPAD key events, which then get sent back to the view hierarchy, and
2821 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002824 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 * @return Return true if you have consumed the event, false if you haven't.
2826 * The default implementation always returns false.
2827 */
2828 public boolean onTrackballEvent(MotionEvent event) {
2829 return false;
2830 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002831
2832 /**
2833 * Called when a generic motion event was not handled by any of the
2834 * views inside of the activity.
2835 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002836 * Generic motion events describe joystick movements, mouse hovers, track pad
2837 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002838 * {@link MotionEvent#getSource() source} of the motion event specifies
2839 * the class of input that was received. Implementations of this method
2840 * must examine the bits in the source before processing the event.
2841 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002842 * </p><p>
2843 * Generic motion events with source class
2844 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2845 * are delivered to the view under the pointer. All other generic motion events are
2846 * delivered to the focused view.
2847 * </p><p>
2848 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2849 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002850 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002851 *
2852 * @param event The generic motion event being processed.
2853 *
2854 * @return Return true if you have consumed the event, false if you haven't.
2855 * The default implementation always returns false.
2856 */
2857 public boolean onGenericMotionEvent(MotionEvent event) {
2858 return false;
2859 }
2860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 /**
2862 * Called whenever a key, touch, or trackball event is dispatched to the
2863 * activity. Implement this method if you wish to know that the user has
2864 * interacted with the device in some way while your activity is running.
2865 * This callback and {@link #onUserLeaveHint} are intended to help
2866 * activities manage status bar notifications intelligently; specifically,
2867 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002868 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2870 * be accompanied by calls to {@link #onUserInteraction}. This
2871 * ensures that your activity will be told of relevant user activity such
2872 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002873 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 * <p>Note that this callback will be invoked for the touch down action
2875 * that begins a touch gesture, but may not be invoked for the touch-moved
2876 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 * @see #onUserLeaveHint()
2879 */
2880 public void onUserInteraction() {
2881 }
RoboErik55011652014-07-09 15:05:53 -07002882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2884 // Update window manager if: we have a view, that view is
2885 // attached to its parent (which will be a RootView), and
2886 // this activity is not embedded.
2887 if (mParent == null) {
2888 View decor = mDecor;
2889 if (decor != null && decor.getParent() != null) {
2890 getWindowManager().updateViewLayout(decor, params);
2891 }
2892 }
2893 }
2894
2895 public void onContentChanged() {
2896 }
2897
2898 /**
2899 * Called when the current {@link Window} of the activity gains or loses
2900 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002901 * to the user. The default implementation clears the key tracking
2902 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002903 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002904 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 * is managed independently of activity lifecycles. As such, while focus
2906 * changes will generally have some relation to lifecycle changes (an
2907 * activity that is stopped will not generally get window focus), you
2908 * should not rely on any particular order between the callbacks here and
2909 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002910 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 * <p>As a general rule, however, a resumed activity will have window
2912 * focus... unless it has displayed other dialogs or popups that take
2913 * input focus, in which case the activity itself will not have focus
2914 * when the other windows have it. Likewise, the system may display
2915 * system-level windows (such as the status bar notification panel or
2916 * a system alert) which will temporarily take window input focus without
2917 * pausing the foreground activity.
2918 *
2919 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002920 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 * @see #hasWindowFocus()
2922 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002923 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 */
2925 public void onWindowFocusChanged(boolean hasFocus) {
2926 }
RoboErik55011652014-07-09 15:05:53 -07002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002929 * Called when the main window associated with the activity has been
2930 * attached to the window manager.
2931 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2932 * for more information.
2933 * @see View#onAttachedToWindow
2934 */
2935 public void onAttachedToWindow() {
2936 }
RoboErik55011652014-07-09 15:05:53 -07002937
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002938 /**
2939 * Called when the main window associated with the activity has been
2940 * detached from the window manager.
2941 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2942 * for more information.
2943 * @see View#onDetachedFromWindow
2944 */
2945 public void onDetachedFromWindow() {
2946 }
RoboErik55011652014-07-09 15:05:53 -07002947
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002948 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 * Returns true if this activity's <em>main</em> window currently has window focus.
2950 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002951 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2955 */
2956 public boolean hasWindowFocus() {
2957 Window w = getWindow();
2958 if (w != null) {
2959 View d = w.getDecorView();
2960 if (d != null) {
2961 return d.hasWindowFocus();
2962 }
2963 }
2964 return false;
2965 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002966
2967 /**
2968 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002969 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002970 */
Adam Powell117b6952014-05-05 18:14:56 -07002971 @Override
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002972 public void onWindowDismissed(boolean finishTask) {
2973 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002974 }
RoboErik55011652014-07-09 15:05:53 -07002975
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002976
Skuhnece2faa52015-08-11 10:36:38 -07002977 /**
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002978 * Moves the activity from
2979 * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to
2980 * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002981 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002982 * @hide
2983 */
2984 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002985 public void exitFreeformMode() throws RemoteException {
2986 ActivityManagerNative.getDefault().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002987 }
2988
2989 /** Returns the current stack Id for the window.
2990 * @hide
2991 */
2992 @Override
2993 public int getWindowStackId() throws RemoteException {
2994 return ActivityManagerNative.getDefault().getActivityStackId(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 }
2996
2997 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09002998 * Puts the activity in picture-in-picture mode if the activity supports.
2999 * @see android.R.attr#supportsPictureInPicture
3000 * @hide
3001 */
3002 @Override
3003 public void enterPictureInPictureModeIfPossible() {
3004 if (mActivityInfo.resizeMode == ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE) {
3005 enterPictureInPictureMode();
3006 }
3007 }
3008
3009 /**
RoboErik55011652014-07-09 15:05:53 -07003010 * Called to process key events. You can override this to intercept all
3011 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003013 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 * @return boolean Return true if this event was consumed.
3017 */
3018 public boolean dispatchKeyEvent(KeyEvent event) {
3019 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003020
3021 // Let action bars open menus in response to the menu key prioritized over
3022 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003023 final int keyCode = event.getKeyCode();
3024 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003025 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3026 return true;
George Mountcbe28352016-01-06 16:24:26 -08003027 } else if (event.isCtrlPressed() &&
3028 event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') {
3029 // Capture the Control-< and send focus to the ActionBar
George Mount5beb26172015-12-15 13:36:01 -08003030 final int action = event.getAction();
3031 if (action == KeyEvent.ACTION_DOWN) {
George Mountcbe28352016-01-06 16:24:26 -08003032 final ActionBar actionBar = getActionBar();
3033 if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) {
3034 mEatKeyUpEvent = true;
3035 return true;
George Mount5beb26172015-12-15 13:36:01 -08003036 }
3037 } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) {
3038 mEatKeyUpEvent = false;
3039 return true;
3040 }
Adam Powell07a74542014-05-30 15:52:44 -07003041 }
3042
Dianne Hackborn8d374262009-09-14 21:21:52 -07003043 Window win = getWindow();
3044 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 return true;
3046 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003047 View decor = mDecor;
3048 if (decor == null) decor = win.getDecorView();
3049 return event.dispatch(this, decor != null
3050 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 }
3052
3053 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003054 * Called to process a key shortcut event.
3055 * You can override this to intercept all key shortcut events before they are
3056 * dispatched to the window. Be sure to call this implementation for key shortcut
3057 * events that should be handled normally.
3058 *
3059 * @param event The key shortcut event.
3060 * @return True if this event was consumed.
3061 */
3062 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3063 onUserInteraction();
3064 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3065 return true;
3066 }
3067 return onKeyShortcut(event.getKeyCode(), event);
3068 }
3069
3070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 * Called to process touch screen events. You can override this to
3072 * intercept all touch screen events before they are dispatched to the
3073 * window. Be sure to call this implementation for touch screen events
3074 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003077 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 * @return boolean Return true if this event was consumed.
3079 */
3080 public boolean dispatchTouchEvent(MotionEvent ev) {
3081 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3082 onUserInteraction();
3083 }
3084 if (getWindow().superDispatchTouchEvent(ev)) {
3085 return true;
3086 }
3087 return onTouchEvent(ev);
3088 }
RoboErik55011652014-07-09 15:05:53 -07003089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 /**
3091 * Called to process trackball events. You can override this to
3092 * intercept all trackball events before they are dispatched to the
3093 * window. Be sure to call this implementation for trackball events
3094 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003095 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003097 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 * @return boolean Return true if this event was consumed.
3099 */
3100 public boolean dispatchTrackballEvent(MotionEvent ev) {
3101 onUserInteraction();
3102 if (getWindow().superDispatchTrackballEvent(ev)) {
3103 return true;
3104 }
3105 return onTrackballEvent(ev);
3106 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003107
Jeff Browncb1404e2011-01-15 18:14:15 -08003108 /**
3109 * Called to process generic motion events. You can override this to
3110 * intercept all generic motion events before they are dispatched to the
3111 * window. Be sure to call this implementation for generic motion events
3112 * that should be handled normally.
3113 *
3114 * @param ev The generic motion event.
3115 *
3116 * @return boolean Return true if this event was consumed.
3117 */
3118 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3119 onUserInteraction();
3120 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3121 return true;
3122 }
3123 return onGenericMotionEvent(ev);
3124 }
3125
svetoslavganov75986cf2009-05-14 22:28:01 -07003126 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3127 event.setClassName(getClass().getName());
3128 event.setPackageName(getPackageName());
3129
3130 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003131 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3132 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003133 event.setFullScreen(isFullScreen);
3134
3135 CharSequence title = getTitle();
3136 if (!TextUtils.isEmpty(title)) {
3137 event.getText().add(title);
3138 }
3139
3140 return true;
3141 }
3142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 /**
3144 * Default implementation of
3145 * {@link android.view.Window.Callback#onCreatePanelView}
3146 * for activities. This
3147 * simply returns null so that all panel sub-windows will have the default
3148 * menu behavior.
3149 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003150 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 public View onCreatePanelView(int featureId) {
3152 return null;
3153 }
3154
3155 /**
3156 * Default implementation of
3157 * {@link android.view.Window.Callback#onCreatePanelMenu}
3158 * for activities. This calls through to the new
3159 * {@link #onCreateOptionsMenu} method for the
3160 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3161 * so that subclasses of Activity don't need to deal with feature codes.
3162 */
3163 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3164 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003165 boolean show = onCreateOptionsMenu(menu);
3166 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3167 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 }
3169 return false;
3170 }
3171
3172 /**
3173 * Default implementation of
3174 * {@link android.view.Window.Callback#onPreparePanel}
3175 * for activities. This
3176 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3177 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3178 * panel, so that subclasses of
3179 * Activity don't need to deal with feature codes.
3180 */
3181 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3182 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3183 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003184 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003185 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
3187 return true;
3188 }
3189
3190 /**
3191 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 * @return The default implementation returns true.
3194 */
3195 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003196 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003197 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003198 if (mActionBar != null) {
3199 mActionBar.dispatchMenuVisibilityChanged(true);
3200 } else {
3201 Log.e(TAG, "Tried to open action bar menu with no action bar");
3202 }
Adam Powell8515ee82010-11-30 14:09:55 -08003203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 return true;
3205 }
3206
3207 /**
3208 * Default implementation of
3209 * {@link android.view.Window.Callback#onMenuItemSelected}
3210 * for activities. This calls through to the new
3211 * {@link #onOptionsItemSelected} method for the
3212 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3213 * panel, so that subclasses of
3214 * Activity don't need to deal with feature codes.
3215 */
3216 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003217 CharSequence titleCondensed = item.getTitleCondensed();
3218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 switch (featureId) {
3220 case Window.FEATURE_OPTIONS_PANEL:
3221 // Put event logging here so it gets called even if subclass
3222 // doesn't call through to superclass's implmeentation of each
3223 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003224 if(titleCondensed != null) {
3225 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3226 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003227 if (onOptionsItemSelected(item)) {
3228 return true;
3229 }
Adam Powell04d58112012-04-09 10:22:12 -07003230 if (mFragments.dispatchOptionsItemSelected(item)) {
3231 return true;
3232 }
3233 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3234 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3235 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003236 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003237 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003238 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003239 }
Adam Powell04d58112012-04-09 10:22:12 -07003240 }
3241 return false;
RoboErik55011652014-07-09 15:05:53 -07003242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003244 if(titleCondensed != null) {
3245 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3246 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003247 if (onContextItemSelected(item)) {
3248 return true;
3249 }
3250 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 default:
3253 return false;
3254 }
3255 }
RoboErik55011652014-07-09 15:05:53 -07003256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 /**
3258 * Default implementation of
3259 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3260 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3261 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3262 * so that subclasses of Activity don't need to deal with feature codes.
3263 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3264 * {@link #onContextMenuClosed(Menu)} will be called.
3265 */
3266 public void onPanelClosed(int featureId, Menu menu) {
3267 switch (featureId) {
3268 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003269 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 onOptionsMenuClosed(menu);
3271 break;
RoboErik55011652014-07-09 15:05:53 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 case Window.FEATURE_CONTEXT_MENU:
3274 onContextMenuClosed(menu);
3275 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003276
3277 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003278 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003279 mActionBar.dispatchMenuVisibilityChanged(false);
3280 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282 }
3283
3284 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003285 * Declare that the options menu has changed, so should be recreated.
3286 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3287 * time it needs to be displayed.
3288 */
3289 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003290 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3291 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003292 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3293 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003294 }
RoboErik55011652014-07-09 15:05:53 -07003295
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 * Initialize the contents of the Activity's standard options menu. You
3298 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 * <p>This is only called once, the first time the options menu is
3301 * displayed. To update the menu every time it is displayed, see
3302 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003303 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003305 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3306 * they will be correctly ordered with application-defined menu items.
3307 * Deriving classes should always call through to the base implementation.
3308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 * <p>You can safely hold on to <var>menu</var> (and any items created
3310 * from it), making modifications to it as desired, until the next
3311 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 * <p>When you add items to the menu, you can implement the Activity's
3314 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 * @return You must return true for the menu to be displayed;
3319 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 * @see #onPrepareOptionsMenu
3322 * @see #onOptionsItemSelected
3323 */
3324 public boolean onCreateOptionsMenu(Menu menu) {
3325 if (mParent != null) {
3326 return mParent.onCreateOptionsMenu(menu);
3327 }
3328 return true;
3329 }
3330
3331 /**
3332 * Prepare the Screen's standard options menu to be displayed. This is
3333 * called right before the menu is shown, every time it is shown. You can
3334 * use this method to efficiently enable/disable items or otherwise
3335 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003336 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 * <p>The default implementation updates the system menu items based on the
3338 * activity's state. Deriving classes should always call through to the
3339 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003340 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 * @param menu The options menu as last shown or first initialized by
3342 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003343 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 * @return You must return true for the menu to be displayed;
3345 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003346 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 * @see #onCreateOptionsMenu
3348 */
3349 public boolean onPrepareOptionsMenu(Menu menu) {
3350 if (mParent != null) {
3351 return mParent.onPrepareOptionsMenu(menu);
3352 }
3353 return true;
3354 }
3355
3356 /**
3357 * This hook is called whenever an item in your options menu is selected.
3358 * The default implementation simply returns false to have the normal
3359 * processing happen (calling the item's Runnable or sending a message to
3360 * its Handler as appropriate). You can use this method for any items
3361 * for which you would like to do processing without those other
3362 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003365 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 * @return boolean Return false to allow normal menu processing to
3370 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003371 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 * @see #onCreateOptionsMenu
3373 */
3374 public boolean onOptionsItemSelected(MenuItem item) {
3375 if (mParent != null) {
3376 return mParent.onOptionsItemSelected(item);
3377 }
3378 return false;
3379 }
3380
3381 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003382 * This method is called whenever the user chooses to navigate Up within your application's
3383 * activity hierarchy from the action bar.
3384 *
3385 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3386 * was specified in the manifest for this activity or an activity-alias to it,
3387 * default Up navigation will be handled automatically. If any activity
3388 * along the parent chain requires extra Intent arguments, the Activity subclass
3389 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3390 * to supply those arguments.</p>
3391 *
Mark Lufa434852016-08-11 17:40:33 -07003392 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07003393 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3394 * from the design guide for more information about navigating within your app.</p>
3395 *
3396 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3397 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3398 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3399 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3400 *
3401 * @return true if Up navigation completed successfully and this Activity was finished,
3402 * false otherwise.
3403 */
3404 public boolean onNavigateUp() {
3405 // Automatically handle hierarchical Up navigation if the proper
3406 // metadata is available.
3407 Intent upIntent = getParentActivityIntent();
3408 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003409 if (mActivityInfo.taskAffinity == null) {
3410 // Activities with a null affinity are special; they really shouldn't
3411 // specify a parent activity intent in the first place. Just finish
3412 // the current activity and call it a day.
3413 finish();
3414 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003415 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003416 onCreateNavigateUpTaskStack(b);
3417 onPrepareNavigateUpTaskStack(b);
3418 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003419
3420 // We can't finishAffinity if we have a result.
3421 // Fall back and simply finish the current activity instead.
3422 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3423 // Tell the developer what's going on to avoid hair-pulling.
3424 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3425 finish();
3426 } else {
3427 finishAffinity();
3428 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003429 } else {
3430 navigateUpTo(upIntent);
3431 }
3432 return true;
3433 }
3434 return false;
3435 }
3436
3437 /**
3438 * This is called when a child activity of this one attempts to navigate up.
3439 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3440 *
3441 * @param child The activity making the call.
3442 */
3443 public boolean onNavigateUpFromChild(Activity child) {
3444 return onNavigateUp();
3445 }
3446
3447 /**
3448 * Define the synthetic task stack that will be generated during Up navigation from
3449 * a different task.
3450 *
3451 * <p>The default implementation of this method adds the parent chain of this activity
3452 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3453 * may choose to override this method to construct the desired task stack in a different
3454 * way.</p>
3455 *
Adam Powellf0195952012-05-02 21:38:54 -07003456 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3457 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3458 * returned by {@link #getParentActivityIntent()}.</p>
3459 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003460 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3461 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3462 *
3463 * @param builder An empty TaskStackBuilder - the application should add intents representing
3464 * the desired task stack
3465 */
3466 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3467 builder.addParentStack(this);
3468 }
3469
3470 /**
3471 * Prepare the synthetic task stack that will be generated during Up navigation
3472 * from a different task.
3473 *
3474 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3475 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3476 * If any extra data should be added to these intents before launching the new task,
3477 * the application should override this method and add that data here.</p>
3478 *
3479 * @param builder A TaskStackBuilder that has been populated with Intents by
3480 * onCreateNavigateUpTaskStack.
3481 */
3482 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3483 }
3484
3485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 * This hook is called whenever the options menu is being closed (either by the user canceling
3487 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 * @param menu The options menu as last shown or first initialized by
3490 * onCreateOptionsMenu().
3491 */
3492 public void onOptionsMenuClosed(Menu menu) {
3493 if (mParent != null) {
3494 mParent.onOptionsMenuClosed(menu);
3495 }
3496 }
RoboErik55011652014-07-09 15:05:53 -07003497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 /**
3499 * Programmatically opens the options menu. If the options menu is already
3500 * open, this method does nothing.
3501 */
3502 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003503 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3504 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003505 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
RoboErik55011652014-07-09 15:05:53 -07003508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 /**
3510 * Progammatically closes the options menu. If the options menu is already
3511 * closed, this method does nothing.
3512 */
3513 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003514 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
3515 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518
3519 /**
3520 * Called when a context menu for the {@code view} is about to be shown.
3521 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3522 * time the context menu is about to be shown and should be populated for
3523 * the view (or item inside the view for {@link AdapterView} subclasses,
3524 * this can be found in the {@code menuInfo})).
3525 * <p>
3526 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3527 * item has been selected.
3528 * <p>
3529 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003530 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 */
3532 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3533 }
3534
3535 /**
3536 * Registers a context menu to be shown for the given view (multiple views
3537 * can show the context menu). This method will set the
3538 * {@link OnCreateContextMenuListener} on the view to this activity, so
3539 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3540 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 * @see #unregisterForContextMenu(View)
3543 * @param view The view that should show a context menu.
3544 */
3545 public void registerForContextMenu(View view) {
3546 view.setOnCreateContextMenuListener(this);
3547 }
RoboErik55011652014-07-09 15:05:53 -07003548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 /**
3550 * Prevents a context menu to be shown for the given view. This method will remove the
3551 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 * @see #registerForContextMenu(View)
3554 * @param view The view that should stop showing a context menu.
3555 */
3556 public void unregisterForContextMenu(View view) {
3557 view.setOnCreateContextMenuListener(null);
3558 }
RoboErik55011652014-07-09 15:05:53 -07003559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 /**
3561 * Programmatically opens the context menu for a particular {@code view}.
3562 * The {@code view} should have been added via
3563 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003564 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 * @param view The view to show the context menu for.
3566 */
3567 public void openContextMenu(View view) {
3568 view.showContextMenu();
3569 }
RoboErik55011652014-07-09 15:05:53 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 /**
3572 * Programmatically closes the most recently opened context menu, if showing.
3573 */
3574 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003575 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3576 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 }
RoboErik55011652014-07-09 15:05:53 -07003579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 /**
3581 * This hook is called whenever an item in a context menu is selected. The
3582 * default implementation simply returns false to have the normal processing
3583 * happen (calling the item's Runnable or sending a message to its Handler
3584 * as appropriate). You can use this method for any items for which you
3585 * would like to do processing without those other facilities.
3586 * <p>
3587 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3588 * View that added this menu item.
3589 * <p>
3590 * Derived classes should call through to the base class for it to perform
3591 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 * @param item The context menu item that was selected.
3594 * @return boolean Return false to allow normal context menu processing to
3595 * proceed, true to consume it here.
3596 */
3597 public boolean onContextItemSelected(MenuItem item) {
3598 if (mParent != null) {
3599 return mParent.onContextItemSelected(item);
3600 }
3601 return false;
3602 }
3603
3604 /**
3605 * This hook is called whenever the context menu is being closed (either by
3606 * the user canceling the menu with the back/menu button, or when an item is
3607 * selected).
RoboErik55011652014-07-09 15:05:53 -07003608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 * @param menu The context menu that is being closed.
3610 */
3611 public void onContextMenuClosed(Menu menu) {
3612 if (mParent != null) {
3613 mParent.onContextMenuClosed(menu);
3614 }
3615 }
3616
3617 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003618 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003620 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 protected Dialog onCreateDialog(int id) {
3622 return null;
3623 }
3624
3625 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003626 * Callback for creating dialogs that are managed (saved and restored) for you
3627 * by the activity. The default implementation calls through to
3628 * {@link #onCreateDialog(int)} for compatibility.
3629 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003630 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3631 * or later, consider instead using a {@link DialogFragment} instead.</em>
3632 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003633 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3634 * this method the first time, and hang onto it thereafter. Any dialog
3635 * that is created by this method will automatically be saved and restored
3636 * for you, including whether it is showing.
3637 *
3638 * <p>If you would like the activity to manage saving and restoring dialogs
3639 * for you, you should override this method and handle any ids that are
3640 * passed to {@link #showDialog}.
3641 *
3642 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3643 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3644 *
3645 * @param id The id of the dialog.
3646 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3647 * @return The dialog. If you return null, the dialog will not be created.
3648 *
3649 * @see #onPrepareDialog(int, Dialog, Bundle)
3650 * @see #showDialog(int, Bundle)
3651 * @see #dismissDialog(int)
3652 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003653 *
3654 * @deprecated Use the new {@link DialogFragment} class with
3655 * {@link FragmentManager} instead; this is also
3656 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003657 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003658 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003659 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003660 protected Dialog onCreateDialog(int id, Bundle args) {
3661 return onCreateDialog(id);
3662 }
3663
3664 /**
3665 * @deprecated Old no-arguments version of
3666 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3667 */
3668 @Deprecated
3669 protected void onPrepareDialog(int id, Dialog dialog) {
3670 dialog.setOwnerActivity(this);
3671 }
3672
3673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003675 * shown. The default implementation calls through to
3676 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003677 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 * <p>
3679 * Override this if you need to update a managed dialog based on the state
3680 * of the application each time it is shown. For example, a time picker
3681 * dialog might want to be updated with the current time. You should call
3682 * through to the superclass's implementation. The default implementation
3683 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 * @param id The id of the managed dialog.
3686 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003687 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3688 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 * @see #showDialog(int)
3690 * @see #dismissDialog(int)
3691 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003692 *
3693 * @deprecated Use the new {@link DialogFragment} class with
3694 * {@link FragmentManager} instead; this is also
3695 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003697 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003698 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3699 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 }
3701
3702 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003703 * Simple version of {@link #showDialog(int, Bundle)} that does not
3704 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3705 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003706 *
3707 * @deprecated Use the new {@link DialogFragment} class with
3708 * {@link FragmentManager} instead; this is also
3709 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003710 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003711 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003712 public final void showDialog(int id) {
3713 showDialog(id, null);
3714 }
3715
3716 /**
3717 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 * will be made with the same id the first time this is called for a given
3719 * id. From thereafter, the dialog will be automatically saved and restored.
3720 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003721 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3722 * or later, consider instead using a {@link DialogFragment} instead.</em>
3723 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003724 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 * be made to provide an opportunity to do any timely preparation.
3726 *
3727 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003728 * @param args Arguments to pass through to the dialog. These will be saved
3729 * and restored for you. Note that if the dialog is already created,
3730 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3731 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003732 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003733 * @return Returns true if the Dialog was created; false is returned if
3734 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003735 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003736 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003737 * @see #onCreateDialog(int, Bundle)
3738 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 * @see #dismissDialog(int)
3740 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003741 *
3742 * @deprecated Use the new {@link DialogFragment} class with
3743 * {@link FragmentManager} instead; this is also
3744 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003746 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003747 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003749 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003751 ManagedDialog md = mManagedDialogs.get(id);
3752 if (md == null) {
3753 md = new ManagedDialog();
3754 md.mDialog = createDialog(id, null, args);
3755 if (md.mDialog == null) {
3756 return false;
3757 }
3758 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 }
RoboErik55011652014-07-09 15:05:53 -07003760
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003761 md.mArgs = args;
3762 onPrepareDialog(id, md.mDialog, args);
3763 md.mDialog.show();
3764 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 }
3766
3767 /**
3768 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3769 *
3770 * @param id The id of the managed dialog.
3771 *
3772 * @throws IllegalArgumentException if the id was not previously shown via
3773 * {@link #showDialog(int)}.
3774 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003775 * @see #onCreateDialog(int, Bundle)
3776 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 * @see #showDialog(int)
3778 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003779 *
3780 * @deprecated Use the new {@link DialogFragment} class with
3781 * {@link FragmentManager} instead; this is also
3782 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003784 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 public final void dismissDialog(int id) {
3786 if (mManagedDialogs == null) {
3787 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 }
RoboErik55011652014-07-09 15:05:53 -07003789
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003790 final ManagedDialog md = mManagedDialogs.get(id);
3791 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 throw missingDialog(id);
3793 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003794 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 }
3796
3797 /**
3798 * Creates an exception to throw if a user passed in a dialog id that is
3799 * unexpected.
3800 */
3801 private IllegalArgumentException missingDialog(int id) {
3802 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3803 + "shown via Activity#showDialog");
3804 }
3805
3806 /**
3807 * Removes any internal references to a dialog managed by this Activity.
3808 * If the dialog is showing, it will dismiss it as part of the clean up.
3809 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003810 * <p>This can be useful if you know that you will never show a dialog again and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 * want to avoid the overhead of saving and restoring it in the future.
3812 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003813 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3814 * will not throw an exception if you try to remove an ID that does not
3815 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 * @param id The id of the managed dialog.
3818 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003819 * @see #onCreateDialog(int, Bundle)
3820 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 * @see #showDialog(int)
3822 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003823 *
3824 * @deprecated Use the new {@link DialogFragment} class with
3825 * {@link FragmentManager} instead; this is also
3826 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003828 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003830 if (mManagedDialogs != null) {
3831 final ManagedDialog md = mManagedDialogs.get(id);
3832 if (md != null) {
3833 md.mDialog.dismiss();
3834 mManagedDialogs.remove(id);
3835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
3838
3839 /**
3840 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003841 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003842 * <p>You can use this function as a simple way to launch the search UI, in response to a
RoboErik55011652014-07-09 15:05:53 -07003843 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003844 * calling this function is the same as calling
3845 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3846 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 * <p>You can override this function to force global search, e.g. in response to a dedicated
3849 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003850 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003851 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3852 * implementation changes to simply return false and you must supply your own custom
3853 * implementation if you want to support search.</p>
3854 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003855 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003856 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3857 * not respond to search. The default implementation always returns {@code true}, except
3858 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 * @see android.app.SearchManager
3861 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003862 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
3863 mSearchEvent = searchEvent;
3864 boolean result = onSearchRequested();
3865 mSearchEvent = null;
3866 return result;
3867 }
3868
3869 /**
3870 * @see #onSearchRequested(SearchEvent)
3871 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003873 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3874 != Configuration.UI_MODE_TYPE_TELEVISION) {
3875 startSearch(null, false, null, false);
3876 return true;
3877 } else {
3878 return false;
3879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
RoboErik55011652014-07-09 15:05:53 -07003881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003883 * During the onSearchRequested() callbacks, this function will return the
3884 * {@link SearchEvent} that triggered the callback, if it exists.
3885 *
3886 * @return SearchEvent The SearchEvent that triggered the {@link
3887 * #onSearchRequested} callback.
3888 */
3889 public final SearchEvent getSearchEvent() {
3890 return mSearchEvent;
3891 }
3892
3893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003895 *
3896 * <p>It is typically called from onSearchRequested(), either directly from
3897 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003899 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 * is to inject specific data such as context data, it is preferred to <i>override</i>
3901 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003902 *
3903 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003905 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 * any further typing will replace it. This is useful for cases where an entire pre-formed
3907 * query is being inserted. If false, the selection point will be placed at the end of the
3908 * inserted query. This is useful when the inserted query is text that the user entered,
3909 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3910 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003911 * @param appSearchData An application can insert application-specific
3912 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 * searches. This data will be returned with SEARCH intent(s). Null if
3914 * no extra data is required.
3915 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003916 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003917 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 * @see android.app.SearchManager
3921 * @see #onSearchRequested
3922 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003923 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3924 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003925 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003926 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003927 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929
3930 /**
krosaend2d60142009-08-17 08:56:48 -07003931 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3932 * the search dialog. Made available for testing purposes.
3933 *
3934 * @param query The query to trigger. If empty, the request will be ignored.
3935 * @param appSearchData An application can insert application-specific
3936 * context here, in order to improve quality or specificity of its own
3937 * searches. This data will be returned with SEARCH intent(s). Null if
3938 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003939 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003940 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003941 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003942 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003943 }
3944
3945 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 * Request that key events come to this activity. Use this if your
3947 * activity has no views with focus, but the activity still wants
3948 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003949 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 * @see android.view.Window#takeKeyEvents
3951 */
3952 public void takeKeyEvents(boolean get) {
3953 getWindow().takeKeyEvents(get);
3954 }
3955
3956 /**
3957 * Enable extended window features. This is a convenience for calling
3958 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003959 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 * @param featureId The desired feature as defined in
3961 * {@link android.view.Window}.
3962 * @return Returns true if the requested feature is supported and now
3963 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003964 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003965 * @see android.view.Window#requestFeature
3966 */
3967 public final boolean requestWindowFeature(int featureId) {
3968 return getWindow().requestFeature(featureId);
3969 }
3970
3971 /**
3972 * Convenience for calling
3973 * {@link android.view.Window#setFeatureDrawableResource}.
3974 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003975 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 getWindow().setFeatureDrawableResource(featureId, resId);
3977 }
3978
3979 /**
3980 * Convenience for calling
3981 * {@link android.view.Window#setFeatureDrawableUri}.
3982 */
3983 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3984 getWindow().setFeatureDrawableUri(featureId, uri);
3985 }
3986
3987 /**
3988 * Convenience for calling
3989 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3990 */
3991 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3992 getWindow().setFeatureDrawable(featureId, drawable);
3993 }
3994
3995 /**
3996 * Convenience for calling
3997 * {@link android.view.Window#setFeatureDrawableAlpha}.
3998 */
3999 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4000 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4001 }
4002
4003 /**
4004 * Convenience for calling
4005 * {@link android.view.Window#getLayoutInflater}.
4006 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004007 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 public LayoutInflater getLayoutInflater() {
4009 return getWindow().getLayoutInflater();
4010 }
4011
4012 /**
4013 * Returns a {@link MenuInflater} with this context.
4014 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004015 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004017 // Make sure that action views can get an appropriate theme.
4018 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004019 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004020 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004021 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004022 } else {
4023 mMenuInflater = new MenuInflater(this);
4024 }
4025 }
4026 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
4028
4029 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004030 public void setTheme(int resid) {
4031 super.setTheme(resid);
4032 mWindow.setTheme(resid);
4033 }
4034
4035 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004036 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004037 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 if (mParent == null) {
4039 super.onApplyThemeResource(theme, resid, first);
4040 } else {
4041 try {
4042 theme.setTo(mParent.getTheme());
4043 } catch (Exception e) {
4044 // Empty
4045 }
4046 theme.applyStyle(resid, false);
4047 }
Winson073a5262016-02-29 16:04:40 -08004048
4049 // Get the primary color and update the TaskDescription for this activity
4050 TypedArray a = theme.obtainStyledAttributes(
4051 com.android.internal.R.styleable.ActivityTaskDescription);
4052 if (mTaskDescription.getPrimaryColor() == 0) {
4053 int colorPrimary = a.getColor(
4054 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4055 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4056 mTaskDescription.setPrimaryColor(colorPrimary);
4057 }
4058 }
4059 // For dev-preview only.
4060 if (mTaskDescription.getBackgroundColor() == 0) {
4061 int colorBackground = a.getColor(
4062 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4063 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4064 mTaskDescription.setBackgroundColor(colorBackground);
4065 }
4066 }
4067 a.recycle();
4068 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004069 }
4070
4071 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004072 * Requests permissions to be granted to this application. These permissions
4073 * must be requested in your manifest, they should not be granted to your app,
4074 * and they should have protection level {@link android.content.pm.PermissionInfo
4075 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4076 * the platform or a third-party app.
4077 * <p>
4078 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4079 * are granted at install time if requested in the manifest. Signature permissions
4080 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4081 * install time if requested in the manifest and the signature of your app matches
4082 * the signature of the app declaring the permissions.
4083 * </p>
4084 * <p>
4085 * If your app does not have the requested permissions the user will be presented
4086 * with UI for accepting them. After the user has accepted or rejected the
4087 * requested permissions you will receive a callback on {@link
4088 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4089 * permissions were granted or not.
4090 * </p>
4091 * <p>
4092 * Note that requesting a permission does not guarantee it will be granted and
4093 * your app should be able to run without having this permission.
4094 * </p>
4095 * <p>
4096 * This method may start an activity allowing the user to choose which permissions
4097 * to grant and which to reject. Hence, you should be prepared that your activity
4098 * may be paused and resumed. Further, granting some permissions may require
4099 * a restart of you application. In such a case, the system will recreate the
4100 * activity stack before delivering the result to {@link
4101 * #onRequestPermissionsResult(int, String[], int[])}.
4102 * </p>
4103 * <p>
4104 * When checking whether you have a permission you should use {@link
4105 * #checkSelfPermission(String)}.
4106 * </p>
4107 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004108 * Calling this API for permissions already granted to your app would show UI
4109 * to the user to decide whether the app can still hold these permissions. This
4110 * can be useful if the way your app uses data guarded by the permissions
4111 * changes significantly.
4112 * </p>
4113 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004114 * You cannot request a permission if your activity sets {@link
4115 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4116 * <code>true</code> because in this case the activity would not receive
4117 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4118 * </p>
4119 * <p>
Trevor Johns682c24e2016-04-12 10:13:47 -07004120 * The <a href="http://developer.android.com/samples/RuntimePermissions/index.html">
4121 * RuntimePermissions</a> sample app demonstrates how to use this method to
4122 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004123 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004124 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004125 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004126 * @param requestCode Application specific request code to match with a result
4127 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004128 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004129 *
Paul Miller77a0e132016-02-11 13:46:49 -08004130 * @throws IllegalArgumentException if requestCode is negative.
4131 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004132 * @see #onRequestPermissionsResult(int, String[], int[])
4133 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004134 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004135 */
4136 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004137 if (requestCode < 0) {
4138 throw new IllegalArgumentException("requestCode should be >= 0");
4139 }
Svetoslavffb32b12015-10-15 16:54:00 -07004140 if (mHasCurrentPermissionsRequest) {
4141 Log.w(TAG, "Can reqeust only one set of permissions at a time");
4142 // Dispatch the callback with empty arrays which means a cancellation.
4143 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4144 return;
4145 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004146 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004147 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004148 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004149 }
4150
4151 /**
4152 * Callback for the result from requesting permissions. This method
4153 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004154 * <p>
4155 * <strong>Note:</strong> It is possible that the permissions request interaction
4156 * with the user is interrupted. In this case you will receive empty permissions
4157 * and results arrays which should be treated as a cancellation.
4158 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004159 *
4160 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4161 * @param permissions The requested permissions. Never null.
4162 * @param grantResults The grant results for the corresponding permissions
4163 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4164 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4165 *
4166 * @see #requestPermissions(String[], int)
4167 */
4168 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4169 @NonNull int[] grantResults) {
4170 /* callback - no nothing */
4171 }
4172
4173 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004174 * Gets whether you should show UI with rationale for requesting a permission.
4175 * You should do this only if you do not have the permission and the context in
4176 * which the permission is requested does not clearly communicate to the user
4177 * what would be the benefit from granting this permission.
4178 * <p>
4179 * For example, if you write a camera app, requesting the camera permission
4180 * would be expected by the user and no rationale for why it is requested is
4181 * needed. If however, the app needs location for tagging photos then a non-tech
4182 * savvy user may wonder how location is related to taking photos. In this case
4183 * you may choose to show UI with rationale of requesting this permission.
4184 * </p>
4185 *
4186 * @param permission A permission your app wants to request.
4187 * @return Whether you can show permission rationale UI.
4188 *
4189 * @see #checkSelfPermission(String)
4190 * @see #requestPermissions(String[], int)
4191 * @see #onRequestPermissionsResult(int, String[], int[])
4192 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004193 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004194 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4195 }
4196
4197 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004198 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4199 * with no options.
4200 *
4201 * @param intent The intent to start.
4202 * @param requestCode If >= 0, this code will be returned in
4203 * onActivityResult() when the activity exits.
4204 *
4205 * @throws android.content.ActivityNotFoundException
4206 *
George Mount0a778ed2013-12-13 13:35:36 -08004207 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004208 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004209 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004210 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004211 }
4212
4213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 * Launch an activity for which you would like a result when it finished.
4215 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004216 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004217 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004219 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 * <p>Note that this method should only be used with Intent protocols
4221 * that are defined to return a result. In other protocols (such as
4222 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4223 * not get the result when you expect. For example, if the activity you
4224 * are launching uses the singleTask launch mode, it will not run in your
4225 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004226 *
RoboErik55011652014-07-09 15:05:53 -07004227 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004229 * activity, then your window will not be displayed until a result is
4230 * returned back from the started activity. This is to avoid visible
4231 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 * <p>This method throws {@link android.content.ActivityNotFoundException}
4234 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 * @param intent The intent to start.
4237 * @param requestCode If >= 0, this code will be returned in
4238 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004239 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004240 * See {@link android.content.Context#startActivity(Intent, Bundle)
4241 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004244 *
George Mount0a778ed2013-12-13 13:35:36 -08004245 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004247 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4248 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07004250 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 Instrumentation.ActivityResult ar =
4252 mInstrumentation.execStartActivity(
4253 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004254 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 if (ar != null) {
4256 mMainThread.sendActivityResult(
4257 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4258 ar.getResultData());
4259 }
4260 if (requestCode >= 0) {
4261 // If this start is requesting a result, we can avoid making
4262 // the activity visible until the result is received. Setting
4263 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4264 // activity hidden during this time, to avoid flickering.
4265 // This can only be done when a result is requested because
4266 // that guarantees we will get information back when the
4267 // activity is finished, no matter what happens to it.
4268 mStartedActivity = true;
4269 }
Adam Powell14874662013-07-18 19:42:41 -07004270
George Mount41725de2015-04-09 08:23:05 -07004271 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004272 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004274 if (options != null) {
4275 mParent.startActivityFromChild(this, intent, requestCode, options);
4276 } else {
4277 // Note we want to go through this method for compatibility with
4278 // existing applications that may have overridden it.
4279 mParent.startActivityFromChild(this, intent, requestCode);
4280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 }
George Mount41725de2015-04-09 08:23:05 -07004282 }
4283
4284 /**
4285 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4286 *
4287 * @param options The ActivityOptions bundle used to start an Activity.
4288 */
4289 private void cancelInputsAndStartExitTransition(Bundle options) {
4290 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4291 if (decor != null) {
4292 decor.cancelPendingInputEvents();
4293 }
George Mountff243282014-07-07 16:12:07 -07004294 if (options != null && !isTopOfTask()) {
4295 mActivityTransitionState.startExitOutTransition(this, options);
4296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 }
4298
George Mount413739e2016-06-08 07:13:37 -07004299 private Bundle transferSpringboardActivityOptions(Bundle options) {
4300 if (options == null && (mWindow != null && !mWindow.isActive())) {
4301 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07004302 if (activityOptions != null &&
4303 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07004304 return activityOptions.toBundle();
4305 }
4306 }
4307 return options;
4308 }
4309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004311 * @hide Implement to provide correct calling token.
4312 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01004313 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4314 startActivityForResultAsUser(intent, requestCode, null, user);
4315 }
4316
4317 /**
4318 * @hide Implement to provide correct calling token.
4319 */
4320 public void startActivityForResultAsUser(Intent intent, int requestCode,
4321 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004322 if (mParent != null) {
4323 throw new RuntimeException("Can't be called from a child");
4324 }
George Mount413739e2016-06-08 07:13:37 -07004325 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004326 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
4327 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
4328 options, user);
4329 if (ar != null) {
4330 mMainThread.sendActivityResult(
4331 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4332 }
4333 if (requestCode >= 0) {
4334 // If this start is requesting a result, we can avoid making
4335 // the activity visible until the result is received. Setting
4336 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4337 // activity hidden during this time, to avoid flickering.
4338 // This can only be done when a result is requested because
4339 // that guarantees we will get information back when the
4340 // activity is finished, no matter what happens to it.
4341 mStartedActivity = true;
4342 }
4343
George Mount41725de2015-04-09 08:23:05 -07004344 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004345 }
4346
4347 /**
4348 * @hide Implement to provide correct calling token.
4349 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004350 public void startActivityAsUser(Intent intent, UserHandle user) {
4351 startActivityAsUser(intent, null, user);
4352 }
4353
4354 /**
4355 * @hide Implement to provide correct calling token.
4356 */
4357 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4358 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004359 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004360 }
George Mount413739e2016-06-08 07:13:37 -07004361 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004362 Instrumentation.ActivityResult ar =
4363 mInstrumentation.execStartActivity(
4364 this, mMainThread.getApplicationThread(), mToken, this,
4365 intent, -1, options, user);
4366 if (ar != null) {
4367 mMainThread.sendActivityResult(
4368 mToken, mEmbeddedID, -1, ar.getResultCode(),
4369 ar.getResultData());
4370 }
George Mount41725de2015-04-09 08:23:05 -07004371 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004372 }
4373
4374 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004375 * Start a new activity as if it was started by the activity that started our
4376 * current activity. This is for the resolver and chooser activities, which operate
4377 * as intermediaries that dispatch their intent to the target the user selects -- to
4378 * do this, they must perform all security checks including permission grants as if
4379 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004380 * @param intent The Intent to start.
4381 * @param options ActivityOptions or null.
4382 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4383 * caller it is doing the start is, is actually allowed to start the target activity.
4384 * If you set this to true, you must set an explicit component in the Intent and do any
4385 * appropriate security checks yourself.
4386 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004387 * @hide
4388 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004389 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
4390 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004391 if (mParent != null) {
4392 throw new RuntimeException("Can't be called from a child");
4393 }
George Mount413739e2016-06-08 07:13:37 -07004394 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004395 Instrumentation.ActivityResult ar =
4396 mInstrumentation.execStartActivityAsCaller(
4397 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004398 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004399 if (ar != null) {
4400 mMainThread.sendActivityResult(
4401 mToken, mEmbeddedID, -1, ar.getResultCode(),
4402 ar.getResultData());
4403 }
George Mount41725de2015-04-09 08:23:05 -07004404 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004405 }
4406
4407 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004408 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4409 * Intent, int, int, int, Bundle)} with no options.
4410 *
4411 * @param intent The IntentSender to launch.
4412 * @param requestCode If >= 0, this code will be returned in
4413 * onActivityResult() when the activity exits.
4414 * @param fillInIntent If non-null, this will be provided as the
4415 * intent parameter to {@link IntentSender#sendIntent}.
4416 * @param flagsMask Intent flags in the original IntentSender that you
4417 * would like to change.
4418 * @param flagsValues Desired values for any bits set in
4419 * <var>flagsMask</var>
4420 * @param extraFlags Always set to 0.
4421 */
4422 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004423 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004424 throws IntentSender.SendIntentException {
4425 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4426 flagsValues, extraFlags, null);
4427 }
4428
4429 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004430 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004431 * to use a IntentSender to describe the activity to be started. If
4432 * the IntentSender is for an activity, that activity will be started
4433 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4434 * here; otherwise, its associated action will be executed (such as
4435 * sending a broadcast) as if you had called
4436 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004437 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004438 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004439 * @param requestCode If >= 0, this code will be returned in
4440 * onActivityResult() when the activity exits.
4441 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004442 * intent parameter to {@link IntentSender#sendIntent}.
4443 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004444 * would like to change.
4445 * @param flagsValues Desired values for any bits set in
4446 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004447 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004448 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004449 * See {@link android.content.Context#startActivity(Intent, Bundle)
4450 * Context.startActivity(Intent, Bundle)} for more details. If options
4451 * have also been supplied by the IntentSender, options given here will
4452 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004453 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004454 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004455 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004456 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004457 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004458 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
4459 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004460 } else if (options != null) {
4461 mParent.startIntentSenderFromChild(this, intent, requestCode,
4462 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004463 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004464 // Note we want to go through this call for compatibility with
4465 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004466 mParent.startIntentSenderFromChild(this, intent, requestCode,
4467 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004468 }
4469 }
4470
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004471 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
4472 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004473 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004474 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004475 try {
4476 String resolvedType = null;
4477 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004478 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004479 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004480 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4481 }
4482 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004483 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004484 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004485 requestCode, flagsMask, flagsValues, options);
4486 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004487 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004488 }
4489 Instrumentation.checkStartActivityResult(result, null);
4490 } catch (RemoteException e) {
4491 }
4492 if (requestCode >= 0) {
4493 // If this start is requesting a result, we can avoid making
4494 // the activity visible until the result is received. Setting
4495 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4496 // activity hidden during this time, to avoid flickering.
4497 // This can only be done when a result is requested because
4498 // that guarantees we will get information back when the
4499 // activity is finished, no matter what happens to it.
4500 mStartedActivity = true;
4501 }
4502 }
4503
4504 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004505 * Same as {@link #startActivity(Intent, Bundle)} with no options
4506 * specified.
4507 *
4508 * @param intent The intent to start.
4509 *
4510 * @throws android.content.ActivityNotFoundException
4511 *
Mark Lufa434852016-08-11 17:40:33 -07004512 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004513 * @see #startActivityForResult
4514 */
4515 @Override
4516 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004517 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004518 }
4519
4520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 * Launch a new activity. You will not receive any information about when
4522 * the activity exits. This implementation overrides the base version,
4523 * providing information about
4524 * the activity performing the launch. Because of this additional
4525 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4526 * required; if not specified, the new activity will be added to the
4527 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004528 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 * <p>This method throws {@link android.content.ActivityNotFoundException}
4530 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004531 *
4532 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004533 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004534 * See {@link android.content.Context#startActivity(Intent, Bundle)
4535 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004538 *
Mark Lufa434852016-08-11 17:40:33 -07004539 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07004540 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 */
4542 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004543 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004544 if (options != null) {
4545 startActivityForResult(intent, -1, options);
4546 } else {
4547 // Note we want to go through this call for compatibility with
4548 // applications that may have overridden the method.
4549 startActivityForResult(intent, -1);
4550 }
4551 }
4552
4553 /**
4554 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4555 * specified.
4556 *
4557 * @param intents The intents to start.
4558 *
4559 * @throws android.content.ActivityNotFoundException
4560 *
Mark Lufa434852016-08-11 17:40:33 -07004561 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004562 * @see #startActivityForResult
4563 */
4564 @Override
4565 public void startActivities(Intent[] intents) {
4566 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 }
4568
4569 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004570 * Launch a new activity. You will not receive any information about when
4571 * the activity exits. This implementation overrides the base version,
4572 * providing information about
4573 * the activity performing the launch. Because of this additional
4574 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4575 * required; if not specified, the new activity will be added to the
4576 * task of the caller.
4577 *
4578 * <p>This method throws {@link android.content.ActivityNotFoundException}
4579 * if there was no Activity found to run the given Intent.
4580 *
4581 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004582 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004583 * See {@link android.content.Context#startActivity(Intent, Bundle)
4584 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004585 *
4586 * @throws android.content.ActivityNotFoundException
4587 *
Mark Lufa434852016-08-11 17:40:33 -07004588 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004589 * @see #startActivityForResult
4590 */
4591 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004592 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004593 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004594 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004595 }
4596
4597 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004598 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4599 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004600 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004601 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004602 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004603 * intent parameter to {@link IntentSender#sendIntent}.
4604 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004605 * would like to change.
4606 * @param flagsValues Desired values for any bits set in
4607 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004608 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004609 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004610 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004611 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004612 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004613 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4614 extraFlags, null);
4615 }
4616
4617 /**
4618 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4619 * to start; see
4620 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4621 * for more information.
4622 *
4623 * @param intent The IntentSender to launch.
4624 * @param fillInIntent If non-null, this will be provided as the
4625 * intent parameter to {@link IntentSender#sendIntent}.
4626 * @param flagsMask Intent flags in the original IntentSender that you
4627 * would like to change.
4628 * @param flagsValues Desired values for any bits set in
4629 * <var>flagsMask</var>
4630 * @param extraFlags Always set to 0.
4631 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004632 * See {@link android.content.Context#startActivity(Intent, Bundle)
4633 * Context.startActivity(Intent, Bundle)} for more details. If options
4634 * have also been supplied by the IntentSender, options given here will
4635 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004636 */
4637 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004638 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004639 Bundle options) throws IntentSender.SendIntentException {
4640 if (options != null) {
4641 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4642 flagsValues, extraFlags, options);
4643 } else {
4644 // Note we want to go through this call for compatibility with
4645 // applications that may have overridden the method.
4646 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4647 flagsValues, extraFlags);
4648 }
4649 }
4650
4651 /**
4652 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4653 * with no options.
4654 *
4655 * @param intent The intent to start.
4656 * @param requestCode If >= 0, this code will be returned in
4657 * onActivityResult() when the activity exits, as described in
4658 * {@link #startActivityForResult}.
4659 *
4660 * @return If a new activity was launched then true is returned; otherwise
4661 * false is returned and you must handle the Intent yourself.
4662 *
4663 * @see #startActivity
4664 * @see #startActivityForResult
4665 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004666 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4667 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004668 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004669 }
4670
4671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 * A special variation to launch an activity only if a new activity
4673 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004674 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004676 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004678 * and the activity
4679 * that handles <var>intent</var> is the same as your currently running
4680 * activity, then a new instance is not needed. In this case, instead of
4681 * the normal behavior of calling {@link #onNewIntent} this function will
4682 * return and you can handle the Intent yourself.
4683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 * <p>This function can only be called from a top-level activity; if it is
4685 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004687 * @param intent The intent to start.
4688 * @param requestCode If >= 0, this code will be returned in
4689 * onActivityResult() when the activity exits, as described in
4690 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004691 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004692 * See {@link android.content.Context#startActivity(Intent, Bundle)
4693 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004694 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 * @return If a new activity was launched then true is returned; otherwise
4696 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004697 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 * @see #startActivity
4699 * @see #startActivityForResult
4700 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004701 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4702 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004704 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004706 Uri referrer = onProvideReferrer();
4707 if (referrer != null) {
4708 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4709 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004710 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004711 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004713 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004714 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4715 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4716 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 } catch (RemoteException e) {
4718 // Empty
4719 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 if (requestCode >= 0) {
4724 // If this start is requesting a result, we can avoid making
4725 // the activity visible until the result is received. Setting
4726 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4727 // activity hidden during this time, to avoid flickering.
4728 // This can only be done when a result is requested because
4729 // that guarantees we will get information back when the
4730 // activity is finished, no matter what happens to it.
4731 mStartedActivity = true;
4732 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004733 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 }
4735
4736 throw new UnsupportedOperationException(
4737 "startActivityIfNeeded can only be called from a top-level activity");
4738 }
4739
4740 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004741 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4742 * no options.
4743 *
4744 * @param intent The intent to dispatch to the next activity. For
4745 * correct behavior, this must be the same as the Intent that started
4746 * your own activity; the only changes you can make are to the extras
4747 * inside of it.
4748 *
4749 * @return Returns a boolean indicating whether there was another Activity
4750 * to start: true if there was a next activity to start, false if there
4751 * wasn't. In general, if true is returned you will then want to call
4752 * finish() on yourself.
4753 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004754 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004755 return startNextMatchingActivity(intent, null);
4756 }
4757
4758 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 * Special version of starting an activity, for use when you are replacing
4760 * other activity components. You can use this to hand the Intent off
4761 * to the next Activity that can handle it. You typically call this in
4762 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 * @param intent The intent to dispatch to the next activity. For
4765 * correct behavior, this must be the same as the Intent that started
4766 * your own activity; the only changes you can make are to the extras
4767 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004768 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004769 * See {@link android.content.Context#startActivity(Intent, Bundle)
4770 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004771 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 * @return Returns a boolean indicating whether there was another Activity
4773 * to start: true if there was a next activity to start, false if there
4774 * wasn't. In general, if true is returned you will then want to call
4775 * finish() on yourself.
4776 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004777 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
4778 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 if (mParent == null) {
4780 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004781 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004782 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004784 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 } catch (RemoteException e) {
4786 // Empty
4787 }
4788 return false;
4789 }
4790
4791 throw new UnsupportedOperationException(
4792 "startNextMatchingActivity can only be called from a top-level activity");
4793 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004794
4795 /**
4796 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4797 * with no options.
4798 *
4799 * @param child The activity making the call.
4800 * @param intent The intent to start.
4801 * @param requestCode Reply request code. < 0 if reply is not requested.
4802 *
4803 * @throws android.content.ActivityNotFoundException
4804 *
4805 * @see #startActivity
4806 * @see #startActivityForResult
4807 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004808 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004809 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004810 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004811 }
4812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 /**
RoboErik55011652014-07-09 15:05:53 -07004814 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 * <p>This method throws {@link android.content.ActivityNotFoundException}
4818 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004819 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 * @param child The activity making the call.
4821 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004822 * @param requestCode Reply request code. < 0 if reply is not requested.
4823 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004824 * See {@link android.content.Context#startActivity(Intent, Bundle)
4825 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004826 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004828 *
4829 * @see #startActivity
4830 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004832 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004833 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07004834 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 Instrumentation.ActivityResult ar =
4836 mInstrumentation.execStartActivity(
4837 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004838 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 if (ar != null) {
4840 mMainThread.sendActivityResult(
4841 mToken, child.mEmbeddedID, requestCode,
4842 ar.getResultCode(), ar.getResultData());
4843 }
George Mount41725de2015-04-09 08:23:05 -07004844 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 }
4846
4847 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004848 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4849 * with no options.
4850 *
4851 * @param fragment The fragment making the call.
4852 * @param intent The intent to start.
4853 * @param requestCode Reply request code. < 0 if reply is not requested.
4854 *
4855 * @throws android.content.ActivityNotFoundException
4856 *
4857 * @see Fragment#startActivity
4858 * @see Fragment#startActivityForResult
4859 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004860 public void startActivityFromFragment(@NonNull Fragment fragment,
4861 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004862 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004863 }
4864
4865 /**
RoboErik55011652014-07-09 15:05:53 -07004866 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004867 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4868 * method.
RoboErik55011652014-07-09 15:05:53 -07004869 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004870 * <p>This method throws {@link android.content.ActivityNotFoundException}
4871 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004872 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004873 * @param fragment The fragment making the call.
4874 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004875 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004876 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004877 * See {@link android.content.Context#startActivity(Intent, Bundle)
4878 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004879 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004880 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004881 *
4882 * @see Fragment#startActivity
4883 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004884 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004885 public void startActivityFromFragment(@NonNull Fragment fragment,
4886 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004887 startActivityForResult(fragment.mWho, intent, requestCode, options);
4888 }
4889
4890 /**
4891 * @hide
4892 */
4893 @Override
4894 public void startActivityForResult(
4895 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004896 Uri referrer = onProvideReferrer();
4897 if (referrer != null) {
4898 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4899 }
George Mount413739e2016-06-08 07:13:37 -07004900 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004901 Instrumentation.ActivityResult ar =
4902 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004903 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004904 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004905 if (ar != null) {
4906 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004907 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004908 ar.getResultCode(), ar.getResultData());
4909 }
George Mount41725de2015-04-09 08:23:05 -07004910 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004911 }
4912
4913 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004914 * @hide
4915 */
4916 @Override
4917 public boolean canStartActivityForResult() {
4918 return true;
4919 }
4920
4921 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004922 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4923 * int, Intent, int, int, int, Bundle)} with no options.
4924 */
4925 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4926 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4927 int extraFlags)
4928 throws IntentSender.SendIntentException {
4929 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4930 flagsMask, flagsValues, extraFlags, null);
4931 }
4932
4933 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004934 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004935 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004936 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004937 * for more information.
4938 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004939 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4940 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004941 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004942 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004943 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
4944 flagsMask, flagsValues, options);
4945 }
4946
4947 /**
4948 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
4949 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
4950 * for more information.
4951 *
4952 * @hide
4953 */
4954 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
4955 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4956 int extraFlags, @Nullable Bundle options)
4957 throws IntentSender.SendIntentException {
4958 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
4959 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004960 }
4961
4962 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004963 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4964 * or {@link #finish} to specify an explicit transition animation to
4965 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004966 *
4967 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4968 * to using this with starting activities is to supply the desired animation
4969 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07004970 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004971 * you to specify a custom animation even when starting an activity from
4972 * outside the context of the current top activity.
4973 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004974 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004975 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004976 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004977 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004978 */
4979 public void overridePendingTransition(int enterAnim, int exitAnim) {
4980 try {
4981 ActivityManagerNative.getDefault().overridePendingTransition(
4982 mToken, getPackageName(), enterAnim, exitAnim);
4983 } catch (RemoteException e) {
4984 }
4985 }
RoboErik55011652014-07-09 15:05:53 -07004986
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 * Call this to set the result that your activity will return to its
4989 * caller.
RoboErik55011652014-07-09 15:05:53 -07004990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 * @param resultCode The result code to propagate back to the originating
4992 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004993 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 * @see #RESULT_CANCELED
4995 * @see #RESULT_OK
4996 * @see #RESULT_FIRST_USER
4997 * @see #setResult(int, Intent)
4998 */
4999 public final void setResult(int resultCode) {
5000 synchronized (this) {
5001 mResultCode = resultCode;
5002 mResultData = null;
5003 }
5004 }
5005
5006 /**
5007 * Call this to set the result that your activity will return to its
5008 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005009 *
5010 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5011 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5012 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5013 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5014 * Activity receiving the result access to the specific URIs in the Intent.
5015 * Access will remain until the Activity has finished (it will remain across the hosting
5016 * process being killed and other temporary destruction) and will be added
5017 * to any existing set of URI permissions it already holds.
5018 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 * @param resultCode The result code to propagate back to the originating
5020 * activity, often RESULT_CANCELED or RESULT_OK
5021 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005022 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 * @see #RESULT_CANCELED
5024 * @see #RESULT_OK
5025 * @see #RESULT_FIRST_USER
5026 * @see #setResult(int)
5027 */
5028 public final void setResult(int resultCode, Intent data) {
5029 synchronized (this) {
5030 mResultCode = resultCode;
5031 mResultData = data;
5032 }
5033 }
5034
5035 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005036 * Return information about who launched this activity. If the launching Intent
5037 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5038 * that will be returned as-is; otherwise, if known, an
5039 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5040 * package name that started the Intent will be returned. This may return null if no
5041 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5042 * application package was involved.
5043 *
5044 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5045 * return the referrer that submitted that new intent to the activity. Otherwise, it
5046 * always returns the referrer of the original Intent.</p>
5047 *
5048 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5049 * referrer information, applications can spoof it.</p>
5050 */
5051 @Nullable
5052 public Uri getReferrer() {
5053 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005054 try {
5055 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5056 if (referrer != null) {
5057 return referrer;
5058 }
5059 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5060 if (referrerName != null) {
5061 return Uri.parse(referrerName);
5062 }
5063 } catch (BadParcelableException e) {
5064 Log.w(TAG, "Cannot read referrer from intent;"
5065 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005066 }
5067 if (mReferrer != null) {
5068 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5069 }
5070 return null;
5071 }
5072
5073 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005074 * Override to generate the desired referrer for the content currently being shown
5075 * by the app. The default implementation returns null, meaning the referrer will simply
5076 * be the android-app: of the package name of this activity. Return a non-null Uri to
5077 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5078 */
5079 public Uri onProvideReferrer() {
5080 return null;
5081 }
5082
5083 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 * Return the name of the package that invoked this activity. This is who
5085 * the data in {@link #setResult setResult()} will be sent to. You can
5086 * use this information to validate that the recipient is allowed to
5087 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005088 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005089 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005090 * did not use the {@link #startActivityForResult}
5091 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005092 * null.</p>
5093 *
5094 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5095 * the result from this method was unstable. If the process hosting the calling
5096 * package was no longer running, it would return null instead of the proper package
5097 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5098 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005099 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 * @return The package of the activity that will receive your
5101 * reply, or null if none.
5102 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005103 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 public String getCallingPackage() {
5105 try {
5106 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
5107 } catch (RemoteException e) {
5108 return null;
5109 }
5110 }
5111
5112 /**
5113 * Return the name of the activity that invoked this activity. This is
5114 * who the data in {@link #setResult setResult()} will be sent to. You
5115 * can use this information to validate that the recipient is allowed to
5116 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005117 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005118 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005119 * did not use the {@link #startActivityForResult}
5120 * form that includes a request code), then the calling package will be
5121 * null.
5122 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005123 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 * reply, or null if none.
5125 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005126 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 public ComponentName getCallingActivity() {
5128 try {
5129 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
5130 } catch (RemoteException e) {
5131 return null;
5132 }
5133 }
5134
5135 /**
5136 * Control whether this activity's main window is visible. This is intended
5137 * only for the special case of an activity that is not going to show a
5138 * UI itself, but can't just finish prior to onResume() because it needs
5139 * to wait for a service binding or such. Setting this to false allows
5140 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 * <p>The default value for this is taken from the
5143 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5144 */
5145 public void setVisible(boolean visible) {
5146 if (mVisibleFromClient != visible) {
5147 mVisibleFromClient = visible;
5148 if (mVisibleFromServer) {
5149 if (visible) makeVisible();
5150 else mDecor.setVisibility(View.INVISIBLE);
5151 }
5152 }
5153 }
RoboErik55011652014-07-09 15:05:53 -07005154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 void makeVisible() {
5156 if (!mWindowAdded) {
5157 ViewManager wm = getWindowManager();
5158 wm.addView(mDecor, getWindow().getAttributes());
5159 mWindowAdded = true;
5160 }
5161 mDecor.setVisibility(View.VISIBLE);
5162 }
RoboErik55011652014-07-09 15:05:53 -07005163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 /**
5165 * Check to see whether this activity is in the process of finishing,
5166 * either because you called {@link #finish} on it or someone else
5167 * has requested that it finished. This is often used in
5168 * {@link #onPause} to determine whether the activity is simply pausing or
5169 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 * @see #finish
5174 */
5175 public boolean isFinishing() {
5176 return mFinished;
5177 }
5178
5179 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005180 * Returns true if the final {@link #onDestroy()} call has been made
5181 * on the Activity, so this instance is now dead.
5182 */
5183 public boolean isDestroyed() {
5184 return mDestroyed;
5185 }
5186
5187 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005188 * Check to see whether this activity is in the process of being destroyed in order to be
5189 * recreated with a new configuration. This is often used in
5190 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5191 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005192 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005193 * @return If the activity is being torn down in order to be recreated with a new configuration,
5194 * returns true; else returns false.
5195 */
5196 public boolean isChangingConfigurations() {
5197 return mChangingConfigurations;
5198 }
5199
5200 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005201 * Cause this Activity to be recreated with a new instance. This results
5202 * in essentially the same flow as when the Activity is created due to
5203 * a configuration change -- the current instance will go through its
5204 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5205 */
5206 public void recreate() {
5207 if (mParent != null) {
5208 throw new IllegalStateException("Can only be called on top-level activity");
5209 }
5210 if (Looper.myLooper() != mMainThread.getLooper()) {
5211 throw new IllegalStateException("Must be called from main thread");
5212 }
Chong Zhangfec694e2016-08-09 12:57:38 -07005213 try {
5214 ActivityManagerNative.getDefault().requestActivityRelaunch(mToken);
5215 } catch (RemoteException e) {
5216 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005217 }
5218
5219 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005220 * Finishes the current activity and specifies whether to remove the task associated with this
5221 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005223 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 if (mParent == null) {
5225 int resultCode;
5226 Intent resultData;
5227 synchronized (this) {
5228 resultCode = mResultCode;
5229 resultData = mResultData;
5230 }
Joe Onorato43a17652011-04-06 19:22:23 -07005231 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005233 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005234 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07005237 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 mFinished = true;
5239 }
5240 } catch (RemoteException e) {
5241 // Empty
5242 }
5243 } else {
5244 mParent.finishFromChild(this);
5245 }
5246 }
5247
5248 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005249 * Call this when your activity is done and should be closed. The
5250 * ActivityResult is propagated back to whoever launched you via
5251 * onActivityResult().
5252 */
5253 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005254 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005255 }
5256
5257 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005258 * Finish this activity as well as all activities immediately below it
5259 * in the current task that have the same affinity. This is typically
5260 * used when an application can be launched on to another task (such as
5261 * from an ACTION_VIEW of a content type it understands) and the user
5262 * has used the up navigation to switch out of the current task and in
5263 * to its own task. In this case, if the user has navigated down into
5264 * any other activities of the second application, all of those should
5265 * be removed from the original task as part of the task switch.
5266 *
5267 * <p>Note that this finish does <em>not</em> allow you to deliver results
5268 * to the previous activity, and an exception will be thrown if you are trying
5269 * to do so.</p>
5270 */
5271 public void finishAffinity() {
5272 if (mParent != null) {
5273 throw new IllegalStateException("Can not be called from an embedded activity");
5274 }
5275 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5276 throw new IllegalStateException("Can not be called to deliver a result");
5277 }
5278 try {
5279 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
5280 mFinished = true;
5281 }
5282 } catch (RemoteException e) {
5283 // Empty
5284 }
5285 }
5286
5287 /**
RoboErik55011652014-07-09 15:05:53 -07005288 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 * {@link #finish} method. The default implementation simply calls
5290 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005291 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 * @see #finish
5295 */
5296 public void finishFromChild(Activity child) {
5297 finish();
5298 }
5299
5300 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005301 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5302 * to reverse its exit Transition. When the exit Transition completes,
5303 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5304 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005305 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005306 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005307 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005308 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005309 finish();
5310 }
George Mountcb4b7d92014-02-25 10:47:55 -08005311 }
5312
5313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 * Force finish another activity that you had previously started with
5315 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 * @param requestCode The request code of the activity that you had
5318 * given to startActivityForResult(). If there are multiple
5319 * activities started with this request code, they
5320 * will all be finished.
5321 */
5322 public void finishActivity(int requestCode) {
5323 if (mParent == null) {
5324 try {
5325 ActivityManagerNative.getDefault()
5326 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5327 } catch (RemoteException e) {
5328 // Empty
5329 }
5330 } else {
5331 mParent.finishActivityFromChild(this, requestCode);
5332 }
5333 }
5334
5335 /**
5336 * This is called when a child activity of this one calls its
5337 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 * @param child The activity making the call.
5340 * @param requestCode Request code that had been used to start the
5341 * activity.
5342 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005343 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 try {
5345 ActivityManagerNative.getDefault()
5346 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5347 } catch (RemoteException e) {
5348 // Empty
5349 }
5350 }
5351
5352 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005353 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005354 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005355 */
5356 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005357 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005358 }
5359
5360 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005361 * Ask that the local app instance of this activity be released to free up its memory.
5362 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5363 * a new instance of the activity will later be re-created if needed due to the user
5364 * navigating back to it.
5365 *
5366 * @return Returns true if the activity was in a state that it has started the process
5367 * of destroying its current instance; returns false if for any reason this could not
5368 * be done: it is currently visible to the user, it is already being destroyed, it is
5369 * being finished, it hasn't yet saved its state, etc.
5370 */
5371 public boolean releaseInstance() {
5372 try {
5373 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
5374 } catch (RemoteException e) {
5375 // Empty
5376 }
5377 return false;
5378 }
5379
5380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 * Called when an activity you launched exits, giving you the requestCode
5382 * you started it with, the resultCode it returned, and any additional
5383 * data from it. The <var>resultCode</var> will be
5384 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5385 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005386 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 * <p>You will receive this call immediately before onResume() when your
5388 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005389 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005390 * <p>This method is never invoked if your activity sets
5391 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5392 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005393 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 * @param requestCode The integer request code originally supplied to
5395 * startActivityForResult(), allowing you to identify who this
5396 * result came from.
5397 * @param resultCode The integer result code returned by the child activity
5398 * through its setResult().
5399 * @param data An Intent, which can return result data to the caller
5400 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 * @see #startActivityForResult
5403 * @see #createPendingResult
5404 * @see #setResult(int)
5405 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005406 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 }
5408
5409 /**
George Mount62ab9b72014-05-02 13:51:17 -07005410 * Called when an activity you launched with an activity transition exposes this
5411 * Activity through a returning activity transition, giving you the resultCode
5412 * and any additional data from it. This method will only be called if the activity
5413 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005414 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005415 *
5416 * <p>The purpose of this function is to let the called Activity send a hint about
5417 * its state so that this underlying Activity can prepare to be exposed. A call to
5418 * this method does not guarantee that the called Activity has or will be exiting soon.
5419 * It only indicates that it will expose this Activity's Window and it has
5420 * some data to pass to prepare it.</p>
5421 *
5422 * @param resultCode The integer result code returned by the child activity
5423 * through its setResult().
5424 * @param data An Intent, which can return result data to the caller
5425 * (various data can be attached to Intent "extras").
5426 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005427 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005428 }
5429
5430 /**
RoboErik55011652014-07-09 15:05:53 -07005431 * Create a new PendingIntent object which you can hand to others
5432 * for them to use to send result data back to your
5433 * {@link #onActivityResult} callback. The created object will be either
5434 * one-shot (becoming invalid after a result is sent back) or multiple
5435 * (allowing any number of results to be sent through it).
5436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 * @param requestCode Private request code for the sender that will be
5438 * associated with the result data when it is returned. The sender can not
5439 * modify this value, allowing you to identify incoming results.
5440 * @param data Default data to supply in the result, which may be modified
5441 * by the sender.
5442 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5443 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5444 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5445 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5446 * or any of the flags as supported by
5447 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5448 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005449 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 * @return Returns an existing or new PendingIntent matching the given
5451 * parameters. May return null only if
5452 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5453 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 * @see PendingIntent
5456 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005457 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5458 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 String packageName = getPackageName();
5460 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005461 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 IIntentSender target =
5463 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005464 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005466 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
5467 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 return target != null ? new PendingIntent(target) : null;
5469 } catch (RemoteException e) {
5470 // Empty
5471 }
5472 return null;
5473 }
5474
5475 /**
5476 * Change the desired orientation of this activity. If the activity
5477 * is currently in the foreground or otherwise impacting the screen
5478 * orientation, the screen will immediately be changed (possibly causing
5479 * the activity to be restarted). Otherwise, this will be used the next
5480 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 * @param requestedOrientation An orientation constant as used in
5483 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5484 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005485 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 if (mParent == null) {
5487 try {
5488 ActivityManagerNative.getDefault().setRequestedOrientation(
5489 mToken, requestedOrientation);
5490 } catch (RemoteException e) {
5491 // Empty
5492 }
5493 } else {
5494 mParent.setRequestedOrientation(requestedOrientation);
5495 }
5496 }
RoboErik55011652014-07-09 15:05:53 -07005497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 /**
5499 * Return the current requested orientation of the activity. This will
5500 * either be the orientation requested in its component's manifest, or
5501 * the last requested orientation given to
5502 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 * @return Returns an orientation constant as used in
5505 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5506 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005507 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 public int getRequestedOrientation() {
5509 if (mParent == null) {
5510 try {
5511 return ActivityManagerNative.getDefault()
5512 .getRequestedOrientation(mToken);
5513 } catch (RemoteException e) {
5514 // Empty
5515 }
5516 } else {
5517 return mParent.getRequestedOrientation();
5518 }
5519 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5520 }
RoboErik55011652014-07-09 15:05:53 -07005521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 /**
5523 * Return the identifier of the task this activity is in. This identifier
5524 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 * @return Task identifier, an opaque integer.
5527 */
5528 public int getTaskId() {
5529 try {
5530 return ActivityManagerNative.getDefault()
5531 .getTaskForActivity(mToken, false);
5532 } catch (RemoteException e) {
5533 return -1;
5534 }
5535 }
5536
5537 /**
5538 * Return whether this activity is the root of a task. The root is the
5539 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 * @return True if this is the root activity, else false.
5542 */
5543 public boolean isTaskRoot() {
5544 try {
Filip Gruszczynski3d026712015-12-16 13:46:38 -08005545 return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 } catch (RemoteException e) {
5547 return false;
5548 }
5549 }
5550
5551 /**
5552 * Move the task containing this activity to the back of the activity
5553 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 * @param nonRoot If false then this only works if the activity is the root
5556 * of a task; if true it will work for any activity in
5557 * a task.
RoboErik55011652014-07-09 15:05:53 -07005558 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 * @return If the task was moved (or it was already at the
5560 * back) true is returned, else false.
5561 */
5562 public boolean moveTaskToBack(boolean nonRoot) {
5563 try {
5564 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
5565 mToken, nonRoot);
5566 } catch (RemoteException e) {
5567 // Empty
5568 }
5569 return false;
5570 }
5571
5572 /**
5573 * Returns class name for this activity with the package prefix removed.
5574 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005575 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 * @return The local class name.
5577 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005578 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 public String getLocalClassName() {
5580 final String pkg = getPackageName();
5581 final String cls = mComponent.getClassName();
5582 int packageLen = pkg.length();
5583 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5584 || cls.charAt(packageLen) != '.') {
5585 return cls;
5586 }
5587 return cls.substring(packageLen+1);
5588 }
RoboErik55011652014-07-09 15:05:53 -07005589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 /**
5591 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 * @return Returns the complete component name for this activity
5594 */
5595 public ComponentName getComponentName()
5596 {
5597 return mComponent;
5598 }
5599
5600 /**
5601 * Retrieve a {@link SharedPreferences} object for accessing preferences
5602 * that are private to this activity. This simply calls the underlying
5603 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5604 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005605 *
5606 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07005607 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 *
5609 * @return Returns the single SharedPreferences instance that can be used
5610 * to retrieve and modify the preference values.
5611 */
5612 public SharedPreferences getPreferences(int mode) {
5613 return getSharedPreferences(getLocalClassName(), mode);
5614 }
RoboErik55011652014-07-09 15:05:53 -07005615
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005616 private void ensureSearchManager() {
5617 if (mSearchManager != null) {
5618 return;
5619 }
RoboErik55011652014-07-09 15:05:53 -07005620
Jeff Sharkey49ca5292016-05-10 12:54:45 -06005621 try {
5622 mSearchManager = new SearchManager(this, null);
5623 } catch (ServiceNotFoundException e) {
5624 throw new IllegalStateException(e);
5625 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005626 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005629 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 if (getBaseContext() == null) {
5631 throw new IllegalStateException(
5632 "System services not available to Activities before onCreate()");
5633 }
5634
5635 if (WINDOW_SERVICE.equals(name)) {
5636 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005637 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005638 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005639 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 }
5641 return super.getSystemService(name);
5642 }
5643
5644 /**
5645 * Change the title associated with this activity. If this is a
5646 * top-level activity, the title for its window will change. If it
5647 * is an embedded activity, the parent can do whatever it wants
5648 * with it.
5649 */
5650 public void setTitle(CharSequence title) {
5651 mTitle = title;
5652 onTitleChanged(title, mTitleColor);
5653
5654 if (mParent != null) {
5655 mParent.onChildTitleChanged(this, title);
5656 }
5657 }
5658
5659 /**
5660 * Change the title associated with this activity. If this is a
5661 * top-level activity, the title for its window will change. If it
5662 * is an embedded activity, the parent can do whatever it wants
5663 * with it.
5664 */
5665 public void setTitle(int titleId) {
5666 setTitle(getText(titleId));
5667 }
5668
Alan Viverette2525d9c2013-11-15 14:42:19 -08005669 /**
5670 * Change the color of the title associated with this activity.
5671 * <p>
5672 * This method is deprecated starting in API Level 11 and replaced by action
5673 * bar styles. For information on styling the Action Bar, read the <a
5674 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5675 * guide.
5676 *
5677 * @deprecated Use action bar styles instead.
5678 */
5679 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 public void setTitleColor(int textColor) {
5681 mTitleColor = textColor;
5682 onTitleChanged(mTitle, textColor);
5683 }
5684
5685 public final CharSequence getTitle() {
5686 return mTitle;
5687 }
5688
5689 public final int getTitleColor() {
5690 return mTitleColor;
5691 }
5692
5693 protected void onTitleChanged(CharSequence title, int color) {
5694 if (mTitleReady) {
5695 final Window win = getWindow();
5696 if (win != null) {
5697 win.setTitle(title);
5698 if (color != 0) {
5699 win.setTitleColor(color);
5700 }
5701 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005702 if (mActionBar != null) {
5703 mActionBar.setWindowTitle(title);
5704 }
Adam Powella557fdc2014-08-21 18:05:53 -07005705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 }
5707
5708 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5709 }
5710
5711 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005712 * Sets information describing the task with this activity for presentation inside the Recents
5713 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5714 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5715 * for each property until a suitable value is found. For each task the taskDescription will be
5716 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005717 *
5718 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005719 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005720 *
Winson Chunga449dc02014-05-16 11:15:04 -07005721 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005722 */
Winson Chunga449dc02014-05-16 11:15:04 -07005723 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08005724 if (mTaskDescription != taskDescription) {
5725 mTaskDescription.copyFrom(taskDescription);
5726 // Scale the icon down to something reasonable if it is provided
5727 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
5728 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
5729 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
5730 true);
5731 mTaskDescription.setIcon(icon);
5732 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07005733 }
5734 try {
Winsonb6403152016-02-23 13:32:09 -08005735 ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005736 } catch (RemoteException e) {
5737 }
5738 }
5739
5740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 * Sets the visibility of the progress bar in the title.
5742 * <p>
5743 * In order for the progress bar to be shown, the feature must be requested
5744 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005745 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005747 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005749 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 public final void setProgressBarVisibility(boolean visible) {
5751 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5752 Window.PROGRESS_VISIBILITY_OFF);
5753 }
5754
5755 /**
5756 * Sets the visibility of the indeterminate progress bar in the title.
5757 * <p>
5758 * In order for the progress bar to be shown, the feature must be requested
5759 * via {@link #requestWindowFeature(int)}.
5760 *
5761 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005762 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005764 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5766 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5767 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5768 }
RoboErik55011652014-07-09 15:05:53 -07005769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 /**
5771 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5772 * is always indeterminate).
5773 * <p>
5774 * In order for the progress bar to be shown, the feature must be requested
5775 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005776 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005778 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005780 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 public final void setProgressBarIndeterminate(boolean indeterminate) {
5782 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005783 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5784 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 }
RoboErik55011652014-07-09 15:05:53 -07005786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 /**
5788 * Sets the progress for the progress bars in the title.
5789 * <p>
5790 * In order for the progress bar to be shown, the feature must be requested
5791 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005792 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 * @param progress The progress for the progress bar. Valid ranges are from
5794 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5795 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005796 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005798 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 public final void setProgress(int progress) {
5800 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5801 }
RoboErik55011652014-07-09 15:05:53 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 /**
5804 * Sets the secondary progress for the progress bar in the title. This
5805 * progress is drawn between the primary progress (set via
5806 * {@link #setProgress(int)} and the background. It can be ideal for media
5807 * scenarios such as showing the buffering progress while the default
5808 * progress shows the play progress.
5809 * <p>
5810 * In order for the progress bar to be shown, the feature must be requested
5811 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005812 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5814 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04005815 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005817 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 public final void setSecondaryProgress(int secondaryProgress) {
5819 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5820 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5821 }
5822
5823 /**
5824 * Suggests an audio stream whose volume should be changed by the hardware
5825 * volume controls.
5826 * <p>
5827 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005828 * Volume requests which are received while the Activity is in the
5829 * foreground will affect this stream.
5830 * <p>
5831 * It is not guaranteed that the hardware volume controls will always change
5832 * this stream's volume (for example, if a call is in progress, its stream's
5833 * volume may be changed instead). To reset back to the default, use
5834 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5835 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005837 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 */
5839 public final void setVolumeControlStream(int streamType) {
5840 getWindow().setVolumeControlStream(streamType);
5841 }
5842
5843 /**
5844 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005845 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005846 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 * @return The suggested audio stream type whose volume should be changed by
5848 * the hardware volume controls.
5849 * @see #setVolumeControlStream(int)
5850 */
5851 public final int getVolumeControlStream() {
5852 return getWindow().getVolumeControlStream();
5853 }
RoboErik55011652014-07-09 15:05:53 -07005854
5855 /**
5856 * Sets a {@link MediaController} to send media keys and volume changes to.
5857 * <p>
5858 * The controller will be tied to the window of this Activity. Media key and
5859 * volume events which are received while the Activity is in the foreground
5860 * will be forwarded to the controller and used to invoke transport controls
5861 * or adjust the volume. This may be used instead of or in addition to
5862 * {@link #setVolumeControlStream} to affect a specific session instead of a
5863 * specific stream.
5864 * <p>
5865 * It is not guaranteed that the hardware volume controls will always change
5866 * this session's volume (for example, if a call is in progress, its
5867 * stream's volume may be changed instead). To reset back to the default use
5868 * null as the controller.
5869 *
5870 * @param controller The controller for the session which should receive
5871 * media keys and volume changes.
5872 */
5873 public final void setMediaController(MediaController controller) {
5874 getWindow().setMediaController(controller);
5875 }
5876
5877 /**
5878 * Gets the controller which should be receiving media key and volume events
5879 * while this activity is in the foreground.
5880 *
5881 * @return The controller which should receive events.
5882 * @see #setMediaController(android.media.session.MediaController)
5883 */
5884 public final MediaController getMediaController() {
5885 return getWindow().getMediaController();
5886 }
5887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 /**
5889 * Runs the specified action on the UI thread. If the current thread is the UI
5890 * thread, then the action is executed immediately. If the current thread is
5891 * not the UI thread, the action is posted to the event queue of the UI thread.
5892 *
5893 * @param action the action to run on the UI thread
5894 */
5895 public final void runOnUiThread(Runnable action) {
5896 if (Thread.currentThread() != mUiThread) {
5897 mHandler.post(action);
5898 } else {
5899 action.run();
5900 }
5901 }
5902
5903 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005904 * Standard implementation of
5905 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5906 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005907 * This implementation does nothing and is for
5908 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5909 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5910 *
5911 * @see android.view.LayoutInflater#createView
5912 * @see android.view.Window#getLayoutInflater
5913 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005914 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005915 public View onCreateView(String name, Context context, AttributeSet attrs) {
5916 return null;
5917 }
5918
5919 /**
5920 * Standard implementation of
5921 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5922 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005923 * This implementation handles <fragment> tags to embed fragments inside
5924 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 *
5926 * @see android.view.LayoutInflater#createView
5927 * @see android.view.Window#getLayoutInflater
5928 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005929 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005930 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005931 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005932 }
RoboErik55011652014-07-09 15:05:53 -07005933
Adam Powell371a8092014-06-20 12:51:12 -07005934 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 }
5936
Daniel Sandler69a48172010-06-23 16:29:36 -04005937 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005938 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005939 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005940 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005941 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005942 * @param fd The raw file descriptor that the dump is being sent to.
5943 * @param writer The PrintWriter to which you should dump your state. This will be
5944 * closed for you after you return.
5945 * @param args additional arguments to the dump request.
5946 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005947 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005948 dumpInner(prefix, fd, writer, args);
5949 }
5950
5951 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005952 writer.print(prefix); writer.print("Local Activity ");
5953 writer.print(Integer.toHexString(System.identityHashCode(this)));
5954 writer.println(" State:");
5955 String innerPrefix = prefix + " ";
5956 writer.print(innerPrefix); writer.print("mResumed=");
5957 writer.print(mResumed); writer.print(" mStopped=");
5958 writer.print(mStopped); writer.print(" mFinished=");
5959 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08005960 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5961 writer.println(mChangingConfigurations);
5962 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5963 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005964
Todd Kennedya5fc6f02015-04-14 18:22:54 -07005965 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
5966 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07005967 if (mVoiceInteractor != null) {
5968 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
5969 }
Jeff Brown5182c782013-10-15 20:31:52 -07005970
Michael Wright5f48dc72013-11-01 12:42:49 -07005971 if (getWindow() != null &&
5972 getWindow().peekDecorView() != null &&
5973 getWindow().peekDecorView().getViewRootImpl() != null) {
5974 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5975 }
Jeff Brown5182c782013-10-15 20:31:52 -07005976
5977 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005978 }
5979
5980 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005981 * Bit indicating that this activity is "immersive" and should not be
5982 * interrupted by notifications if possible.
5983 *
5984 * This value is initially set by the manifest property
5985 * <code>android:immersive</code> but may be changed at runtime by
5986 * {@link #setImmersive}.
5987 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005988 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005989 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5990 */
5991 public boolean isImmersive() {
5992 try {
5993 return ActivityManagerNative.getDefault().isImmersive(mToken);
5994 } catch (RemoteException e) {
5995 return false;
5996 }
5997 }
5998
5999 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006000 * Indication of whether this is the highest level activity in this task. Can be used to
6001 * determine whether an activity launched by this activity was placed in the same task or
6002 * another task.
6003 *
6004 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006005 */
George Mountff243282014-07-07 16:12:07 -07006006 private boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006007 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006008 return false;
6009 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006010 try {
Chong Zhang6f9ae4f2016-06-03 11:30:20 -07006011 return ActivityManagerNative.getDefault().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006012 } catch (RemoteException e) {
6013 return false;
6014 }
6015 }
6016
6017 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006018 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6019 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006020 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006021 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006022 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6023 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006024 * This call has no effect on non-translucent activities or on activities with the
6025 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006026 *
George Mount62ab9b72014-05-02 13:51:17 -07006027 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6028 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006029 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006030 *
6031 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006032 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006033 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006034 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006035 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006036 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006037 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
6038 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6039 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006040 } catch (RemoteException e) {
6041 // pass
6042 }
6043 }
6044
6045 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006046 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6047 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6048 * <p>
6049 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6050 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6051 * be called indicating that it is safe to make this activity translucent again. Until
6052 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6053 * behind the frontmost Activity will be indeterminate.
6054 * <p>
6055 * This call has no effect on non-translucent activities or on activities with the
6056 * {@link android.R.attr#windowIsFloating} attribute.
6057 *
6058 * @param callback the method to call when all visible Activities behind this one have been
6059 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006060 * @param options activity options delivered to the activity below this one. The options
6061 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006062 * @return <code>true</code> if Window was opaque and will become translucent or
6063 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006064 *
6065 * @see #convertFromTranslucent()
6066 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006067 *
6068 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006069 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006070 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006071 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006072 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006073 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006074 try {
6075 mTranslucentCallback = callback;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006076 mChangeCanvasToTranslucent = ActivityManagerNative.getDefault().convertToTranslucent(
6077 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006078 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006079 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006080 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006081 // Make callback return as though it timed out.
6082 mChangeCanvasToTranslucent = false;
6083 drawComplete = false;
6084 }
6085 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6086 // Window is already translucent.
6087 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006088 }
George Mount3cc716c2014-06-12 16:35:35 -07006089 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006090 }
6091
6092 /** @hide */
6093 void onTranslucentConversionComplete(boolean drawComplete) {
6094 if (mTranslucentCallback != null) {
6095 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6096 mTranslucentCallback = null;
6097 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006098 if (mChangeCanvasToTranslucent) {
6099 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6100 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006101 }
6102
Craig Mautnereb8abf72014-07-02 15:04:09 -07006103 /** @hide */
6104 public void onNewActivityOptions(ActivityOptions options) {
6105 mActivityTransitionState.setEnterActivityOptions(this, options);
6106 if (!mStopped) {
6107 mActivityTransitionState.enterReady(this);
6108 }
6109 }
6110
Craig Mautner5eda9b32013-07-02 11:58:16 -07006111 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006112 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6113 * from an activity launched by this activity in its call to {@link
6114 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6115 *
6116 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6117 * @hide
6118 */
6119 ActivityOptions getActivityOptions() {
6120 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006121 return ActivityOptions.fromBundle(
6122 ActivityManagerNative.getDefault().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07006123 } catch (RemoteException e) {
6124 }
6125 return null;
6126 }
6127
6128 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006129 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006130 * this method anytime between the start of {@link #onResume()} and the return from
6131 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6132 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6133 *
6134 * <p>The actions of this call are reset each time that this activity is brought to the
6135 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6136 * to not have requested visible behind. Therefore, if you want this activity to continue to
6137 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006138 *
6139 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6140 * for dialog and translucent activities.
6141 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006142 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6143 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6144 *
6145 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006146 * the next call to onResume.
6147 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006148 * @param visible true to notify the system that the activity wishes to be visible behind other
6149 * translucent activities, false to indicate otherwise. Resources must be
6150 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07006151 * @return the resulting visibiity state. If true the activity will remain visible beyond
6152 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6153 * then the activity may not count on being visible behind other translucent activities,
6154 * and must stop any media playback and release resources.
6155 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6156 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006157 *
Jose Limafcf70832014-08-27 23:09:05 -07006158 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07006159 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006160 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006161 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006162 // Do not permit paused or stopped activities to do this.
6163 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006164 }
6165 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006166 mVisibleBehind = ActivityManagerNative.getDefault()
6167 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006168 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006169 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006170 }
Jose Lima4b6c6692014-08-12 17:41:12 -07006171 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006172 }
6173
6174 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006175 * Called when a translucent activity over this activity is becoming opaque or another
6176 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006177 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006178 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006179 * <p>When this method is called the activity has 500 msec to release any resources it may be
6180 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006181 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006182 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006183 * process. Otherwise {@link #onStop()} will be called following return.
6184 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006185 * @see #requestVisibleBehind(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006186 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006187 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006188 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006189 mCalled = true;
6190 }
6191
6192 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006193 * Translucent activities may call this to determine if there is an activity below them that
6194 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006195 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006196 * @return true if an activity below is set to visible according to the most recent call to
6197 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006198 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006199 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006200 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006201 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006202 * @hide
6203 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006204 @SystemApi
6205 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006206 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006207 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07006208 } catch (RemoteException e) {
6209 }
6210 return false;
6211 }
6212
6213 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006214 * The topmost foreground activity will receive this call when the background visibility state
6215 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006216 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006217 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006218 * due to a background activity finishing itself.
6219 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006220 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006221 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006222 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006223 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006224 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006225 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006226 @SystemApi
6227 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006228 }
6229
6230 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006231 * Activities cannot draw during the period that their windows are animating in. In order
6232 * to know when it is safe to begin drawing they can override this method which will be
6233 * called when the entering animation has completed.
6234 */
6235 public void onEnterAnimationComplete() {
6236 }
6237
6238 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006239 * @hide
6240 */
6241 public void dispatchEnterAnimationComplete() {
6242 onEnterAnimationComplete();
6243 if (getWindow() != null && getWindow().getDecorView() != null) {
6244 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6245 }
6246 }
6247
6248 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006249 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006250 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006251 * Note that changing this value will have no effect on the activity's
6252 * {@link android.content.pm.ActivityInfo} structure; that is, if
6253 * <code>android:immersive</code> is set to <code>true</code>
6254 * in the application's manifest entry for this activity, the {@link
6255 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6256 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6257 * FLAG_IMMERSIVE} bit set.
6258 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006259 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006260 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6261 */
6262 public void setImmersive(boolean i) {
6263 try {
6264 ActivityManagerNative.getDefault().setImmersive(mToken, i);
6265 } catch (RemoteException e) {
6266 // pass
6267 }
6268 }
6269
Adam Powell6e346362010-07-23 10:18:23 -07006270 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07006271 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006272 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006273 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
6274 * while this Activity has user focus.</p>
6275 *
6276 * <p>It is recommended that applications additionally declare
6277 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
6278 * transitions when switching between VR activities.</p>
6279 *
6280 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
6281 * VR mode will not be started. Developers can handle this case as follows:</p>
6282 *
6283 * <pre>
6284 * String servicePackage = "com.whatever.app";
6285 * String serviceClass = "com.whatever.app.MyVrListenerService";
6286 *
6287 * // Name of the component of the VrListenerService to start.
6288 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
6289 *
6290 * try {
6291 * setVrModeEnabled(true, myComponentName);
6292 * } catch (PackageManager.NameNotFoundException e) {
6293 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
6294 * boolean isInstalled = false;
6295 * for (ApplicationInfo app : installed) {
6296 * if (app.packageName.equals(servicePackage)) {
6297 * isInstalled = true;
6298 * break;
6299 * }
6300 * }
6301 * if (isInstalled) {
6302 * // Package is installed, but not enabled in Settings. Let user enable it.
6303 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
6304 * } else {
6305 * // Package is not installed. Send an intent to download this.
6306 * sentIntentToLaunchAppStore(servicePackage);
6307 * }
6308 * }
6309 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006310 *
6311 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08006312 * @param requestedComponent the name of the component to use as a
6313 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
6314 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006315 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
6316 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
6317 * not been enabled in user settings.
6318 *
6319 * @see android.content.pm.PackageManager#FEATURE_VR_MODE
6320 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
6321 * @see android.service.vr.VrListenerService
6322 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
6323 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006324 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08006325 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
6326 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006327 try {
Ruben Brunke24b9a62016-02-16 21:38:24 -08006328 if (ActivityManagerNative.getDefault().setVrMode(mToken, enabled, requestedComponent)
6329 != 0) {
6330 throw new PackageManager.NameNotFoundException(
6331 requestedComponent.flattenToString());
6332 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006333 } catch (RemoteException e) {
6334 // pass
6335 }
6336 }
6337
6338 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006339 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006340 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006341 * @param callback Callback that will manage lifecycle events for this action mode
6342 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006343 *
6344 * @see ActionMode
6345 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006346 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006347 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006348 return mWindow.getDecorView().startActionMode(callback);
6349 }
6350
Adam Powelldebf3be2010-11-15 18:58:48 -08006351 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006352 * Start an action mode of the given type.
6353 *
6354 * @param callback Callback that will manage lifecycle events for this action mode
6355 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6356 * @return The ActionMode that was started, or null if it was canceled
6357 *
6358 * @see ActionMode
6359 */
6360 @Nullable
6361 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6362 return mWindow.getDecorView().startActionMode(callback, type);
6363 }
6364
6365 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006366 * Give the Activity a chance to control the UI for an action mode requested
6367 * by the system.
6368 *
6369 * <p>Note: If you are looking for a notification callback that an action mode
6370 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6371 *
6372 * @param callback The callback that should control the new action mode
6373 * @return The new action mode, or <code>null</code> if the activity does not want to
6374 * provide special handling for this action mode. (It will be handled by the system.)
6375 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006376 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006377 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006378 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006379 // Only Primary ActionModes are represented in the ActionBar.
6380 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6381 initWindowDecorActionBar();
6382 if (mActionBar != null) {
6383 return mActionBar.startActionMode(callback);
6384 }
Adam Powell6e346362010-07-23 10:18:23 -07006385 }
6386 return null;
6387 }
6388
Adam Powelldebf3be2010-11-15 18:58:48 -08006389 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006390 * {@inheritDoc}
6391 */
6392 @Nullable
6393 @Override
6394 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6395 try {
6396 mActionModeTypeStarting = type;
6397 return onWindowStartingActionMode(callback);
6398 } finally {
6399 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6400 }
6401 }
6402
6403 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006404 * Notifies the Activity that an action mode has been started.
6405 * Activity subclasses overriding this method should call the superclass implementation.
6406 *
6407 * @param mode The new action mode.
6408 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006409 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006410 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006411 public void onActionModeStarted(ActionMode mode) {
6412 }
6413
6414 /**
6415 * Notifies the activity that an action mode has finished.
6416 * Activity subclasses overriding this method should call the superclass implementation.
6417 *
6418 * @param mode The action mode that just finished.
6419 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006420 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006421 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006422 public void onActionModeFinished(ActionMode mode) {
6423 }
6424
Adam Powelldd8fab22012-03-22 17:47:27 -07006425 /**
6426 * Returns true if the app should recreate the task when navigating 'up' from this activity
6427 * by using targetIntent.
6428 *
6429 * <p>If this method returns false the app can trivially call
6430 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6431 * up navigation. If this method returns false, the app should synthesize a new task stack
6432 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6433 *
6434 * @param targetIntent An intent representing the target destination for up navigation
6435 * @return true if navigating up should recreate a new task stack, false if the same task
6436 * should be used for the destination
6437 */
6438 public boolean shouldUpRecreateTask(Intent targetIntent) {
6439 try {
6440 PackageManager pm = getPackageManager();
6441 ComponentName cn = targetIntent.getComponent();
6442 if (cn == null) {
6443 cn = targetIntent.resolveActivity(pm);
6444 }
6445 ActivityInfo info = pm.getActivityInfo(cn, 0);
6446 if (info.taskAffinity == null) {
6447 return false;
6448 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006449 return ActivityManagerNative.getDefault()
6450 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006451 } catch (RemoteException e) {
6452 return false;
6453 } catch (NameNotFoundException e) {
6454 return false;
6455 }
6456 }
6457
6458 /**
6459 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6460 * in the process. If the activity indicated by upIntent already exists in the task's history,
6461 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006462 * finished.
6463 *
6464 * <p>If the indicated activity does not appear in the history stack, this will finish
6465 * each activity in this task until the root activity of the task is reached, resulting in
6466 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6467 * when an activity may be reached by a path not passing through a canonical parent
6468 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006469 *
6470 * <p>This method should be used when performing up navigation from within the same task
6471 * as the destination. If up navigation should cross tasks in some cases, see
6472 * {@link #shouldUpRecreateTask(Intent)}.</p>
6473 *
6474 * @param upIntent An intent representing the target destination for up navigation
6475 *
6476 * @return true if up navigation successfully reached the activity indicated by upIntent and
6477 * upIntent was delivered to it. false if an instance of the indicated activity could
6478 * not be found and this activity was simply finished normally.
6479 */
6480 public boolean navigateUpTo(Intent upIntent) {
6481 if (mParent == null) {
6482 ComponentName destInfo = upIntent.getComponent();
6483 if (destInfo == null) {
6484 destInfo = upIntent.resolveActivity(getPackageManager());
6485 if (destInfo == null) {
6486 return false;
6487 }
6488 upIntent = new Intent(upIntent);
6489 upIntent.setComponent(destInfo);
6490 }
6491 int resultCode;
6492 Intent resultData;
6493 synchronized (this) {
6494 resultCode = mResultCode;
6495 resultData = mResultData;
6496 }
6497 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006498 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006499 }
6500 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006501 upIntent.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006502 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
6503 resultCode, resultData);
6504 } catch (RemoteException e) {
6505 return false;
6506 }
6507 } else {
6508 return mParent.navigateUpToFromChild(this, upIntent);
6509 }
6510 }
6511
6512 /**
6513 * This is called when a child activity of this one calls its
6514 * {@link #navigateUpTo} method. The default implementation simply calls
6515 * navigateUpTo(upIntent) on this activity (the parent).
6516 *
6517 * @param child The activity making the call.
6518 * @param upIntent An intent representing the target destination for up navigation
6519 *
6520 * @return true if up navigation successfully reached the activity indicated by upIntent and
6521 * upIntent was delivered to it. false if an instance of the indicated activity could
6522 * not be found and this activity was simply finished normally.
6523 */
6524 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6525 return navigateUpTo(upIntent);
6526 }
6527
6528 /**
6529 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6530 * this activity's logical parent. The logical parent is named in the application's manifest
6531 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006532 * Activity subclasses may override this method to modify the Intent returned by
6533 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6534 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006535 *
Adam Powell04d58112012-04-09 10:22:12 -07006536 * @return a new Intent targeting the defined parent of this activity or null if
6537 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006538 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006539 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006540 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006541 final String parentName = mActivityInfo.parentActivityName;
6542 if (TextUtils.isEmpty(parentName)) {
6543 return null;
6544 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006545
6546 // If the parent itself has no parent, generate a main activity intent.
6547 final ComponentName target = new ComponentName(this, parentName);
6548 try {
6549 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6550 final String parentActivity = parentInfo.parentActivityName;
6551 final Intent parentIntent = parentActivity == null
6552 ? Intent.makeMainActivity(target)
6553 : new Intent().setComponent(target);
6554 return parentIntent;
6555 } catch (NameNotFoundException e) {
6556 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
6557 "' in manifest");
6558 return null;
6559 }
Adam Powelldd8fab22012-03-22 17:47:27 -07006560 }
6561
George Mount31a21722014-03-24 17:44:36 -07006562 /**
George Mount62ab9b72014-05-02 13:51:17 -07006563 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006564 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006565 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07006566 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07006567 *
George Mount65580562014-08-29 08:15:48 -07006568 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07006569 */
George Mount65580562014-08-29 08:15:48 -07006570 public void setEnterSharedElementCallback(SharedElementCallback callback) {
6571 if (callback == null) {
6572 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07006573 }
George Mount65580562014-08-29 08:15:48 -07006574 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07006575 }
6576
6577 /**
6578 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006579 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006580 * will be called to handle shared elements on the <i>launching</i> Activity. Most
6581 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07006582 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07006583 *
George Mount65580562014-08-29 08:15:48 -07006584 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07006585 */
George Mount65580562014-08-29 08:15:48 -07006586 public void setExitSharedElementCallback(SharedElementCallback callback) {
6587 if (callback == null) {
6588 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07006589 }
George Mount65580562014-08-29 08:15:48 -07006590 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07006591 }
6592
George Mount8c2614c2014-06-10 11:12:01 -07006593 /**
6594 * Postpone the entering activity transition when Activity was started with
6595 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6596 * android.util.Pair[])}.
6597 * <p>This method gives the Activity the ability to delay starting the entering and
6598 * shared element transitions until all data is loaded. Until then, the Activity won't
6599 * draw into its window, leaving the window transparent. This may also cause the
6600 * returning animation to be delayed until data is ready. This method should be
6601 * called in {@link #onCreate(android.os.Bundle)} or in
6602 * {@link #onActivityReenter(int, android.content.Intent)}.
6603 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
6604 * start the transitions. If the Activity did not use
6605 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6606 * android.util.Pair[])}, then this method does nothing.</p>
6607 */
6608 public void postponeEnterTransition() {
6609 mActivityTransitionState.postponeEnterTransition();
6610 }
6611
6612 /**
6613 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
6614 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
6615 * to have your Activity start drawing.
6616 */
6617 public void startPostponedEnterTransition() {
6618 mActivityTransitionState.startPostponedEnterTransition();
6619 }
6620
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006621 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07006622 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
6623 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006624 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07006625 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
6626 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006627 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07006628 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
6629 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
6630 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
6631 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006632 }
6633 return null;
6634 }
6635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07006637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 final void setParent(Activity parent) {
6639 mParent = parent;
6640 }
6641
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006642 final void attach(Context context, ActivityThread aThread,
6643 Instrumentation instr, IBinder token, int ident,
6644 Application application, Intent intent, ActivityInfo info,
6645 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006646 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006647 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
6648 Window window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 attachBaseContext(context);
6650
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006651 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07006652
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006653 mWindow = new PhoneWindow(this, window);
Skuhnece2faa52015-08-11 10:36:38 -07006654 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006655 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006656 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006657 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6659 mWindow.setSoftInputMode(info.softInputMode);
6660 }
Adam Powell269248d2011-08-02 10:26:54 -07006661 if (info.uiOptions != 0) {
6662 mWindow.setUiOptions(info.uiOptions);
6663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006666 mMainThread = aThread;
6667 mInstrumentation = instr;
6668 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006669 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 mApplication = application;
6671 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006672 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 mComponent = intent.getComponent();
6674 mActivityInfo = info;
6675 mTitle = title;
6676 mParent = parent;
6677 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006678 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006679 if (voiceInteractor != null) {
6680 if (lastNonConfigurationInstances != null) {
6681 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6682 } else {
6683 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6684 Looper.myLooper());
6685 }
6686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687
Jeff Brown98365d72012-08-19 20:30:52 -07006688 mWindow.setWindowManager(
6689 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6690 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006691 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 if (mParent != null) {
6693 mWindow.setContainer(mParent.getWindow());
6694 }
6695 mWindowManager = mWindow.getWindowManager();
6696 mCurrentConfig = config;
6697 }
6698
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006699 /** @hide */
6700 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006701 return mParent != null ? mParent.getActivityToken() : mToken;
6702 }
6703
Craig Mautnera0026042014-04-23 11:45:37 -07006704 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006705 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6706 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006707 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00006708 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006709 }
George Mount0a778ed2013-12-13 13:35:36 -08006710
Craig Mautnera0026042014-04-23 11:45:37 -07006711 final void performCreate(Bundle icicle) {
Svetoslavffb32b12015-10-15 16:54:00 -07006712 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006713 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07006714 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006715 performCreateCommon();
6716 }
6717
6718 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Svetoslavffb32b12015-10-15 16:54:00 -07006719 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006720 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07006721 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006722 performCreateCommon();
6723 }
6724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006726 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006727 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07006729 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 mInstrumentation.callActivityOnStart(this);
6731 if (!mCalled) {
6732 throw new SuperNotCalledException(
6733 "Activity " + mComponent.toShortString() +
6734 " did not call through to super.onStart()");
6735 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006736 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006737 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08006738
6739 // This property is set for all builds except final release
6740 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
6741 boolean isAppDebuggable =
6742 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
6743
6744 if (isAppDebuggable || isDlwarningEnabled) {
6745 String dlwarning = getDlWarning();
6746 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08006747 String appName = getApplicationInfo().loadLabel(getPackageManager())
6748 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08006749 String warning = "Detected problems with app native libraries\n" +
6750 "(please consult log for detail):\n" + dlwarning;
6751 if (isAppDebuggable) {
6752 new AlertDialog.Builder(this).
6753 setTitle(appName).
6754 setMessage(warning).
6755 setPositiveButton(android.R.string.ok, null).
6756 setCancelable(false).
6757 show();
6758 } else {
6759 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
6760 }
6761 }
6762 }
6763
George Mount62ab9b72014-05-02 13:51:17 -07006764 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 }
RoboErik55011652014-07-09 15:05:53 -07006766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006768 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07006769
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006770 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08006771 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006772 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
6773 }
6774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 if (mStopped) {
6776 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006777
6778 synchronized (mManagedCursors) {
6779 final int N = mManagedCursors.size();
6780 for (int i=0; i<N; i++) {
6781 ManagedCursor mc = mManagedCursors.get(i);
6782 if (mc.mReleased || mc.mUpdated) {
6783 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006784 if (getApplicationInfo().targetSdkVersion
6785 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6786 throw new IllegalStateException(
6787 "trying to requery an already closed cursor "
6788 + mc.mCursor);
6789 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006790 }
6791 mc.mReleased = false;
6792 mc.mUpdated = false;
6793 }
6794 }
6795 }
6796
6797 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 mInstrumentation.callActivityOnRestart(this);
6799 if (!mCalled) {
6800 throw new SuperNotCalledException(
6801 "Activity " + mComponent.toShortString() +
6802 " did not call through to super.onRestart()");
6803 }
6804 performStart();
6805 }
6806 }
RoboErik55011652014-07-09 15:05:53 -07006807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 final void performResume() {
6809 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006810
Dianne Hackborn445646c2010-06-25 15:52:59 -07006811 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006812
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006813 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006816 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 mInstrumentation.callActivityOnResume(this);
6818 if (!mCalled) {
6819 throw new SuperNotCalledException(
6820 "Activity " + mComponent.toShortString() +
6821 " did not call through to super.onResume()");
6822 }
6823
Todd Kennedyee8c9c62014-12-10 14:22:59 -08006824 // invisible activities must be finished before onResume() completes
6825 if (!mVisibleFromClient && !mFinished) {
6826 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
6827 if (getApplicationInfo().targetSdkVersion
6828 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
6829 throw new IllegalStateException(
6830 "Activity " + mComponent.toShortString() +
6831 " did not call finish() prior to onResume() completing");
6832 }
6833 }
6834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006837
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006838 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006839 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 onPostResume();
6842 if (!mCalled) {
6843 throw new SuperNotCalledException(
6844 "Activity " + mComponent.toShortString() +
6845 " did not call through to super.onPostResume()");
6846 }
6847 }
6848
6849 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006850 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006851 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006852 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006854 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006855 if (!mCalled && getApplicationInfo().targetSdkVersion
6856 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6857 throw new SuperNotCalledException(
6858 "Activity " + mComponent.toShortString() +
6859 " did not call through to super.onPause()");
6860 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006861 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006862 }
RoboErik55011652014-07-09 15:05:53 -07006863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 final void performUserLeaving() {
6865 onUserInteraction();
6866 onUserLeaveHint();
6867 }
RoboErik55011652014-07-09 15:05:53 -07006868
Chong Zhang7687f252016-02-26 12:03:33 -08006869 final void performStop(boolean preserveWindow) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006870 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006871 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
RoboErik55011652014-07-09 15:05:53 -07006872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 if (!mStopped) {
6874 if (mWindow != null) {
6875 mWindow.closeAllPanels();
6876 }
6877
Chong Zhang7687f252016-02-26 12:03:33 -08006878 // If we're preserving the window, don't setStoppedState to true, since we
6879 // need the window started immediately again. Stopping the window will
6880 // destroys hardware resources and causes flicker.
6881 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006882 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006883 }
RoboErik55011652014-07-09 15:05:53 -07006884
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006885 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 mCalled = false;
6888 mInstrumentation.callActivityOnStop(this);
6889 if (!mCalled) {
6890 throw new SuperNotCalledException(
6891 "Activity " + mComponent.toShortString() +
6892 " did not call through to super.onStop()");
6893 }
RoboErik55011652014-07-09 15:05:53 -07006894
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006895 synchronized (mManagedCursors) {
6896 final int N = mManagedCursors.size();
6897 for (int i=0; i<N; i++) {
6898 ManagedCursor mc = mManagedCursors.get(i);
6899 if (!mc.mReleased) {
6900 mc.mCursor.deactivate();
6901 mc.mReleased = true;
6902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 }
6904 }
George Mount0a778ed2013-12-13 13:35:36 -08006905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 mStopped = true;
6907 }
6908 mResumed = false;
6909 }
6910
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006911 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006912 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006913 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006914 mFragments.dispatchDestroy();
6915 onDestroy();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006916 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07006917 if (mVoiceInteractor != null) {
6918 mVoiceInteractor.detachActivity();
6919 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006920 }
George Mount0a778ed2013-12-13 13:35:36 -08006921
Adam Powell858cf032016-05-09 15:45:37 -07006922 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
6923 if (DEBUG_LIFECYCLE) Slog.v(TAG,
6924 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode);
6925 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode);
6926 if (mWindow != null) {
6927 mWindow.onMultiWindowModeChanged();
6928 }
6929 onMultiWindowModeChanged(isInMultiWindowMode);
6930 }
6931
6932 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
6933 if (DEBUG_LIFECYCLE) Slog.v(TAG,
6934 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode);
6935 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode);
6936 onPictureInPictureModeChanged(isInPictureInPictureMode);
6937 }
6938
George Mount0a778ed2013-12-13 13:35:36 -08006939 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006940 * @hide
6941 */
6942 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 return mResumed;
6944 }
6945
Svetoslavffb32b12015-10-15 16:54:00 -07006946 private void storeHasCurrentPermissionRequest(Bundle bundle) {
6947 if (bundle != null && mHasCurrentPermissionsRequest) {
6948 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
6949 }
6950 }
6951
6952 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
6953 if (bundle != null) {
6954 mHasCurrentPermissionsRequest = bundle.getBoolean(
6955 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
6956 }
6957 }
6958
George Mount0a778ed2013-12-13 13:35:36 -08006959 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006961 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6963 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006964 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006966 onActivityResult(requestCode, resultCode, data);
6967 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
6968 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
6969 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08006970 dispatchRequestPermissionsResult(requestCode, data);
6971 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006972 Fragment frag = mFragments.findFragmentByWho(who);
6973 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006974 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08006975 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006976 }
Svetoslav970b59c2015-06-09 16:05:21 -07006977 } else if (who.startsWith("@android:view:")) {
6978 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
6979 getActivityToken());
6980 for (ViewRootImpl viewRoot : views) {
6981 if (viewRoot.getView() != null
6982 && viewRoot.getView().dispatchActivityResult(
6983 who, requestCode, resultCode, data)) {
6984 return;
6985 }
6986 }
6987 } else {
6988 Fragment frag = mFragments.findFragmentByWho(who);
6989 if (frag != null) {
6990 frag.onActivityResult(requestCode, resultCode, data);
6991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 }
6993 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006994
Jason Monka57e5e02014-05-07 10:06:48 -04006995 /**
Jason Monk62515be2014-05-21 16:06:19 -04006996 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006997 * current task.
6998 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006999 * This will prevent the user from launching other apps, going to settings, or reaching the
7000 * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
7001 * values permit launching while locked.
Jason Monka57e5e02014-05-07 10:06:48 -04007002 *
Craig Mautner15df08a2015-04-01 12:17:18 -07007003 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
7004 * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
7005 * Lock Task mode. The user will not be able to exit this mode until
7006 * {@link Activity#stopLockTask()} is called.
Jason Monk62515be2014-05-21 16:06:19 -04007007 *
7008 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
7009 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04007010 * this mode. When entered through this method the user can exit at any time through
7011 * an action described by the request dialog. Calling stopLockTask will also exit the
7012 * mode.
Craig Mautner15df08a2015-04-01 12:17:18 -07007013 *
7014 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04007015 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007016 public void startLockTask() {
7017 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007018 ActivityManagerNative.getDefault().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007019 } catch (RemoteException e) {
7020 }
7021 }
7022
Jason Monka57e5e02014-05-07 10:06:48 -04007023 /**
7024 * Allow the user to switch away from the current task.
7025 *
7026 * Called to end the mode started by {@link Activity#startLockTask}. This
7027 * can only be called by activities that have successfully called
7028 * startLockTask previously.
7029 *
7030 * This will allow the user to exit this app and move onto other activities.
Craig Mautner15df08a2015-04-01 12:17:18 -07007031 * <p>Note: This method should only be called when the activity is user-facing. That is,
7032 * between onResume() and onPause().
7033 * <p>Note: If there are other tasks below this one that are also locked then calling this
7034 * method will immediately finish this task and resume the previous locked one, remaining in
7035 * lockTask mode.
7036 *
7037 * @see android.R.attr#lockTaskMode
7038 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04007039 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007040 public void stopLockTask() {
7041 try {
7042 ActivityManagerNative.getDefault().stopLockTaskMode();
7043 } catch (RemoteException e) {
7044 }
7045 }
7046
Craig Mautner5eda9b32013-07-02 11:58:16 -07007047 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007048 * Shows the user the system defined message for telling the user how to exit
7049 * lock task mode. The task containing this activity must be in lock task mode at the time
7050 * of this call for the message to be displayed.
7051 */
7052 public void showLockTaskEscapeMessage() {
7053 try {
7054 ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken);
7055 } catch (RemoteException e) {
7056 }
7057 }
7058
7059 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07007060 * Check whether the caption on freeform windows is displayed directly on the content.
7061 *
7062 * @return True if caption is displayed on content, false if it pushes the content down.
7063 *
7064 * @see {@link #setOverlayWithDecorCaptionEnabled(boolean)}
7065 */
7066 public boolean isOverlayWithDecorCaptionEnabled() {
7067 return mWindow.isOverlayWithDecorCaptionEnabled();
7068 }
7069
7070 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08007071 * Set whether the caption should displayed directly on the content rather than push it down.
7072 *
7073 * This affects only freeform windows since they display the caption and only the main
7074 * window of the activity. The caption is used to drag the window around and also shows
7075 * maximize and close action buttons.
7076 */
Andrii Kulian933076d2016-03-29 17:04:42 -07007077 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
7078 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08007079 }
7080
7081 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007082 * Interface for informing a translucent {@link Activity} once all visible activities below it
7083 * have completed drawing. This is necessary only after an {@link Activity} has been made
7084 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
7085 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07007086 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7087 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07007088 *
7089 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007090 */
Jose Lima14914852014-08-14 09:14:12 -07007091 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007092 public interface TranslucentConversionListener {
7093 /**
7094 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
7095 * below the top one have been redrawn. Following this callback it is safe to make the top
7096 * Activity translucent because the underlying Activity has been drawn.
7097 *
7098 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
7099 * occurred waiting for the Activity to complete drawing.
7100 *
7101 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07007102 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007103 */
7104 public void onTranslucentConversionComplete(boolean drawComplete);
7105 }
Svetoslavc6d1c342015-02-26 14:44:43 -08007106
7107 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07007108 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07007109 // If the package installer crashed we may have not data - best effort.
7110 String[] permissions = (data != null) ? data.getStringArrayExtra(
7111 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7112 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7113 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08007114 onRequestPermissionsResult(requestCode, permissions, grantResults);
7115 }
7116
7117 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07007118 Fragment fragment) {
7119 // If the package installer crashed we may have not data - best effort.
7120 String[] permissions = (data != null) ? data.getStringArrayExtra(
7121 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7122 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7123 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
7124 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08007125 }
7126
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007127 class HostCallbacks extends FragmentHostCallback<Activity> {
7128 public HostCallbacks() {
7129 super(Activity.this /*activity*/);
7130 }
7131
7132 @Override
7133 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
7134 Activity.this.dump(prefix, fd, writer, args);
7135 }
7136
7137 @Override
7138 public boolean onShouldSaveFragmentState(Fragment fragment) {
7139 return !isFinishing();
7140 }
7141
7142 @Override
7143 public LayoutInflater onGetLayoutInflater() {
7144 final LayoutInflater result = Activity.this.getLayoutInflater();
7145 if (onUseFragmentManagerInflaterFactory()) {
7146 return result.cloneInContext(Activity.this);
7147 }
7148 return result;
7149 }
7150
7151 @Override
7152 public boolean onUseFragmentManagerInflaterFactory() {
7153 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
7154 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
7155 }
7156
7157 @Override
7158 public Activity onGetHost() {
7159 return Activity.this;
7160 }
7161
7162 @Override
7163 public void onInvalidateOptionsMenu() {
7164 Activity.this.invalidateOptionsMenu();
7165 }
7166
7167 @Override
7168 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
7169 Bundle options) {
7170 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
7171 }
7172
7173 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01007174 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
7175 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
7176 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
7177 if (mParent == null) {
7178 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
7179 flagsMask, flagsValues, options);
7180 } else if (options != null) {
7181 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
7182 fillInIntent, flagsMask, flagsValues, extraFlags, options);
7183 }
7184 }
7185
7186 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007187 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7188 int requestCode) {
7189 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7190 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7191 startActivityForResult(who, intent, requestCode, null);
7192 }
7193
7194 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007195 public boolean onHasWindowAnimations() {
7196 return getWindow() != null;
7197 }
7198
7199 @Override
7200 public int onGetWindowAnimations() {
7201 final Window w = getWindow();
7202 return (w == null) ? 0 : w.getAttributes().windowAnimations;
7203 }
7204
Todd Kennedy434bd652015-05-04 12:29:50 -07007205 @Override
7206 public void onAttachFragment(Fragment fragment) {
7207 Activity.this.onAttachFragment(fragment);
7208 }
7209
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007210 @Nullable
7211 @Override
7212 public View onFindViewById(int id) {
7213 return Activity.this.findViewById(id);
7214 }
7215
7216 @Override
7217 public boolean onHasView() {
7218 final Window w = getWindow();
7219 return (w != null && w.peekDecorView() != null);
7220 }
7221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222}