blob: cfda8037072fb9aab9de2001046d0c92f1bbd116 [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
Jorim Jaggif84e2f62018-01-16 14:17:59 +010019import static android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS;
Felipe Leme5b32ebe2018-02-15 12:52:19 -080020
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070021import static java.lang.Character.MIN_VALUE;
Winson Chung1af8eda2016-02-05 17:55:56 +000022
Tor Norbyec615c6f2015-03-02 10:11:44 -080023import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.DrawableRes;
25import android.annotation.IdRes;
26import android.annotation.IntDef;
27import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070028import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070029import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070030import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070031import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070032import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070033import android.annotation.SystemApi;
Tiger Huangfc218452018-09-27 00:38:50 +080034import android.annotation.TestApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010035import android.annotation.UnsupportedAppUsage;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070036import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040037import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070038import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070039import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.ComponentName;
41import android.content.ContentResolver;
42import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050043import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070044import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070045import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070046import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.SharedPreferences;
48import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080049import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070050import android.content.pm.PackageManager;
51import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.res.Configuration;
53import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070054import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.database.Cursor;
56import android.graphics.Bitmap;
57import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080058import android.graphics.Color;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070059import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.graphics.drawable.Drawable;
61import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070062import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070064import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070065import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Handler;
68import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080069import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070070import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000071import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060073import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080074import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080075import android.os.SystemProperties;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070076import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.text.Selection;
78import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070079import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000081import android.transition.Scene;
82import android.transition.TransitionManager;
83import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
86import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070087import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070088import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000090import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070091import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070093import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070095import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +000096import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +010098import android.view.KeyboardShortcutGroup;
99import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.view.LayoutInflater;
101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
104import android.view.MotionEvent;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100105import android.view.RemoteAnimationDefinition;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700106import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700108import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700110import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000112import android.view.ViewRootImpl;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700113import android.view.ViewRootImpl.ActivityConfigCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000115import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700117import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700118import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov24c90452017-12-27 15:17:14 -0800119import android.view.autofill.AutofillId;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700120import android.view.autofill.AutofillManager;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700121import android.view.autofill.AutofillManager.AutofillClient;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700122import android.view.autofill.AutofillPopupWindow;
123import android.view.autofill.IAutofillWindowPresenter;
Felipe Leme749b8892018-12-03 16:30:30 -0800124import android.view.contentcapture.ContentCaptureEvent;
125import android.view.contentcapture.ContentCaptureManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800127import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000128import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100129
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700130import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800131import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700132import com.android.internal.app.IVoiceInteractor;
133import com.android.internal.app.ToolbarActionBar;
134import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700135import com.android.internal.policy.PhoneWindow;
136
Dianne Hackborn625ac272010-09-17 18:29:22 -0700137import java.io.FileDescriptor;
138import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700139import java.lang.annotation.Retention;
140import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700141import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800142import java.util.Arrays;
Adam Powell6e346362010-07-23 10:18:23 -0700143import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700144import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Bryce Leee83f34cd2017-10-31 19:50:54 -0700146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147/**
148 * An activity is a single, focused thing that the user can do. Almost all
149 * activities interact with the user, so the Activity class takes care of
150 * creating a window for you in which you can place your UI with
151 * {@link #setContentView}. While activities are often presented to the user
152 * as full-screen windows, they can also be used in other ways: as floating
153 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
154 * or embedded inside of another activity (using {@link ActivityGroup}).
155 *
156 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 * <ul>
159 * <li> {@link #onCreate} is where you initialize your activity. Most
160 * importantly, here you will usually call {@link #setContentView(int)}
161 * with a layout resource defining your UI, and using {@link #findViewById}
162 * to retrieve the widgets in that UI that you need to interact with
163 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700164 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * <li> {@link #onPause} is where you deal with the user leaving your
166 * activity. Most importantly, any changes made by the user should at this
167 * point be committed (usually to the
168 * {@link android.content.ContentProvider} holding the data).
169 * </ul>
170 *
171 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
172 * activity classes must have a corresponding
173 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
174 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700175 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * <p>Topics covered here:
177 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700178 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
180 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
181 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
182 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
183 * <li><a href="#Permissions">Permissions</a>
184 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
185 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700186 *
187 * <div class="special reference">
188 * <h3>Developer Guides</h3>
189 * <p>The Activity class is an important part of an application's overall lifecycle,
190 * and the way activities are launched and put together is a fundamental
191 * part of the platform's application model. For a detailed perspective on the structure of an
192 * Android application and how activities behave, please read the
193 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700194 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700195 * developer guides.</p>
196 *
197 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700198 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700199 * developer guide.</p>
200 * </div>
201 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700202 * <a name="Fragments"></a>
203 * <h3>Fragments</h3>
204 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800205 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
206 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700207 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800208 * screens, and help scale their application between small and large screens.</p>
209 *
210 * <p>For more information about using fragments, read the
211 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700212 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * <a name="ActivityLifecycle"></a>
214 * <h3>Activity Lifecycle</h3>
215 *
216 * <p>Activities in the system are managed as an <em>activity stack</em>.
217 * When a new activity is started, it is placed on the top of the stack
218 * and becomes the running activity -- the previous activity always remains
219 * below it in the stack, and will not come to the foreground again until
220 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700221 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <p>An activity has essentially four states:</p>
223 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700224 * <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 -0800225 * the stack),
226 * it is <em>active</em> or <em>running</em>. </li>
227 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700228 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * is <em>paused</em>. A paused activity is completely alive (it
230 * maintains all state and member information and remains attached to
231 * the window manager), but can be killed by the system in extreme
232 * low memory situations.
233 * <li>If an activity is completely obscured by another activity,
234 * it is <em>stopped</em>. It still retains all state and member information,
235 * however, it is no longer visible to the user so its window is hidden
236 * and it will often be killed by the system when memory is needed
237 * elsewhere.</li>
238 * <li>If an activity is paused or stopped, the system can drop the activity
239 * from memory by either asking it to finish, or simply killing its
240 * process. When it is displayed again to the user, it must be
241 * completely restarted and restored to its previous state.</li>
242 * </ul>
243 *
244 * <p>The following diagram shows the important state paths of an Activity.
245 * The square rectangles represent callback methods you can implement to
246 * perform operations when the Activity moves between states. The colored
247 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * <p><img src="../../../images/activity_lifecycle.png"
250 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * <p>There are three key loops you may be interested in monitoring within your
253 * activity:
RoboErik55011652014-07-09 15:05:53 -0700254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * <ul>
256 * <li>The <b>entire lifetime</b> of an activity happens between the first call
257 * to {@link android.app.Activity#onCreate} through to a single final call
258 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
259 * of "global" state in onCreate(), and release all remaining resources in
260 * onDestroy(). For example, if it has a thread running in the background
261 * to download data from the network, it may create that thread in onCreate()
262 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * <li>The <b>visible lifetime</b> of an activity happens between a call to
265 * {@link android.app.Activity#onStart} until a corresponding call to
266 * {@link android.app.Activity#onStop}. During this time the user can see the
267 * activity on-screen, though it may not be in the foreground and interacting
268 * with the user. Between these two methods you can maintain resources that
269 * are needed to show the activity to the user. For example, you can register
270 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900271 * that impact your UI, and unregister it in onStop() when the user no
272 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * can be called multiple times, as the activity becomes visible and hidden
274 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
277 * {@link android.app.Activity#onResume} until a corresponding call to
278 * {@link android.app.Activity#onPause}. During this time the activity is
279 * in front of all other activities and interacting with the user. An activity
280 * can frequently go between the resumed and paused states -- for example when
281 * the device goes to sleep, when an activity result is delivered, when a new
282 * intent is delivered -- so the code in these methods should be fairly
283 * lightweight.
284 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * <p>The entire lifecycle of an activity is defined by the following
287 * Activity methods. All of these are hooks that you can override
288 * to do appropriate work when the activity changes state. All
289 * activities will implement {@link android.app.Activity#onCreate}
290 * to do their initial setup; many will also implement
291 * {@link android.app.Activity#onPause} to commit changes to data and
292 * otherwise prepare to stop interacting with the user. You should always
293 * call up to your superclass when implementing these methods.</p>
294 *
295 * </p>
296 * <pre class="prettyprint">
297 * public class Activity extends ApplicationContext {
298 * protected void onCreate(Bundle savedInstanceState);
299 *
300 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700301 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 * protected void onRestart();
303 *
304 * protected void onResume();
305 *
306 * protected void onPause();
307 *
308 * protected void onStop();
309 *
310 * protected void onDestroy();
311 * }
312 * </pre>
313 *
314 * <p>In general the movement through an activity's lifecycle looks like
315 * this:</p>
316 *
317 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
318 * <colgroup align="left" span="3" />
319 * <colgroup align="left" />
320 * <colgroup align="center" />
321 * <colgroup align="center" />
322 *
323 * <thead>
324 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
325 * </thead>
326 *
327 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700328 * <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 -0800329 * <td>Called when the activity is first created.
330 * This is where you should do all of your normal static set up:
331 * create views, bind data to lists, etc. This method also
332 * provides you with a Bundle containing the activity's previously
333 * frozen state, if there was one.
334 * <p>Always followed by <code>onStart()</code>.</td>
335 * <td align="center">No</td>
336 * <td align="center"><code>onStart()</code></td>
337 * </tr>
338 *
339 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700340 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 * <td>Called after your activity has been stopped, prior to it being
342 * started again.
343 * <p>Always followed by <code>onStart()</code></td>
344 * <td align="center">No</td>
345 * <td align="center"><code>onStart()</code></td>
346 * </tr>
347 *
smain@google.com10e29b82016-06-23 12:09:30 -0700348 * <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 -0800349 * <td>Called when the activity is becoming visible to the user.
350 * <p>Followed by <code>onResume()</code> if the activity comes
351 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
352 * <td align="center">No</td>
353 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
354 * </tr>
355 *
356 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700357 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * <td>Called when the activity will start
359 * interacting with the user. At this point your activity is at
360 * the top of the activity stack, with user input going to it.
361 * <p>Always followed by <code>onPause()</code>.</td>
362 * <td align="center">No</td>
363 * <td align="center"><code>onPause()</code></td>
364 * </tr>
365 *
smain@google.com10e29b82016-06-23 12:09:30 -0700366 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * <td>Called when the system is about to start resuming a previous
368 * activity. This is typically used to commit unsaved changes to
369 * persistent data, stop animations and other things that may be consuming
370 * CPU, etc. Implementations of this method must be very quick because
371 * the next activity will not be resumed until this method returns.
372 * <p>Followed by either <code>onResume()</code> if the activity
373 * returns back to the front, or <code>onStop()</code> if it becomes
374 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800375 * <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 -0800376 * <td align="center"><code>onResume()</code> or<br>
377 * <code>onStop()</code></td>
378 * </tr>
379 *
smain@google.com10e29b82016-06-23 12:09:30 -0700380 * <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 -0800381 * <td>Called when the activity is no longer visible to the user, because
382 * another activity has been resumed and is covering this one. This
383 * may happen either because a new activity is being started, an existing
384 * one is being brought in front of this one, or this one is being
385 * destroyed.
386 * <p>Followed by either <code>onRestart()</code> if
387 * this activity is coming back to interact with the user, or
388 * <code>onDestroy()</code> if this activity is going away.</td>
389 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
390 * <td align="center"><code>onRestart()</code> or<br>
391 * <code>onDestroy()</code></td>
392 * </tr>
393 *
smain@google.com10e29b82016-06-23 12:09:30 -0700394 * <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 -0800395 * <td>The final call you receive before your
396 * activity is destroyed. This can happen either because the
397 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700398 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 * instance of the activity to save space. You can distinguish
400 * between these two scenarios with the {@link
401 * Activity#isFinishing} method.</td>
402 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
403 * <td align="center"><em>nothing</em></td>
404 * </tr>
405 * </tbody>
406 * </table>
407 *
408 * <p>Note the "Killable" column in the above table -- for those methods that
409 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800410 * 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 -0800411 * of its code being executed. Because of this, you should use the
412 * {@link #onPause} method to write any persistent data (such as user edits)
413 * to storage. In addition, the method
414 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
415 * in such a background state, allowing you to save away any dynamic instance
416 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700417 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
419 * section for more information on how the lifecycle of a process is tied
420 * to the activities it is hosting. Note that it is important to save
421 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800422 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * be called in every situation as described in its documentation.</p>
424 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800425 * <p class="note">Be aware that these semantics will change slightly between
426 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
427 * vs. those targeting prior platforms. Starting with Honeycomb, an application
428 * is not in the killable state until its {@link #onStop} has returned. This
429 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700430 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800431 * wait until {@link #onStop()} to save persistent state.</p>
432 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800433 * <p class="note">For applications targeting platforms starting with
434 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
435 * will always be called after {@link #onStop}, so an application may safely
436 * perform fragment transactions in {@link #onStop} and will be able to save
437 * persistent state later.</p>
438 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * <p>For those methods that are not marked as being killable, the activity's
440 * process will not be killed by the system starting from the time the method
441 * is called and continuing after it returns. Thus an activity is in the killable
442 * state, for example, between after <code>onPause()</code> to the start of
443 * <code>onResume()</code>.</p>
444 *
445 * <a name="ConfigurationChanges"></a>
446 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * <p>If the configuration of the device (as defined by the
449 * {@link Configuration Resources.Configuration} class) changes,
450 * then anything displaying a user interface will need to update to match that
451 * configuration. Because Activity is the primary mechanism for interacting
452 * with the user, it includes special support for handling configuration
453 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * <p>Unless you specify otherwise, a configuration change (such as a change
456 * in screen orientation, language, input devices, etc) will cause your
457 * current activity to be <em>destroyed</em>, going through the normal activity
458 * lifecycle process of {@link #onPause},
459 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
460 * had been in the foreground or visible to the user, once {@link #onDestroy} is
461 * called in that instance then a new instance of the activity will be
462 * created, with whatever savedInstanceState the previous instance had generated
463 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * <p>This is done because any application resource,
466 * including layout files, can change based on any configuration value. Thus
467 * the only safe way to handle a configuration change is to re-retrieve all
468 * resources, including layouts, drawables, and strings. Because activities
469 * must already know how to save their state and re-create themselves from
470 * that state, this is a convenient way to have an activity restart itself
471 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700472 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * <p>In some special cases, you may want to bypass restarting of your
474 * activity based on one or more types of configuration changes. This is
475 * done with the {@link android.R.attr#configChanges android:configChanges}
476 * attribute in its manifest. For any types of configuration changes you say
477 * that you handle there, you will receive a call to your current activity's
478 * {@link #onConfigurationChanged} method instead of being restarted. If
479 * a configuration change involves any that you do not handle, however, the
480 * activity will still be restarted and {@link #onConfigurationChanged}
481 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700482 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 * <a name="StartingActivities"></a>
484 * <h3>Starting Activities and Getting Results</h3>
485 *
486 * <p>The {@link android.app.Activity#startActivity}
487 * method is used to start a
488 * new activity, which will be placed at the top of the activity stack. It
489 * takes a single argument, an {@link android.content.Intent Intent},
490 * which describes the activity
491 * to be executed.</p>
492 *
493 * <p>Sometimes you want to get a result back from an activity when it
494 * ends. For example, you may start an activity that lets the user pick
495 * a person in a list of contacts; when it ends, it returns the person
496 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700497 * {@link android.app.Activity#startActivityForResult(Intent, int)}
498 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700500 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 *
502 * <p>When an activity exits, it can call
503 * {@link android.app.Activity#setResult(int)}
504 * to return data back to its parent. It must always supply a result code,
505 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
506 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
507 * return back an Intent containing any additional data it wants. All of this
508 * information appears back on the
509 * parent's <code>Activity.onActivityResult()</code>, along with the integer
510 * identifier it originally supplied.</p>
511 *
512 * <p>If a child activity fails for any reason (such as crashing), the parent
513 * activity will receive a result with the code RESULT_CANCELED.</p>
514 *
515 * <pre class="prettyprint">
516 * public class MyActivity extends Activity {
517 * ...
518 *
519 * static final int PICK_CONTACT_REQUEST = 0;
520 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700521 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
523 * // When the user center presses, let them pick a contact.
524 * startActivityForResult(
525 * new Intent(Intent.ACTION_PICK,
526 * new Uri("content://contacts")),
527 * PICK_CONTACT_REQUEST);
528 * return true;
529 * }
530 * return false;
531 * }
532 *
533 * protected void onActivityResult(int requestCode, int resultCode,
534 * Intent data) {
535 * if (requestCode == PICK_CONTACT_REQUEST) {
536 * if (resultCode == RESULT_OK) {
537 * // A contact was picked. Here we will just display it
538 * // to the user.
539 * startActivity(new Intent(Intent.ACTION_VIEW, data));
540 * }
541 * }
542 * }
543 * }
544 * </pre>
545 *
546 * <a name="SavingPersistentState"></a>
547 * <h3>Saving Persistent State</h3>
548 *
koprivafc3949e2018-09-17 11:35:13 -0700549 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 * will deal with: shared document-like data (typically stored in a SQLite
551 * database using a {@linkplain android.content.ContentProvider content provider})
552 * and internal state such as user preferences.</p>
553 *
koprivafc3949e2018-09-17 11:35:13 -0700554 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * "edit in place" user model. That is, any edits a user makes are effectively
556 * made immediately without requiring an additional confirmation step.
557 * Supporting this model is generally a simple matter of following two rules:</p>
558 *
559 * <ul>
560 * <li> <p>When creating a new document, the backing database entry or file for
561 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700562 * a new email, a new entry for that email is created as soon as they
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700564 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
566 * commit to the backing content provider or file any changes the user
567 * has made. This ensures that those changes will be seen by any other
568 * activity that is about to run. You will probably want to commit
569 * your data even more aggressively at key times during your
570 * activity's lifecycle: for example before starting a new
571 * activity, before finishing your own activity, when the user
572 * switches between input fields, etc.</p>
573 * </ul>
574 *
575 * <p>This model is designed to prevent data loss when a user is navigating
576 * between activities, and allows the system to safely kill an activity (because
577 * system resources are needed somewhere else) at any time after it has been
578 * paused. Note this implies
579 * that the user pressing BACK from your activity does <em>not</em>
580 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800581 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
583 *
584 * <p>See the {@linkplain android.content.ContentProvider content package} for
585 * more information about content providers. These are a key aspect of how
586 * different activities invoke and propagate data between themselves.</p>
587 *
588 * <p>The Activity class also provides an API for managing internal persistent state
589 * associated with an activity. This can be used, for example, to remember
590 * the user's preferred initial display in a calendar (day view or week view)
591 * or the user's default home page in a web browser.</p>
592 *
593 * <p>Activity persistent state is managed
594 * with the method {@link #getPreferences},
595 * allowing you to retrieve and
596 * modify a set of name/value pairs associated with the activity. To use
597 * preferences that are shared across multiple application components
598 * (activities, receivers, services, providers), you can use the underlying
599 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
600 * to retrieve a preferences
601 * object stored under a specific name.
602 * (Note that it is not possible to share settings data across application
603 * packages -- for that you will need a content provider.)</p>
604 *
605 * <p>Here is an excerpt from a calendar activity that stores the user's
606 * preferred view mode in its persistent settings:</p>
607 *
608 * <pre class="prettyprint">
609 * public class CalendarActivity extends Activity {
610 * ...
611 *
612 * static final int DAY_VIEW_MODE = 0;
613 * static final int WEEK_VIEW_MODE = 1;
614 *
615 * private SharedPreferences mPrefs;
616 * private int mCurViewMode;
617 *
618 * protected void onCreate(Bundle savedInstanceState) {
619 * super.onCreate(savedInstanceState);
620 *
621 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700622 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * }
624 *
625 * protected void onPause() {
626 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700627 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 * SharedPreferences.Editor ed = mPrefs.edit();
629 * ed.putInt("view_mode", mCurViewMode);
630 * ed.commit();
631 * }
632 * }
633 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * <a name="Permissions"></a>
636 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 * <p>The ability to start a particular Activity can be enforced when it is
639 * declared in its
640 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
641 * tag. By doing so, other applications will need to declare a corresponding
642 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
643 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800644 *
645 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
646 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
647 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
648 * Activity access to the specific URIs in the Intent. Access will remain
649 * until the Activity has finished (it will remain across the hosting
650 * process being killed and other temporary destruction). As of
651 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
652 * was already created and a new Intent is being delivered to
653 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
654 * to the existing ones it holds.
655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
657 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700658 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 * <a name="ProcessLifecycle"></a>
660 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700661 *
kopriva4dddc632018-03-19 14:13:08 -0700662 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700664 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700666 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700668 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 * processes (the last ones) before it resorts to killing more important
670 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700671 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 * <ol>
673 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
674 * that the user is currently interacting with) is considered the most important.
675 * Its process will only be killed as a last resort, if it uses more memory
676 * than is available on the device. Generally at this point the device has
677 * reached a memory paging state, so this is required in order to keep the user
678 * interface responsive.
679 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
680 * but not in the foreground, such as one sitting behind a foreground dialog)
681 * is considered extremely important and will not be killed unless that is
682 * required to keep the foreground activity running.
683 * <li> <p>A <b>background activity</b> (an activity that is not visible to
684 * the user and has been paused) is no longer critical, so the system may
685 * safely kill its process to reclaim memory for other foreground or
686 * visible processes. If its process needs to be killed, when the user navigates
687 * back to the activity (making it visible on the screen again), its
688 * {@link #onCreate} method will be called with the savedInstanceState it had previously
689 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
690 * state as the user last left it.
691 * <li> <p>An <b>empty process</b> is one hosting no activities or other
692 * application components (such as {@link Service} or
693 * {@link android.content.BroadcastReceiver} classes). These are killed very
694 * quickly by the system as memory becomes low. For this reason, any
695 * background operation you do outside of an activity must be executed in the
696 * context of an activity BroadcastReceiver or Service to ensure that the system
697 * knows it needs to keep your process around.
698 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700699 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 * <p>Sometimes an Activity may need to do a long-running operation that exists
701 * independently of the activity lifecycle itself. An example may be a camera
702 * application that allows you to upload a picture to a web site. The upload
703 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700704 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 * should start a {@link Service} in which the upload takes place. This allows
706 * the system to properly prioritize your process (considering it to be more
707 * important than other non-visible applications) for the duration of the
708 * upload, independent of whether the original activity is paused, stopped,
709 * or finished.
710 */
711public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700712 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700714 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000715 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Leme640f30a2017-03-06 15:44:06 -0800716 AutofillManager.AutofillClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700718 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
720 /** Standard activity result: operation canceled. */
721 public static final int RESULT_CANCELED = 0;
722 /** Standard activity result: operation succeeded. */
723 public static final int RESULT_OK = -1;
724 /** Start of user-defined activity results. */
725 public static final int RESULT_FIRST_USER = 1;
726
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700727 /** @hide Task isn't finished when activity is finished */
728 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700729 /**
730 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
731 * past behavior the task is also removed from recents.
732 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700733 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700734 /**
735 * @hide Task is finished along with the finishing activity, but it is not removed from
736 * recents.
737 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700738 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
739
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100740 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700741 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700742 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700743
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700744 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
746 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
747 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
748 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800749 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700750 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
751 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
Svetoslav970b59c2015-06-09 16:05:21 -0700753 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000754 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700755
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100756 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100757
Andrii Kuliand25680c2018-02-21 15:16:58 -0800758 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
759 private static final int LOG_AM_ON_START_CALLED = 30059;
760 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
761 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
762 private static final int LOG_AM_ON_STOP_CALLED = 30049;
763 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
764 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700765 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800766
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800767 private static class ManagedDialog {
768 Dialog mDialog;
769 Bundle mArgs;
770 }
771 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772
773 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100774 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 private Instrumentation mInstrumentation;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100776 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 private IBinder mToken;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100778 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700779 private int mIdent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100780 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 /*package*/ String mEmbeddedID;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100782 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 private Application mApplication;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100784 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700785 /*package*/ Intent mIntent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100786 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800787 /*package*/ String mReferrer;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100788 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 private ComponentName mComponent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100790 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100792 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 /*package*/ ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100794 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 Activity mParent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100796 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 boolean mCalled;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100798 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600799 /*package*/ boolean mResumed;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100800 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700801 /*package*/ boolean mStopped;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100802 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 boolean mFinished;
804 boolean mStartedActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100805 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700806 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700807 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200808 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700809
810 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
811 * This only affects the client-side exception, the actual state check still happens in AMS. */
812 private boolean mCanEnterPictureInPicture = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700813 /** true if the activity is going through a transient pause */
814 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500815 /** true if the activity is being destroyed in order to recreate it with a new configuration */
816 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100817 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 /*package*/ int mConfigChangeFlags;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100819 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100821 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700822 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700824 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
825 @Nullable private AutofillManager mAutofillManager;
826
Felipe Lemeecb08be2018-11-27 15:48:47 -0800827 /** The content capture manager. Always access via {@link #getContentCaptureManager()}. */
828 @Nullable private ContentCaptureManager mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800829
Ian Lake8a88cd62018-09-28 13:25:20 -0700830 private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
831 new ArrayList<Application.ActivityLifecycleCallbacks>();
Felipe Lemee348dc32018-11-05 12:35:29 -0800832
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700833 static final class NonConfigurationInstances {
834 Object activity;
835 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800836 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700837 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700838 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700839 }
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100840 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700841 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700842
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100843 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 private Window mWindow;
845
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100846 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 private WindowManager mWindowManager;
848 /*package*/ View mDecor = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100849 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 /*package*/ boolean mWindowAdded = false;
851 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100852 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700854 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700855 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856
Mathew Inwood8c854f82018-09-14 12:35:36 +0100857 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn91097de2014-04-04 18:02:06 -0700858 private VoiceInteractor mVoiceInteractor;
859
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100860 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 private CharSequence mTitle;
862 private int mTitleColor = 0;
863
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700864 // we must have a handler before the FragmentController is constructed
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100865 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700866 final Handler mHandler = new Handler();
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100867 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700868 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 private static final class ManagedCursor {
871 ManagedCursor(Cursor cursor) {
872 mCursor = cursor;
873 mReleased = false;
874 mUpdated = false;
875 }
876
877 private final Cursor mCursor;
878 private boolean mReleased;
879 private boolean mUpdated;
880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700882 @GuardedBy("mManagedCursors")
883 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
884
885 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100886 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700888 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100889 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700891
Craig Mautner5eda9b32013-07-02 11:58:16 -0700892 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700893 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700895 private SearchEvent mSearchEvent;
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000898 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899
900 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
901 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700902
Winsonb6403152016-02-23 13:32:09 -0800903 private ActivityManager.TaskDescription mTaskDescription =
904 new ActivityManager.TaskDescription();
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
907
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700908 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700909 private final Object mInstanceTracker = StrictMode.trackActivity(this);
910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700912
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100913 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700914 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700915 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
916 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
Svetoslavffb32b12015-10-15 16:54:00 -0700918 private boolean mHasCurrentPermissionsRequest;
919
Svet Ganov17db9dc2017-02-21 19:54:31 -0800920 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800921 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800922
Phil Weaver846cda932017-06-15 10:10:06 -0700923 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
924 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700925
Felipe Leme4753bb02017-03-22 20:24:00 -0700926 private AutofillPopupWindow mAutofillPopupWindow;
927
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800928 private static native String getDlWarning();
929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 /** Return the intent that started this activity. */
931 public Intent getIntent() {
932 return mIntent;
933 }
934
RoboErik55011652014-07-09 15:05:53 -0700935 /**
936 * Change the intent returned by {@link #getIntent}. This holds a
937 * reference to the given intent; it does not copy it. Often used in
938 * conjunction with {@link #onNewIntent}.
939 *
940 * @param newIntent The new Intent object to return from getIntent
941 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 * @see #getIntent
943 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700944 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 public void setIntent(Intent newIntent) {
946 mIntent = newIntent;
947 }
948
949 /** Return the application that owns this activity. */
950 public final Application getApplication() {
951 return mApplication;
952 }
953
954 /** Is this activity embedded inside of another activity? */
955 public final boolean isChild() {
956 return mParent != null;
957 }
RoboErik55011652014-07-09 15:05:53 -0700958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 /** Return the parent activity if this view is an embedded child. */
960 public final Activity getParent() {
961 return mParent;
962 }
963
964 /** Retrieve the window manager for showing custom windows. */
965 public WindowManager getWindowManager() {
966 return mWindowManager;
967 }
968
969 /**
970 * Retrieve the current {@link android.view.Window} for the activity.
971 * This can be used to directly access parts of the Window API that
972 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 * @return Window The current window, or null if the activity is not
975 * visual.
976 */
977 public Window getWindow() {
978 return mWindow;
979 }
980
981 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800982 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -0800983 *
984 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -0700985 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800986 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -0700987 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700988 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700989 }
RoboErik55011652014-07-09 15:05:53 -0700990
Dianne Hackbornc8017682010-07-06 13:34:38 -0700991 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 * Calls {@link android.view.Window#getCurrentFocus} on the
993 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700994 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700996 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 * @see #getWindow
998 * @see android.view.Window#getCurrentFocus
999 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001000 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public View getCurrentFocus() {
1002 return mWindow != null ? mWindow.getCurrentFocus() : null;
1003 }
1004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 /**
Felipe Lemee348dc32018-11-05 12:35:29 -08001006 * (Creates, sets and) returns the autofill manager
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001007 *
1008 * @return The autofill manager
1009 */
1010 @NonNull private AutofillManager getAutofillManager() {
1011 if (mAutofillManager == null) {
1012 mAutofillManager = getSystemService(AutofillManager.class);
1013 }
1014
1015 return mAutofillManager;
1016 }
1017
Felipe Lemee348dc32018-11-05 12:35:29 -08001018 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -08001019 * (Creates, sets, and ) returns the content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001020 *
Felipe Lemeecb08be2018-11-27 15:48:47 -08001021 * @return The content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001022 */
Felipe Lemeecb08be2018-11-27 15:48:47 -08001023 @NonNull private ContentCaptureManager getContentCaptureManager() {
1024 if (mContentCaptureManager == null) {
1025 mContentCaptureManager = getSystemService(ContentCaptureManager.class);
Felipe Lemee348dc32018-11-05 12:35:29 -08001026 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001027 return mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -08001028 }
1029
Felipe Lemeecb08be2018-11-27 15:48:47 -08001030 private void notifyContentCaptureManagerIfNeeded(@ContentCaptureEvent.EventType int event) {
1031 final ContentCaptureManager cm = getContentCaptureManager();
1032 if (cm == null || !cm.isContentCaptureEnabled()) {
Felipe Leme7a534082018-11-05 15:03:04 -08001033 return;
1034 }
1035 switch (event) {
1036 case ContentCaptureEvent.TYPE_ACTIVITY_CREATED:
1037 //TODO(b/111276913): decide whether the InteractionSessionId should be
1038 // saved / restored in the activity bundle.
Felipe Lemeecb08be2018-11-27 15:48:47 -08001039 cm.onActivityCreated(mToken, getComponentName());
Felipe Leme7a534082018-11-05 15:03:04 -08001040 break;
1041 case ContentCaptureEvent.TYPE_ACTIVITY_DESTROYED:
Felipe Lemeecb08be2018-11-27 15:48:47 -08001042 cm.onActivityDestroyed();
Felipe Leme7a534082018-11-05 15:03:04 -08001043 break;
1044 case ContentCaptureEvent.TYPE_ACTIVITY_STARTED:
1045 case ContentCaptureEvent.TYPE_ACTIVITY_RESUMED:
1046 case ContentCaptureEvent.TYPE_ACTIVITY_PAUSED:
1047 case ContentCaptureEvent.TYPE_ACTIVITY_STOPPED:
Felipe Lemeecb08be2018-11-27 15:48:47 -08001048 cm.onActivityLifecycleEvent(event);
Felipe Leme7a534082018-11-05 15:03:04 -08001049 break;
1050 default:
Felipe Lemeecb08be2018-11-27 15:48:47 -08001051 Log.w(TAG, "notifyContentCaptureManagerIfNeeded(): invalid type " + event);
Felipe Leme7a534082018-11-05 15:03:04 -08001052 }
1053 }
1054
Felipe Lemebb567ae2017-10-04 09:56:21 -07001055 @Override
1056 protected void attachBaseContext(Context newBase) {
1057 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001058 if (newBase != null) {
1059 newBase.setAutofillClient(this);
Felipe Lemeecb08be2018-11-27 15:48:47 -08001060 newBase.setContentCaptureSupported(true);
Felipe Lemed247de82018-05-14 17:51:58 -07001061 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001062 }
1063
1064 /** @hide */
1065 @Override
1066 public final AutofillClient getAutofillClient() {
1067 return this;
1068 }
1069
Felipe Lemeecb08be2018-11-27 15:48:47 -08001070 /** @hide */
1071 @Override
1072 public boolean isContentCaptureSupported() {
1073 return true;
1074 }
1075
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001076 /**
Ian Lake8a88cd62018-09-28 13:25:20 -07001077 * Register an {@link Application.ActivityLifecycleCallbacks} instance that receives
1078 * lifecycle callbacks for only this Activity.
1079 * <p>
1080 * In relation to any
1081 * {@link Application#registerActivityLifecycleCallbacks Application registered callbacks},
1082 * the callbacks registered here will always occur nested within those callbacks. This means:
1083 * <ul>
1084 * <li>Pre events will first be sent to Application registered callbacks, then to callbacks
1085 * registered here.</li>
1086 * <li>{@link Application.ActivityLifecycleCallbacks#onActivityCreated(Activity, Bundle)},
1087 * {@link Application.ActivityLifecycleCallbacks#onActivityStarted(Activity)}, and
1088 * {@link Application.ActivityLifecycleCallbacks#onActivityResumed(Activity)} will
1089 * be sent first to Application registered callbacks, then to callbacks registered here.
1090 * For all other events, callbacks registered here will be sent first.</li>
1091 * <li>Post events will first be sent to callbacks registered here, then to
1092 * Application registered callbacks.</li>
1093 * </ul>
1094 * <p>
1095 * If multiple callbacks are registered here, they receive events in a first in (up through
1096 * {@link Application.ActivityLifecycleCallbacks#onActivityPostResumed}, last out
1097 * ordering.
1098 * <p>
1099 * It is strongly recommended to register this in the constructor of your Activity to ensure
1100 * you get all available callbacks. As this callback is associated with only this Activity,
1101 * it is not usually necessary to {@link #unregisterActivityLifecycleCallbacks unregister} it
1102 * unless you specifically do not want to receive further lifecycle callbacks.
1103 *
1104 * @param callback The callback instance to register
1105 */
1106 public void registerActivityLifecycleCallbacks(
1107 @NonNull Application.ActivityLifecycleCallbacks callback) {
1108 synchronized (mActivityLifecycleCallbacks) {
1109 mActivityLifecycleCallbacks.add(callback);
1110 }
1111 }
1112
1113 /**
1114 * Unregister an {@link Application.ActivityLifecycleCallbacks} previously registered
1115 * with {@link #registerActivityLifecycleCallbacks}. It will not receive any further
1116 * callbacks.
1117 *
1118 * @param callback The callback instance to unregister
1119 * @see #registerActivityLifecycleCallbacks
1120 */
1121 public void unregisterActivityLifecycleCallbacks(
1122 @NonNull Application.ActivityLifecycleCallbacks callback) {
1123 synchronized (mActivityLifecycleCallbacks) {
1124 mActivityLifecycleCallbacks.remove(callback);
1125 }
1126 }
1127
1128 private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
1129 getApplication().dispatchActivityPreCreated(this, savedInstanceState);
1130 Object[] callbacks = collectActivityLifecycleCallbacks();
1131 if (callbacks != null) {
1132 for (int i = 0; i < callbacks.length; i++) {
1133 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
1134 savedInstanceState);
1135 }
1136 }
1137 }
1138
1139 private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
1140 getApplication().dispatchActivityCreated(this, savedInstanceState);
1141 Object[] callbacks = collectActivityLifecycleCallbacks();
1142 if (callbacks != null) {
1143 for (int i = 0; i < callbacks.length; i++) {
1144 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
1145 savedInstanceState);
1146 }
1147 }
1148 }
1149
1150 private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
1151 Object[] callbacks = collectActivityLifecycleCallbacks();
1152 if (callbacks != null) {
1153 for (int i = 0; i < callbacks.length; i++) {
1154 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
1155 savedInstanceState);
1156 }
1157 }
1158 getApplication().dispatchActivityPostCreated(this, savedInstanceState);
1159 }
1160
1161 private void dispatchActivityPreStarted() {
1162 getApplication().dispatchActivityPreStarted(this);
1163 Object[] callbacks = collectActivityLifecycleCallbacks();
1164 if (callbacks != null) {
1165 for (int i = 0; i < callbacks.length; i++) {
1166 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(this);
1167 }
1168 }
1169 }
1170
1171 private void dispatchActivityStarted() {
1172 getApplication().dispatchActivityStarted(this);
1173 Object[] callbacks = collectActivityLifecycleCallbacks();
1174 if (callbacks != null) {
1175 for (int i = 0; i < callbacks.length; i++) {
1176 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
1177 }
1178 }
1179 }
1180
1181 private void dispatchActivityPostStarted() {
1182 Object[] callbacks = collectActivityLifecycleCallbacks();
1183 if (callbacks != null) {
1184 for (int i = 0; i < callbacks.length; i++) {
1185 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1186 .onActivityPostStarted(this);
1187 }
1188 }
1189 getApplication().dispatchActivityPostStarted(this);
1190 }
1191
1192 private void dispatchActivityPreResumed() {
1193 getApplication().dispatchActivityPreResumed(this);
1194 Object[] callbacks = collectActivityLifecycleCallbacks();
1195 if (callbacks != null) {
1196 for (int i = 0; i < callbacks.length; i++) {
1197 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(this);
1198 }
1199 }
1200 }
1201
1202 private void dispatchActivityResumed() {
1203 getApplication().dispatchActivityResumed(this);
1204 Object[] callbacks = collectActivityLifecycleCallbacks();
1205 if (callbacks != null) {
1206 for (int i = 0; i < callbacks.length; i++) {
1207 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
1208 }
1209 }
1210 }
1211
1212 private void dispatchActivityPostResumed() {
1213 Object[] callbacks = collectActivityLifecycleCallbacks();
1214 if (callbacks != null) {
1215 for (int i = 0; i < callbacks.length; i++) {
1216 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(this);
1217 }
1218 }
1219 getApplication().dispatchActivityPostResumed(this);
1220 }
1221
1222 private void dispatchActivityPrePaused() {
1223 getApplication().dispatchActivityPrePaused(this);
1224 Object[] callbacks = collectActivityLifecycleCallbacks();
1225 if (callbacks != null) {
1226 for (int i = callbacks.length - 1; i >= 0; i--) {
1227 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(this);
1228 }
1229 }
1230 }
1231
1232 private void dispatchActivityPaused() {
1233 Object[] callbacks = collectActivityLifecycleCallbacks();
1234 if (callbacks != null) {
1235 for (int i = callbacks.length - 1; i >= 0; i--) {
1236 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(this);
1237 }
1238 }
1239 getApplication().dispatchActivityPaused(this);
1240 }
1241
1242 private void dispatchActivityPostPaused() {
1243 Object[] callbacks = collectActivityLifecycleCallbacks();
1244 if (callbacks != null) {
1245 for (int i = callbacks.length - 1; i >= 0; i--) {
1246 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(this);
1247 }
1248 }
1249 getApplication().dispatchActivityPostPaused(this);
1250 }
1251
1252 private void dispatchActivityPreStopped() {
1253 getApplication().dispatchActivityPreStopped(this);
1254 Object[] callbacks = collectActivityLifecycleCallbacks();
1255 if (callbacks != null) {
1256 for (int i = callbacks.length - 1; i >= 0; i--) {
1257 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(this);
1258 }
1259 }
1260 }
1261
1262 private void dispatchActivityStopped() {
1263 Object[] callbacks = collectActivityLifecycleCallbacks();
1264 if (callbacks != null) {
1265 for (int i = callbacks.length - 1; i >= 0; i--) {
1266 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(this);
1267 }
1268 }
1269 getApplication().dispatchActivityStopped(this);
1270 }
1271
1272 private void dispatchActivityPostStopped() {
1273 Object[] callbacks = collectActivityLifecycleCallbacks();
1274 if (callbacks != null) {
1275 for (int i = callbacks.length - 1; i >= 0; i--) {
1276 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1277 .onActivityPostStopped(this);
1278 }
1279 }
1280 getApplication().dispatchActivityPostStopped(this);
1281 }
1282
1283 private void dispatchActivityPreSaveInstanceState(@NonNull Bundle outState) {
1284 getApplication().dispatchActivityPreSaveInstanceState(this, outState);
1285 Object[] callbacks = collectActivityLifecycleCallbacks();
1286 if (callbacks != null) {
1287 for (int i = callbacks.length - 1; i >= 0; i--) {
1288 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1289 .onActivityPreSaveInstanceState(this, outState);
1290 }
1291 }
1292 }
1293
1294 private void dispatchActivitySaveInstanceState(@NonNull Bundle outState) {
1295 Object[] callbacks = collectActivityLifecycleCallbacks();
1296 if (callbacks != null) {
1297 for (int i = callbacks.length - 1; i >= 0; i--) {
1298 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1299 .onActivitySaveInstanceState(this, outState);
1300 }
1301 }
1302 getApplication().dispatchActivitySaveInstanceState(this, outState);
1303 }
1304
1305 private void dispatchActivityPostSaveInstanceState(@NonNull Bundle outState) {
1306 Object[] callbacks = collectActivityLifecycleCallbacks();
1307 if (callbacks != null) {
1308 for (int i = callbacks.length - 1; i >= 0; i--) {
1309 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1310 .onActivityPostSaveInstanceState(this, outState);
1311 }
1312 }
1313 getApplication().dispatchActivityPostSaveInstanceState(this, outState);
1314 }
1315
1316 private void dispatchActivityPreDestroyed() {
1317 getApplication().dispatchActivityPreDestroyed(this);
1318 Object[] callbacks = collectActivityLifecycleCallbacks();
1319 if (callbacks != null) {
1320 for (int i = callbacks.length - 1; i >= 0; i--) {
1321 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1322 .onActivityPreDestroyed(this);
1323 }
1324 }
1325 }
1326
1327 private void dispatchActivityDestroyed() {
1328 Object[] callbacks = collectActivityLifecycleCallbacks();
1329 if (callbacks != null) {
1330 for (int i = callbacks.length - 1; i >= 0; i--) {
1331 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
1332 }
1333 }
1334 getApplication().dispatchActivityDestroyed(this);
1335 }
1336
1337 private void dispatchActivityPostDestroyed() {
1338 Object[] callbacks = collectActivityLifecycleCallbacks();
1339 if (callbacks != null) {
1340 for (int i = callbacks.length - 1; i >= 0; i--) {
1341 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1342 .onActivityPostDestroyed(this);
1343 }
1344 }
1345 getApplication().dispatchActivityPostDestroyed(this);
1346 }
1347
1348 private Object[] collectActivityLifecycleCallbacks() {
1349 Object[] callbacks = null;
1350 synchronized (mActivityLifecycleCallbacks) {
1351 if (mActivityLifecycleCallbacks.size() > 0) {
1352 callbacks = mActivityLifecycleCallbacks.toArray();
1353 }
1354 }
1355 return callbacks;
1356 }
1357
1358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 * Called when the activity is starting. This is where most initialization
1360 * should go: calling {@link #setContentView(int)} to inflate the
1361 * activity's UI, using {@link #findViewById} to programmatically interact
1362 * with widgets in the UI, calling
1363 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1364 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001367 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1368 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1369 * executing.
RoboErik55011652014-07-09 15:05:53 -07001370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 * <p><em>Derived classes must call through to the super class's
1372 * implementation of this method. If they do not, an exception will be
1373 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001374 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 * @param savedInstanceState If the activity is being re-initialized after
1376 * previously being shut down then this Bundle contains the data it most
1377 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001378 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 * @see #onStart
1380 * @see #onSaveInstanceState
1381 * @see #onRestoreInstanceState
1382 * @see #onPostCreate
1383 */
Tor Norbye83c68962015-03-10 20:55:31 -07001384 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001385 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001386 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001387 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001388
Dianne Hackborn2707d602010-07-09 18:01:20 -07001389 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001390 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001391 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001392 if (mActivityInfo.parentActivityName != null) {
1393 if (mActionBar == null) {
1394 mEnableDefaultActionBarUp = true;
1395 } else {
1396 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1397 }
1398 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001399 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001400 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001401 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1402 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001403
1404 if (mAutoFillResetNeeded) {
1405 getAutofillManager().onCreate(savedInstanceState);
1406 }
1407
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001408 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1409 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1410 ? mLastNonConfigurationInstances.fragments : null);
1411 }
1412 mFragments.dispatchCreate();
Ian Lake8a88cd62018-09-28 13:25:20 -07001413 dispatchActivityCreated(savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001414 if (mVoiceInteractor != null) {
1415 mVoiceInteractor.attachActivity(this);
1416 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001417 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 mCalled = true;
Felipe Lemee348dc32018-11-05 12:35:29 -08001419
Felipe Lemeecb08be2018-11-27 15:48:47 -08001420 notifyContentCaptureManagerIfNeeded(ContentCaptureEvent.TYPE_ACTIVITY_CREATED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422
1423 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001424 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001425 * the attribute {@link android.R.attr#persistableMode} set to
1426 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001427 *
1428 * @param savedInstanceState if the activity is being re-initialized after
1429 * previously being shut down then this Bundle contains the data it most
1430 * recently supplied in {@link #onSaveInstanceState}.
1431 * <b><i>Note: Otherwise it is null.</i></b>
1432 * @param persistentState if the activity is being re-initialized after
1433 * previously being shut down or powered off then this Bundle contains the data it most
1434 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1435 * <b><i>Note: Otherwise it is null.</i></b>
1436 *
1437 * @see #onCreate(android.os.Bundle)
1438 * @see #onStart
1439 * @see #onSaveInstanceState
1440 * @see #onRestoreInstanceState
1441 * @see #onPostCreate
1442 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001443 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001444 @Nullable PersistableBundle persistentState) {
1445 onCreate(savedInstanceState);
1446 }
1447
1448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 * The hook for {@link ActivityThread} to restore the state of this activity.
1450 *
1451 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1452 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1453 *
1454 * @param savedInstanceState contains the saved state
1455 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001456 final void performRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 onRestoreInstanceState(savedInstanceState);
1458 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
1460
1461 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001462 * The hook for {@link ActivityThread} to restore the state of this activity.
1463 *
1464 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1465 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1466 *
1467 * @param savedInstanceState contains the saved state
1468 * @param persistentState contains the persistable saved state
1469 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001470 final void performRestoreInstanceState(@Nullable Bundle savedInstanceState,
1471 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001472 onRestoreInstanceState(savedInstanceState, persistentState);
1473 if (savedInstanceState != null) {
1474 restoreManagedDialogs(savedInstanceState);
1475 }
1476 }
1477
1478 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 * This method is called after {@link #onStart} when the activity is
1480 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001481 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 * to restore their state, but it is sometimes convenient to do it here
1483 * after all of the initialization has been done or to allow subclasses to
1484 * decide whether to use your default implementation. The default
1485 * implementation of this method performs a restore of any view state that
1486 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 * <p>This method is called between {@link #onStart} and
1489 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001490 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001492 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * @see #onCreate
1494 * @see #onPostCreate
1495 * @see #onResume
1496 * @see #onSaveInstanceState
1497 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001498 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 if (mWindow != null) {
1500 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1501 if (windowState != null) {
1502 mWindow.restoreHierarchyState(windowState);
1503 }
1504 }
1505 }
Craig Mautnera0026042014-04-23 11:45:37 -07001506
1507 /**
1508 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001509 * created with the attribute {@link android.R.attr#persistableMode} set to
1510 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1511 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001512 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1513 *
1514 * <p>This method is called between {@link #onStart} and
1515 * {@link #onPostCreate}.
1516 *
1517 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1518 *
Jake Wharton63f4d892018-07-10 12:27:59 -04001519 * <p>At least one of {@code savedInstanceState} or {@code persistentState} will not be null.
1520 *
1521 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}
1522 * or null.
1523 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}
1524 * or null.
Craig Mautnera0026042014-04-23 11:45:37 -07001525 *
1526 * @see #onRestoreInstanceState(Bundle)
1527 * @see #onCreate
1528 * @see #onPostCreate
1529 * @see #onResume
1530 * @see #onSaveInstanceState
1531 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001532 public void onRestoreInstanceState(@Nullable Bundle savedInstanceState,
1533 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001534 if (savedInstanceState != null) {
1535 onRestoreInstanceState(savedInstanceState);
1536 }
1537 }
1538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 /**
1540 * Restore the state of any saved managed dialogs.
1541 *
1542 * @param savedInstanceState The bundle to restore from.
1543 */
1544 private void restoreManagedDialogs(Bundle savedInstanceState) {
1545 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1546 if (b == null) {
1547 return;
1548 }
1549
1550 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1551 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001552 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 for (int i = 0; i < numDialogs; i++) {
1554 final Integer dialogId = ids[i];
1555 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1556 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001557 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1558 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001559 final ManagedDialog md = new ManagedDialog();
1560 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1561 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1562 if (md.mDialog != null) {
1563 mManagedDialogs.put(dialogId, md);
1564 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1565 md.mDialog.onRestoreInstanceState(dialogState);
1566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568 }
1569 }
1570
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001571 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1572 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001573 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001574 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001575 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001576 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001577 return dialog;
1578 }
1579
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001580 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 return SAVED_DIALOG_KEY_PREFIX + key;
1582 }
1583
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001584 private static String savedDialogArgsKeyFor(int key) {
1585 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587
1588 /**
1589 * Called when activity start-up is complete (after {@link #onStart}
1590 * and {@link #onRestoreInstanceState} have been called). Applications will
1591 * generally not implement this method; it is intended for system
1592 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001593 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 * <p><em>Derived classes must call through to the super class's
1595 * implementation of this method. If they do not, an exception will be
1596 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001597 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 * @param savedInstanceState If the activity is being re-initialized after
1599 * previously being shut down then this Bundle contains the data it most
1600 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1601 * @see #onCreate
1602 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001603 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001604 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 if (!isChild()) {
1606 mTitleReady = true;
1607 onTitleChanged(getTitle(), getTitleColor());
1608 }
Winsonb6403152016-02-23 13:32:09 -08001609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 mCalled = true;
1611 }
1612
1613 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001614 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001615 * created with the attribute {@link android.R.attr#persistableMode} set to
1616 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001617 *
1618 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1619 * @param persistentState The data caming from the PersistableBundle first
1620 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1621 *
1622 * @see #onCreate
1623 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001624 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001625 @Nullable PersistableBundle persistentState) {
1626 onPostCreate(savedInstanceState);
1627 }
1628
1629 /**
RoboErik55011652014-07-09 15:05:53 -07001630 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1631 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001632 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 *
1634 * <p><em>Derived classes must call through to the super class's
1635 * implementation of this method. If they do not, an exception will be
1636 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 * @see #onCreate
1639 * @see #onStop
1640 * @see #onResume
1641 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001642 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001644 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001646
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001647 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001648
Ian Lake8a88cd62018-09-28 13:25:20 -07001649 dispatchActivityStarted();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001650
1651 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001652 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001653 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001654 notifyContentCaptureManagerIfNeeded(ContentCaptureEvent.TYPE_ACTIVITY_STARTED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656
1657 /**
1658 * Called after {@link #onStop} when the current activity is being
1659 * re-displayed to the user (the user has navigated back to it). It will
1660 * be followed by {@link #onStart} and then {@link #onResume}.
1661 *
1662 * <p>For activities that are using raw {@link Cursor} objects (instead of
1663 * creating them through
1664 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1665 * this is usually the place
1666 * where the cursor should be requeried (because you had deactivated it in
1667 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001668 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 * <p><em>Derived classes must call through to the super class's
1670 * implementation of this method. If they do not, an exception will be
1671 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001672 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 * @see #onStop
1674 * @see #onStart
1675 * @see #onResume
1676 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001677 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 protected void onRestart() {
1679 mCalled = true;
1680 }
1681
1682 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001683 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1684 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1685 * to give the activity a hint that its state is no longer saved -- it will generally
1686 * be called after {@link #onSaveInstanceState} and prior to the activity being
1687 * resumed/started again.
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001688 *
1689 * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
1690 * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
1691 * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001692 */
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001693 @Deprecated
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001694 public void onStateNotSaved() {
1695 }
1696
1697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1699 * {@link #onPause}, for your activity to start interacting with the user.
1700 * This is a good place to begin animations, open exclusive-access devices
1701 * (such as the camera), etc.
1702 *
1703 * <p>Keep in mind that onResume is not the best indicator that your activity
1704 * is visible to the user; a system window such as the keyguard may be in
1705 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1706 * activity is visible to the user (for example, to resume a game).
1707 *
1708 * <p><em>Derived classes must call through to the super class's
1709 * implementation of this method. If they do not, an exception will be
1710 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 * @see #onRestoreInstanceState
1713 * @see #onRestart
1714 * @see #onPostResume
1715 * @see #onPause
1716 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001717 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001719 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07001720 dispatchActivityResumed();
George Mountbdc4d8d2016-03-23 13:10:13 -07001721 mActivityTransitionState.onResume(this, isTopOfTask());
Felipe Lemece404982018-09-17 09:46:13 -07001722 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001723 if (mAutoFillResetNeeded) {
1724 if (!mAutoFillIgnoreFirstResumePause) {
1725 View focus = getCurrentFocus();
Ben Line5790332018-10-01 16:50:10 -07001726 // On Activity rotation situation (mRestoredFromBundle is true),
1727 // we should not call on AutofillManager in onResume()
1728 // since the next Layout pass will do that.
1729 // However, there are both cases where Activity#getCurrentFocus()
1730 // will return null (window not preserved) and not null (window IS
1731 // preserved), so we need to explicitly check for mRestoredFromBundle
1732 // here.
1733 if (!mRestoredFromBundle && focus != null
1734 && focus.canNotifyAutofillEnterExitEvent()) {
Dake Gu67decfa2017-12-27 11:48:08 -08001735 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1736 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1737 // window visibility after recreation is INVISIBLE in onResume() and next frame
1738 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1739 // So we cannot call View.notifyEnterOrExited() which will do nothing
1740 // when View.isVisibleToUser() is false.
1741 getAutofillManager().notifyViewEntered(focus);
1742 }
1743 }
1744 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001745 notifyContentCaptureManagerIfNeeded(ContentCaptureEvent.TYPE_ACTIVITY_RESUMED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 mCalled = true;
1747 }
1748
1749 /**
1750 * Called when activity resume is complete (after {@link #onResume} has
1751 * been called). Applications will generally not implement this method;
1752 * it is intended for system classes to do final setup after application
1753 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 * <p><em>Derived classes must call through to the super class's
1756 * implementation of this method. If they do not, an exception will be
1757 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001758 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 * @see #onResume
1760 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001761 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 protected void onPostResume() {
1763 final Window win = getWindow();
1764 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001765 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 mCalled = true;
1767 }
1768
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001769 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001770 if (mVoiceInteractor != null) {
1771 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1772 activeRequest.cancel();
1773 activeRequest.clear();
1774 }
1775 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001776 if (voiceInteractor == null) {
1777 mVoiceInteractor = null;
1778 } else {
1779 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1780 Looper.myLooper());
1781 }
1782 }
1783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001785 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001786 *
Phil Weaver846cda932017-06-15 10:10:06 -07001787 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001788 * will be unique.
1789 *
1790 * @return A ID that is unique in the activity
1791 *
1792 * {@hide}
1793 */
Felipe Leme42b97932018-02-20 13:04:31 -08001794 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001795 public int getNextAutofillId() {
1796 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1797 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001798 }
1799
Phil Weaver846cda932017-06-15 10:10:06 -07001800 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001801
Phil Weaver846cda932017-06-15 10:10:06 -07001802 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001803 }
1804
1805 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001806 * @hide
1807 */
1808 @Override
1809 public AutofillId autofillClientGetNextAutofillId() {
Felipe Leme305fd402018-09-11 18:20:42 +00001810 return new AutofillId(getNextAutofillId());
Felipe Leme42b97932018-02-20 13:04:31 -08001811 }
1812
1813 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001814 * Check whether this activity is running as part of a voice interaction with the user.
1815 * If true, it should perform its interaction with the user through the
1816 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1817 */
1818 public boolean isVoiceInteraction() {
1819 return mVoiceInteractor != null;
1820 }
1821
1822 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001823 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1824 * of a voice interaction. That is, returns true if this activity was directly
1825 * started by the voice interaction service as the initiation of a voice interaction.
1826 * Otherwise, for example if it was started by another activity while under voice
1827 * interaction, returns false.
1828 */
1829 public boolean isVoiceInteractionRoot() {
1830 try {
1831 return mVoiceInteractor != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001832 && ActivityTaskManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001833 } catch (RemoteException e) {
1834 }
1835 return false;
1836 }
1837
1838 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001839 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1840 * interact with this activity.
1841 */
1842 public VoiceInteractor getVoiceInteractor() {
1843 return mVoiceInteractor;
1844 }
1845
1846 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001847 * Queries whether the currently enabled voice interaction service supports returning
1848 * a voice interactor for use by the activity. This is valid only for the duration of the
1849 * activity.
1850 *
1851 * @return whether the current voice interaction service supports local voice interaction
1852 */
1853 public boolean isLocalVoiceInteractionSupported() {
1854 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001855 return ActivityTaskManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001856 } catch (RemoteException re) {
1857 }
1858 return false;
1859 }
1860
1861 /**
1862 * Starts a local voice interaction session. When ready,
1863 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1864 * to the registered voice interaction service.
1865 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1866 */
1867 public void startLocalVoiceInteraction(Bundle privateOptions) {
1868 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001869 ActivityTaskManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001870 } catch (RemoteException re) {
1871 }
1872 }
1873
1874 /**
1875 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1876 * voice interaction session being started. You can now retrieve a voice interactor using
1877 * {@link #getVoiceInteractor()}.
1878 */
1879 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001880 }
1881
1882 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001883 * Callback to indicate that the local voice interaction has stopped either
1884 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1885 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1886 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001887 */
1888 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001889 }
1890
1891 /**
1892 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001893 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1894 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001895 */
1896 public void stopLocalVoiceInteraction() {
1897 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001898 ActivityTaskManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001899 } catch (RemoteException re) {
1900 }
1901 }
1902
1903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 * This is called for activities that set launchMode to "singleTop" in
1905 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1906 * flag when calling {@link #startActivity}. In either case, when the
1907 * activity is re-launched while at the top of the activity stack instead
1908 * of a new instance of the activity being started, onNewIntent() will be
1909 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001910 * re-launch it.
1911 *
1912 * <p>An activity will always be paused before receiving a new intent, so
1913 * you can count on {@link #onResume} being called after this method.
1914 *
1915 * <p>Note that {@link #getIntent} still returns the original Intent. You
1916 * can use {@link #setIntent} to update it to this new Intent.
1917 *
1918 * @param intent The new intent that was started for the activity.
1919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001921 * @see #setIntent
1922 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 */
1924 protected void onNewIntent(Intent intent) {
1925 }
1926
1927 /**
1928 * The hook for {@link ActivityThread} to save the state of this activity.
1929 *
1930 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1931 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1932 *
1933 * @param outState The bundle to save the state to.
1934 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001935 final void performSaveInstanceState(@NonNull Bundle outState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07001936 dispatchActivityPreSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 onSaveInstanceState(outState);
1938 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001939 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001940 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001941 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
Ian Lake8a88cd62018-09-28 13:25:20 -07001942 dispatchActivityPostSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944
1945 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001946 * The hook for {@link ActivityThread} to save the state of this activity.
1947 *
1948 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1949 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1950 *
1951 * @param outState The bundle to save the state to.
1952 * @param outPersistentState The bundle to save persistent state to.
1953 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001954 final void performSaveInstanceState(@NonNull Bundle outState,
1955 @NonNull PersistableBundle outPersistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07001956 dispatchActivityPreSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001957 onSaveInstanceState(outState, outPersistentState);
1958 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001959 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001960 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1961 ", " + outPersistentState);
Ian Lake8a88cd62018-09-28 13:25:20 -07001962 dispatchActivityPostSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001963 }
1964
1965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 * Called to retrieve per-instance state from an activity before being killed
1967 * so that the state can be restored in {@link #onCreate} or
1968 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1969 * will be passed to both).
1970 *
1971 * <p>This method is called before an activity may be killed so that when it
1972 * comes back some time in the future it can restore its state. For example,
1973 * if activity B is launched in front of activity A, and at some point activity
1974 * A is killed to reclaim resources, activity A will have a chance to save the
1975 * current state of its user interface via this method so that when the user
1976 * returns to activity A, the state of the user interface can be restored
1977 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1978 *
1979 * <p>Do not confuse this method with activity lifecycle callbacks such as
1980 * {@link #onPause}, which is always called when an activity is being placed
1981 * in the background or on its way to destruction, or {@link #onStop} which
1982 * is called before destruction. One example of when {@link #onPause} and
1983 * {@link #onStop} is called and not this method is when a user navigates back
1984 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1985 * on B because that particular instance will never be restored, so the
1986 * system avoids calling it. An example when {@link #onPause} is called and
1987 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1988 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1989 * killed during the lifetime of B since the state of the user interface of
1990 * A will stay intact.
1991 *
1992 * <p>The default implementation takes care of most of the UI per-instance
1993 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1994 * view in the hierarchy that has an id, and by saving the id of the currently
1995 * focused view (all of which is restored by the default implementation of
1996 * {@link #onRestoreInstanceState}). If you override this method to save additional
1997 * information not captured by each individual view, you will likely want to
1998 * call through to the default implementation, otherwise be prepared to save
1999 * all of the state of each view yourself.
2000 *
Andrii Kulian391161f2018-01-29 10:50:02 -08002001 * <p>If called, this method will occur after {@link #onStop} for applications
2002 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
2003 * For applications targeting earlier platform versions this method will occur
2004 * before {@link #onStop} and there are no guarantees about whether it will
2005 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07002006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07002008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 * @see #onCreate
2010 * @see #onRestoreInstanceState
2011 * @see #onPause
2012 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002013 protected void onSaveInstanceState(@NonNull Bundle outState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002015
Phil Weaver846cda932017-06-15 10:10:06 -07002016 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002017 Parcelable p = mFragments.saveAllState();
2018 if (p != null) {
2019 outState.putParcelable(FRAGMENTS_TAG, p);
2020 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002021 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002022 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
2023 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002024 }
Ian Lake8a88cd62018-09-28 13:25:20 -07002025 dispatchActivitySaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027
2028 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002029 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07002030 * created with the attribute {@link android.R.attr#persistableMode} set to
2031 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
2032 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
2033 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07002034 *
2035 * @param outState Bundle in which to place your saved state.
2036 * @param outPersistentState State which will be saved across reboots.
2037 *
2038 * @see #onSaveInstanceState(Bundle)
2039 * @see #onCreate
2040 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
2041 * @see #onPause
2042 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002043 public void onSaveInstanceState(@NonNull Bundle outState,
2044 @NonNull PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07002045 onSaveInstanceState(outState);
2046 }
2047
2048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 * Save the state of any managed dialogs.
2050 *
2051 * @param outState place to store the saved state.
2052 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002053 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 private void saveManagedDialogs(Bundle outState) {
2055 if (mManagedDialogs == null) {
2056 return;
2057 }
2058
2059 final int numDialogs = mManagedDialogs.size();
2060 if (numDialogs == 0) {
2061 return;
2062 }
2063
2064 Bundle dialogState = new Bundle();
2065
2066 int[] ids = new int[mManagedDialogs.size()];
2067
2068 // save each dialog's bundle, gather the ids
2069 for (int i = 0; i < numDialogs; i++) {
2070 final int key = mManagedDialogs.keyAt(i);
2071 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002072 final ManagedDialog md = mManagedDialogs.valueAt(i);
2073 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
2074 if (md.mArgs != null) {
2075 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
2076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078
2079 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
2080 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
2081 }
2082
2083
2084 /**
2085 * Called as part of the activity lifecycle when an activity is going into
2086 * the background, but has not (yet) been killed. The counterpart to
2087 * {@link #onResume}.
2088 *
2089 * <p>When activity B is launched in front of activity A, this callback will
2090 * be invoked on A. B will not be created until A's {@link #onPause} returns,
2091 * so be sure to not do anything lengthy here.
2092 *
2093 * <p>This callback is mostly used for saving any persistent state the
2094 * activity is editing, to present a "edit in place" model to the user and
2095 * making sure nothing is lost if there are not enough resources to start
2096 * the new activity without first killing this one. This is also a good
2097 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08002098 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 * as fast as possible, or to close resources that are exclusive access
2100 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07002101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 * <p>In situations where the system needs more memory it may kill paused
2103 * processes to reclaim resources. Because of this, you should be sure
2104 * that all of your state is saved by the time you return from
2105 * this function. In general {@link #onSaveInstanceState} is used to save
2106 * per-instance state in the activity and this method is used to store
2107 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07002108 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 * <p>After receiving this call you will usually receive a following call
2110 * to {@link #onStop} (after the next activity has been resumed and
2111 * displayed), however in some cases there will be a direct call back to
2112 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07002113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 * <p><em>Derived classes must call through to the super class's
2115 * implementation of this method. If they do not, an exception will be
2116 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 * @see #onResume
2119 * @see #onSaveInstanceState
2120 * @see #onStop
2121 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002122 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002124 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07002125 dispatchActivityPaused();
Dake Gu67decfa2017-12-27 11:48:08 -08002126 if (mAutoFillResetNeeded) {
2127 if (!mAutoFillIgnoreFirstResumePause) {
2128 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
2129 View focus = getCurrentFocus();
2130 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
2131 getAutofillManager().notifyViewExited(focus);
2132 }
2133 } else {
2134 // reset after first pause()
2135 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
2136 mAutoFillIgnoreFirstResumePause = false;
2137 }
2138 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08002139 notifyContentCaptureManagerIfNeeded(ContentCaptureEvent.TYPE_ACTIVITY_PAUSED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 mCalled = true;
2141 }
2142
2143 /**
2144 * Called as part of the activity lifecycle when an activity is about to go
2145 * into the background as the result of user choice. For example, when the
2146 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
2147 * when an incoming phone call causes the in-call Activity to be automatically
2148 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
2149 * the activity being interrupted. In cases when it is invoked, this method
2150 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07002151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 * <p>This callback and {@link #onUserInteraction} are intended to help
2153 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002154 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07002155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 * @see #onUserInteraction()
2157 */
2158 protected void onUserLeaveHint() {
2159 }
RoboErik55011652014-07-09 15:05:53 -07002160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002162 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002164 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002166 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168
2169 /**
2170 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002171 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07002173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 * <p>The default implementation returns null, which will cause you to
2175 * inherit the description from the previous activity. If all activities
2176 * return null, generally the label of the top activity will be used as the
2177 * description.
RoboErik55011652014-07-09 15:05:53 -07002178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 * @return A description of what the user is doing. It should be short and
2180 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07002181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002183 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002185 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 public CharSequence onCreateDescription() {
2187 return null;
2188 }
2189
2190 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002191 * This is called when the user is requesting an assist, to build a full
2192 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
2193 * application. You can override this method to place into the bundle anything
2194 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002195 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002196 *
2197 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07002198 * been registered with {@link Application#registerOnProvideAssistDataListener
2199 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002200 */
2201 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002202 }
2203
2204 /**
2205 * This is called when the user is requesting an assist, to provide references
2206 * to content related to the current activity. Before being called, the
2207 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
2208 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
2209 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
2210 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
2211 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
2212 *
2213 * <p>Custom implementation may adjust the content intent to better reflect the top-level
2214 * context of the activity, and fill in its ClipData with additional content of
2215 * interest that the user is currently viewing. For example, an image gallery application
2216 * that has launched in to an activity allowing the user to swipe through pictures should
2217 * modify the intent to reference the current image they are looking it; such an
2218 * application when showing a list of pictures should add a ClipData that has
2219 * references to all of the pictures currently visible on screen.</p>
2220 *
2221 * @param outContent The assist content to return.
2222 */
2223 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002224 }
2225
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002226 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01002227 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002228 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
2229 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01002230 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002231 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002232 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
2233 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002234 }
2235
2236 /**
2237 * Dismiss the Keyboard Shortcuts screen.
2238 */
2239 public final void dismissKeyboardShortcutsHelper() {
2240 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002241 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
2242 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002243 }
2244
Clara Bayarri75e09792015-07-29 16:20:40 +01002245 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00002246 public void onProvideKeyboardShortcuts(
2247 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01002248 if (menu == null) {
2249 return;
2250 }
2251 KeyboardShortcutGroup group = null;
2252 int menuSize = menu.size();
2253 for (int i = 0; i < menuSize; ++i) {
2254 final MenuItem item = menu.getItem(i);
2255 final CharSequence title = item.getTitle();
2256 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01002257 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01002258 if (title != null && alphaShortcut != MIN_VALUE) {
2259 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00002260 final int resource = mApplication.getApplicationInfo().labelRes;
2261 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01002262 }
2263 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01002264 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01002265 }
2266 }
2267 if (group != null) {
2268 data.add(group);
2269 }
2270 }
2271
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002272 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07002273 * Ask to have the current assistant shown to the user. This only works if the calling
2274 * activity is the current foreground activity. It is the same as calling
2275 * {@link android.service.voice.VoiceInteractionService#showSession
2276 * VoiceInteractionService.showSession} and requesting all of the possible context.
2277 * The receiver will always see
2278 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
2279 * @return Returns true if the assistant was successfully invoked, else false. For example
2280 * false will be returned if the caller is not the current top activity.
2281 */
2282 public boolean showAssist(Bundle args) {
2283 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002284 return ActivityTaskManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07002285 } catch (RemoteException e) {
2286 }
2287 return false;
2288 }
2289
2290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 * Called when you are no longer visible to the user. You will next
2292 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
2293 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07002294 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 * <p><em>Derived classes must call through to the super class's
2296 * implementation of this method. If they do not, an exception will be
2297 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 * @see #onRestart
2300 * @see #onResume
2301 * @see #onSaveInstanceState
2302 * @see #onDestroy
2303 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002304 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002306 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08002307 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07002308 mActivityTransitionState.onStop();
Ian Lake8a88cd62018-09-28 13:25:20 -07002309 dispatchActivityStopped();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002310 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07002312
Dake Gu67decfa2017-12-27 11:48:08 -08002313 if (mAutoFillResetNeeded) {
2314 getAutofillManager().onInvisibleForAutofill();
2315 }
2316
Felipe Lemec24a56a2017-08-03 14:27:57 -07002317 if (isFinishing()) {
2318 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08002319 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07002320 } else if (mIntent != null
2321 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
2322 // Activity was launched when user tapped a link in the Autofill Save UI - since
2323 // user launched another activity, the Save UI should not be restored when this
2324 // activity is finished.
2325 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
2326 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
2327 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08002328 notifyContentCaptureManagerIfNeeded(ContentCaptureEvent.TYPE_ACTIVITY_STOPPED);
Felipe Lemebab851c2017-02-03 18:45:08 -08002329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 }
2331
2332 /**
2333 * Perform any final cleanup before an activity is destroyed. This can
2334 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07002335 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 * this instance of the activity to save space. You can distinguish
2337 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07002338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 * <p><em>Note: do not count on this method being called as a place for
2340 * saving data! For example, if an activity is editing data in a content
2341 * provider, those edits should be committed in either {@link #onPause} or
2342 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
2343 * free resources like threads that are associated with an activity, so
2344 * that a destroyed activity does not leave such things around while the
2345 * rest of its application is still running. There are situations where
2346 * the system will simply kill the activity's hosting process without
2347 * calling this method (or any others) in it, so it should not be used to
2348 * do things that are intended to remain around after the process goes
2349 * away.
RoboErik55011652014-07-09 15:05:53 -07002350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 * <p><em>Derived classes must call through to the super class's
2352 * implementation of this method. If they do not, an exception will be
2353 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 * @see #onPause
2356 * @see #onStop
2357 * @see #finish
2358 * @see #isFinishing
2359 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002360 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002362 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 mCalled = true;
2364
2365 // dismiss any dialogs we are managing.
2366 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 final int numDialogs = mManagedDialogs.size();
2368 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002369 final ManagedDialog md = mManagedDialogs.valueAt(i);
2370 if (md.mDialog.isShowing()) {
2371 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
2373 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002374 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376
2377 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002378 synchronized (mManagedCursors) {
2379 int numCursors = mManagedCursors.size();
2380 for (int i = 0; i < numCursors; i++) {
2381 ManagedCursor c = mManagedCursors.get(i);
2382 if (c != null) {
2383 c.mCursor.close();
2384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002386 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 }
Amith Yamasani49860442010-03-17 20:54:10 -07002388
2389 // Close any open search dialog
2390 if (mSearchManager != null) {
2391 mSearchManager.stopSearch();
2392 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002393
Chris Banes21b25772016-01-04 20:41:59 +00002394 if (mActionBar != null) {
2395 mActionBar.onDestroy();
2396 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002397
Ian Lake8a88cd62018-09-28 13:25:20 -07002398 dispatchActivityDestroyed();
Felipe Lemee348dc32018-11-05 12:35:29 -08002399
Felipe Lemeecb08be2018-11-27 15:48:47 -08002400 notifyContentCaptureManagerIfNeeded(ContentCaptureEvent.TYPE_ACTIVITY_DESTROYED);
Felipe Leme7a534082018-11-05 15:03:04 -08002401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403
2404 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002405 * Report to the system that your app is now fully drawn, purely for diagnostic
2406 * purposes (calling it does not impact the visible behavior of the activity).
2407 * This is only used to help instrument application launch times, so that the
2408 * app can report when it is fully in a usable state; without this, the only thing
2409 * the system itself can determine is the point at which the activity's window
2410 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002411 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002412 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002413 * entirely drawn your UI and populated with all of the significant data. You
2414 * can safely call this method any time after first launch as well, in which case
2415 * it will simply be ignored.
2416 */
2417 public void reportFullyDrawn() {
2418 if (mDoReportFullyDrawn) {
2419 mDoReportFullyDrawn = false;
2420 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002421 ActivityTaskManager.getService().reportActivityFullyDrawn(
2422 mToken, mRestoredFromBundle);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002423 } catch (RemoteException e) {
2424 }
2425 }
2426 }
2427
2428 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002429 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002430 * visa-versa. This method provides the same configuration that will be sent in the following
2431 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2432 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002433 * @see android.R.attr#resizeableActivity
2434 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002435 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002436 * @param newConfig The new configuration of the activity with the state
2437 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002438 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002439 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2440 // Left deliberately empty. There should be no side effects if a direct
2441 // subclass of Activity does not call super.
2442 onMultiWindowModeChanged(isInMultiWindowMode);
2443 }
2444
2445 /**
2446 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2447 * visa-versa.
2448 *
2449 * @see android.R.attr#resizeableActivity
2450 *
2451 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2452 *
2453 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2454 */
2455 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002456 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002457 // Left deliberately empty. There should be no side effects if a direct
2458 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002459 }
2460
2461 /**
2462 * Returns true if the activity is currently in multi-window mode.
2463 * @see android.R.attr#resizeableActivity
2464 *
2465 * @return True if the activity is in multi-window mode.
2466 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002467 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002468 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002469 return ActivityTaskManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002470 } catch (RemoteException e) {
2471 }
2472 return false;
2473 }
2474
2475 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002476 * Called by the system when the activity changes to and from picture-in-picture mode. This
2477 * method provides the same configuration that will be sent in the following
2478 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2479 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002480 * @see android.R.attr#supportsPictureInPicture
2481 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002482 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002483 * @param newConfig The new configuration of the activity with the state
2484 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002485 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002486 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2487 Configuration newConfig) {
2488 // Left deliberately empty. There should be no side effects if a direct
2489 // subclass of Activity does not call super.
2490 onPictureInPictureModeChanged(isInPictureInPictureMode);
2491 }
2492
2493 /**
2494 * Called by the system when the activity changes to and from picture-in-picture mode.
2495 *
2496 * @see android.R.attr#supportsPictureInPicture
2497 *
2498 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2499 *
2500 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2501 */
2502 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002503 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002504 // Left deliberately empty. There should be no side effects if a direct
2505 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002506 }
2507
2508 /**
2509 * Returns true if the activity is currently in picture-in-picture mode.
2510 * @see android.R.attr#supportsPictureInPicture
2511 *
2512 * @return True if the activity is in picture-in-picture mode.
2513 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002514 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002515 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002516 return ActivityTaskManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002517 } catch (RemoteException e) {
2518 }
2519 return false;
2520 }
2521
2522 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002523 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002524 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2525 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002526 *
Winson Chung709904f2017-04-25 11:00:48 -07002527 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002528 * @see android.R.attr#supportsPictureInPicture
2529 */
Winson Chung709904f2017-04-25 11:00:48 -07002530 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002531 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002532 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002533 }
2534
Jeff Sharkey000ce802017-04-29 13:13:27 -06002535 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002536 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002537 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002538 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002539 }
2540
2541 /**
Winson Chung709904f2017-04-25 11:00:48 -07002542 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2543 * set parameters in {@param params} will be combined with the parameters from prior calls to
2544 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2545 *
2546 * The system may disallow entering picture-in-picture in various cases, including when the
2547 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2548 *
2549 * @see android.R.attr#supportsPictureInPicture
2550 * @see PictureInPictureParams
2551 *
2552 * @param params non-null parameters to be combined with previously set parameters when entering
2553 * picture-in-picture.
2554 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002555 * @return true if the system successfully put this activity into picture-in-picture mode or was
2556 * already in picture-in-picture mode (@see {@link #isInPictureInPictureMode()). If the device
2557 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002558 */
2559 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2560 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002561 if (!deviceSupportsPictureInPictureMode()) {
2562 return false;
2563 }
Winson Chung709904f2017-04-25 11:00:48 -07002564 if (params == null) {
2565 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2566 }
Winson Chung298f95b2017-08-10 15:57:18 -07002567 if (!mCanEnterPictureInPicture) {
2568 throw new IllegalStateException("Activity must be resumed to enter"
2569 + " picture-in-picture");
2570 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002571 return ActivityTaskManager.getService().enterPictureInPictureMode(mToken, params);
Winson Chung709904f2017-04-25 11:00:48 -07002572 } catch (RemoteException e) {
2573 return false;
2574 }
2575 }
2576
Jeff Sharkey000ce802017-04-29 13:13:27 -06002577 /** @removed */
2578 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002579 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002580 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002581 }
2582
2583 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002584 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2585 * {@link #enterPictureInPictureMode()} is called.
2586 *
Winson Chung709904f2017-04-25 11:00:48 -07002587 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002588 */
Winson Chung709904f2017-04-25 11:00:48 -07002589 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002590 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002591 if (!deviceSupportsPictureInPictureMode()) {
2592 return;
2593 }
Winson Chung709904f2017-04-25 11:00:48 -07002594 if (params == null) {
2595 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002596 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002597 ActivityTaskManager.getService().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002598 } catch (RemoteException e) {
2599 }
2600 }
2601
Winson Chung8802eac2017-04-17 14:21:44 -07002602 /**
Winson Chung709904f2017-04-25 11:00:48 -07002603 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2604 * user interacts with the activity currently in picture-in-picture mode. This number may change
2605 * if the global configuration changes (ie. if the device is plugged into an external display),
2606 * but will always be larger than three.
2607 */
2608 public int getMaxNumPictureInPictureActions() {
2609 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002610 return ActivityTaskManager.getService().getMaxNumPictureInPictureActions(mToken);
Winson Chung709904f2017-04-25 11:00:48 -07002611 } catch (RemoteException e) {
2612 return 0;
2613 }
2614 }
2615
Winson Chung80fa2d82018-01-25 13:58:20 -08002616 /**
2617 * @return Whether this device supports picture-in-picture.
2618 */
2619 private boolean deviceSupportsPictureInPictureMode() {
2620 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2621 }
2622
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002623 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002624 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002625 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002626 }
2627
2628 /**
2629 * Called by the system when the activity is moved from one display to another without
2630 * recreation. This means that this activity is declared to handle all changes to configuration
2631 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002632 * again.
2633 *
2634 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2635 * applied configuration actually changed. It is up to app developer to choose whether to handle
2636 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2637 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002638 *
2639 * <p>Use this callback to track changes to the displays if some activity functionality relies
2640 * on an association with some display properties.
2641 *
2642 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002643 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002644 *
2645 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002646 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002647 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002648 */
Tiger Huangfc218452018-09-27 00:38:50 +08002649 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002650 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002651 }
2652
Winson Chungb5c41b72016-12-07 15:00:47 -08002653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * Called by the system when the device configuration changes while your
2655 * activity is running. Note that this will <em>only</em> be called if
2656 * you have selected configurations you would like to handle with the
2657 * {@link android.R.attr#configChanges} attribute in your manifest. If
2658 * any configuration change occurs that is not selected to be reported
2659 * by that attribute, then instead of reporting it the system will stop
2660 * and restart the activity (to have it launched with the new
2661 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 * <p>At the time that this function has been called, your Resources
2664 * object will have been updated to return resource values matching the
2665 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 * @param newConfig The new device configuration.
2668 */
Ian Lake666a9652018-09-12 15:13:19 -07002669 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002670 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002672
Dianne Hackborn9d071802010-12-08 14:49:15 -08002673 mFragments.dispatchConfigurationChanged(newConfig);
2674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 if (mWindow != null) {
2676 // Pass the configuration changed event to the window
2677 mWindow.onConfigurationChanged(newConfig);
2678 }
Adam Powell45c0b192011-07-28 15:11:57 -07002679
2680 if (mActionBar != null) {
2681 // Do this last; the action bar will need to access
2682 // view changes from above.
2683 mActionBar.onConfigurationChanged(newConfig);
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
RoboErik55011652014-07-09 15:05:53 -07002686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 /**
2688 * If this activity is being destroyed because it can not handle a
2689 * configuration parameter being changed (and thus its
2690 * {@link #onConfigurationChanged(Configuration)} method is
2691 * <em>not</em> being called), then you can use this method to discover
2692 * the set of changes that have occurred while in the process of being
2693 * destroyed. Note that there is no guarantee that these will be
2694 * accurate (other changes could have happened at any time), so you should
2695 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 * @return Returns a bit field of the configuration parameters that are
2698 * changing, as defined by the {@link android.content.res.Configuration}
2699 * class.
2700 */
2701 public int getChangingConfigurations() {
2702 return mConfigChangeFlags;
2703 }
RoboErik55011652014-07-09 15:05:53 -07002704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 /**
2706 * Retrieve the non-configuration instance data that was previously
2707 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2708 * be available from the initial {@link #onCreate} and
2709 * {@link #onStart} calls to the new instance, allowing you to extract
2710 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 * <p>Note that the data you retrieve here should <em>only</em> be used
2713 * as an optimization for handling configuration changes. You should always
2714 * be able to handle getting a null pointer back, and an activity must
2715 * still be able to restore itself to its previous state (through the
2716 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2717 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002718 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002719 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002720 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002721 * available on older platforms through the Android support libraries.
2722 *
2723 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002725 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002727 return mLastNonConfigurationInstances != null
2728 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
RoboErik55011652014-07-09 15:05:53 -07002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 /**
2732 * Called by the system, as part of destroying an
2733 * activity due to a configuration change, when it is known that a new
2734 * instance will immediately be created for the new configuration. You
2735 * can return any object you like here, including the activity instance
2736 * itself, which can later be retrieved by calling
2737 * {@link #getLastNonConfigurationInstance()} in the new activity
2738 * instance.
RoboErik55011652014-07-09 15:05:53 -07002739 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002740 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2741 * or later, consider instead using a {@link Fragment} with
2742 * {@link Fragment#setRetainInstance(boolean)
2743 * Fragment.setRetainInstance(boolean}.</em>
2744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 * <p>This function is called purely as an optimization, and you must
2746 * not rely on it being called. When it is called, a number of guarantees
2747 * will be made to help optimize configuration switching:
2748 * <ul>
2749 * <li> The function will be called between {@link #onStop} and
2750 * {@link #onDestroy}.
2751 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002752 * created after this one's {@link #onDestroy()} is called. In particular,
2753 * <em>no</em> messages will be dispatched during this time (when the returned
2754 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 * <li> The object you return here will <em>always</em> be available from
2756 * the {@link #getLastNonConfigurationInstance()} method of the following
2757 * activity instance as described there.
2758 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002759 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 * <p>These guarantees are designed so that an activity can use this API
2761 * to propagate extensive state from the old to new activity instance, from
2762 * loaded bitmaps, to network connections, to evenly actively running
2763 * threads. Note that you should <em>not</em> propagate any data that
2764 * may change based on the configuration, including any data loaded from
2765 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002766 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002767 * <p>The guarantee of no message handling during the switch to the next
2768 * activity simplifies use with active objects. For example if your retained
2769 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2770 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2771 * not be called from the call here until you execute the next instance's
2772 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2773 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2774 * running in a separate thread.)
2775 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002776 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002777 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002778 * available on older platforms through the Android support libraries.
2779 *
2780 * @return any Object holding the desired state to propagate to the
2781 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 */
2783 public Object onRetainNonConfigurationInstance() {
2784 return null;
2785 }
RoboErik55011652014-07-09 15:05:53 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 /**
2788 * Retrieve the non-configuration instance data that was previously
2789 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2790 * be available from the initial {@link #onCreate} and
2791 * {@link #onStart} calls to the new instance, allowing you to extract
2792 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002793 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 * <p>Note that the data you retrieve here should <em>only</em> be used
2795 * as an optimization for handling configuration changes. You should always
2796 * be able to handle getting a null pointer back, and an activity must
2797 * still be able to restore itself to its previous state (through the
2798 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2799 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 * @return Returns the object previously returned by
2802 * {@link #onRetainNonConfigurationChildInstances()}
2803 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002804 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002805 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2806 return mLastNonConfigurationInstances != null
2807 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 }
RoboErik55011652014-07-09 15:05:53 -07002809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 /**
2811 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2812 * it should return either a mapping from child activity id strings to arbitrary objects,
2813 * or null. This method is intended to be used by Activity framework subclasses that control a
2814 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2815 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2816 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002817 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2819 return null;
2820 }
RoboErik55011652014-07-09 15:05:53 -07002821
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002822 NonConfigurationInstances retainNonConfigurationInstances() {
2823 Object activity = onRetainNonConfigurationInstance();
2824 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002825 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002826
2827 // We're already stopped but we've been asked to retain.
2828 // Our fragments are taken care of but we need to mark the loaders for retention.
2829 // In order to do this correctly we need to restart the loaders first before
2830 // handing them off to the next activity.
2831 mFragments.doLoaderStart();
2832 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002833 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002834
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002835 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002836 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002837 return null;
2838 }
RoboErik55011652014-07-09 15:05:53 -07002839
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002840 NonConfigurationInstances nci = new NonConfigurationInstances();
2841 nci.activity = activity;
2842 nci.children = children;
2843 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002844 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002845 if (mVoiceInteractor != null) {
2846 mVoiceInteractor.retainInstance();
2847 nci.voiceInteractor = mVoiceInteractor;
2848 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002849 return nci;
2850 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002853 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002855 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002857
2858 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002859 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002860 mCalled = true;
2861 mFragments.dispatchTrimMemory(level);
2862 }
2863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002865 * Return the FragmentManager for interacting with fragments associated
2866 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08002867 *
2868 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002869 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002870 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002871 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002872 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002873 }
RoboErik55011652014-07-09 15:05:53 -07002874
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002875 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002876 * Called when a Fragment is being attached to this activity, immediately
2877 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2878 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08002879 *
2880 * @deprecated Use {@link
2881 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07002882 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002883 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07002884 public void onAttachFragment(Fragment fragment) {
2885 }
RoboErik55011652014-07-09 15:05:53 -07002886
Dianne Hackbornc8017682010-07-06 13:34:38 -07002887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 * Wrapper around
2889 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2890 * that gives the resulting {@link Cursor} to call
2891 * {@link #startManagingCursor} so that the activity will manage its
2892 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002893 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002894 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2895 * or later, consider instead using {@link LoaderManager} instead, available
2896 * via {@link #getLoaderManager()}.</em>
2897 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002898 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2899 * this method, because the activity will do that for you at the appropriate time. However, if
2900 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2901 * not</em> automatically close the cursor and, in that case, you must call
2902 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 * @param uri The URI of the content provider to query.
2905 * @param projection List of columns to return.
2906 * @param selection SQL WHERE clause.
2907 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002908 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002910 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2912 * @see #startManagingCursor
2913 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002914 *
2915 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 */
Jason parks6ed50de2010-08-25 10:18:50 -05002917 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002918 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07002919 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2920 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2922 if (c != null) {
2923 startManagingCursor(c);
2924 }
2925 return c;
2926 }
2927
2928 /**
2929 * Wrapper around
2930 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2931 * that gives the resulting {@link Cursor} to call
2932 * {@link #startManagingCursor} so that the activity will manage its
2933 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002934 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002935 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2936 * or later, consider instead using {@link LoaderManager} instead, available
2937 * via {@link #getLoaderManager()}.</em>
2938 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002939 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2940 * this method, because the activity will do that for you at the appropriate time. However, if
2941 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2942 * not</em> automatically close the cursor and, in that case, you must call
2943 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 * @param uri The URI of the content provider to query.
2946 * @param projection List of columns to return.
2947 * @param selection SQL WHERE clause.
2948 * @param selectionArgs The arguments to selection, if any ?s are pesent
2949 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002950 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2954 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002955 *
2956 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 */
Jason parks6ed50de2010-08-25 10:18:50 -05002958 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002959 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2960 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2962 if (c != null) {
2963 startManagingCursor(c);
2964 }
2965 return c;
2966 }
2967
2968 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 * This method allows the activity to take care of managing the given
2970 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2971 * That is, when the activity is stopped it will automatically call
2972 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2973 * it will call {@link Cursor#requery} for you. When the activity is
2974 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002975 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002976 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2977 * or later, consider instead using {@link LoaderManager} instead, available
2978 * via {@link #getLoaderManager()}.</em>
2979 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002980 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2981 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2982 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2983 * <em>will not</em> automatically close the cursor and, in that case, you must call
2984 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002985 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002987 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2989 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002990 *
2991 * @deprecated Use the new {@link android.content.CursorLoader} class with
2992 * {@link LoaderManager} instead; this is also
2993 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 */
Jason parks6ed50de2010-08-25 10:18:50 -05002995 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 public void startManagingCursor(Cursor c) {
2997 synchronized (mManagedCursors) {
2998 mManagedCursors.add(new ManagedCursor(c));
2999 }
3000 }
3001
3002 /**
3003 * Given a Cursor that was previously given to
3004 * {@link #startManagingCursor}, stop the activity's management of that
3005 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003006 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003007 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003008 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003009 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003010 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003012 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003014 *
3015 * @deprecated Use the new {@link android.content.CursorLoader} class with
3016 * {@link LoaderManager} instead; this is also
3017 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 */
Jason parks6ed50de2010-08-25 10:18:50 -05003019 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 public void stopManagingCursor(Cursor c) {
3021 synchronized (mManagedCursors) {
3022 final int N = mManagedCursors.size();
3023 for (int i=0; i<N; i++) {
3024 ManagedCursor mc = mManagedCursors.get(i);
3025 if (mc.mCursor == c) {
3026 mManagedCursors.remove(i);
3027 break;
3028 }
3029 }
3030 }
3031 }
3032
3033 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003034 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3035 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003036 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003038 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003039 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 }
3042
3043 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003044 * Finds a view that was identified by the {@code android:id} XML attribute
3045 * that was processed in {@link #onCreate}.
3046 * <p>
3047 * <strong>Note:</strong> In most cases -- depending on compiler support --
3048 * the resulting view is automatically cast to the target class type. If
3049 * the target class type is unconstrained, an explicit cast may be
3050 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003052 * @param id the ID to search for
3053 * @return a view with given ID if found, or {@code null} otherwise
3054 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003055 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003057 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003058 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 return getWindow().findViewById(id);
3060 }
RoboErik55011652014-07-09 15:05:53 -07003061
Adam Powell33b97432010-04-20 10:01:14 -07003062 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003063 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3064 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3065 * no matching view in the hierarchy.
3066 * <p>
3067 * <strong>Note:</strong> In most cases -- depending on compiler support --
3068 * the resulting view is automatically cast to the target class type. If
3069 * the target class type is unconstrained, an explicit cast may be
3070 * necessary.
3071 *
3072 * @param id the ID to search for
3073 * @return a view with given ID
3074 * @see View#requireViewById(int)
3075 * @see Activity#findViewById(int)
3076 */
3077 @NonNull
3078 public final <T extends View> T requireViewById(@IdRes int id) {
3079 T view = findViewById(id);
3080 if (view == null) {
3081 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3082 }
3083 return view;
3084 }
3085
3086 /**
Adam Powell33b97432010-04-20 10:01:14 -07003087 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003088 *
Adam Powell33b97432010-04-20 10:01:14 -07003089 * @return The Activity's ActionBar, or null if it does not have one.
3090 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003091 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003092 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003093 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003094 return mActionBar;
3095 }
Adam Powelle43340c2014-03-17 19:10:43 -07003096
3097 /**
3098 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3099 * Activity window.
3100 *
3101 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3102 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3103 * a traditional window decor action bar. The toolbar's menu will be populated with the
3104 * Activity's options menu and the navigation button will be wired through the standard
3105 * {@link android.R.id#home home} menu select action.</p>
3106 *
3107 * <p>In order to use a Toolbar within the Activity's window content the application
3108 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3109 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003110 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003111 */
Adam Powell37780142014-06-01 13:31:00 -07003112 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003113 final ActionBar ab = getActionBar();
3114 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003115 throw new IllegalStateException("This Activity already has an action bar supplied " +
3116 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3117 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3118 }
Chris Banes21b25772016-01-04 20:41:59 +00003119
3120 // If we reach here then we're setting a new action bar
3121 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003122 mMenuInflater = null;
3123
Chris Banes21b25772016-01-04 20:41:59 +00003124 // If we have an action bar currently, destroy it
3125 if (ab != null) {
3126 ab.onDestroy();
3127 }
3128
Chris Banesc7d6c322016-01-27 14:09:16 +00003129 if (toolbar != null) {
3130 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3131 mActionBar = tbab;
3132 mWindow.setCallback(tbab.getWrappedWindowCallback());
3133 } else {
3134 mActionBar = null;
3135 // Re-set the original window callback since we may have already set a Toolbar wrapper
3136 mWindow.setCallback(this);
3137 }
3138
3139 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003140 }
RoboErik55011652014-07-09 15:05:53 -07003141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 /**
Adam Powell33b97432010-04-20 10:01:14 -07003143 * Creates a new ActionBar, locates the inflated ActionBarView,
3144 * initializes the ActionBar with the view, and sets mActionBar.
3145 */
Adam Powelle43340c2014-03-17 19:10:43 -07003146 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003147 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003148
3149 // Initializing the window decor can change window feature flags.
3150 // Make sure that we have the correct set before performing the test below.
3151 window.getDecorView();
3152
Adam Powell9b4c8042010-08-10 15:36:44 -07003153 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003154 return;
3155 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003156
Adam Powelle43340c2014-03-17 19:10:43 -07003157 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003158 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003159
3160 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3161 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003162 }
RoboErik55011652014-07-09 15:05:53 -07003163
Adam Powell33b97432010-04-20 10:01:14 -07003164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 * Set the activity content from a layout resource. The resource will be
3166 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003169 *
Romain Guy482b34a62011-01-20 10:59:28 -08003170 * @see #setContentView(android.view.View)
3171 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003173 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003175 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 }
3177
3178 /**
3179 * Set the activity content to an explicit view. This view is placed
3180 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003181 * view hierarchy. When calling this method, the layout parameters of the
3182 * specified view are ignored. Both the width and the height of the view are
3183 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3184 * your own layout parameters, invoke
3185 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3186 * instead.
RoboErik55011652014-07-09 15:05:53 -07003187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003189 *
3190 * @see #setContentView(int)
3191 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 */
3193 public void setContentView(View view) {
3194 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003195 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
3197
3198 /**
3199 * Set the activity content to an explicit view. This view is placed
3200 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003201 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 * @param view The desired content to display.
3204 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003205 *
3206 * @see #setContentView(android.view.View)
3207 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 */
3209 public void setContentView(View view, ViewGroup.LayoutParams params) {
3210 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003211 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 }
3213
3214 /**
3215 * Add an additional content view to the activity. Added after any existing
3216 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003217 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 * @param view The desired content to display.
3219 * @param params Layout parameters for the view.
3220 */
3221 public void addContentView(View view, ViewGroup.LayoutParams params) {
3222 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003223 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225
3226 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003227 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3228 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3229 *
3230 * <p>This method will return non-null after content has been initialized (e.g. by using
3231 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3232 *
3233 * @return This window's content TransitionManager or null if none is set.
3234 */
3235 public TransitionManager getContentTransitionManager() {
3236 return getWindow().getTransitionManager();
3237 }
3238
3239 /**
3240 * Set the {@link TransitionManager} to use for default transitions in this window.
3241 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3242 *
3243 * @param tm The TransitionManager to use for scene changes.
3244 */
3245 public void setContentTransitionManager(TransitionManager tm) {
3246 getWindow().setTransitionManager(tm);
3247 }
3248
3249 /**
3250 * Retrieve the {@link Scene} representing this window's current content.
3251 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3252 *
3253 * <p>This method will return null if the current content is not represented by a Scene.</p>
3254 *
3255 * @return Current Scene being shown or null
3256 */
3257 public Scene getContentScene() {
3258 return getWindow().getContentScene();
3259 }
3260
3261 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003262 * Sets whether this activity is finished when touched outside its window's
3263 * bounds.
3264 */
3265 public void setFinishOnTouchOutside(boolean finish) {
3266 mWindow.setCloseOnTouchOutside(finish);
3267 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003268
3269 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003270 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003271 DEFAULT_KEYS_DISABLE,
3272 DEFAULT_KEYS_DIALER,
3273 DEFAULT_KEYS_SHORTCUT,
3274 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003275 DEFAULT_KEYS_SEARCH_GLOBAL
3276 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003277 @Retention(RetentionPolicy.SOURCE)
3278 @interface DefaultKeyMode {}
3279
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3282 * keys.
RoboErik55011652014-07-09 15:05:53 -07003283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 * @see #setDefaultKeyMode
3285 */
3286 static public final int DEFAULT_KEYS_DISABLE = 0;
3287 /**
3288 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3289 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 * @see #setDefaultKeyMode
3292 */
3293 static public final int DEFAULT_KEYS_DIALER = 1;
3294 /**
3295 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3296 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003297 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 * @see #setDefaultKeyMode
3301 */
3302 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3303 /**
3304 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3305 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003306 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 * @see #setDefaultKeyMode
3311 */
3312 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3313
3314 /**
3315 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3316 * will start a global search (typically web search, but some platforms may define alternate
3317 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003318 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 * @see #setDefaultKeyMode
3322 */
3323 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3324
3325 /**
3326 * Select the default key handling for this activity. This controls what
3327 * will happen to key events that are not otherwise handled. The default
3328 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3329 * floor. Other modes allow you to launch the dialer
3330 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3331 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003332 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003334 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 * <p>Note that the mode selected here does not impact the default
3336 * handling of system keys, such as the "back" and "menu" keys, and your
3337 * activity and its views always get a first chance to receive and handle
3338 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003341 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 * @see #onKeyDown
3343 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003344 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 // Some modes use a SpannableStringBuilder to track & dispatch input events
3348 // This list must remain in sync with the switch in onKeyDown()
3349 switch (mode) {
3350 case DEFAULT_KEYS_DISABLE:
3351 case DEFAULT_KEYS_SHORTCUT:
3352 mDefaultKeySsb = null; // not used in these modes
3353 break;
3354 case DEFAULT_KEYS_DIALER:
3355 case DEFAULT_KEYS_SEARCH_LOCAL:
3356 case DEFAULT_KEYS_SEARCH_GLOBAL:
3357 mDefaultKeySsb = new SpannableStringBuilder();
3358 Selection.setSelection(mDefaultKeySsb,0);
3359 break;
3360 default:
3361 throw new IllegalArgumentException();
3362 }
3363 }
3364
3365 /**
3366 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003367 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 * is inside a TextView will not trigger the event (unless it is a navigation
3369 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 * <p>If the focused view didn't want this event, this method is called.
3372 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003373 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3374 * by calling {@link #onBackPressed()}, though the behavior varies based
3375 * on the application compatibility mode: for
3376 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3377 * it will set up the dispatch to call {@link #onKeyUp} where the action
3378 * will be performed; for earlier applications, it will perform the
3379 * action immediately in on-down, as those versions of the platform
3380 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003381 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003382 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003383 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003386 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 * this event and it should continue to be propagated.
3388 * @see #onKeyUp
3389 * @see android.view.KeyEvent
3390 */
3391 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003392 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003393 if (getApplicationInfo().targetSdkVersion
3394 >= Build.VERSION_CODES.ECLAIR) {
3395 event.startTracking();
3396 } else {
3397 onBackPressed();
3398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 return true;
3400 }
RoboErik55011652014-07-09 15:05:53 -07003401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3403 return false;
3404 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003405 Window w = getWindow();
3406 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3407 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3408 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003409 return true;
3410 }
3411 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003412 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3413 // Don't consume TAB here since it's used for navigation. Arrow keys
3414 // aren't considered "typing keys" so they already won't get consumed.
3415 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 } else {
3417 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3418 boolean clearSpannable = false;
3419 boolean handled;
3420 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3421 clearSpannable = true;
3422 handled = false;
3423 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003424 handled = TextKeyListener.getInstance().onKeyDown(
3425 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 if (handled && mDefaultKeySsb.length() > 0) {
3427 // something useable has been typed - dispatch it now.
3428
3429 final String str = mDefaultKeySsb.toString();
3430 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 switch (mDefaultKeyMode) {
3433 case DEFAULT_KEYS_DIALER:
3434 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3435 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003436 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 break;
3438 case DEFAULT_KEYS_SEARCH_LOCAL:
3439 startSearch(str, false, null, false);
3440 break;
3441 case DEFAULT_KEYS_SEARCH_GLOBAL:
3442 startSearch(str, false, null, true);
3443 break;
3444 }
3445 }
3446 }
3447 if (clearSpannable) {
3448 mDefaultKeySsb.clear();
3449 mDefaultKeySsb.clearSpans();
3450 Selection.setSelection(mDefaultKeySsb,0);
3451 }
3452 return handled;
3453 }
3454 }
3455
3456 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003457 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3458 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3459 * the event).
3460 */
3461 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3462 return false;
3463 }
3464
3465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003467 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 * is inside a TextView will not trigger the event (unless it is a navigation
3469 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003470 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003471 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3472 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003473 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003475 * further, or <code>false</code> to indicate that you have not handled
3476 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 * @see #onKeyDown
3478 * @see KeyEvent
3479 */
3480 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003481 if (getApplicationInfo().targetSdkVersion
3482 >= Build.VERSION_CODES.ECLAIR) {
3483 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3484 && !event.isCanceled()) {
3485 onBackPressed();
3486 return true;
3487 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 return false;
3490 }
3491
3492 /**
3493 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3494 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3495 * the event).
3496 */
3497 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3498 return false;
3499 }
RoboErik55011652014-07-09 15:05:53 -07003500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003502 * Called when the activity has detected the user's press of the back
3503 * key. The default implementation simply finishes the current activity,
3504 * but you can override this to do whatever you want.
3505 */
3506 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003507 if (mActionBar != null && mActionBar.collapseActionView()) {
3508 return;
3509 }
3510
George Mount27b0dc02017-02-21 10:24:09 -08003511 FragmentManager fragmentManager = mFragments.getFragmentManager();
3512
3513 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003514 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003515 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003516 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003517
3518 /**
3519 * Called when a key shortcut event is not handled by any of the views in the Activity.
3520 * Override this method to implement global key shortcuts for the Activity.
3521 * Key shortcuts can also be implemented by setting the
3522 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3523 *
3524 * @param keyCode The value in event.getKeyCode().
3525 * @param event Description of the key event.
3526 * @return True if the key shortcut was handled.
3527 */
3528 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003529 // Let the Action Bar have a chance at handling the shortcut.
3530 ActionBar actionBar = getActionBar();
3531 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003532 }
3533
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 * Called when a touch screen event was not handled by any of the views
3536 * under it. This is most useful to process touch events that happen
3537 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003538 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 * @return Return true if you have consumed the event, false if you haven't.
3542 * The default implementation always returns false.
3543 */
3544 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003545 if (mWindow.shouldCloseOnTouch(this, event)) {
3546 finish();
3547 return true;
3548 }
RoboErik55011652014-07-09 15:05:53 -07003549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 return false;
3551 }
RoboErik55011652014-07-09 15:05:53 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 /**
3554 * Called when the trackball was moved and not handled by any of the
3555 * views inside of the activity. So, for example, if the trackball moves
3556 * while focus is on a button, you will receive a call here because
3557 * buttons do not normally do anything with trackball events. The call
3558 * here happens <em>before</em> trackball movements are converted to
3559 * DPAD key events, which then get sent back to the view hierarchy, and
3560 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 * @return Return true if you have consumed the event, false if you haven't.
3565 * The default implementation always returns false.
3566 */
3567 public boolean onTrackballEvent(MotionEvent event) {
3568 return false;
3569 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003570
3571 /**
3572 * Called when a generic motion event was not handled by any of the
3573 * views inside of the activity.
3574 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003575 * Generic motion events describe joystick movements, mouse hovers, track pad
3576 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003577 * {@link MotionEvent#getSource() source} of the motion event specifies
3578 * the class of input that was received. Implementations of this method
3579 * must examine the bits in the source before processing the event.
3580 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003581 * </p><p>
3582 * Generic motion events with source class
3583 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3584 * are delivered to the view under the pointer. All other generic motion events are
3585 * delivered to the focused view.
3586 * </p><p>
3587 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3588 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003589 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003590 *
3591 * @param event The generic motion event being processed.
3592 *
3593 * @return Return true if you have consumed the event, false if you haven't.
3594 * The default implementation always returns false.
3595 */
3596 public boolean onGenericMotionEvent(MotionEvent event) {
3597 return false;
3598 }
3599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 /**
3601 * Called whenever a key, touch, or trackball event is dispatched to the
3602 * activity. Implement this method if you wish to know that the user has
3603 * interacted with the device in some way while your activity is running.
3604 * This callback and {@link #onUserLeaveHint} are intended to help
3605 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003606 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003607 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3609 * be accompanied by calls to {@link #onUserInteraction}. This
3610 * ensures that your activity will be told of relevant user activity such
3611 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003612 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 * <p>Note that this callback will be invoked for the touch down action
3614 * that begins a touch gesture, but may not be invoked for the touch-moved
3615 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 * @see #onUserLeaveHint()
3618 */
3619 public void onUserInteraction() {
3620 }
RoboErik55011652014-07-09 15:05:53 -07003621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3623 // Update window manager if: we have a view, that view is
3624 // attached to its parent (which will be a RootView), and
3625 // this activity is not embedded.
3626 if (mParent == null) {
3627 View decor = mDecor;
3628 if (decor != null && decor.getParent() != null) {
3629 getWindowManager().updateViewLayout(decor, params);
3630 }
3631 }
3632 }
3633
3634 public void onContentChanged() {
3635 }
3636
3637 /**
3638 * Called when the current {@link Window} of the activity gains or loses
3639 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003640 * to the user. The default implementation clears the key tracking
3641 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003642 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003643 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 * is managed independently of activity lifecycles. As such, while focus
3645 * changes will generally have some relation to lifecycle changes (an
3646 * activity that is stopped will not generally get window focus), you
3647 * should not rely on any particular order between the callbacks here and
3648 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 * <p>As a general rule, however, a resumed activity will have window
3651 * focus... unless it has displayed other dialogs or popups that take
3652 * input focus, in which case the activity itself will not have focus
3653 * when the other windows have it. Likewise, the system may display
3654 * system-level windows (such as the status bar notification panel or
3655 * a system alert) which will temporarily take window input focus without
3656 * pausing the foreground activity.
3657 *
3658 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 * @see #hasWindowFocus()
3661 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003662 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 */
3664 public void onWindowFocusChanged(boolean hasFocus) {
3665 }
RoboErik55011652014-07-09 15:05:53 -07003666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003668 * Called when the main window associated with the activity has been
3669 * attached to the window manager.
3670 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3671 * for more information.
3672 * @see View#onAttachedToWindow
3673 */
3674 public void onAttachedToWindow() {
3675 }
RoboErik55011652014-07-09 15:05:53 -07003676
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003677 /**
3678 * Called when the main window associated with the activity has been
3679 * detached from the window manager.
3680 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3681 * for more information.
3682 * @see View#onDetachedFromWindow
3683 */
3684 public void onDetachedFromWindow() {
3685 }
RoboErik55011652014-07-09 15:05:53 -07003686
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 * Returns true if this activity's <em>main</em> window currently has window focus.
3689 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003690 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003692 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3694 */
3695 public boolean hasWindowFocus() {
3696 Window w = getWindow();
3697 if (w != null) {
3698 View d = w.getDecorView();
3699 if (d != null) {
3700 return d.hasWindowFocus();
3701 }
3702 }
3703 return false;
3704 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003705
3706 /**
3707 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003708 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003709 */
Adam Powell117b6952014-05-05 18:14:56 -07003710 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003711 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003712 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003713 if (suppressWindowTransition) {
3714 overridePendingTransition(0, 0);
3715 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003716 }
RoboErik55011652014-07-09 15:05:53 -07003717
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003718
Skuhnece2faa52015-08-11 10:36:38 -07003719 /**
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003720 * Moves the activity from {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode to
3721 * {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003722 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003723 * @hide
3724 */
3725 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08003726 public void exitFreeformMode() throws RemoteException {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003727 ActivityTaskManager.getService().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003728 }
3729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003731 * Puts the activity in picture-in-picture mode if the activity supports.
3732 * @see android.R.attr#supportsPictureInPicture
3733 * @hide
3734 */
3735 @Override
3736 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003737 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003738 enterPictureInPictureMode();
3739 }
3740 }
3741
3742 /**
RoboErik55011652014-07-09 15:05:53 -07003743 * Called to process key events. You can override this to intercept all
3744 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003746 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003748 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 * @return boolean Return true if this event was consumed.
3750 */
3751 public boolean dispatchKeyEvent(KeyEvent event) {
3752 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003753
3754 // Let action bars open menus in response to the menu key prioritized over
3755 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003756 final int keyCode = event.getKeyCode();
3757 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003758 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3759 return true;
3760 }
3761
Dianne Hackborn8d374262009-09-14 21:21:52 -07003762 Window win = getWindow();
3763 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 return true;
3765 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003766 View decor = mDecor;
3767 if (decor == null) decor = win.getDecorView();
3768 return event.dispatch(this, decor != null
3769 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771
3772 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003773 * Called to process a key shortcut event.
3774 * You can override this to intercept all key shortcut events before they are
3775 * dispatched to the window. Be sure to call this implementation for key shortcut
3776 * events that should be handled normally.
3777 *
3778 * @param event The key shortcut event.
3779 * @return True if this event was consumed.
3780 */
3781 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3782 onUserInteraction();
3783 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3784 return true;
3785 }
3786 return onKeyShortcut(event.getKeyCode(), event);
3787 }
3788
3789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 * Called to process touch screen events. You can override this to
3791 * intercept all touch screen events before they are dispatched to the
3792 * window. Be sure to call this implementation for touch screen events
3793 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003794 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003796 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 * @return boolean Return true if this event was consumed.
3798 */
3799 public boolean dispatchTouchEvent(MotionEvent ev) {
3800 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3801 onUserInteraction();
3802 }
3803 if (getWindow().superDispatchTouchEvent(ev)) {
3804 return true;
3805 }
3806 return onTouchEvent(ev);
3807 }
RoboErik55011652014-07-09 15:05:53 -07003808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 /**
3810 * Called to process trackball events. You can override this to
3811 * intercept all trackball events before they are dispatched to the
3812 * window. Be sure to call this implementation for trackball events
3813 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003814 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 * @return boolean Return true if this event was consumed.
3818 */
3819 public boolean dispatchTrackballEvent(MotionEvent ev) {
3820 onUserInteraction();
3821 if (getWindow().superDispatchTrackballEvent(ev)) {
3822 return true;
3823 }
3824 return onTrackballEvent(ev);
3825 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003826
Jeff Browncb1404e2011-01-15 18:14:15 -08003827 /**
3828 * Called to process generic motion events. You can override this to
3829 * intercept all generic motion events before they are dispatched to the
3830 * window. Be sure to call this implementation for generic motion events
3831 * that should be handled normally.
3832 *
3833 * @param ev The generic motion event.
3834 *
3835 * @return boolean Return true if this event was consumed.
3836 */
3837 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3838 onUserInteraction();
3839 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3840 return true;
3841 }
3842 return onGenericMotionEvent(ev);
3843 }
3844
svetoslavganov75986cf2009-05-14 22:28:01 -07003845 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3846 event.setClassName(getClass().getName());
3847 event.setPackageName(getPackageName());
3848
3849 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003850 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3851 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003852 event.setFullScreen(isFullScreen);
3853
3854 CharSequence title = getTitle();
3855 if (!TextUtils.isEmpty(title)) {
3856 event.getText().add(title);
3857 }
3858
3859 return true;
3860 }
3861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 /**
3863 * Default implementation of
3864 * {@link android.view.Window.Callback#onCreatePanelView}
3865 * for activities. This
3866 * simply returns null so that all panel sub-windows will have the default
3867 * menu behavior.
3868 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003869 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 public View onCreatePanelView(int featureId) {
3871 return null;
3872 }
3873
3874 /**
3875 * Default implementation of
3876 * {@link android.view.Window.Callback#onCreatePanelMenu}
3877 * for activities. This calls through to the new
3878 * {@link #onCreateOptionsMenu} method for the
3879 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3880 * so that subclasses of Activity don't need to deal with feature codes.
3881 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003882 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003884 boolean show = onCreateOptionsMenu(menu);
3885 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3886 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 }
3888 return false;
3889 }
3890
3891 /**
3892 * Default implementation of
3893 * {@link android.view.Window.Callback#onPreparePanel}
3894 * for activities. This
3895 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3896 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3897 * panel, so that subclasses of
3898 * Activity don't need to deal with feature codes.
3899 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003900 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
3901 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003903 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003904 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 }
3906 return true;
3907 }
3908
3909 /**
3910 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003911 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 * @return The default implementation returns true.
3913 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003914 @Override
3915 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003916 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003917 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003918 if (mActionBar != null) {
3919 mActionBar.dispatchMenuVisibilityChanged(true);
3920 } else {
3921 Log.e(TAG, "Tried to open action bar menu with no action bar");
3922 }
Adam Powell8515ee82010-11-30 14:09:55 -08003923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 return true;
3925 }
3926
3927 /**
3928 * Default implementation of
3929 * {@link android.view.Window.Callback#onMenuItemSelected}
3930 * for activities. This calls through to the new
3931 * {@link #onOptionsItemSelected} method for the
3932 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3933 * panel, so that subclasses of
3934 * Activity don't need to deal with feature codes.
3935 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003936 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003937 CharSequence titleCondensed = item.getTitleCondensed();
3938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 switch (featureId) {
3940 case Window.FEATURE_OPTIONS_PANEL:
3941 // Put event logging here so it gets called even if subclass
3942 // doesn't call through to superclass's implmeentation of each
3943 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003944 if(titleCondensed != null) {
3945 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3946 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003947 if (onOptionsItemSelected(item)) {
3948 return true;
3949 }
Adam Powell04d58112012-04-09 10:22:12 -07003950 if (mFragments.dispatchOptionsItemSelected(item)) {
3951 return true;
3952 }
3953 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3954 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3955 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003956 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003957 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003958 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003959 }
Adam Powell04d58112012-04-09 10:22:12 -07003960 }
3961 return false;
RoboErik55011652014-07-09 15:05:53 -07003962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003964 if(titleCondensed != null) {
3965 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3966 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003967 if (onContextItemSelected(item)) {
3968 return true;
3969 }
3970 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 default:
3973 return false;
3974 }
3975 }
RoboErik55011652014-07-09 15:05:53 -07003976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 /**
3978 * Default implementation of
3979 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3980 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3981 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3982 * so that subclasses of Activity don't need to deal with feature codes.
3983 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3984 * {@link #onContextMenuClosed(Menu)} will be called.
3985 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003986 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 switch (featureId) {
3988 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003989 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 onOptionsMenuClosed(menu);
3991 break;
RoboErik55011652014-07-09 15:05:53 -07003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 case Window.FEATURE_CONTEXT_MENU:
3994 onContextMenuClosed(menu);
3995 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003996
3997 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003998 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003999 mActionBar.dispatchMenuVisibilityChanged(false);
4000 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 }
4002 }
4003
4004 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004005 * Declare that the options menu has changed, so should be recreated.
4006 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4007 * time it needs to be displayed.
4008 */
4009 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004010 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4011 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004012 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4013 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004014 }
RoboErik55011652014-07-09 15:05:53 -07004015
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004016 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 * Initialize the contents of the Activity's standard options menu. You
4018 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004019 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 * <p>This is only called once, the first time the options menu is
4021 * displayed. To update the menu every time it is displayed, see
4022 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004023 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004025 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4026 * they will be correctly ordered with application-defined menu items.
4027 * Deriving classes should always call through to the base implementation.
4028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 * <p>You can safely hold on to <var>menu</var> (and any items created
4030 * from it), making modifications to it as desired, until the next
4031 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 * <p>When you add items to the menu, you can implement the Activity's
4034 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004035 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 * @return You must return true for the menu to be displayed;
4039 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004040 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 * @see #onPrepareOptionsMenu
4042 * @see #onOptionsItemSelected
4043 */
4044 public boolean onCreateOptionsMenu(Menu menu) {
4045 if (mParent != null) {
4046 return mParent.onCreateOptionsMenu(menu);
4047 }
4048 return true;
4049 }
4050
4051 /**
4052 * Prepare the Screen's standard options menu to be displayed. This is
4053 * called right before the menu is shown, every time it is shown. You can
4054 * use this method to efficiently enable/disable items or otherwise
4055 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 * <p>The default implementation updates the system menu items based on the
4058 * activity's state. Deriving classes should always call through to the
4059 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004060 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 * @param menu The options menu as last shown or first initialized by
4062 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004063 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 * @return You must return true for the menu to be displayed;
4065 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004066 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 * @see #onCreateOptionsMenu
4068 */
4069 public boolean onPrepareOptionsMenu(Menu menu) {
4070 if (mParent != null) {
4071 return mParent.onPrepareOptionsMenu(menu);
4072 }
4073 return true;
4074 }
4075
4076 /**
4077 * This hook is called whenever an item in your options menu is selected.
4078 * The default implementation simply returns false to have the normal
4079 * processing happen (calling the item's Runnable or sending a message to
4080 * its Handler as appropriate). You can use this method for any items
4081 * for which you would like to do processing without those other
4082 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004085 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004088 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 * @return boolean Return false to allow normal menu processing to
4090 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004091 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 * @see #onCreateOptionsMenu
4093 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004094 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 if (mParent != null) {
4096 return mParent.onOptionsItemSelected(item);
4097 }
4098 return false;
4099 }
4100
4101 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004102 * This method is called whenever the user chooses to navigate Up within your application's
4103 * activity hierarchy from the action bar.
4104 *
4105 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4106 * was specified in the manifest for this activity or an activity-alias to it,
4107 * default Up navigation will be handled automatically. If any activity
4108 * along the parent chain requires extra Intent arguments, the Activity subclass
4109 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4110 * to supply those arguments.</p>
4111 *
Mark Lufa434852016-08-11 17:40:33 -07004112 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004113 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4114 * from the design guide for more information about navigating within your app.</p>
4115 *
4116 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4117 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4118 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4119 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4120 *
4121 * @return true if Up navigation completed successfully and this Activity was finished,
4122 * false otherwise.
4123 */
4124 public boolean onNavigateUp() {
4125 // Automatically handle hierarchical Up navigation if the proper
4126 // metadata is available.
4127 Intent upIntent = getParentActivityIntent();
4128 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004129 if (mActivityInfo.taskAffinity == null) {
4130 // Activities with a null affinity are special; they really shouldn't
4131 // specify a parent activity intent in the first place. Just finish
4132 // the current activity and call it a day.
4133 finish();
4134 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004135 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004136 onCreateNavigateUpTaskStack(b);
4137 onPrepareNavigateUpTaskStack(b);
4138 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004139
4140 // We can't finishAffinity if we have a result.
4141 // Fall back and simply finish the current activity instead.
4142 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4143 // Tell the developer what's going on to avoid hair-pulling.
4144 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4145 finish();
4146 } else {
4147 finishAffinity();
4148 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004149 } else {
4150 navigateUpTo(upIntent);
4151 }
4152 return true;
4153 }
4154 return false;
4155 }
4156
4157 /**
4158 * This is called when a child activity of this one attempts to navigate up.
4159 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4160 *
4161 * @param child The activity making the call.
4162 */
4163 public boolean onNavigateUpFromChild(Activity child) {
4164 return onNavigateUp();
4165 }
4166
4167 /**
4168 * Define the synthetic task stack that will be generated during Up navigation from
4169 * a different task.
4170 *
4171 * <p>The default implementation of this method adds the parent chain of this activity
4172 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4173 * may choose to override this method to construct the desired task stack in a different
4174 * way.</p>
4175 *
Adam Powellf0195952012-05-02 21:38:54 -07004176 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4177 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4178 * returned by {@link #getParentActivityIntent()}.</p>
4179 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004180 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4181 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4182 *
4183 * @param builder An empty TaskStackBuilder - the application should add intents representing
4184 * the desired task stack
4185 */
4186 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4187 builder.addParentStack(this);
4188 }
4189
4190 /**
4191 * Prepare the synthetic task stack that will be generated during Up navigation
4192 * from a different task.
4193 *
4194 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4195 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4196 * If any extra data should be added to these intents before launching the new task,
4197 * the application should override this method and add that data here.</p>
4198 *
4199 * @param builder A TaskStackBuilder that has been populated with Intents by
4200 * onCreateNavigateUpTaskStack.
4201 */
4202 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4203 }
4204
4205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 * This hook is called whenever the options menu is being closed (either by the user canceling
4207 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004208 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 * @param menu The options menu as last shown or first initialized by
4210 * onCreateOptionsMenu().
4211 */
4212 public void onOptionsMenuClosed(Menu menu) {
4213 if (mParent != null) {
4214 mParent.onOptionsMenuClosed(menu);
4215 }
4216 }
RoboErik55011652014-07-09 15:05:53 -07004217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 /**
4219 * Programmatically opens the options menu. If the options menu is already
4220 * open, this method does nothing.
4221 */
4222 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004223 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4224 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004225 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
RoboErik55011652014-07-09 15:05:53 -07004228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 /**
4230 * Progammatically closes the options menu. If the options menu is already
4231 * closed, this method does nothing.
4232 */
4233 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004234 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4235 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004236 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 }
4239
4240 /**
4241 * Called when a context menu for the {@code view} is about to be shown.
4242 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4243 * time the context menu is about to be shown and should be populated for
4244 * the view (or item inside the view for {@link AdapterView} subclasses,
4245 * this can be found in the {@code menuInfo})).
4246 * <p>
4247 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4248 * item has been selected.
4249 * <p>
4250 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 */
4253 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4254 }
4255
4256 /**
4257 * Registers a context menu to be shown for the given view (multiple views
4258 * can show the context menu). This method will set the
4259 * {@link OnCreateContextMenuListener} on the view to this activity, so
4260 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4261 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 * @see #unregisterForContextMenu(View)
4264 * @param view The view that should show a context menu.
4265 */
4266 public void registerForContextMenu(View view) {
4267 view.setOnCreateContextMenuListener(this);
4268 }
RoboErik55011652014-07-09 15:05:53 -07004269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 /**
4271 * Prevents a context menu to be shown for the given view. This method will remove the
4272 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 * @see #registerForContextMenu(View)
4275 * @param view The view that should stop showing a context menu.
4276 */
4277 public void unregisterForContextMenu(View view) {
4278 view.setOnCreateContextMenuListener(null);
4279 }
RoboErik55011652014-07-09 15:05:53 -07004280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 /**
4282 * Programmatically opens the context menu for a particular {@code view}.
4283 * The {@code view} should have been added via
4284 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 * @param view The view to show the context menu for.
4287 */
4288 public void openContextMenu(View view) {
4289 view.showContextMenu();
4290 }
RoboErik55011652014-07-09 15:05:53 -07004291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 /**
4293 * Programmatically closes the most recently opened context menu, if showing.
4294 */
4295 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004296 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4297 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 }
RoboErik55011652014-07-09 15:05:53 -07004300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 /**
4302 * This hook is called whenever an item in a context menu is selected. The
4303 * default implementation simply returns false to have the normal processing
4304 * happen (calling the item's Runnable or sending a message to its Handler
4305 * as appropriate). You can use this method for any items for which you
4306 * would like to do processing without those other facilities.
4307 * <p>
4308 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4309 * View that added this menu item.
4310 * <p>
4311 * Derived classes should call through to the base class for it to perform
4312 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004313 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 * @param item The context menu item that was selected.
4315 * @return boolean Return false to allow normal context menu processing to
4316 * proceed, true to consume it here.
4317 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004318 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 if (mParent != null) {
4320 return mParent.onContextItemSelected(item);
4321 }
4322 return false;
4323 }
4324
4325 /**
4326 * This hook is called whenever the context menu is being closed (either by
4327 * the user canceling the menu with the back/menu button, or when an item is
4328 * selected).
RoboErik55011652014-07-09 15:05:53 -07004329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 * @param menu The context menu that is being closed.
4331 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004332 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 if (mParent != null) {
4334 mParent.onContextMenuClosed(menu);
4335 }
4336 }
4337
4338 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004339 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004341 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 protected Dialog onCreateDialog(int id) {
4343 return null;
4344 }
4345
4346 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004347 * Callback for creating dialogs that are managed (saved and restored) for you
4348 * by the activity. The default implementation calls through to
4349 * {@link #onCreateDialog(int)} for compatibility.
4350 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004351 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4352 * or later, consider instead using a {@link DialogFragment} instead.</em>
4353 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004354 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4355 * this method the first time, and hang onto it thereafter. Any dialog
4356 * that is created by this method will automatically be saved and restored
4357 * for you, including whether it is showing.
4358 *
4359 * <p>If you would like the activity to manage saving and restoring dialogs
4360 * for you, you should override this method and handle any ids that are
4361 * passed to {@link #showDialog}.
4362 *
4363 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4364 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4365 *
4366 * @param id The id of the dialog.
4367 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4368 * @return The dialog. If you return null, the dialog will not be created.
4369 *
4370 * @see #onPrepareDialog(int, Dialog, Bundle)
4371 * @see #showDialog(int, Bundle)
4372 * @see #dismissDialog(int)
4373 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004374 *
4375 * @deprecated Use the new {@link DialogFragment} class with
4376 * {@link FragmentManager} instead; this is also
4377 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004378 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004379 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004380 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004381 protected Dialog onCreateDialog(int id, Bundle args) {
4382 return onCreateDialog(id);
4383 }
4384
4385 /**
4386 * @deprecated Old no-arguments version of
4387 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4388 */
4389 @Deprecated
4390 protected void onPrepareDialog(int id, Dialog dialog) {
4391 dialog.setOwnerActivity(this);
4392 }
4393
4394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004396 * shown. The default implementation calls through to
4397 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004398 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 * <p>
4400 * Override this if you need to update a managed dialog based on the state
4401 * of the application each time it is shown. For example, a time picker
4402 * dialog might want to be updated with the current time. You should call
4403 * through to the superclass's implementation. The default implementation
4404 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 * @param id The id of the managed dialog.
4407 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004408 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4409 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 * @see #showDialog(int)
4411 * @see #dismissDialog(int)
4412 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004413 *
4414 * @deprecated Use the new {@link DialogFragment} class with
4415 * {@link FragmentManager} instead; this is also
4416 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004418 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004419 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4420 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 }
4422
4423 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004424 * Simple version of {@link #showDialog(int, Bundle)} that does not
4425 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4426 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004427 *
4428 * @deprecated Use the new {@link DialogFragment} class with
4429 * {@link FragmentManager} instead; this is also
4430 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004431 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004432 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004433 public final void showDialog(int id) {
4434 showDialog(id, null);
4435 }
4436
4437 /**
4438 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 * will be made with the same id the first time this is called for a given
4440 * id. From thereafter, the dialog will be automatically saved and restored.
4441 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004442 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4443 * or later, consider instead using a {@link DialogFragment} instead.</em>
4444 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004445 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 * be made to provide an opportunity to do any timely preparation.
4447 *
4448 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004449 * @param args Arguments to pass through to the dialog. These will be saved
4450 * and restored for you. Note that if the dialog is already created,
4451 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4452 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004453 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004454 * @return Returns true if the Dialog was created; false is returned if
4455 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004456 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004457 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004458 * @see #onCreateDialog(int, Bundle)
4459 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 * @see #dismissDialog(int)
4461 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004462 *
4463 * @deprecated Use the new {@link DialogFragment} class with
4464 * {@link FragmentManager} instead; this is also
4465 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004467 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004468 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004470 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004472 ManagedDialog md = mManagedDialogs.get(id);
4473 if (md == null) {
4474 md = new ManagedDialog();
4475 md.mDialog = createDialog(id, null, args);
4476 if (md.mDialog == null) {
4477 return false;
4478 }
4479 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 }
RoboErik55011652014-07-09 15:05:53 -07004481
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004482 md.mArgs = args;
4483 onPrepareDialog(id, md.mDialog, args);
4484 md.mDialog.show();
4485 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 }
4487
4488 /**
4489 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4490 *
4491 * @param id The id of the managed dialog.
4492 *
4493 * @throws IllegalArgumentException if the id was not previously shown via
4494 * {@link #showDialog(int)}.
4495 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004496 * @see #onCreateDialog(int, Bundle)
4497 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 * @see #showDialog(int)
4499 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004500 *
4501 * @deprecated Use the new {@link DialogFragment} class with
4502 * {@link FragmentManager} instead; this is also
4503 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004505 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 public final void dismissDialog(int id) {
4507 if (mManagedDialogs == null) {
4508 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 }
RoboErik55011652014-07-09 15:05:53 -07004510
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004511 final ManagedDialog md = mManagedDialogs.get(id);
4512 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 throw missingDialog(id);
4514 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004515 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 }
4517
4518 /**
4519 * Creates an exception to throw if a user passed in a dialog id that is
4520 * unexpected.
4521 */
4522 private IllegalArgumentException missingDialog(int id) {
4523 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4524 + "shown via Activity#showDialog");
4525 }
4526
4527 /**
4528 * Removes any internal references to a dialog managed by this Activity.
4529 * If the dialog is showing, it will dismiss it as part of the clean up.
4530 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004531 * <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 -08004532 * want to avoid the overhead of saving and restoring it in the future.
4533 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004534 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4535 * will not throw an exception if you try to remove an ID that does not
4536 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004537 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 * @param id The id of the managed dialog.
4539 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004540 * @see #onCreateDialog(int, Bundle)
4541 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 * @see #showDialog(int)
4543 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004544 *
4545 * @deprecated Use the new {@link DialogFragment} class with
4546 * {@link FragmentManager} instead; this is also
4547 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004549 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004551 if (mManagedDialogs != null) {
4552 final ManagedDialog md = mManagedDialogs.get(id);
4553 if (md != null) {
4554 md.mDialog.dismiss();
4555 mManagedDialogs.remove(id);
4556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 }
4559
4560 /**
4561 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004562 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004563 * <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 -07004564 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004565 * calling this function is the same as calling
4566 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4567 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004568 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 * <p>You can override this function to force global search, e.g. in response to a dedicated
4570 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004571 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004572 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4573 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4574 * return false and you must supply your own custom implementation if you want to support
4575 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004576 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004577 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004578 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4579 * not respond to search. The default implementation always returns {@code true}, except
4580 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004581 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 * @see android.app.SearchManager
4583 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004584 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4585 mSearchEvent = searchEvent;
4586 boolean result = onSearchRequested();
4587 mSearchEvent = null;
4588 return result;
4589 }
4590
4591 /**
4592 * @see #onSearchRequested(SearchEvent)
4593 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004595 final int uiMode = getResources().getConfiguration().uiMode
4596 & Configuration.UI_MODE_TYPE_MASK;
4597 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4598 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004599 startSearch(null, false, null, false);
4600 return true;
4601 } else {
4602 return false;
4603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 }
RoboErik55011652014-07-09 15:05:53 -07004605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004606 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004607 * During the onSearchRequested() callbacks, this function will return the
4608 * {@link SearchEvent} that triggered the callback, if it exists.
4609 *
4610 * @return SearchEvent The SearchEvent that triggered the {@link
4611 * #onSearchRequested} callback.
4612 */
4613 public final SearchEvent getSearchEvent() {
4614 return mSearchEvent;
4615 }
4616
4617 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004619 *
4620 * <p>It is typically called from onSearchRequested(), either directly from
4621 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004623 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 * is to inject specific data such as context data, it is preferred to <i>override</i>
4625 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004626 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004627 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4628 * not supported.
4629 *
RoboErik55011652014-07-09 15:05:53 -07004630 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004632 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 * any further typing will replace it. This is useful for cases where an entire pre-formed
4634 * query is being inserted. If false, the selection point will be placed at the end of the
4635 * inserted query. This is useful when the inserted query is text that the user entered,
4636 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4637 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004638 * @param appSearchData An application can insert application-specific
4639 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 * searches. This data will be returned with SEARCH intent(s). Null if
4641 * no extra data is required.
4642 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004643 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004644 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 * @see android.app.SearchManager
4648 * @see #onSearchRequested
4649 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004650 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4651 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004652 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004653 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004654 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 }
4656
4657 /**
krosaend2d60142009-08-17 08:56:48 -07004658 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4659 * the search dialog. Made available for testing purposes.
4660 *
4661 * @param query The query to trigger. If empty, the request will be ignored.
4662 * @param appSearchData An application can insert application-specific
4663 * context here, in order to improve quality or specificity of its own
4664 * searches. This data will be returned with SEARCH intent(s). Null if
4665 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004666 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004667 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004668 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004669 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004670 }
4671
4672 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 * Request that key events come to this activity. Use this if your
4674 * activity has no views with focus, but the activity still wants
4675 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 * @see android.view.Window#takeKeyEvents
4678 */
4679 public void takeKeyEvents(boolean get) {
4680 getWindow().takeKeyEvents(get);
4681 }
4682
4683 /**
4684 * Enable extended window features. This is a convenience for calling
4685 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004687 * @param featureId The desired feature as defined in
4688 * {@link android.view.Window}.
4689 * @return Returns true if the requested feature is supported and now
4690 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004691 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 * @see android.view.Window#requestFeature
4693 */
4694 public final boolean requestWindowFeature(int featureId) {
4695 return getWindow().requestFeature(featureId);
4696 }
4697
4698 /**
4699 * Convenience for calling
4700 * {@link android.view.Window#setFeatureDrawableResource}.
4701 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004702 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 getWindow().setFeatureDrawableResource(featureId, resId);
4704 }
4705
4706 /**
4707 * Convenience for calling
4708 * {@link android.view.Window#setFeatureDrawableUri}.
4709 */
4710 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4711 getWindow().setFeatureDrawableUri(featureId, uri);
4712 }
4713
4714 /**
4715 * Convenience for calling
4716 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4717 */
4718 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4719 getWindow().setFeatureDrawable(featureId, drawable);
4720 }
4721
4722 /**
4723 * Convenience for calling
4724 * {@link android.view.Window#setFeatureDrawableAlpha}.
4725 */
4726 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4727 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4728 }
4729
4730 /**
4731 * Convenience for calling
4732 * {@link android.view.Window#getLayoutInflater}.
4733 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004734 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 public LayoutInflater getLayoutInflater() {
4736 return getWindow().getLayoutInflater();
4737 }
4738
4739 /**
4740 * Returns a {@link MenuInflater} with this context.
4741 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004742 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004744 // Make sure that action views can get an appropriate theme.
4745 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004746 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004747 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004748 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004749 } else {
4750 mMenuInflater = new MenuInflater(this);
4751 }
4752 }
4753 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 }
4755
4756 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004757 public void setTheme(int resid) {
4758 super.setTheme(resid);
4759 mWindow.setTheme(resid);
4760 }
4761
4762 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004763 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004764 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 if (mParent == null) {
4766 super.onApplyThemeResource(theme, resid, first);
4767 } else {
4768 try {
4769 theme.setTo(mParent.getTheme());
4770 } catch (Exception e) {
4771 // Empty
4772 }
4773 theme.applyStyle(resid, false);
4774 }
Winson073a5262016-02-29 16:04:40 -08004775
4776 // Get the primary color and update the TaskDescription for this activity
4777 TypedArray a = theme.obtainStyledAttributes(
4778 com.android.internal.R.styleable.ActivityTaskDescription);
4779 if (mTaskDescription.getPrimaryColor() == 0) {
4780 int colorPrimary = a.getColor(
4781 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4782 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4783 mTaskDescription.setPrimaryColor(colorPrimary);
4784 }
4785 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004786
4787 int colorBackground = a.getColor(
4788 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4789 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4790 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004791 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004792
4793 final int statusBarColor = a.getColor(
4794 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4795 if (statusBarColor != 0) {
4796 mTaskDescription.setStatusBarColor(statusBarColor);
4797 }
4798
4799 final int navigationBarColor = a.getColor(
4800 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4801 if (navigationBarColor != 0) {
4802 mTaskDescription.setNavigationBarColor(navigationBarColor);
4803 }
4804
Winson073a5262016-02-29 16:04:40 -08004805 a.recycle();
4806 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004807 }
4808
4809 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004810 * Requests permissions to be granted to this application. These permissions
4811 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01004812 * and they should have protection level {@link
4813 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
4814 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08004815 * <p>
4816 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4817 * are granted at install time if requested in the manifest. Signature permissions
4818 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4819 * install time if requested in the manifest and the signature of your app matches
4820 * the signature of the app declaring the permissions.
4821 * </p>
4822 * <p>
4823 * If your app does not have the requested permissions the user will be presented
4824 * with UI for accepting them. After the user has accepted or rejected the
4825 * requested permissions you will receive a callback on {@link
4826 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4827 * permissions were granted or not.
4828 * </p>
4829 * <p>
4830 * Note that requesting a permission does not guarantee it will be granted and
4831 * your app should be able to run without having this permission.
4832 * </p>
4833 * <p>
4834 * This method may start an activity allowing the user to choose which permissions
4835 * to grant and which to reject. Hence, you should be prepared that your activity
4836 * may be paused and resumed. Further, granting some permissions may require
4837 * a restart of you application. In such a case, the system will recreate the
4838 * activity stack before delivering the result to {@link
4839 * #onRequestPermissionsResult(int, String[], int[])}.
4840 * </p>
4841 * <p>
4842 * When checking whether you have a permission you should use {@link
4843 * #checkSelfPermission(String)}.
4844 * </p>
4845 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004846 * Calling this API for permissions already granted to your app would show UI
4847 * to the user to decide whether the app can still hold these permissions. This
4848 * can be useful if the way your app uses data guarded by the permissions
4849 * changes significantly.
4850 * </p>
4851 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004852 * You cannot request a permission if your activity sets {@link
4853 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4854 * <code>true</code> because in this case the activity would not receive
4855 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4856 * </p>
4857 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01004858 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07004859 * RuntimePermissions</a> sample app demonstrates how to use this method to
4860 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004861 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004862 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004863 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004864 * @param requestCode Application specific request code to match with a result
4865 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004866 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004867 *
Paul Miller77a0e132016-02-11 13:46:49 -08004868 * @throws IllegalArgumentException if requestCode is negative.
4869 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004870 * @see #onRequestPermissionsResult(int, String[], int[])
4871 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004872 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004873 */
4874 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004875 if (requestCode < 0) {
4876 throw new IllegalArgumentException("requestCode should be >= 0");
4877 }
Svetoslavffb32b12015-10-15 16:54:00 -07004878 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00004879 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07004880 // Dispatch the callback with empty arrays which means a cancellation.
4881 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4882 return;
4883 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004884 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004885 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004886 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004887 }
4888
4889 /**
4890 * Callback for the result from requesting permissions. This method
4891 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004892 * <p>
4893 * <strong>Note:</strong> It is possible that the permissions request interaction
4894 * with the user is interrupted. In this case you will receive empty permissions
4895 * and results arrays which should be treated as a cancellation.
4896 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004897 *
4898 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4899 * @param permissions The requested permissions. Never null.
4900 * @param grantResults The grant results for the corresponding permissions
4901 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4902 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4903 *
4904 * @see #requestPermissions(String[], int)
4905 */
4906 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4907 @NonNull int[] grantResults) {
4908 /* callback - no nothing */
4909 }
4910
4911 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004912 * Gets whether you should show UI with rationale for requesting a permission.
4913 * You should do this only if you do not have the permission and the context in
4914 * which the permission is requested does not clearly communicate to the user
4915 * what would be the benefit from granting this permission.
4916 * <p>
4917 * For example, if you write a camera app, requesting the camera permission
4918 * would be expected by the user and no rationale for why it is requested is
4919 * needed. If however, the app needs location for tagging photos then a non-tech
4920 * savvy user may wonder how location is related to taking photos. In this case
4921 * you may choose to show UI with rationale of requesting this permission.
4922 * </p>
4923 *
4924 * @param permission A permission your app wants to request.
4925 * @return Whether you can show permission rationale UI.
4926 *
4927 * @see #checkSelfPermission(String)
4928 * @see #requestPermissions(String[], int)
4929 * @see #onRequestPermissionsResult(int, String[], int[])
4930 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004931 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004932 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4933 }
4934
4935 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004936 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4937 * with no options.
4938 *
4939 * @param intent The intent to start.
4940 * @param requestCode If >= 0, this code will be returned in
4941 * onActivityResult() when the activity exits.
4942 *
4943 * @throws android.content.ActivityNotFoundException
4944 *
George Mount0a778ed2013-12-13 13:35:36 -08004945 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004946 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004947 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004948 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004949 }
4950
4951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 * Launch an activity for which you would like a result when it finished.
4953 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004954 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004955 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 * <p>Note that this method should only be used with Intent protocols
4959 * that are defined to return a result. In other protocols (such as
4960 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4961 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05004962 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
4963 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004964 *
RoboErik55011652014-07-09 15:05:53 -07004965 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004967 * activity, then your window will not be displayed until a result is
4968 * returned back from the started activity. This is to avoid visible
4969 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004970 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 * <p>This method throws {@link android.content.ActivityNotFoundException}
4972 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 * @param intent The intent to start.
4975 * @param requestCode If >= 0, this code will be returned in
4976 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004977 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004978 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004979 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004980 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004982 *
George Mount0a778ed2013-12-13 13:35:36 -08004983 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004985 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4986 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07004988 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 Instrumentation.ActivityResult ar =
4990 mInstrumentation.execStartActivity(
4991 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004992 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 if (ar != null) {
4994 mMainThread.sendActivityResult(
4995 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4996 ar.getResultData());
4997 }
4998 if (requestCode >= 0) {
4999 // If this start is requesting a result, we can avoid making
5000 // the activity visible until the result is received. Setting
5001 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5002 // activity hidden during this time, to avoid flickering.
5003 // This can only be done when a result is requested because
5004 // that guarantees we will get information back when the
5005 // activity is finished, no matter what happens to it.
5006 mStartedActivity = true;
5007 }
Adam Powell14874662013-07-18 19:42:41 -07005008
George Mount41725de2015-04-09 08:23:05 -07005009 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005010 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005012 if (options != null) {
5013 mParent.startActivityFromChild(this, intent, requestCode, options);
5014 } else {
5015 // Note we want to go through this method for compatibility with
5016 // existing applications that may have overridden it.
5017 mParent.startActivityFromChild(this, intent, requestCode);
5018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 }
George Mount41725de2015-04-09 08:23:05 -07005020 }
5021
5022 /**
5023 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5024 *
5025 * @param options The ActivityOptions bundle used to start an Activity.
5026 */
5027 private void cancelInputsAndStartExitTransition(Bundle options) {
5028 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5029 if (decor != null) {
5030 decor.cancelPendingInputEvents();
5031 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005032 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005033 mActivityTransitionState.startExitOutTransition(this, options);
5034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
5036
Chet Haaseb64e777f2016-12-09 07:17:10 -08005037 /**
5038 * Returns whether there are any activity transitions currently running on this
5039 * activity. A return value of {@code true} can mean that either an enter or
5040 * exit transition is running, including whether the background of the activity
5041 * is animating as a part of that transition.
5042 *
5043 * @return true if a transition is currently running on this activity, false otherwise.
5044 */
5045 public boolean isActivityTransitionRunning() {
5046 return mActivityTransitionState.isTransitionRunning();
5047 }
5048
George Mount413739e2016-06-08 07:13:37 -07005049 private Bundle transferSpringboardActivityOptions(Bundle options) {
5050 if (options == null && (mWindow != null && !mWindow.isActive())) {
5051 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005052 if (activityOptions != null &&
5053 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005054 return activityOptions.toBundle();
5055 }
5056 }
5057 return options;
5058 }
5059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005061 * @hide Implement to provide correct calling token.
5062 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005063 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005064 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5065 startActivityForResultAsUser(intent, requestCode, null, user);
5066 }
5067
5068 /**
5069 * @hide Implement to provide correct calling token.
5070 */
5071 public void startActivityForResultAsUser(Intent intent, int requestCode,
5072 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005073 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5074 }
5075
5076 /**
5077 * @hide Implement to provide correct calling token.
5078 */
5079 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5080 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005081 if (mParent != null) {
5082 throw new RuntimeException("Can't be called from a child");
5083 }
George Mount413739e2016-06-08 07:13:37 -07005084 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005085 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005086 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005087 options, user);
5088 if (ar != null) {
5089 mMainThread.sendActivityResult(
5090 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5091 }
5092 if (requestCode >= 0) {
5093 // If this start is requesting a result, we can avoid making
5094 // the activity visible until the result is received. Setting
5095 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5096 // activity hidden during this time, to avoid flickering.
5097 // This can only be done when a result is requested because
5098 // that guarantees we will get information back when the
5099 // activity is finished, no matter what happens to it.
5100 mStartedActivity = true;
5101 }
5102
George Mount41725de2015-04-09 08:23:05 -07005103 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005104 }
5105
5106 /**
5107 * @hide Implement to provide correct calling token.
5108 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005109 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005110 public void startActivityAsUser(Intent intent, UserHandle user) {
5111 startActivityAsUser(intent, null, user);
5112 }
5113
5114 /**
5115 * @hide Implement to provide correct calling token.
5116 */
5117 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5118 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005119 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005120 }
George Mount413739e2016-06-08 07:13:37 -07005121 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005122 Instrumentation.ActivityResult ar =
5123 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005124 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005125 intent, -1, options, user);
5126 if (ar != null) {
5127 mMainThread.sendActivityResult(
5128 mToken, mEmbeddedID, -1, ar.getResultCode(),
5129 ar.getResultData());
5130 }
George Mount41725de2015-04-09 08:23:05 -07005131 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005132 }
5133
5134 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005135 * Start a new activity as if it was started by the activity that started our
5136 * current activity. This is for the resolver and chooser activities, which operate
5137 * as intermediaries that dispatch their intent to the target the user selects -- to
5138 * do this, they must perform all security checks including permission grants as if
5139 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005140 * @param intent The Intent to start.
5141 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005142 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005143 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5144 * caller it is doing the start is, is actually allowed to start the target activity.
5145 * If you set this to true, you must set an explicit component in the Intent and do any
5146 * appropriate security checks yourself.
5147 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005148 * @hide
5149 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005150 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005151 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005152 if (mParent != null) {
5153 throw new RuntimeException("Can't be called from a child");
5154 }
George Mount413739e2016-06-08 07:13:37 -07005155 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005156 Instrumentation.ActivityResult ar =
5157 mInstrumentation.execStartActivityAsCaller(
5158 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005159 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005160 if (ar != null) {
5161 mMainThread.sendActivityResult(
5162 mToken, mEmbeddedID, -1, ar.getResultCode(),
5163 ar.getResultData());
5164 }
George Mount41725de2015-04-09 08:23:05 -07005165 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005166 }
5167
5168 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005169 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5170 * Intent, int, int, int, Bundle)} with no options.
5171 *
5172 * @param intent The IntentSender to launch.
5173 * @param requestCode If >= 0, this code will be returned in
5174 * onActivityResult() when the activity exits.
5175 * @param fillInIntent If non-null, this will be provided as the
5176 * intent parameter to {@link IntentSender#sendIntent}.
5177 * @param flagsMask Intent flags in the original IntentSender that you
5178 * would like to change.
5179 * @param flagsValues Desired values for any bits set in
5180 * <var>flagsMask</var>
5181 * @param extraFlags Always set to 0.
5182 */
5183 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005184 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005185 throws IntentSender.SendIntentException {
5186 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5187 flagsValues, extraFlags, null);
5188 }
5189
5190 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005191 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005192 * to use a IntentSender to describe the activity to be started. If
5193 * the IntentSender is for an activity, that activity will be started
5194 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5195 * here; otherwise, its associated action will be executed (such as
5196 * sending a broadcast) as if you had called
5197 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005198 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005199 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005200 * @param requestCode If >= 0, this code will be returned in
5201 * onActivityResult() when the activity exits.
5202 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005203 * intent parameter to {@link IntentSender#sendIntent}.
5204 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005205 * would like to change.
5206 * @param flagsValues Desired values for any bits set in
5207 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005208 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005209 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005210 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005211 * Context.startActivity(Intent, Bundle)} for more details. If options
5212 * have also been supplied by the IntentSender, options given here will
5213 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005214 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005215 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005216 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005217 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005218 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005219 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5220 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005221 } else if (options != null) {
5222 mParent.startIntentSenderFromChild(this, intent, requestCode,
5223 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005224 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005225 // Note we want to go through this call for compatibility with
5226 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005227 mParent.startIntentSenderFromChild(this, intent, requestCode,
5228 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005229 }
5230 }
5231
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005232 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5233 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005234 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005235 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005236 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005237 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005238 String resolvedType = null;
5239 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005240 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005241 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005242 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5243 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005244 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005245 .startActivityIntentSender(mMainThread.getApplicationThread(),
5246 intent != null ? intent.getTarget() : null,
5247 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005248 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005249 requestCode, flagsMask, flagsValues, options);
5250 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005251 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005252 }
5253 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005254
5255 if (options != null) {
5256 // Only when the options are not null, as the intent can point to something other
5257 // than an Activity.
5258 cancelInputsAndStartExitTransition(options);
5259 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005260 } catch (RemoteException e) {
5261 }
5262 if (requestCode >= 0) {
5263 // If this start is requesting a result, we can avoid making
5264 // the activity visible until the result is received. Setting
5265 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5266 // activity hidden during this time, to avoid flickering.
5267 // This can only be done when a result is requested because
5268 // that guarantees we will get information back when the
5269 // activity is finished, no matter what happens to it.
5270 mStartedActivity = true;
5271 }
5272 }
5273
5274 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005275 * Same as {@link #startActivity(Intent, Bundle)} with no options
5276 * specified.
5277 *
5278 * @param intent The intent to start.
5279 *
5280 * @throws android.content.ActivityNotFoundException
5281 *
Mark Lufa434852016-08-11 17:40:33 -07005282 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005283 * @see #startActivityForResult
5284 */
5285 @Override
5286 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005287 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005288 }
5289
5290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 * Launch a new activity. You will not receive any information about when
5292 * the activity exits. This implementation overrides the base version,
5293 * providing information about
5294 * the activity performing the launch. Because of this additional
5295 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5296 * required; if not specified, the new activity will be added to the
5297 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 * <p>This method throws {@link android.content.ActivityNotFoundException}
5300 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005301 *
5302 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005303 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005304 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005305 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005308 *
Mark Lufa434852016-08-11 17:40:33 -07005309 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005310 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 */
5312 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005313 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005314 if (options != null) {
5315 startActivityForResult(intent, -1, options);
5316 } else {
5317 // Note we want to go through this call for compatibility with
5318 // applications that may have overridden the method.
5319 startActivityForResult(intent, -1);
5320 }
5321 }
5322
5323 /**
5324 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5325 * specified.
5326 *
5327 * @param intents The intents to start.
5328 *
5329 * @throws android.content.ActivityNotFoundException
5330 *
Mark Lufa434852016-08-11 17:40:33 -07005331 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005332 * @see #startActivityForResult
5333 */
5334 @Override
5335 public void startActivities(Intent[] intents) {
5336 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 }
5338
5339 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005340 * Launch a new activity. You will not receive any information about when
5341 * the activity exits. This implementation overrides the base version,
5342 * providing information about
5343 * the activity performing the launch. Because of this additional
5344 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5345 * required; if not specified, the new activity will be added to the
5346 * task of the caller.
5347 *
5348 * <p>This method throws {@link android.content.ActivityNotFoundException}
5349 * if there was no Activity found to run the given Intent.
5350 *
5351 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005352 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005353 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005354 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005355 *
5356 * @throws android.content.ActivityNotFoundException
5357 *
Mark Lufa434852016-08-11 17:40:33 -07005358 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005359 * @see #startActivityForResult
5360 */
5361 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005362 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005363 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005364 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005365 }
5366
5367 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005368 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5369 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005370 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005371 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005372 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005373 * intent parameter to {@link IntentSender#sendIntent}.
5374 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005375 * would like to change.
5376 * @param flagsValues Desired values for any bits set in
5377 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005378 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005379 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005380 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005381 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005382 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005383 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5384 extraFlags, null);
5385 }
5386
5387 /**
5388 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5389 * to start; see
5390 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5391 * for more information.
5392 *
5393 * @param intent The IntentSender to launch.
5394 * @param fillInIntent If non-null, this will be provided as the
5395 * intent parameter to {@link IntentSender#sendIntent}.
5396 * @param flagsMask Intent flags in the original IntentSender that you
5397 * would like to change.
5398 * @param flagsValues Desired values for any bits set in
5399 * <var>flagsMask</var>
5400 * @param extraFlags Always set to 0.
5401 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005402 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005403 * Context.startActivity(Intent, Bundle)} for more details. If options
5404 * have also been supplied by the IntentSender, options given here will
5405 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005406 */
5407 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005408 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005409 Bundle options) throws IntentSender.SendIntentException {
5410 if (options != null) {
5411 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5412 flagsValues, extraFlags, options);
5413 } else {
5414 // Note we want to go through this call for compatibility with
5415 // applications that may have overridden the method.
5416 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5417 flagsValues, extraFlags);
5418 }
5419 }
5420
5421 /**
5422 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5423 * with no options.
5424 *
5425 * @param intent The intent to start.
5426 * @param requestCode If >= 0, this code will be returned in
5427 * onActivityResult() when the activity exits, as described in
5428 * {@link #startActivityForResult}.
5429 *
5430 * @return If a new activity was launched then true is returned; otherwise
5431 * false is returned and you must handle the Intent yourself.
5432 *
5433 * @see #startActivity
5434 * @see #startActivityForResult
5435 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005436 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5437 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005438 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005439 }
5440
5441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 * A special variation to launch an activity only if a new activity
5443 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005444 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005446 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005448 * and the activity
5449 * that handles <var>intent</var> is the same as your currently running
5450 * activity, then a new instance is not needed. In this case, instead of
5451 * the normal behavior of calling {@link #onNewIntent} this function will
5452 * return and you can handle the Intent yourself.
5453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 * <p>This function can only be called from a top-level activity; if it is
5455 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 * @param intent The intent to start.
5458 * @param requestCode If >= 0, this code will be returned in
5459 * onActivityResult() when the activity exits, as described in
5460 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005461 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005462 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005463 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 * @return If a new activity was launched then true is returned; otherwise
5466 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005467 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 * @see #startActivity
5469 * @see #startActivityForResult
5470 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005471 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5472 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005474 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005476 Uri referrer = onProvideReferrer();
5477 if (referrer != null) {
5478 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5479 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005480 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005481 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005482 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005483 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005484 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5485 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5486 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 } catch (RemoteException e) {
5488 // Empty
5489 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 if (requestCode >= 0) {
5494 // If this start is requesting a result, we can avoid making
5495 // the activity visible until the result is received. Setting
5496 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5497 // activity hidden during this time, to avoid flickering.
5498 // This can only be done when a result is requested because
5499 // that guarantees we will get information back when the
5500 // activity is finished, no matter what happens to it.
5501 mStartedActivity = true;
5502 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005503 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 }
5505
5506 throw new UnsupportedOperationException(
5507 "startActivityIfNeeded can only be called from a top-level activity");
5508 }
5509
5510 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005511 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5512 * no options.
5513 *
5514 * @param intent The intent to dispatch to the next activity. For
5515 * correct behavior, this must be the same as the Intent that started
5516 * your own activity; the only changes you can make are to the extras
5517 * inside of it.
5518 *
5519 * @return Returns a boolean indicating whether there was another Activity
5520 * to start: true if there was a next activity to start, false if there
5521 * wasn't. In general, if true is returned you will then want to call
5522 * finish() on yourself.
5523 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005524 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005525 return startNextMatchingActivity(intent, null);
5526 }
5527
5528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 * Special version of starting an activity, for use when you are replacing
5530 * other activity components. You can use this to hand the Intent off
5531 * to the next Activity that can handle it. You typically call this in
5532 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 * @param intent The intent to dispatch to the next activity. For
5535 * correct behavior, this must be the same as the Intent that started
5536 * your own activity; the only changes you can make are to the extras
5537 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005538 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005539 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005540 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 * @return Returns a boolean indicating whether there was another Activity
5543 * to start: true if there was a next activity to start, false if there
5544 * wasn't. In general, if true is returned you will then want to call
5545 * finish() on yourself.
5546 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005547 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5548 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 if (mParent == null) {
5550 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005551 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005552 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005553 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005554 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 } catch (RemoteException e) {
5556 // Empty
5557 }
5558 return false;
5559 }
5560
5561 throw new UnsupportedOperationException(
5562 "startNextMatchingActivity can only be called from a top-level activity");
5563 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005564
5565 /**
5566 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5567 * with no options.
5568 *
5569 * @param child The activity making the call.
5570 * @param intent The intent to start.
5571 * @param requestCode Reply request code. < 0 if reply is not requested.
5572 *
5573 * @throws android.content.ActivityNotFoundException
5574 *
5575 * @see #startActivity
5576 * @see #startActivityForResult
5577 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005578 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005579 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005580 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005581 }
5582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 /**
RoboErik55011652014-07-09 15:05:53 -07005584 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 * <p>This method throws {@link android.content.ActivityNotFoundException}
5588 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 * @param child The activity making the call.
5591 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005592 * @param requestCode Reply request code. < 0 if reply is not requested.
5593 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005594 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005595 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005596 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005598 *
5599 * @see #startActivity
5600 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005602 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005603 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005604 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 Instrumentation.ActivityResult ar =
5606 mInstrumentation.execStartActivity(
5607 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005608 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 if (ar != null) {
5610 mMainThread.sendActivityResult(
5611 mToken, child.mEmbeddedID, requestCode,
5612 ar.getResultCode(), ar.getResultData());
5613 }
George Mount41725de2015-04-09 08:23:05 -07005614 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 }
5616
5617 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005618 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5619 * with no options.
5620 *
5621 * @param fragment The fragment making the call.
5622 * @param intent The intent to start.
5623 * @param requestCode Reply request code. < 0 if reply is not requested.
5624 *
5625 * @throws android.content.ActivityNotFoundException
5626 *
5627 * @see Fragment#startActivity
5628 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005629 *
5630 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5631 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005632 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005633 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005634 public void startActivityFromFragment(@NonNull Fragment fragment,
5635 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005636 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005637 }
5638
5639 /**
RoboErik55011652014-07-09 15:05:53 -07005640 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005641 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5642 * method.
RoboErik55011652014-07-09 15:05:53 -07005643 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005644 * <p>This method throws {@link android.content.ActivityNotFoundException}
5645 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005646 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005647 * @param fragment The fragment making the call.
5648 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005649 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005650 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005651 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005652 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005653 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005654 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005655 *
5656 * @see Fragment#startActivity
5657 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005658 *
5659 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5660 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005661 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005662 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005663 public void startActivityFromFragment(@NonNull Fragment fragment,
5664 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005665 startActivityForResult(fragment.mWho, intent, requestCode, options);
5666 }
5667
5668 /**
5669 * @hide
5670 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005671 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5672 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5673 UserHandle user) {
5674 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5675 }
5676
5677 /**
5678 * @hide
5679 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005680 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005681 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005682 public void startActivityForResult(
5683 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005684 Uri referrer = onProvideReferrer();
5685 if (referrer != null) {
5686 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5687 }
George Mount413739e2016-06-08 07:13:37 -07005688 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005689 Instrumentation.ActivityResult ar =
5690 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005691 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005692 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005693 if (ar != null) {
5694 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005695 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005696 ar.getResultCode(), ar.getResultData());
5697 }
George Mount41725de2015-04-09 08:23:05 -07005698 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005699 }
5700
5701 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005702 * @hide
5703 */
5704 @Override
5705 public boolean canStartActivityForResult() {
5706 return true;
5707 }
5708
5709 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005710 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5711 * int, Intent, int, int, int, Bundle)} with no options.
5712 */
5713 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5714 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5715 int extraFlags)
5716 throws IntentSender.SendIntentException {
5717 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5718 flagsMask, flagsValues, extraFlags, null);
5719 }
5720
5721 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005722 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005723 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005724 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005725 * for more information.
5726 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005727 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5728 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005729 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005730 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005731 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5732 flagsMask, flagsValues, options);
5733 }
5734
5735 /**
5736 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5737 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5738 * for more information.
5739 *
5740 * @hide
5741 */
5742 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5743 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5744 int extraFlags, @Nullable Bundle options)
5745 throws IntentSender.SendIntentException {
5746 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5747 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005748 }
5749
5750 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005751 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5752 * or {@link #finish} to specify an explicit transition animation to
5753 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005754 *
5755 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5756 * to using this with starting activities is to supply the desired animation
5757 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005758 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005759 * you to specify a custom animation even when starting an activity from
5760 * outside the context of the current top activity.
5761 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005762 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005763 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005764 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005765 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005766 */
5767 public void overridePendingTransition(int enterAnim, int exitAnim) {
5768 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005769 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005770 mToken, getPackageName(), enterAnim, exitAnim);
5771 } catch (RemoteException e) {
5772 }
5773 }
RoboErik55011652014-07-09 15:05:53 -07005774
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 * Call this to set the result that your activity will return to its
5777 * caller.
RoboErik55011652014-07-09 15:05:53 -07005778 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 * @param resultCode The result code to propagate back to the originating
5780 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005781 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 * @see #RESULT_CANCELED
5783 * @see #RESULT_OK
5784 * @see #RESULT_FIRST_USER
5785 * @see #setResult(int, Intent)
5786 */
5787 public final void setResult(int resultCode) {
5788 synchronized (this) {
5789 mResultCode = resultCode;
5790 mResultData = null;
5791 }
5792 }
5793
5794 /**
5795 * Call this to set the result that your activity will return to its
5796 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005797 *
5798 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5799 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5800 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5801 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5802 * Activity receiving the result access to the specific URIs in the Intent.
5803 * Access will remain until the Activity has finished (it will remain across the hosting
5804 * process being killed and other temporary destruction) and will be added
5805 * to any existing set of URI permissions it already holds.
5806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 * @param resultCode The result code to propagate back to the originating
5808 * activity, often RESULT_CANCELED or RESULT_OK
5809 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 * @see #RESULT_CANCELED
5812 * @see #RESULT_OK
5813 * @see #RESULT_FIRST_USER
5814 * @see #setResult(int)
5815 */
5816 public final void setResult(int resultCode, Intent data) {
5817 synchronized (this) {
5818 mResultCode = resultCode;
5819 mResultData = data;
5820 }
5821 }
5822
5823 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005824 * Return information about who launched this activity. If the launching Intent
5825 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5826 * that will be returned as-is; otherwise, if known, an
5827 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5828 * package name that started the Intent will be returned. This may return null if no
5829 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5830 * application package was involved.
5831 *
5832 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5833 * return the referrer that submitted that new intent to the activity. Otherwise, it
5834 * always returns the referrer of the original Intent.</p>
5835 *
5836 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5837 * referrer information, applications can spoof it.</p>
5838 */
5839 @Nullable
5840 public Uri getReferrer() {
5841 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005842 try {
5843 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5844 if (referrer != null) {
5845 return referrer;
5846 }
5847 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5848 if (referrerName != null) {
5849 return Uri.parse(referrerName);
5850 }
5851 } catch (BadParcelableException e) {
5852 Log.w(TAG, "Cannot read referrer from intent;"
5853 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005854 }
5855 if (mReferrer != null) {
5856 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5857 }
5858 return null;
5859 }
5860
5861 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005862 * Override to generate the desired referrer for the content currently being shown
5863 * by the app. The default implementation returns null, meaning the referrer will simply
5864 * be the android-app: of the package name of this activity. Return a non-null Uri to
5865 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5866 */
5867 public Uri onProvideReferrer() {
5868 return null;
5869 }
5870
5871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 * Return the name of the package that invoked this activity. This is who
5873 * the data in {@link #setResult setResult()} will be sent to. You can
5874 * use this information to validate that the recipient is allowed to
5875 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005876 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005877 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005878 * did not use the {@link #startActivityForResult}
5879 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005880 * null.</p>
5881 *
5882 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5883 * the result from this method was unstable. If the process hosting the calling
5884 * package was no longer running, it would return null instead of the proper package
5885 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5886 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 * @return The package of the activity that will receive your
5889 * reply, or null if none.
5890 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005891 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 public String getCallingPackage() {
5893 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005894 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 } catch (RemoteException e) {
5896 return null;
5897 }
5898 }
5899
5900 /**
5901 * Return the name of the activity that invoked this activity. This is
5902 * who the data in {@link #setResult setResult()} will be sent to. You
5903 * can use this information to validate that the recipient is allowed to
5904 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005905 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005906 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005907 * did not use the {@link #startActivityForResult}
5908 * form that includes a request code), then the calling package will be
5909 * null.
5910 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005911 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * reply, or null if none.
5913 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005914 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 public ComponentName getCallingActivity() {
5916 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005917 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005918 } catch (RemoteException e) {
5919 return null;
5920 }
5921 }
5922
5923 /**
5924 * Control whether this activity's main window is visible. This is intended
5925 * only for the special case of an activity that is not going to show a
5926 * UI itself, but can't just finish prior to onResume() because it needs
5927 * to wait for a service binding or such. Setting this to false allows
5928 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 * <p>The default value for this is taken from the
5931 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5932 */
5933 public void setVisible(boolean visible) {
5934 if (mVisibleFromClient != visible) {
5935 mVisibleFromClient = visible;
5936 if (mVisibleFromServer) {
5937 if (visible) makeVisible();
5938 else mDecor.setVisibility(View.INVISIBLE);
5939 }
5940 }
5941 }
RoboErik55011652014-07-09 15:05:53 -07005942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 void makeVisible() {
5944 if (!mWindowAdded) {
5945 ViewManager wm = getWindowManager();
5946 wm.addView(mDecor, getWindow().getAttributes());
5947 mWindowAdded = true;
5948 }
5949 mDecor.setVisibility(View.VISIBLE);
5950 }
RoboErik55011652014-07-09 15:05:53 -07005951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 /**
5953 * Check to see whether this activity is in the process of finishing,
5954 * either because you called {@link #finish} on it or someone else
5955 * has requested that it finished. This is often used in
5956 * {@link #onPause} to determine whether the activity is simply pausing or
5957 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005958 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005960 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 * @see #finish
5962 */
5963 public boolean isFinishing() {
5964 return mFinished;
5965 }
5966
5967 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005968 * Returns true if the final {@link #onDestroy()} call has been made
5969 * on the Activity, so this instance is now dead.
5970 */
5971 public boolean isDestroyed() {
5972 return mDestroyed;
5973 }
5974
5975 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005976 * Check to see whether this activity is in the process of being destroyed in order to be
5977 * recreated with a new configuration. This is often used in
5978 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5979 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005980 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005981 * @return If the activity is being torn down in order to be recreated with a new configuration,
5982 * returns true; else returns false.
5983 */
5984 public boolean isChangingConfigurations() {
5985 return mChangingConfigurations;
5986 }
5987
5988 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005989 * Cause this Activity to be recreated with a new instance. This results
5990 * in essentially the same flow as when the Activity is created due to
5991 * a configuration change -- the current instance will go through its
5992 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5993 */
5994 public void recreate() {
5995 if (mParent != null) {
5996 throw new IllegalStateException("Can only be called on top-level activity");
5997 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07005998 if (Looper.myLooper() != mMainThread.getLooper()) {
5999 throw new IllegalStateException("Must be called from main thread");
6000 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006001 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006002 }
6003
6004 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006005 * Finishes the current activity and specifies whether to remove the task associated with this
6006 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006008 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006009 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006010 if (mParent == null) {
6011 int resultCode;
6012 Intent resultData;
6013 synchronized (this) {
6014 resultCode = mResultCode;
6015 resultData = mResultData;
6016 }
Joe Onorato43a17652011-04-06 19:22:23 -07006017 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006019 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006020 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006021 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006022 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006023 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 mFinished = true;
6025 }
6026 } catch (RemoteException e) {
6027 // Empty
6028 }
6029 } else {
6030 mParent.finishFromChild(this);
6031 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006032
6033 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6034 // be restored now.
6035 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
6036 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6037 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
6038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 }
6040
6041 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006042 * Call this when your activity is done and should be closed. The
6043 * ActivityResult is propagated back to whoever launched you via
6044 * onActivityResult().
6045 */
6046 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006047 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006048 }
6049
6050 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006051 * Finish this activity as well as all activities immediately below it
6052 * in the current task that have the same affinity. This is typically
6053 * used when an application can be launched on to another task (such as
6054 * from an ACTION_VIEW of a content type it understands) and the user
6055 * has used the up navigation to switch out of the current task and in
6056 * to its own task. In this case, if the user has navigated down into
6057 * any other activities of the second application, all of those should
6058 * be removed from the original task as part of the task switch.
6059 *
6060 * <p>Note that this finish does <em>not</em> allow you to deliver results
6061 * to the previous activity, and an exception will be thrown if you are trying
6062 * to do so.</p>
6063 */
6064 public void finishAffinity() {
6065 if (mParent != null) {
6066 throw new IllegalStateException("Can not be called from an embedded activity");
6067 }
6068 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6069 throw new IllegalStateException("Can not be called to deliver a result");
6070 }
6071 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006072 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006073 mFinished = true;
6074 }
6075 } catch (RemoteException e) {
6076 // Empty
6077 }
6078 }
6079
6080 /**
RoboErik55011652014-07-09 15:05:53 -07006081 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 * {@link #finish} method. The default implementation simply calls
6083 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 * @see #finish
6088 */
6089 public void finishFromChild(Activity child) {
6090 finish();
6091 }
6092
6093 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006094 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6095 * to reverse its exit Transition. When the exit Transition completes,
6096 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6097 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006098 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006099 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006100 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006101 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006102 finish();
6103 }
George Mountcb4b7d92014-02-25 10:47:55 -08006104 }
6105
6106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 * Force finish another activity that you had previously started with
6108 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 * @param requestCode The request code of the activity that you had
6111 * given to startActivityForResult(). If there are multiple
6112 * activities started with this request code, they
6113 * will all be finished.
6114 */
6115 public void finishActivity(int requestCode) {
6116 if (mParent == null) {
6117 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006118 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6120 } catch (RemoteException e) {
6121 // Empty
6122 }
6123 } else {
6124 mParent.finishActivityFromChild(this, requestCode);
6125 }
6126 }
6127
6128 /**
6129 * This is called when a child activity of this one calls its
6130 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 * @param child The activity making the call.
6133 * @param requestCode Request code that had been used to start the
6134 * activity.
6135 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006136 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006138 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6140 } catch (RemoteException e) {
6141 // Empty
6142 }
6143 }
6144
6145 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006146 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006147 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006148 */
6149 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006150 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006151 }
6152
6153 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006154 * Ask that the local app instance of this activity be released to free up its memory.
6155 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6156 * a new instance of the activity will later be re-created if needed due to the user
6157 * navigating back to it.
6158 *
6159 * @return Returns true if the activity was in a state that it has started the process
6160 * of destroying its current instance; returns false if for any reason this could not
6161 * be done: it is currently visible to the user, it is already being destroyed, it is
6162 * being finished, it hasn't yet saved its state, etc.
6163 */
6164 public boolean releaseInstance() {
6165 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006166 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006167 } catch (RemoteException e) {
6168 // Empty
6169 }
6170 return false;
6171 }
6172
6173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006174 * Called when an activity you launched exits, giving you the requestCode
6175 * you started it with, the resultCode it returned, and any additional
6176 * data from it. The <var>resultCode</var> will be
6177 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6178 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 * <p>You will receive this call immediately before onResume() when your
6181 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006182 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006183 * <p>This method is never invoked if your activity sets
6184 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6185 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 * @param requestCode The integer request code originally supplied to
6188 * startActivityForResult(), allowing you to identify who this
6189 * result came from.
6190 * @param resultCode The integer result code returned by the child activity
6191 * through its setResult().
6192 * @param data An Intent, which can return result data to the caller
6193 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006194 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 * @see #startActivityForResult
6196 * @see #createPendingResult
6197 * @see #setResult(int)
6198 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006199 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 }
6201
6202 /**
George Mount62ab9b72014-05-02 13:51:17 -07006203 * Called when an activity you launched with an activity transition exposes this
6204 * Activity through a returning activity transition, giving you the resultCode
6205 * and any additional data from it. This method will only be called if the activity
6206 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006207 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006208 *
6209 * <p>The purpose of this function is to let the called Activity send a hint about
6210 * its state so that this underlying Activity can prepare to be exposed. A call to
6211 * this method does not guarantee that the called Activity has or will be exiting soon.
6212 * It only indicates that it will expose this Activity's Window and it has
6213 * some data to pass to prepare it.</p>
6214 *
6215 * @param resultCode The integer result code returned by the child activity
6216 * through its setResult().
6217 * @param data An Intent, which can return result data to the caller
6218 * (various data can be attached to Intent "extras").
6219 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006220 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006221 }
6222
6223 /**
RoboErik55011652014-07-09 15:05:53 -07006224 * Create a new PendingIntent object which you can hand to others
6225 * for them to use to send result data back to your
6226 * {@link #onActivityResult} callback. The created object will be either
6227 * one-shot (becoming invalid after a result is sent back) or multiple
6228 * (allowing any number of results to be sent through it).
6229 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 * @param requestCode Private request code for the sender that will be
6231 * associated with the result data when it is returned. The sender can not
6232 * modify this value, allowing you to identify incoming results.
6233 * @param data Default data to supply in the result, which may be modified
6234 * by the sender.
6235 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6236 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6237 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6238 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6239 * or any of the flags as supported by
6240 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6241 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 * @return Returns an existing or new PendingIntent matching the given
6244 * parameters. May return null only if
6245 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6246 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 * @see PendingIntent
6249 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006250 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6251 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 String packageName = getPackageName();
6253 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006254 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006256 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07006257 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07006259 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006260 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006261 return target != null ? new PendingIntent(target) : null;
6262 } catch (RemoteException e) {
6263 // Empty
6264 }
6265 return null;
6266 }
6267
6268 /**
6269 * Change the desired orientation of this activity. If the activity
6270 * is currently in the foreground or otherwise impacting the screen
6271 * orientation, the screen will immediately be changed (possibly causing
6272 * the activity to be restarted). Otherwise, this will be used the next
6273 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 * @param requestedOrientation An orientation constant as used in
6276 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6277 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006278 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 if (mParent == null) {
6280 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006281 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 mToken, requestedOrientation);
6283 } catch (RemoteException e) {
6284 // Empty
6285 }
6286 } else {
6287 mParent.setRequestedOrientation(requestedOrientation);
6288 }
6289 }
RoboErik55011652014-07-09 15:05:53 -07006290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006291 /**
6292 * Return the current requested orientation of the activity. This will
6293 * either be the orientation requested in its component's manifest, or
6294 * the last requested orientation given to
6295 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006297 * @return Returns an orientation constant as used in
6298 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6299 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006300 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 public int getRequestedOrientation() {
6302 if (mParent == null) {
6303 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006304 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 .getRequestedOrientation(mToken);
6306 } catch (RemoteException e) {
6307 // Empty
6308 }
6309 } else {
6310 return mParent.getRequestedOrientation();
6311 }
6312 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6313 }
RoboErik55011652014-07-09 15:05:53 -07006314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 /**
6316 * Return the identifier of the task this activity is in. This identifier
6317 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006318 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 * @return Task identifier, an opaque integer.
6320 */
6321 public int getTaskId() {
6322 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006323 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 } catch (RemoteException e) {
6325 return -1;
6326 }
6327 }
6328
6329 /**
6330 * Return whether this activity is the root of a task. The root is the
6331 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 * @return True if this is the root activity, else false.
6334 */
Winson Chung4d8681f2017-05-23 16:22:08 -07006335 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 public boolean isTaskRoot() {
6337 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006338 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 } catch (RemoteException e) {
6340 return false;
6341 }
6342 }
6343
6344 /**
6345 * Move the task containing this activity to the back of the activity
6346 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006347 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 * @param nonRoot If false then this only works if the activity is the root
6349 * of a task; if true it will work for any activity in
6350 * a task.
RoboErik55011652014-07-09 15:05:53 -07006351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 * @return If the task was moved (or it was already at the
6353 * back) true is returned, else false.
6354 */
6355 public boolean moveTaskToBack(boolean nonRoot) {
6356 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006357 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 } catch (RemoteException e) {
6359 // Empty
6360 }
6361 return false;
6362 }
6363
6364 /**
6365 * Returns class name for this activity with the package prefix removed.
6366 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006367 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 * @return The local class name.
6369 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006370 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 public String getLocalClassName() {
6372 final String pkg = getPackageName();
6373 final String cls = mComponent.getClassName();
6374 int packageLen = pkg.length();
6375 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6376 || cls.charAt(packageLen) != '.') {
6377 return cls;
6378 }
6379 return cls.substring(packageLen+1);
6380 }
RoboErik55011652014-07-09 15:05:53 -07006381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006382 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006383 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 * @return Returns the complete component name for this activity
6386 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006387 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 return mComponent;
6389 }
6390
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006391 /** @hide */
6392 @Override
6393 public final ComponentName autofillClientGetComponentName() {
6394 return getComponentName();
6395 }
6396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 /**
6398 * Retrieve a {@link SharedPreferences} object for accessing preferences
6399 * that are private to this activity. This simply calls the underlying
6400 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6401 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006402 *
6403 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006404 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 *
6406 * @return Returns the single SharedPreferences instance that can be used
6407 * to retrieve and modify the preference values.
6408 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006409 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 return getSharedPreferences(getLocalClassName(), mode);
6411 }
RoboErik55011652014-07-09 15:05:53 -07006412
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006413 private void ensureSearchManager() {
6414 if (mSearchManager != null) {
6415 return;
6416 }
RoboErik55011652014-07-09 15:05:53 -07006417
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006418 try {
6419 mSearchManager = new SearchManager(this, null);
6420 } catch (ServiceNotFoundException e) {
6421 throw new IllegalStateException(e);
6422 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006423 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006426 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006427 if (getBaseContext() == null) {
6428 throw new IllegalStateException(
6429 "System services not available to Activities before onCreate()");
6430 }
6431
6432 if (WINDOW_SERVICE.equals(name)) {
6433 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006434 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006435 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006436 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 }
6438 return super.getSystemService(name);
6439 }
6440
6441 /**
6442 * Change the title associated with this activity. If this is a
6443 * top-level activity, the title for its window will change. If it
6444 * is an embedded activity, the parent can do whatever it wants
6445 * with it.
6446 */
6447 public void setTitle(CharSequence title) {
6448 mTitle = title;
6449 onTitleChanged(title, mTitleColor);
6450
6451 if (mParent != null) {
6452 mParent.onChildTitleChanged(this, title);
6453 }
6454 }
6455
6456 /**
6457 * Change the title associated with this activity. If this is a
6458 * top-level activity, the title for its window will change. If it
6459 * is an embedded activity, the parent can do whatever it wants
6460 * with it.
6461 */
6462 public void setTitle(int titleId) {
6463 setTitle(getText(titleId));
6464 }
6465
Alan Viverette2525d9c2013-11-15 14:42:19 -08006466 /**
6467 * Change the color of the title associated with this activity.
6468 * <p>
6469 * This method is deprecated starting in API Level 11 and replaced by action
6470 * bar styles. For information on styling the Action Bar, read the <a
6471 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6472 * guide.
6473 *
6474 * @deprecated Use action bar styles instead.
6475 */
6476 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 public void setTitleColor(int textColor) {
6478 mTitleColor = textColor;
6479 onTitleChanged(mTitle, textColor);
6480 }
6481
6482 public final CharSequence getTitle() {
6483 return mTitle;
6484 }
6485
6486 public final int getTitleColor() {
6487 return mTitleColor;
6488 }
6489
6490 protected void onTitleChanged(CharSequence title, int color) {
6491 if (mTitleReady) {
6492 final Window win = getWindow();
6493 if (win != null) {
6494 win.setTitle(title);
6495 if (color != 0) {
6496 win.setTitleColor(color);
6497 }
6498 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006499 if (mActionBar != null) {
6500 mActionBar.setWindowTitle(title);
6501 }
Adam Powella557fdc2014-08-21 18:05:53 -07006502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 }
6504
6505 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6506 }
6507
6508 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006509 * Sets information describing the task with this activity for presentation inside the Recents
6510 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6511 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6512 * for each property until a suitable value is found. For each task the taskDescription will be
6513 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006514 *
6515 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006516 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006517 *
Winson Chunga449dc02014-05-16 11:15:04 -07006518 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006519 */
Winson Chunga449dc02014-05-16 11:15:04 -07006520 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006521 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006522 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006523 // Scale the icon down to something reasonable if it is provided
6524 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6525 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6526 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6527 true);
6528 mTaskDescription.setIcon(icon);
6529 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006530 }
6531 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006532 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006533 } catch (RemoteException e) {
6534 }
6535 }
6536
6537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 * Sets the visibility of the progress bar in the title.
6539 * <p>
6540 * In order for the progress bar to be shown, the feature must be requested
6541 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006544 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006546 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 public final void setProgressBarVisibility(boolean visible) {
6548 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6549 Window.PROGRESS_VISIBILITY_OFF);
6550 }
6551
6552 /**
6553 * Sets the visibility of the indeterminate progress bar in the title.
6554 * <p>
6555 * In order for the progress bar to be shown, the feature must be requested
6556 * via {@link #requestWindowFeature(int)}.
6557 *
6558 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006559 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006561 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6563 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6564 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6565 }
RoboErik55011652014-07-09 15:05:53 -07006566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006567 /**
6568 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6569 * is always indeterminate).
6570 * <p>
6571 * In order for the progress bar to be shown, the feature must be requested
6572 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006575 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006577 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 public final void setProgressBarIndeterminate(boolean indeterminate) {
6579 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006580 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6581 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 }
RoboErik55011652014-07-09 15:05:53 -07006583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584 /**
6585 * Sets the progress for the progress bars in the title.
6586 * <p>
6587 * In order for the progress bar to be shown, the feature must be requested
6588 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 * @param progress The progress for the progress bar. Valid ranges are from
6591 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6592 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006593 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006595 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 public final void setProgress(int progress) {
6597 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6598 }
RoboErik55011652014-07-09 15:05:53 -07006599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006600 /**
6601 * Sets the secondary progress for the progress bar in the title. This
6602 * progress is drawn between the primary progress (set via
6603 * {@link #setProgress(int)} and the background. It can be ideal for media
6604 * scenarios such as showing the buffering progress while the default
6605 * progress shows the play progress.
6606 * <p>
6607 * In order for the progress bar to be shown, the feature must be requested
6608 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6611 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006612 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006614 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 public final void setSecondaryProgress(int secondaryProgress) {
6616 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6617 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6618 }
6619
6620 /**
6621 * Suggests an audio stream whose volume should be changed by the hardware
6622 * volume controls.
6623 * <p>
6624 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006625 * Volume requests which are received while the Activity is in the
6626 * foreground will affect this stream.
6627 * <p>
6628 * It is not guaranteed that the hardware volume controls will always change
6629 * this stream's volume (for example, if a call is in progress, its stream's
6630 * volume may be changed instead). To reset back to the default, use
6631 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006634 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 */
6636 public final void setVolumeControlStream(int streamType) {
6637 getWindow().setVolumeControlStream(streamType);
6638 }
6639
6640 /**
6641 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006642 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 * @return The suggested audio stream type whose volume should be changed by
6645 * the hardware volume controls.
6646 * @see #setVolumeControlStream(int)
6647 */
6648 public final int getVolumeControlStream() {
6649 return getWindow().getVolumeControlStream();
6650 }
RoboErik55011652014-07-09 15:05:53 -07006651
6652 /**
6653 * Sets a {@link MediaController} to send media keys and volume changes to.
6654 * <p>
6655 * The controller will be tied to the window of this Activity. Media key and
6656 * volume events which are received while the Activity is in the foreground
6657 * will be forwarded to the controller and used to invoke transport controls
6658 * or adjust the volume. This may be used instead of or in addition to
6659 * {@link #setVolumeControlStream} to affect a specific session instead of a
6660 * specific stream.
6661 * <p>
6662 * It is not guaranteed that the hardware volume controls will always change
6663 * this session's volume (for example, if a call is in progress, its
6664 * stream's volume may be changed instead). To reset back to the default use
6665 * null as the controller.
6666 *
6667 * @param controller The controller for the session which should receive
6668 * media keys and volume changes.
6669 */
6670 public final void setMediaController(MediaController controller) {
6671 getWindow().setMediaController(controller);
6672 }
6673
6674 /**
6675 * Gets the controller which should be receiving media key and volume events
6676 * while this activity is in the foreground.
6677 *
6678 * @return The controller which should receive events.
6679 * @see #setMediaController(android.media.session.MediaController)
6680 */
6681 public final MediaController getMediaController() {
6682 return getWindow().getMediaController();
6683 }
6684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 /**
6686 * Runs the specified action on the UI thread. If the current thread is the UI
6687 * thread, then the action is executed immediately. If the current thread is
6688 * not the UI thread, the action is posted to the event queue of the UI thread.
6689 *
6690 * @param action the action to run on the UI thread
6691 */
6692 public final void runOnUiThread(Runnable action) {
6693 if (Thread.currentThread() != mUiThread) {
6694 mHandler.post(action);
6695 } else {
6696 action.run();
6697 }
6698 }
6699
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006700 /** @hide */
6701 @Override
6702 public final void autofillClientRunOnUiThread(Runnable action) {
6703 runOnUiThread(action);
6704 }
6705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006707 * Standard implementation of
6708 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6709 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006710 * This implementation does nothing and is for
6711 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6712 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6713 *
6714 * @see android.view.LayoutInflater#createView
6715 * @see android.view.Window#getLayoutInflater
6716 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006717 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07006718 public View onCreateView(@NonNull String name, @NonNull Context context,
6719 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006720 return null;
6721 }
6722
6723 /**
6724 * Standard implementation of
6725 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6726 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006727 * This implementation handles <fragment> tags to embed fragments inside
6728 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 *
6730 * @see android.view.LayoutInflater#createView
6731 * @see android.view.Window#getLayoutInflater
6732 */
Ian Lake0c807f42018-09-12 14:03:27 -07006733 @Nullable
6734 public View onCreateView(@Nullable View parent, @NonNull String name,
6735 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006736 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006737 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006738 }
RoboErik55011652014-07-09 15:05:53 -07006739
Adam Powell371a8092014-06-20 12:51:12 -07006740 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 }
6742
Daniel Sandler69a48172010-06-23 16:29:36 -04006743 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006744 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006745 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006746 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006747 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006748 * @param fd The raw file descriptor that the dump is being sent to.
6749 * @param writer The PrintWriter to which you should dump your state. This will be
6750 * closed for you after you return.
6751 * @param args additional arguments to the dump request.
6752 */
Ian Lake140fe122018-09-13 14:59:38 -07006753 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
6754 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006755 dumpInner(prefix, fd, writer, args);
6756 }
6757
Ian Lake140fe122018-09-13 14:59:38 -07006758 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
6759 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08006760 if (args != null && args.length > 0) {
6761 // Handle special cases
6762 switch (args[0]) {
6763 case "--autofill":
6764 dumpAutofillManager(prefix, writer);
6765 return;
6766 case "--intelligence":
6767 dumpIntelligenceManager(prefix, writer);
6768 return;
6769 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07006770 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006771 writer.print(prefix); writer.print("Local Activity ");
6772 writer.print(Integer.toHexString(System.identityHashCode(this)));
6773 writer.println(" State:");
6774 String innerPrefix = prefix + " ";
6775 writer.print(innerPrefix); writer.print("mResumed=");
6776 writer.print(mResumed); writer.print(" mStopped=");
6777 writer.print(mStopped); writer.print(" mFinished=");
6778 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006779 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6780 writer.println(mChangingConfigurations);
6781 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6782 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006783
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006784 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6785 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006786 if (mVoiceInteractor != null) {
6787 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6788 }
Jeff Brown5182c782013-10-15 20:31:52 -07006789
Michael Wright5f48dc72013-11-01 12:42:49 -07006790 if (getWindow() != null &&
6791 getWindow().peekDecorView() != null &&
6792 getWindow().peekDecorView().getViewRootImpl() != null) {
6793 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6794 }
Jeff Brown5182c782013-10-15 20:31:52 -07006795
6796 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07006797
Felipe Lemeb546ca72018-08-15 08:44:12 -07006798 dumpAutofillManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006799 dumpIntelligenceManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07006800
6801 ResourcesManager.getInstance().dump(prefix, writer);
6802 }
6803
6804 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08006805 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006806 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07006807 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08006808 writer.print(prefix); writer.print("Autofill Compat Mode: ");
6809 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07006810 } else {
6811 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07006812 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07006813 }
6814
Felipe Lemee348dc32018-11-05 12:35:29 -08006815 void dumpIntelligenceManager(String prefix, PrintWriter writer) {
Felipe Lemeecb08be2018-11-27 15:48:47 -08006816 final ContentCaptureManager im = getContentCaptureManager();
Felipe Lemee348dc32018-11-05 12:35:29 -08006817 if (im != null) {
6818 im.dump(prefix, writer);
6819 } else {
6820 writer.print(prefix); writer.println("No IntelligenceManager");
6821 }
6822 }
6823
Dianne Hackborn625ac272010-09-17 18:29:22 -07006824 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006825 * Bit indicating that this activity is "immersive" and should not be
6826 * interrupted by notifications if possible.
6827 *
6828 * This value is initially set by the manifest property
6829 * <code>android:immersive</code> but may be changed at runtime by
6830 * {@link #setImmersive}.
6831 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006832 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006833 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6834 */
6835 public boolean isImmersive() {
6836 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006837 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006838 } catch (RemoteException e) {
6839 return false;
6840 }
6841 }
6842
6843 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006844 * Indication of whether this is the highest level activity in this task. Can be used to
6845 * determine whether an activity launched by this activity was placed in the same task or
6846 * another task.
6847 *
6848 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006849 */
Sunny Goyald85bed52018-09-25 12:01:01 -07006850 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006851 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006852 return false;
6853 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006854 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006855 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006856 } catch (RemoteException e) {
6857 return false;
6858 }
6859 }
6860
6861 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006862 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6863 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006864 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006865 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006866 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6867 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006868 * This call has no effect on non-translucent activities or on activities with the
6869 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006870 *
George Mount62ab9b72014-05-02 13:51:17 -07006871 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6872 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006873 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006874 *
6875 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006876 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006877 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006878 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006879 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006880 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006881 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006882 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6883 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006884 } catch (RemoteException e) {
6885 // pass
6886 }
6887 }
6888
6889 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006890 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6891 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6892 * <p>
6893 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6894 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6895 * be called indicating that it is safe to make this activity translucent again. Until
6896 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6897 * behind the frontmost Activity will be indeterminate.
6898 * <p>
6899 * This call has no effect on non-translucent activities or on activities with the
6900 * {@link android.R.attr#windowIsFloating} attribute.
6901 *
6902 * @param callback the method to call when all visible Activities behind this one have been
6903 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006904 * @param options activity options delivered to the activity below this one. The options
6905 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006906 * @return <code>true</code> if Window was opaque and will become translucent or
6907 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006908 *
6909 * @see #convertFromTranslucent()
6910 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006911 *
6912 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006913 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006914 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006915 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006916 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006917 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006918 try {
6919 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006920 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006921 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006922 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006923 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006924 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006925 // Make callback return as though it timed out.
6926 mChangeCanvasToTranslucent = false;
6927 drawComplete = false;
6928 }
6929 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6930 // Window is already translucent.
6931 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006932 }
George Mount3cc716c2014-06-12 16:35:35 -07006933 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006934 }
6935
6936 /** @hide */
6937 void onTranslucentConversionComplete(boolean drawComplete) {
6938 if (mTranslucentCallback != null) {
6939 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6940 mTranslucentCallback = null;
6941 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006942 if (mChangeCanvasToTranslucent) {
6943 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6944 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006945 }
6946
Craig Mautnereb8abf72014-07-02 15:04:09 -07006947 /** @hide */
6948 public void onNewActivityOptions(ActivityOptions options) {
6949 mActivityTransitionState.setEnterActivityOptions(this, options);
6950 if (!mStopped) {
6951 mActivityTransitionState.enterReady(this);
6952 }
6953 }
6954
Craig Mautner5eda9b32013-07-02 11:58:16 -07006955 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006956 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6957 * from an activity launched by this activity in its call to {@link
6958 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6959 *
6960 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6961 * @hide
6962 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006963 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07006964 ActivityOptions getActivityOptions() {
6965 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006966 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006967 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07006968 } catch (RemoteException e) {
6969 }
6970 return null;
6971 }
6972
6973 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006974 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006975 * this method anytime between the start of {@link #onResume()} and the return from
6976 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6977 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6978 *
6979 * <p>The actions of this call are reset each time that this activity is brought to the
6980 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6981 * to not have requested visible behind. Therefore, if you want this activity to continue to
6982 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006983 *
6984 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6985 * for dialog and translucent activities.
6986 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006987 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6988 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6989 *
6990 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006991 * the next call to onResume.
6992 *
Bryce Leed59629e2017-04-18 14:35:29 -07006993 * @deprecated This method's functionality is no longer supported as of
6994 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6995 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006996 * @param visible true to notify the system that the activity wishes to be visible behind other
6997 * translucent activities, false to indicate otherwise. Resources must be
6998 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07006999 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007000 * @return the resulting visibiity state. If true the activity will remain visible beyond
7001 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7002 * then the activity may not count on being visible behind other translucent activities,
7003 * and must stop any media playback and release resources.
7004 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7005 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007006 *
Jose Limafcf70832014-08-27 23:09:05 -07007007 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007008 */
Bryce Leed59629e2017-04-18 14:35:29 -07007009 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007010 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007011 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007012 }
7013
7014 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007015 * Called when a translucent activity over this activity is becoming opaque or another
7016 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007017 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007018 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007019 * <p>When this method is called the activity has 500 msec to release any resources it may be
7020 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007021 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007022 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007023 * process. Otherwise {@link #onStop()} will be called following return.
7024 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007025 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007026 *
7027 * @deprecated This method's functionality is no longer supported as of
7028 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007029 */
Bryce Leed59629e2017-04-18 14:35:29 -07007030 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007031 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007032 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007033 mCalled = true;
7034 }
7035
7036 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007037 * Translucent activities may call this to determine if there is an activity below them that
7038 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007039 *
Bryce Leed59629e2017-04-18 14:35:29 -07007040 * @deprecated This method's functionality is no longer supported as of
7041 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7042 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007043 * @return true if an activity below is set to visible according to the most recent call to
7044 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007045 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007046 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007047 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007048 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007049 * @hide
7050 */
Bryce Leed59629e2017-04-18 14:35:29 -07007051 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007052 @SystemApi
7053 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007054 return false;
7055 }
7056
7057 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007058 * The topmost foreground activity will receive this call when the background visibility state
7059 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007060 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007061 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007062 * due to a background activity finishing itself.
7063 *
Bryce Leed59629e2017-04-18 14:35:29 -07007064 * @deprecated This method's functionality is no longer supported as of
7065 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7066 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007067 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007068 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007069 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007070 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007071 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007072 */
Bryce Leed59629e2017-04-18 14:35:29 -07007073 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007074 @SystemApi
7075 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007076 }
7077
7078 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007079 * Activities cannot draw during the period that their windows are animating in. In order
7080 * to know when it is safe to begin drawing they can override this method which will be
7081 * called when the entering animation has completed.
7082 */
7083 public void onEnterAnimationComplete() {
7084 }
7085
7086 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007087 * @hide
7088 */
7089 public void dispatchEnterAnimationComplete() {
7090 onEnterAnimationComplete();
7091 if (getWindow() != null && getWindow().getDecorView() != null) {
7092 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
7093 }
7094 }
7095
7096 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007097 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007098 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007099 * Note that changing this value will have no effect on the activity's
7100 * {@link android.content.pm.ActivityInfo} structure; that is, if
7101 * <code>android:immersive</code> is set to <code>true</code>
7102 * in the application's manifest entry for this activity, the {@link
7103 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7104 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7105 * FLAG_IMMERSIVE} bit set.
7106 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007107 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007108 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7109 */
7110 public void setImmersive(boolean i) {
7111 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007112 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007113 } catch (RemoteException e) {
7114 // pass
7115 }
7116 }
7117
Adam Powell6e346362010-07-23 10:18:23 -07007118 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007119 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007120 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007121 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7122 * while this Activity has user focus.</p>
7123 *
7124 * <p>It is recommended that applications additionally declare
7125 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7126 * transitions when switching between VR activities.</p>
7127 *
7128 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7129 * VR mode will not be started. Developers can handle this case as follows:</p>
7130 *
7131 * <pre>
7132 * String servicePackage = "com.whatever.app";
7133 * String serviceClass = "com.whatever.app.MyVrListenerService";
7134 *
7135 * // Name of the component of the VrListenerService to start.
7136 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7137 *
7138 * try {
7139 * setVrModeEnabled(true, myComponentName);
7140 * } catch (PackageManager.NameNotFoundException e) {
7141 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7142 * boolean isInstalled = false;
7143 * for (ApplicationInfo app : installed) {
7144 * if (app.packageName.equals(servicePackage)) {
7145 * isInstalled = true;
7146 * break;
7147 * }
7148 * }
7149 * if (isInstalled) {
7150 * // Package is installed, but not enabled in Settings. Let user enable it.
7151 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7152 * } else {
7153 * // Package is not installed. Send an intent to download this.
7154 * sentIntentToLaunchAppStore(servicePackage);
7155 * }
7156 * }
7157 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007158 *
7159 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007160 * @param requestedComponent the name of the component to use as a
7161 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7162 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007163 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7164 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7165 * not been enabled in user settings.
7166 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007167 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7168 * @see android.service.vr.VrListenerService
7169 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7170 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007171 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007172 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7173 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007174 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007175 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007176 != 0) {
7177 throw new PackageManager.NameNotFoundException(
7178 requestedComponent.flattenToString());
7179 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007180 } catch (RemoteException e) {
7181 // pass
7182 }
7183 }
7184
7185 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007186 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007187 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007188 * @param callback Callback that will manage lifecycle events for this action mode
7189 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007190 *
7191 * @see ActionMode
7192 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007193 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007194 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007195 return mWindow.getDecorView().startActionMode(callback);
7196 }
7197
Adam Powelldebf3be2010-11-15 18:58:48 -08007198 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007199 * Start an action mode of the given type.
7200 *
7201 * @param callback Callback that will manage lifecycle events for this action mode
7202 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7203 * @return The ActionMode that was started, or null if it was canceled
7204 *
7205 * @see ActionMode
7206 */
7207 @Nullable
7208 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7209 return mWindow.getDecorView().startActionMode(callback, type);
7210 }
7211
7212 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007213 * Give the Activity a chance to control the UI for an action mode requested
7214 * by the system.
7215 *
7216 * <p>Note: If you are looking for a notification callback that an action mode
7217 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7218 *
7219 * @param callback The callback that should control the new action mode
7220 * @return The new action mode, or <code>null</code> if the activity does not want to
7221 * provide special handling for this action mode. (It will be handled by the system.)
7222 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007223 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007224 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007225 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007226 // Only Primary ActionModes are represented in the ActionBar.
7227 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7228 initWindowDecorActionBar();
7229 if (mActionBar != null) {
7230 return mActionBar.startActionMode(callback);
7231 }
Adam Powell6e346362010-07-23 10:18:23 -07007232 }
7233 return null;
7234 }
7235
Adam Powelldebf3be2010-11-15 18:58:48 -08007236 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007237 * {@inheritDoc}
7238 */
7239 @Nullable
7240 @Override
7241 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7242 try {
7243 mActionModeTypeStarting = type;
7244 return onWindowStartingActionMode(callback);
7245 } finally {
7246 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7247 }
7248 }
7249
7250 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007251 * Notifies the Activity that an action mode has been started.
7252 * Activity subclasses overriding this method should call the superclass implementation.
7253 *
7254 * @param mode The new action mode.
7255 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007256 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007257 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007258 public void onActionModeStarted(ActionMode mode) {
7259 }
7260
7261 /**
7262 * Notifies the activity that an action mode has finished.
7263 * Activity subclasses overriding this method should call the superclass implementation.
7264 *
7265 * @param mode The action mode that just finished.
7266 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007267 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007268 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007269 public void onActionModeFinished(ActionMode mode) {
7270 }
7271
Adam Powelldd8fab22012-03-22 17:47:27 -07007272 /**
7273 * Returns true if the app should recreate the task when navigating 'up' from this activity
7274 * by using targetIntent.
7275 *
7276 * <p>If this method returns false the app can trivially call
7277 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7278 * up navigation. If this method returns false, the app should synthesize a new task stack
7279 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7280 *
7281 * @param targetIntent An intent representing the target destination for up navigation
7282 * @return true if navigating up should recreate a new task stack, false if the same task
7283 * should be used for the destination
7284 */
7285 public boolean shouldUpRecreateTask(Intent targetIntent) {
7286 try {
7287 PackageManager pm = getPackageManager();
7288 ComponentName cn = targetIntent.getComponent();
7289 if (cn == null) {
7290 cn = targetIntent.resolveActivity(pm);
7291 }
7292 ActivityInfo info = pm.getActivityInfo(cn, 0);
7293 if (info.taskAffinity == null) {
7294 return false;
7295 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007296 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007297 } catch (RemoteException e) {
7298 return false;
7299 } catch (NameNotFoundException e) {
7300 return false;
7301 }
7302 }
7303
7304 /**
7305 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7306 * in the process. If the activity indicated by upIntent already exists in the task's history,
7307 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007308 * finished.
7309 *
7310 * <p>If the indicated activity does not appear in the history stack, this will finish
7311 * each activity in this task until the root activity of the task is reached, resulting in
7312 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7313 * when an activity may be reached by a path not passing through a canonical parent
7314 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007315 *
7316 * <p>This method should be used when performing up navigation from within the same task
7317 * as the destination. If up navigation should cross tasks in some cases, see
7318 * {@link #shouldUpRecreateTask(Intent)}.</p>
7319 *
7320 * @param upIntent An intent representing the target destination for up navigation
7321 *
7322 * @return true if up navigation successfully reached the activity indicated by upIntent and
7323 * upIntent was delivered to it. false if an instance of the indicated activity could
7324 * not be found and this activity was simply finished normally.
7325 */
7326 public boolean navigateUpTo(Intent upIntent) {
7327 if (mParent == null) {
7328 ComponentName destInfo = upIntent.getComponent();
7329 if (destInfo == null) {
7330 destInfo = upIntent.resolveActivity(getPackageManager());
7331 if (destInfo == null) {
7332 return false;
7333 }
7334 upIntent = new Intent(upIntent);
7335 upIntent.setComponent(destInfo);
7336 }
7337 int resultCode;
7338 Intent resultData;
7339 synchronized (this) {
7340 resultCode = mResultCode;
7341 resultData = mResultData;
7342 }
7343 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007344 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007345 }
7346 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007347 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007348 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007349 resultCode, resultData);
7350 } catch (RemoteException e) {
7351 return false;
7352 }
7353 } else {
7354 return mParent.navigateUpToFromChild(this, upIntent);
7355 }
7356 }
7357
7358 /**
7359 * This is called when a child activity of this one calls its
7360 * {@link #navigateUpTo} method. The default implementation simply calls
7361 * navigateUpTo(upIntent) on this activity (the parent).
7362 *
7363 * @param child The activity making the call.
7364 * @param upIntent An intent representing the target destination for up navigation
7365 *
7366 * @return true if up navigation successfully reached the activity indicated by upIntent and
7367 * upIntent was delivered to it. false if an instance of the indicated activity could
7368 * not be found and this activity was simply finished normally.
7369 */
7370 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7371 return navigateUpTo(upIntent);
7372 }
7373
7374 /**
7375 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7376 * this activity's logical parent. The logical parent is named in the application's manifest
7377 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007378 * Activity subclasses may override this method to modify the Intent returned by
7379 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7380 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007381 *
Adam Powell04d58112012-04-09 10:22:12 -07007382 * @return a new Intent targeting the defined parent of this activity or null if
7383 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007384 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007385 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007386 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007387 final String parentName = mActivityInfo.parentActivityName;
7388 if (TextUtils.isEmpty(parentName)) {
7389 return null;
7390 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007391
7392 // If the parent itself has no parent, generate a main activity intent.
7393 final ComponentName target = new ComponentName(this, parentName);
7394 try {
7395 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7396 final String parentActivity = parentInfo.parentActivityName;
7397 final Intent parentIntent = parentActivity == null
7398 ? Intent.makeMainActivity(target)
7399 : new Intent().setComponent(target);
7400 return parentIntent;
7401 } catch (NameNotFoundException e) {
7402 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7403 "' in manifest");
7404 return null;
7405 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007406 }
7407
George Mount31a21722014-03-24 17:44:36 -07007408 /**
George Mount62ab9b72014-05-02 13:51:17 -07007409 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007410 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007411 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007412 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007413 *
George Mount65580562014-08-29 08:15:48 -07007414 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007415 */
George Mount65580562014-08-29 08:15:48 -07007416 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7417 if (callback == null) {
7418 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007419 }
George Mount65580562014-08-29 08:15:48 -07007420 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007421 }
7422
7423 /**
7424 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007425 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007426 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7427 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007428 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007429 *
George Mount65580562014-08-29 08:15:48 -07007430 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007431 */
George Mount65580562014-08-29 08:15:48 -07007432 public void setExitSharedElementCallback(SharedElementCallback callback) {
7433 if (callback == null) {
7434 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007435 }
George Mount65580562014-08-29 08:15:48 -07007436 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007437 }
7438
George Mount8c2614c2014-06-10 11:12:01 -07007439 /**
7440 * Postpone the entering activity transition when Activity was started with
7441 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7442 * android.util.Pair[])}.
7443 * <p>This method gives the Activity the ability to delay starting the entering and
7444 * shared element transitions until all data is loaded. Until then, the Activity won't
7445 * draw into its window, leaving the window transparent. This may also cause the
7446 * returning animation to be delayed until data is ready. This method should be
7447 * called in {@link #onCreate(android.os.Bundle)} or in
7448 * {@link #onActivityReenter(int, android.content.Intent)}.
7449 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7450 * start the transitions. If the Activity did not use
7451 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7452 * android.util.Pair[])}, then this method does nothing.</p>
7453 */
7454 public void postponeEnterTransition() {
7455 mActivityTransitionState.postponeEnterTransition();
7456 }
7457
7458 /**
7459 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7460 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7461 * to have your Activity start drawing.
7462 */
7463 public void startPostponedEnterTransition() {
7464 mActivityTransitionState.startPostponedEnterTransition();
7465 }
7466
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007467 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007468 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7469 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007470 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007471 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7472 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007473 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007474 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7475 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7476 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7477 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007478 }
7479 return null;
7480 }
7481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007483
Mathew Inwood8c854f82018-09-14 12:35:36 +01007484 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 final void setParent(Activity parent) {
7486 mParent = parent;
7487 }
7488
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007489 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007490 final void attach(Context context, ActivityThread aThread,
7491 Instrumentation instr, IBinder token, int ident,
7492 Application application, Intent intent, ActivityInfo info,
7493 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007494 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007495 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07007496 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 attachBaseContext(context);
7498
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007499 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007500
Andrii Kulian51c1b672017-04-07 18:39:32 -07007501 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007502 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007504 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007505 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7507 mWindow.setSoftInputMode(info.softInputMode);
7508 }
Adam Powell269248d2011-08-02 10:26:54 -07007509 if (info.uiOptions != 0) {
7510 mWindow.setUiOptions(info.uiOptions);
7511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 mMainThread = aThread;
7515 mInstrumentation = instr;
7516 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007517 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 mApplication = application;
7519 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007520 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 mComponent = intent.getComponent();
7522 mActivityInfo = info;
7523 mTitle = title;
7524 mParent = parent;
7525 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007526 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007527 if (voiceInteractor != null) {
7528 if (lastNonConfigurationInstances != null) {
7529 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7530 } else {
7531 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7532 Looper.myLooper());
7533 }
7534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535
Jeff Brown98365d72012-08-19 20:30:52 -07007536 mWindow.setWindowManager(
7537 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7538 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007539 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540 if (mParent != null) {
7541 mWindow.setContainer(mParent.getWindow());
7542 }
7543 mWindowManager = mWindow.getWindowManager();
7544 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007545
7546 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007547
7548 setAutofillCompatibilityEnabled(application.isAutofillCompatibilityEnabled());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007549 }
7550
7551 private void enableAutofillCompatibilityIfNeeded() {
7552 if (isAutofillCompatibilityEnabled()) {
7553 final AutofillManager afm = getSystemService(AutofillManager.class);
7554 if (afm != null) {
7555 afm.enableCompatibilityMode();
7556 }
7557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 }
7559
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007560 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007561 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007562 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 return mParent != null ? mParent.getActivityToken() : mToken;
7564 }
7565
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007566 /** @hide */
7567 @VisibleForTesting
7568 public final ActivityThread getActivityThread() {
7569 return mMainThread;
7570 }
7571
Winson Chung298f95b2017-08-10 15:57:18 -07007572 final void performCreate(Bundle icicle) {
7573 performCreate(icicle, null);
7574 }
7575
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007576 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007577 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007578 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007579 mCanEnterPictureInPicture = true;
7580 restoreHasCurrentPermissionRequest(icicle);
7581 if (persistentState != null) {
7582 onCreate(icicle, persistentState);
7583 } else {
7584 onCreate(icicle);
7585 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007586 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007587 mActivityTransitionState.readState(icicle);
7588
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007589 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7590 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007591 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007592 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007593 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007594 }
George Mount0a778ed2013-12-13 13:35:36 -08007595
Ian Lake0b71b8e2018-09-13 13:56:26 -07007596 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07007597 mCanEnterPictureInPicture = true;
7598 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007599 }
7600
Andrii Kuliand25680c2018-02-21 15:16:58 -08007601 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007602 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07007603 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007604 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007606 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007608 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 if (!mCalled) {
7611 throw new SuperNotCalledException(
7612 "Activity " + mComponent.toShortString() +
7613 " did not call through to super.onStart()");
7614 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007615 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007616 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007617
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007618 boolean isAppDebuggable =
7619 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
7620
David Brazdil795cbfb2018-01-26 12:23:15 +00007621 // This property is set for all non-user builds except final release
David Brazdil927d6de2018-01-24 19:54:30 +00007622 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
7623
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007624 if (isAppDebuggable || isDlwarningEnabled) {
7625 String dlwarning = getDlWarning();
7626 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007627 String appName = getApplicationInfo().loadLabel(getPackageManager())
7628 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007629 String warning = "Detected problems with app native libraries\n" +
7630 "(please consult log for detail):\n" + dlwarning;
7631 if (isAppDebuggable) {
7632 new AlertDialog.Builder(this).
7633 setTitle(appName).
7634 setMessage(warning).
7635 setPositiveButton(android.R.string.ok, null).
7636 setCancelable(false).
7637 show();
7638 } else {
7639 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7640 }
7641 }
7642 }
7643
George Mount62ab9b72014-05-02 13:51:17 -07007644 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07007645 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 }
RoboErik55011652014-07-09 15:05:53 -07007647
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007648 /**
7649 * Restart the activity.
7650 * @param start Indicates whether the activity should also be started after restart.
7651 * The option to not start immediately is needed in case a transaction with
7652 * multiple lifecycle transitions is in progress.
7653 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007654 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007655 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007656 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007657
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007658 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007659 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007660 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7661 }
7662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 if (mStopped) {
7664 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007665
7666 synchronized (mManagedCursors) {
7667 final int N = mManagedCursors.size();
7668 for (int i=0; i<N; i++) {
7669 ManagedCursor mc = mManagedCursors.get(i);
7670 if (mc.mReleased || mc.mUpdated) {
7671 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007672 if (getApplicationInfo().targetSdkVersion
7673 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7674 throw new IllegalStateException(
7675 "trying to requery an already closed cursor "
7676 + mc.mCursor);
7677 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007678 }
7679 mc.mReleased = false;
7680 mc.mUpdated = false;
7681 }
7682 }
7683 }
7684
7685 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007687 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688 if (!mCalled) {
7689 throw new SuperNotCalledException(
7690 "Activity " + mComponent.toShortString() +
7691 " did not call through to super.onRestart()");
7692 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007693 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007694 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 }
7697 }
RoboErik55011652014-07-09 15:05:53 -07007698
Andrii Kuliand25680c2018-02-21 15:16:58 -08007699 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007700 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007701 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007702
Dianne Hackborn445646c2010-06-25 15:52:59 -07007703 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007704
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007705 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007706
Dake Gu67decfa2017-12-27 11:48:08 -08007707 if (mAutoFillResetNeeded) {
7708 // When Activity is destroyed in paused state, and relaunch activity, there will be
7709 // extra onResume and onPause event, ignore the first onResume and onPause.
7710 // see ActivityThread.handleRelaunchActivity()
7711 mAutoFillIgnoreFirstResumePause = followedByPause;
7712 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7713 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7714 }
7715 }
7716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007717 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007718 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007720 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 if (!mCalled) {
7722 throw new SuperNotCalledException(
7723 "Activity " + mComponent.toShortString() +
7724 " did not call through to super.onResume()");
7725 }
7726
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007727 // invisible activities must be finished before onResume() completes
7728 if (!mVisibleFromClient && !mFinished) {
7729 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7730 if (getApplicationInfo().targetSdkVersion
7731 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7732 throw new IllegalStateException(
7733 "Activity " + mComponent.toShortString() +
7734 " did not call finish() prior to onResume() completing");
7735 }
7736 }
7737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007740
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007741 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007742 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 onPostResume();
7745 if (!mCalled) {
7746 throw new SuperNotCalledException(
7747 "Activity " + mComponent.toShortString() +
7748 " did not call through to super.onPostResume()");
7749 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007750 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 }
7752
7753 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007754 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007755 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007756 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007757 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007759 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007760 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007761 if (!mCalled && getApplicationInfo().targetSdkVersion
7762 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7763 throw new SuperNotCalledException(
7764 "Activity " + mComponent.toShortString() +
7765 " did not call through to super.onPause()");
7766 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007767 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 }
RoboErik55011652014-07-09 15:05:53 -07007769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 final void performUserLeaving() {
7771 onUserInteraction();
7772 onUserLeaveHint();
7773 }
RoboErik55011652014-07-09 15:05:53 -07007774
Andrii Kuliand25680c2018-02-21 15:16:58 -08007775 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007776 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007777 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007778
Winson Chung298f95b2017-08-10 15:57:18 -07007779 // Disallow entering picture-in-picture after the activity has been stopped
7780 mCanEnterPictureInPicture = false;
7781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007783 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784 if (mWindow != null) {
7785 mWindow.closeAllPanels();
7786 }
7787
Chong Zhang7687f252016-02-26 12:03:33 -08007788 // If we're preserving the window, don't setStoppedState to true, since we
7789 // need the window started immediately again. Stopping the window will
7790 // destroys hardware resources and causes flicker.
7791 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07007792 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08007793 }
RoboErik55011652014-07-09 15:05:53 -07007794
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007795 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07007796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 mCalled = false;
7798 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007799 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 if (!mCalled) {
7801 throw new SuperNotCalledException(
7802 "Activity " + mComponent.toShortString() +
7803 " did not call through to super.onStop()");
7804 }
RoboErik55011652014-07-09 15:05:53 -07007805
Makoto Onuki2f6a0182010-02-22 13:26:59 -08007806 synchronized (mManagedCursors) {
7807 final int N = mManagedCursors.size();
7808 for (int i=0; i<N; i++) {
7809 ManagedCursor mc = mManagedCursors.get(i);
7810 if (!mc.mReleased) {
7811 mc.mCursor.deactivate();
7812 mc.mReleased = true;
7813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007814 }
7815 }
George Mount0a778ed2013-12-13 13:35:36 -08007816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07007818 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 }
7820 mResumed = false;
7821 }
7822
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007823 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007824 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07007825 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07007826 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007827 mFragments.dispatchDestroy();
7828 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007829 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007830 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07007831 if (mVoiceInteractor != null) {
7832 mVoiceInteractor.detachActivity();
7833 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007834 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007835 }
George Mount0a778ed2013-12-13 13:35:36 -08007836
Winson Chung5af42fc2017-03-24 17:11:33 -07007837 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
7838 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007839 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007840 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
7841 + " " + newConfig);
7842 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007843 if (mWindow != null) {
7844 mWindow.onMultiWindowModeChanged();
7845 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007846 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007847 }
7848
Winson Chung5af42fc2017-03-24 17:11:33 -07007849 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
7850 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007851 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007852 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
7853 + " " + newConfig);
7854 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07007855 if (mWindow != null) {
7856 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
7857 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007858 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007859 }
7860
George Mount0a778ed2013-12-13 13:35:36 -08007861 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06007862 * @hide
7863 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007864 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06007865 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 return mResumed;
7867 }
7868
Svetoslavffb32b12015-10-15 16:54:00 -07007869 private void storeHasCurrentPermissionRequest(Bundle bundle) {
7870 if (bundle != null && mHasCurrentPermissionsRequest) {
7871 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
7872 }
7873 }
7874
7875 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
7876 if (bundle != null) {
7877 mHasCurrentPermissionsRequest = bundle.getBoolean(
7878 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
7879 }
7880 }
7881
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007882 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07007883 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
7884 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07007885 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
7887 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007888 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007890 onActivityResult(requestCode, resultCode, data);
7891 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
7892 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
7893 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08007894 dispatchRequestPermissionsResult(requestCode, data);
7895 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00007896 Fragment frag = mFragments.findFragmentByWho(who);
7897 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007898 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08007899 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07007900 }
Svetoslav970b59c2015-06-09 16:05:21 -07007901 } else if (who.startsWith("@android:view:")) {
7902 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
7903 getActivityToken());
7904 for (ViewRootImpl viewRoot : views) {
7905 if (viewRoot.getView() != null
7906 && viewRoot.getView().dispatchActivityResult(
7907 who, requestCode, resultCode, data)) {
7908 return;
7909 }
7910 }
Svet Ganov782043c2017-02-11 00:52:02 +00007911 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08007912 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08007913 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07007914 } else {
7915 Fragment frag = mFragments.findFragmentByWho(who);
7916 if (frag != null) {
7917 frag.onActivityResult(requestCode, resultCode, data);
7918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007919 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07007920 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007922
Jason Monka57e5e02014-05-07 10:06:48 -04007923 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01007924 * Request to put this activity in a mode where the user is locked to a restricted set of
7925 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04007926 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007927 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
7928 * for this component, the current task will be launched directly into LockTask mode. Only apps
7929 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
7930 * be launched while LockTask mode is active. The user will not be able to leave this mode
7931 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
7932 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04007933 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007934 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
7935 * system will prompt the user with a dialog requesting permission to use this mode.
7936 * The user can exit at any time through instructions shown on the request dialog. Calling
7937 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04007938 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007939 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
7940 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07007941 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007942 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07007943 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04007944 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007945 public void startLockTask() {
7946 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007947 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007948 } catch (RemoteException e) {
7949 }
7950 }
7951
Jason Monka57e5e02014-05-07 10:06:48 -04007952 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01007953 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04007954 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007955 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
7956 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04007957 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007958 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
7959 * by this activity, then calling this method will not terminate the LockTask mode, but only
7960 * finish its own task. The device will remain in LockTask mode, until the activity which
7961 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
7962 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07007963 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007964 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07007965 * @see android.R.attr#lockTaskMode
7966 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04007967 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007968 public void stopLockTask() {
7969 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007970 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007971 } catch (RemoteException e) {
7972 }
7973 }
7974
Craig Mautner5eda9b32013-07-02 11:58:16 -07007975 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007976 * Shows the user the system defined message for telling the user how to exit
7977 * lock task mode. The task containing this activity must be in lock task mode at the time
7978 * of this call for the message to be displayed.
7979 */
7980 public void showLockTaskEscapeMessage() {
7981 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007982 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007983 } catch (RemoteException e) {
7984 }
7985 }
7986
7987 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07007988 * Check whether the caption on freeform windows is displayed directly on the content.
7989 *
7990 * @return True if caption is displayed on content, false if it pushes the content down.
7991 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08007992 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07007993 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07007994 */
7995 public boolean isOverlayWithDecorCaptionEnabled() {
7996 return mWindow.isOverlayWithDecorCaptionEnabled();
7997 }
7998
7999 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008000 * Set whether the caption should displayed directly on the content rather than push it down.
8001 *
8002 * This affects only freeform windows since they display the caption and only the main
8003 * window of the activity. The caption is used to drag the window around and also shows
8004 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008005 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008006 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008007 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8008 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008009 }
8010
8011 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008012 * Interface for informing a translucent {@link Activity} once all visible activities below it
8013 * have completed drawing. This is necessary only after an {@link Activity} has been made
8014 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8015 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008016 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8017 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008018 *
8019 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008020 */
Jose Lima14914852014-08-14 09:14:12 -07008021 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008022 public interface TranslucentConversionListener {
8023 /**
8024 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8025 * below the top one have been redrawn. Following this callback it is safe to make the top
8026 * Activity translucent because the underlying Activity has been drawn.
8027 *
8028 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8029 * occurred waiting for the Activity to complete drawing.
8030 *
8031 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008032 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008033 */
8034 public void onTranslucentConversionComplete(boolean drawComplete);
8035 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008036
8037 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008038 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008039 // If the package installer crashed we may have not data - best effort.
8040 String[] permissions = (data != null) ? data.getStringArrayExtra(
8041 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8042 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8043 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008044 onRequestPermissionsResult(requestCode, permissions, grantResults);
8045 }
8046
8047 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008048 Fragment fragment) {
8049 // If the package installer crashed we may have not data - best effort.
8050 String[] permissions = (data != null) ? data.getStringArrayExtra(
8051 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8052 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8053 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8054 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008055 }
8056
Svet Ganov782043c2017-02-11 00:52:02 +00008057 /** @hide */
8058 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008059 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008060 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00008061 try {
8062 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008063 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008064 } catch (IntentSender.SendIntentException e) {
8065 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8066 }
8067 }
8068
Svet Ganov17db9dc2017-02-21 19:54:31 -08008069 /** @hide */
8070 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008071 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008072 mAutoFillResetNeeded = true;
8073 }
8074
Felipe Leme4753bb02017-03-22 20:24:00 -07008075 /** @hide */
8076 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008077 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008078 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008079 final boolean wasShowing;
8080
8081 if (mAutofillPopupWindow == null) {
8082 wasShowing = false;
8083 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8084 } else {
8085 wasShowing = mAutofillPopupWindow.isShowing();
8086 }
Svet Ganov374cae12017-05-10 13:42:33 -07008087 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008088
8089 return !wasShowing && mAutofillPopupWindow.isShowing();
8090 }
8091
8092 /** @hide */
8093 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008094 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8095 @NonNull KeyEvent keyEvent) {
8096 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8097 if (rootImpl != null) {
8098 // dont care if anchorView is current focus, for example a custom view may only receive
8099 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8100 // might be inside parent of the custom view.
8101 rootImpl.dispatchKeyFromAutofill(keyEvent);
8102 }
8103 }
8104
8105 /** @hide */
8106 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008107 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008108 if (mAutofillPopupWindow == null) {
8109 return false;
8110 }
8111 mAutofillPopupWindow.dismiss();
8112 mAutofillPopupWindow = null;
8113 return true;
8114 }
8115
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008116 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008117 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008118 public final boolean autofillClientIsFillUiShowing() {
8119 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8120 }
8121
8122 /** @hide */
8123 @Override
8124 @NonNull
8125 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8126 @NonNull AutofillId[] autofillId) {
8127 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008128 final ArrayList<ViewRootImpl> roots =
8129 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8130
8131 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8132 final View rootView = roots.get(rootNum).getView();
8133
8134 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008135 final int viewCount = autofillId.length;
8136 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008137 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008138 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008139 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008140 }
8141 }
8142 }
8143 }
8144
8145 return views;
8146 }
8147
8148 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008149 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008150 @Nullable
8151 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008152 final ArrayList<ViewRootImpl> roots =
8153 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8154 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8155 final View rootView = roots.get(rootNum).getView();
8156
8157 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008158 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008159 if (view != null) {
8160 return view;
8161 }
8162 }
8163 }
8164
8165 return null;
8166 }
8167
8168 /** @hide */
8169 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008170 public final @NonNull boolean[] autofillClientGetViewVisibility(
8171 @NonNull AutofillId[] autofillIds) {
8172 final int autofillIdCount = autofillIds.length;
8173 final boolean[] visible = new boolean[autofillIdCount];
8174 for (int i = 0; i < autofillIdCount; i++) {
8175 final AutofillId autofillId = autofillIds[i];
8176 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8177 if (view != null) {
8178 if (!autofillId.isVirtual()) {
8179 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008180 } else {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008181 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008182 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008183 }
8184 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008185 if (android.view.autofill.Helper.sVerbose) {
8186 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8187 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008188 return visible;
8189 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008190
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008191 /** @hide */
8192 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8193 int windowId) {
8194 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8195 .getRootViews(getActivityToken());
8196 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8197 final View rootView = roots.get(rootNum).getView();
8198 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8199 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8200 if (view != null) {
8201 return view;
8202 }
8203 }
8204 }
8205 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008206 }
8207
8208 /** @hide */
8209 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008210 public final @Nullable IBinder autofillClientGetActivityToken() {
8211 return getActivityToken();
8212 }
8213
8214 /** @hide */
8215 @Override
8216 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008217 return !mStopped;
8218 }
8219
Dake Gu67decfa2017-12-27 11:48:08 -08008220 /** @hide */
8221 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008222 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008223 return isAutofillCompatibilityEnabled();
8224 }
8225
8226 /** @hide */
8227 @Override
8228 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008229 return mAutoFillIgnoreFirstResumePause || !mResumed;
8230 }
8231
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008232 /**
8233 * If set to true, this indicates to the system that it should never take a
8234 * screenshot of the activity to be used as a representation while it is not in a started state.
8235 * <p>
8236 * Note that the system may use the window background of the theme instead to represent
8237 * the window when it is not running.
8238 * <p>
8239 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8240 * this only affects the behavior when the activity's screenshot would be used as a
8241 * representation when the activity is not in a started state, i.e. in Overview. The system may
8242 * still take screenshots of the activity in other contexts; for example, when the user takes a
8243 * screenshot of the entire screen, or when the active
8244 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8245 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8246 *
8247 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8248 * @hide
8249 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008250 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008251 public void setDisablePreviewScreenshots(boolean disable) {
8252 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008253 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008254 } catch (RemoteException e) {
8255 Log.e(TAG, "Failed to call setDisablePreviewScreenshots", e);
8256 }
8257 }
8258
chaviw59b98852017-06-13 12:05:44 -07008259 /**
koprivaa1a78482018-10-09 10:09:23 -07008260 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008261 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8262 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8263 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8264 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8265 *
8266 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8267 * {@code false} otherwise.
8268 * @see #setTurnScreenOn(boolean)
8269 * @see android.R.attr#turnScreenOn
8270 * @see android.R.attr#showWhenLocked
8271 */
8272 public void setShowWhenLocked(boolean showWhenLocked) {
8273 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008274 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008275 } catch (RemoteException e) {
8276 Log.e(TAG, "Failed to call setShowWhenLocked", e);
8277 }
8278 }
8279
8280 /**
8281 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8282 * Normally an activity will be transitioned to the stopped state if it is started while the
8283 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8284 * activity will be visible and resumed due to the screen coming on. The screen will not be
8285 * turned on if the activity won't be visible after the screen is turned on. This flag is
8286 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8287 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8288 * if this flag is set and the activity calls {@link
8289 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8290 * the screen will turn on.
8291 *
8292 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8293 *
8294 * @see #setShowWhenLocked(boolean)
8295 * @see android.R.attr#turnScreenOn
8296 * @see android.R.attr#showWhenLocked
8297 */
8298 public void setTurnScreenOn(boolean turnScreenOn) {
8299 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008300 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008301 } catch (RemoteException e) {
8302 Log.e(TAG, "Failed to call setTurnScreenOn", e);
8303 }
8304 }
8305
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008306 /**
8307 * Registers remote animations per transition type for this activity.
8308 *
8309 * @param definition The remote animation definition that defines which transition whould run
8310 * which remote animation.
8311 * @hide
8312 */
8313 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008314 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008315 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8316 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008317 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008318 } catch (RemoteException e) {
8319 Log.e(TAG, "Failed to call registerRemoteAnimations", e);
8320 }
8321 }
8322
Andrii Kuliand25680c2018-02-21 15:16:58 -08008323 /** Log a lifecycle event for current user id and component class. */
8324 private void writeEventLog(int event, String reason) {
8325 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
8326 reason);
8327 }
8328
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008329 class HostCallbacks extends FragmentHostCallback<Activity> {
8330 public HostCallbacks() {
8331 super(Activity.this /*activity*/);
8332 }
8333
8334 @Override
8335 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8336 Activity.this.dump(prefix, fd, writer, args);
8337 }
8338
8339 @Override
8340 public boolean onShouldSaveFragmentState(Fragment fragment) {
8341 return !isFinishing();
8342 }
8343
8344 @Override
8345 public LayoutInflater onGetLayoutInflater() {
8346 final LayoutInflater result = Activity.this.getLayoutInflater();
8347 if (onUseFragmentManagerInflaterFactory()) {
8348 return result.cloneInContext(Activity.this);
8349 }
8350 return result;
8351 }
8352
8353 @Override
8354 public boolean onUseFragmentManagerInflaterFactory() {
8355 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8356 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8357 }
8358
8359 @Override
8360 public Activity onGetHost() {
8361 return Activity.this;
8362 }
8363
8364 @Override
8365 public void onInvalidateOptionsMenu() {
8366 Activity.this.invalidateOptionsMenu();
8367 }
8368
8369 @Override
8370 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8371 Bundle options) {
8372 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8373 }
8374
8375 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008376 public void onStartActivityAsUserFromFragment(
8377 Fragment fragment, Intent intent, int requestCode, Bundle options,
8378 UserHandle user) {
8379 Activity.this.startActivityAsUserFromFragment(
8380 fragment, intent, requestCode, options, user);
8381 }
8382
8383 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008384 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8385 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8386 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8387 if (mParent == null) {
8388 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8389 flagsMask, flagsValues, options);
8390 } else if (options != null) {
8391 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
8392 fillInIntent, flagsMask, flagsValues, extraFlags, options);
8393 }
8394 }
8395
8396 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008397 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8398 int requestCode) {
8399 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8400 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8401 startActivityForResult(who, intent, requestCode, null);
8402 }
8403
8404 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008405 public boolean onHasWindowAnimations() {
8406 return getWindow() != null;
8407 }
8408
8409 @Override
8410 public int onGetWindowAnimations() {
8411 final Window w = getWindow();
8412 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8413 }
8414
Todd Kennedy434bd652015-05-04 12:29:50 -07008415 @Override
8416 public void onAttachFragment(Fragment fragment) {
8417 Activity.this.onAttachFragment(fragment);
8418 }
8419
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008420 @Nullable
8421 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008422 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008423 return Activity.this.findViewById(id);
8424 }
8425
8426 @Override
8427 public boolean onHasView() {
8428 final Window w = getWindow();
8429 return (w != null && w.peekDecorView() != null);
8430 }
8431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008432}