blob: bf7b2f8b7be4791d8b8a2bfb05e8bc2012b3625c [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 Leme0c6b23d2019-01-15 17:02:49 -080020import static android.os.Process.myUid;
Felipe Leme5b32ebe2018-02-15 12:52:19 -080021
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070022import static java.lang.Character.MIN_VALUE;
Winson Chung1af8eda2016-02-05 17:55:56 +000023
Tor Norbyec615c6f2015-03-02 10:11:44 -080024import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070025import android.annotation.DrawableRes;
26import android.annotation.IdRes;
27import android.annotation.IntDef;
28import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070029import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070030import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070031import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070032import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070033import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070034import android.annotation.SystemApi;
Tiger Huangfc218452018-09-27 00:38:50 +080035import android.annotation.TestApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010036import android.annotation.UnsupportedAppUsage;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070037import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040038import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070039import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070040import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050044import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070045import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070046import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070047import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.SharedPreferences;
49import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080050import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070051import android.content.pm.PackageManager;
52import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.res.Configuration;
54import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070055import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.database.Cursor;
57import android.graphics.Bitmap;
58import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080059import android.graphics.Color;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070060import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.graphics.drawable.Drawable;
62import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070063import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070065import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070066import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Bundle;
Sunny Goyald40c3452019-03-20 12:46:55 -070068import android.os.CancellationSignal;
Tim Van Pattenddc43912018-12-18 17:47:52 -070069import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080072import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070073import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000074import android.os.PersistableBundle;
Nicholas Sauer28348ed742019-02-15 11:58:32 -080075import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060077import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080078import android.os.StrictMode;
Felipe Lemeb337e1c2019-01-18 13:06:17 -080079import android.os.Trace;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070080import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.text.Selection;
82import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070083import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000085import android.transition.Scene;
86import android.transition.TransitionManager;
87import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.util.EventLog;
90import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070091import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070092import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000094import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070095import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070097import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070099import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +0000100import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +0100102import android.view.KeyboardShortcutGroup;
103import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.LayoutInflater;
105import android.view.Menu;
106import android.view.MenuInflater;
107import android.view.MenuItem;
108import android.view.MotionEvent;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100109import android.view.RemoteAnimationDefinition;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700110import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700112import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700114import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000116import android.view.ViewRootImpl;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700117import android.view.ViewRootImpl.ActivityConfigCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000119import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700121import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700122import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov24c90452017-12-27 15:17:14 -0800123import android.view.autofill.AutofillId;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700124import android.view.autofill.AutofillManager;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700125import android.view.autofill.AutofillManager.AutofillClient;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700126import android.view.autofill.AutofillPopupWindow;
127import android.view.autofill.IAutofillWindowPresenter;
Felipe Leme749b8892018-12-03 16:30:30 -0800128import android.view.contentcapture.ContentCaptureManager;
Felipe Lemecbf7f262019-04-17 13:57:59 -0700129import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800131import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000132import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100133
Adrian Roos4c864592019-04-10 14:47:57 +0200134import com.android.internal.R;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700135import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800136import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700137import com.android.internal.app.IVoiceInteractor;
138import com.android.internal.app.ToolbarActionBar;
139import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700140import com.android.internal.policy.PhoneWindow;
141
Mathieu Chartier63e14c12019-03-07 09:05:44 -0800142import dalvik.system.VMRuntime;
143
Dianne Hackborn625ac272010-09-17 18:29:22 -0700144import java.io.FileDescriptor;
145import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700146import java.lang.annotation.Retention;
147import java.lang.annotation.RetentionPolicy;
Mark Renouf1c25fc52019-09-09 13:13:43 -0400148import java.lang.ref.WeakReference;
Adam Powell6e346362010-07-23 10:18:23 -0700149import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800150import java.util.Arrays;
Sunny Goyald40c3452019-03-20 12:46:55 -0700151import java.util.Collections;
Adam Powell6e346362010-07-23 10:18:23 -0700152import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700153import java.util.List;
Sunny Goyald40c3452019-03-20 12:46:55 -0700154import java.util.concurrent.Executor;
155import java.util.function.Consumer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Bryce Leee83f34cd2017-10-31 19:50:54 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158/**
159 * An activity is a single, focused thing that the user can do. Almost all
160 * activities interact with the user, so the Activity class takes care of
161 * creating a window for you in which you can place your UI with
162 * {@link #setContentView}. While activities are often presented to the user
163 * as full-screen windows, they can also be used in other ways: as floating
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700164 * windows (via a theme with {@link android.R.attr#windowIsFloating} set),
165 * <a href="https://developer.android.com/guide/topics/ui/multi-window">
166 * Multi-Window mode</a> or embedded into other windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 *
168 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * <ul>
171 * <li> {@link #onCreate} is where you initialize your activity. Most
172 * importantly, here you will usually call {@link #setContentView(int)}
173 * with a layout resource defining your UI, and using {@link #findViewById}
174 * to retrieve the widgets in that UI that you need to interact with
175 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700176 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700177 * <li> {@link #onPause} is where you deal with the user pausing active
178 * interaction with the activity. Any changes made by the user should at
179 * this point be committed (usually to the
180 * {@link android.content.ContentProvider} holding the data). In this
181 * state the activity is still visible on screen.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * </ul>
183 *
184 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
185 * activity classes must have a corresponding
186 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
187 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <p>Topics covered here:
190 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700191 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
193 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
194 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
195 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
196 * <li><a href="#Permissions">Permissions</a>
197 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
198 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700199 *
200 * <div class="special reference">
201 * <h3>Developer Guides</h3>
202 * <p>The Activity class is an important part of an application's overall lifecycle,
203 * and the way activities are launched and put together is a fundamental
204 * part of the platform's application model. For a detailed perspective on the structure of an
205 * Android application and how activities behave, please read the
206 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700207 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700208 * developer guides.</p>
209 *
210 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700211 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700212 * developer guide.</p>
213 * </div>
214 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700215 * <a name="Fragments"></a>
216 * <h3>Fragments</h3>
217 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800218 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
219 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700220 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800221 * screens, and help scale their application between small and large screens.</p>
222 *
223 * <p>For more information about using fragments, read the
224 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * <a name="ActivityLifecycle"></a>
227 * <h3>Activity Lifecycle</h3>
228 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700229 * <p>Activities in the system are managed as
230 * <a href="https://developer.android.com/guide/components/activities/tasks-and-back-stack">
231 * activity stacks</a>. When a new activity is started, it is usually placed on the top of the
232 * current stack and becomes the running activity -- the previous activity always remains
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 * below it in the stack, and will not come to the foreground again until
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700234 * the new activity exits. There can be one or multiple activity stacks visible
235 * on screen.</p>
RoboErik55011652014-07-09 15:05:53 -0700236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <p>An activity has essentially four states:</p>
238 * <ul>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700239 * <li>If an activity is in the foreground of the screen (at the highest position of the topmost
240 * stack), it is <em>active</em> or <em>running</em>. This is usually the activity that the
241 * user is currently interacting with.</li>
242 * <li>If an activity has lost focus but is still presented to the user, it is <em>visible</em>.
243 * It is possible if a new non-full-sized or transparent activity has focus on top of your
244 * activity, another activity has higher position in multi-window mode, or the activity
245 * itself is not focusable in current windowing mode. Such activity is completely alive (it
246 * maintains all state and member information and remains attached to the window manager).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <li>If an activity is completely obscured by another activity,
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700248 * it is <em>stopped</em> or <em>hidden</em>. It still retains all state and member
249 * information, however, it is no longer visible to the user so its window is hidden
250 * and it will often be killed by the system when memory is needed elsewhere.</li>
251 * <li>The system can drop the activity from memory by either asking it to finish,
252 * or simply killing its process, making it <em>destroyed</em>. When it is displayed again
253 * to the user, it must be completely restarted and restored to its previous state.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * </ul>
255 *
256 * <p>The following diagram shows the important state paths of an Activity.
257 * The square rectangles represent callback methods you can implement to
258 * perform operations when the Activity moves between states. The colored
259 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700260 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * <p><img src="../../../images/activity_lifecycle.png"
262 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * <p>There are three key loops you may be interested in monitoring within your
265 * activity:
RoboErik55011652014-07-09 15:05:53 -0700266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * <ul>
268 * <li>The <b>entire lifetime</b> of an activity happens between the first call
269 * to {@link android.app.Activity#onCreate} through to a single final call
270 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
271 * of "global" state in onCreate(), and release all remaining resources in
272 * onDestroy(). For example, if it has a thread running in the background
273 * to download data from the network, it may create that thread in onCreate()
274 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * <li>The <b>visible lifetime</b> of an activity happens between a call to
277 * {@link android.app.Activity#onStart} until a corresponding call to
278 * {@link android.app.Activity#onStop}. During this time the user can see the
279 * activity on-screen, though it may not be in the foreground and interacting
280 * with the user. Between these two methods you can maintain resources that
281 * are needed to show the activity to the user. For example, you can register
282 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900283 * that impact your UI, and unregister it in onStop() when the user no
284 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * can be called multiple times, as the activity becomes visible and hidden
286 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700287 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
289 * {@link android.app.Activity#onResume} until a corresponding call to
290 * {@link android.app.Activity#onPause}. During this time the activity is
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700291 * in visible, active and interacting with the user. An activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 * can frequently go between the resumed and paused states -- for example when
293 * the device goes to sleep, when an activity result is delivered, when a new
294 * intent is delivered -- so the code in these methods should be fairly
295 * lightweight.
296 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700297 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 * <p>The entire lifecycle of an activity is defined by the following
299 * Activity methods. All of these are hooks that you can override
300 * to do appropriate work when the activity changes state. All
301 * activities will implement {@link android.app.Activity#onCreate}
302 * to do their initial setup; many will also implement
303 * {@link android.app.Activity#onPause} to commit changes to data and
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700304 * prepare to pause interacting with the user, and {@link android.app.Activity#onStop}
305 * to handle no longer being visible on screen. You should always
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * call up to your superclass when implementing these methods.</p>
307 *
308 * </p>
309 * <pre class="prettyprint">
310 * public class Activity extends ApplicationContext {
311 * protected void onCreate(Bundle savedInstanceState);
312 *
313 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * protected void onRestart();
316 *
317 * protected void onResume();
318 *
319 * protected void onPause();
320 *
321 * protected void onStop();
322 *
323 * protected void onDestroy();
324 * }
325 * </pre>
326 *
327 * <p>In general the movement through an activity's lifecycle looks like
328 * this:</p>
329 *
330 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
331 * <colgroup align="left" span="3" />
332 * <colgroup align="left" />
333 * <colgroup align="center" />
334 * <colgroup align="center" />
335 *
336 * <thead>
337 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
338 * </thead>
339 *
340 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700341 * <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 -0800342 * <td>Called when the activity is first created.
343 * This is where you should do all of your normal static set up:
344 * create views, bind data to lists, etc. This method also
345 * provides you with a Bundle containing the activity's previously
346 * frozen state, if there was one.
347 * <p>Always followed by <code>onStart()</code>.</td>
348 * <td align="center">No</td>
349 * <td align="center"><code>onStart()</code></td>
350 * </tr>
351 *
352 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700353 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * <td>Called after your activity has been stopped, prior to it being
355 * started again.
356 * <p>Always followed by <code>onStart()</code></td>
357 * <td align="center">No</td>
358 * <td align="center"><code>onStart()</code></td>
359 * </tr>
360 *
smain@google.com10e29b82016-06-23 12:09:30 -0700361 * <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 -0800362 * <td>Called when the activity is becoming visible to the user.
363 * <p>Followed by <code>onResume()</code> if the activity comes
364 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
365 * <td align="center">No</td>
366 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
367 * </tr>
368 *
369 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700370 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * <td>Called when the activity will start
372 * interacting with the user. At this point your activity is at
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700373 * the top of its activity stack, with user input going to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * <p>Always followed by <code>onPause()</code>.</td>
375 * <td align="center">No</td>
376 * <td align="center"><code>onPause()</code></td>
377 * </tr>
378 *
smain@google.com10e29b82016-06-23 12:09:30 -0700379 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700380 * <td>Called when the activity loses foreground state, is no longer focusable or before
381 * transition to stopped/hidden or destroyed state. The activity is still visible to
382 * user, so it's recommended to keep it visually active and continue updating the UI.
383 * Implementations of this method must be very quick because
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * the next activity will not be resumed until this method returns.
385 * <p>Followed by either <code>onResume()</code> if the activity
386 * returns back to the front, or <code>onStop()</code> if it becomes
387 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800388 * <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 -0800389 * <td align="center"><code>onResume()</code> or<br>
390 * <code>onStop()</code></td>
391 * </tr>
392 *
smain@google.com10e29b82016-06-23 12:09:30 -0700393 * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</td>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700394 * <td>Called when the activity is no longer visible to the user. This may happen either
395 * because a new activity is being started on top, an existing one is being brought in
396 * front of this one, or this one is being destroyed. This is typically used to stop
397 * animations and refreshing the UI, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * <p>Followed by either <code>onRestart()</code> if
399 * this activity is coming back to interact with the user, or
400 * <code>onDestroy()</code> if this activity is going away.</td>
401 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
402 * <td align="center"><code>onRestart()</code> or<br>
403 * <code>onDestroy()</code></td>
404 * </tr>
405 *
smain@google.com10e29b82016-06-23 12:09:30 -0700406 * <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 -0800407 * <td>The final call you receive before your
408 * activity is destroyed. This can happen either because the
409 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700410 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * instance of the activity to save space. You can distinguish
412 * between these two scenarios with the {@link
413 * Activity#isFinishing} method.</td>
414 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
415 * <td align="center"><em>nothing</em></td>
416 * </tr>
417 * </tbody>
418 * </table>
419 *
420 * <p>Note the "Killable" column in the above table -- for those methods that
421 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800422 * 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 -0800423 * of its code being executed. Because of this, you should use the
424 * {@link #onPause} method to write any persistent data (such as user edits)
425 * to storage. In addition, the method
426 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
427 * in such a background state, allowing you to save away any dynamic instance
428 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700429 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
431 * section for more information on how the lifecycle of a process is tied
432 * to the activities it is hosting. Note that it is important to save
433 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800434 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 * be called in every situation as described in its documentation.</p>
436 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800437 * <p class="note">Be aware that these semantics will change slightly between
438 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
439 * vs. those targeting prior platforms. Starting with Honeycomb, an application
440 * is not in the killable state until its {@link #onStop} has returned. This
441 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700442 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800443 * wait until {@link #onStop()} to save persistent state.</p>
444 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800445 * <p class="note">For applications targeting platforms starting with
446 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
447 * will always be called after {@link #onStop}, so an application may safely
448 * perform fragment transactions in {@link #onStop} and will be able to save
449 * persistent state later.</p>
450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 * <p>For those methods that are not marked as being killable, the activity's
452 * process will not be killed by the system starting from the time the method
453 * is called and continuing after it returns. Thus an activity is in the killable
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700454 * state, for example, between after <code>onStop()</code> to the start of
455 * <code>onResume()</code>. Keep in mind that under extreme memory pressure the
456 * system can kill the application process at any time.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 *
458 * <a name="ConfigurationChanges"></a>
459 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 * <p>If the configuration of the device (as defined by the
462 * {@link Configuration Resources.Configuration} class) changes,
463 * then anything displaying a user interface will need to update to match that
464 * configuration. Because Activity is the primary mechanism for interacting
465 * with the user, it includes special support for handling configuration
466 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700467 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 * <p>Unless you specify otherwise, a configuration change (such as a change
469 * in screen orientation, language, input devices, etc) will cause your
470 * current activity to be <em>destroyed</em>, going through the normal activity
471 * lifecycle process of {@link #onPause},
472 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
473 * had been in the foreground or visible to the user, once {@link #onDestroy} is
474 * called in that instance then a new instance of the activity will be
475 * created, with whatever savedInstanceState the previous instance had generated
476 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 * <p>This is done because any application resource,
479 * including layout files, can change based on any configuration value. Thus
480 * the only safe way to handle a configuration change is to re-retrieve all
481 * resources, including layouts, drawables, and strings. Because activities
482 * must already know how to save their state and re-create themselves from
483 * that state, this is a convenient way to have an activity restart itself
484 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700485 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 * <p>In some special cases, you may want to bypass restarting of your
487 * activity based on one or more types of configuration changes. This is
488 * done with the {@link android.R.attr#configChanges android:configChanges}
489 * attribute in its manifest. For any types of configuration changes you say
490 * that you handle there, you will receive a call to your current activity's
491 * {@link #onConfigurationChanged} method instead of being restarted. If
492 * a configuration change involves any that you do not handle, however, the
493 * activity will still be restarted and {@link #onConfigurationChanged}
494 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700495 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 * <a name="StartingActivities"></a>
497 * <h3>Starting Activities and Getting Results</h3>
498 *
499 * <p>The {@link android.app.Activity#startActivity}
500 * method is used to start a
501 * new activity, which will be placed at the top of the activity stack. It
502 * takes a single argument, an {@link android.content.Intent Intent},
503 * which describes the activity
504 * to be executed.</p>
505 *
506 * <p>Sometimes you want to get a result back from an activity when it
507 * ends. For example, you may start an activity that lets the user pick
508 * a person in a list of contacts; when it ends, it returns the person
509 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700510 * {@link android.app.Activity#startActivityForResult(Intent, int)}
511 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700513 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 *
515 * <p>When an activity exits, it can call
516 * {@link android.app.Activity#setResult(int)}
517 * to return data back to its parent. It must always supply a result code,
518 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
519 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
520 * return back an Intent containing any additional data it wants. All of this
521 * information appears back on the
522 * parent's <code>Activity.onActivityResult()</code>, along with the integer
523 * identifier it originally supplied.</p>
524 *
525 * <p>If a child activity fails for any reason (such as crashing), the parent
526 * activity will receive a result with the code RESULT_CANCELED.</p>
527 *
528 * <pre class="prettyprint">
529 * public class MyActivity extends Activity {
530 * ...
531 *
532 * static final int PICK_CONTACT_REQUEST = 0;
533 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700534 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
536 * // When the user center presses, let them pick a contact.
537 * startActivityForResult(
538 * new Intent(Intent.ACTION_PICK,
539 * new Uri("content://contacts")),
540 * PICK_CONTACT_REQUEST);
541 * return true;
542 * }
543 * return false;
544 * }
545 *
546 * protected void onActivityResult(int requestCode, int resultCode,
547 * Intent data) {
548 * if (requestCode == PICK_CONTACT_REQUEST) {
549 * if (resultCode == RESULT_OK) {
550 * // A contact was picked. Here we will just display it
551 * // to the user.
552 * startActivity(new Intent(Intent.ACTION_VIEW, data));
553 * }
554 * }
555 * }
556 * }
557 * </pre>
558 *
559 * <a name="SavingPersistentState"></a>
560 * <h3>Saving Persistent State</h3>
561 *
koprivafc3949e2018-09-17 11:35:13 -0700562 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * will deal with: shared document-like data (typically stored in a SQLite
564 * database using a {@linkplain android.content.ContentProvider content provider})
565 * and internal state such as user preferences.</p>
566 *
koprivafc3949e2018-09-17 11:35:13 -0700567 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * "edit in place" user model. That is, any edits a user makes are effectively
569 * made immediately without requiring an additional confirmation step.
570 * Supporting this model is generally a simple matter of following two rules:</p>
571 *
572 * <ul>
573 * <li> <p>When creating a new document, the backing database entry or file for
574 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700575 * 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 -0800576 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700577 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
579 * commit to the backing content provider or file any changes the user
580 * has made. This ensures that those changes will be seen by any other
581 * activity that is about to run. You will probably want to commit
582 * your data even more aggressively at key times during your
583 * activity's lifecycle: for example before starting a new
584 * activity, before finishing your own activity, when the user
585 * switches between input fields, etc.</p>
586 * </ul>
587 *
588 * <p>This model is designed to prevent data loss when a user is navigating
589 * between activities, and allows the system to safely kill an activity (because
590 * system resources are needed somewhere else) at any time after it has been
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700591 * stopped (or paused on platform versions before {@link android.os.Build.VERSION_CODES#HONEYCOMB}).
592 * Note this implies that the user pressing BACK from your activity does <em>not</em>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800594 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
596 *
597 * <p>See the {@linkplain android.content.ContentProvider content package} for
598 * more information about content providers. These are a key aspect of how
599 * different activities invoke and propagate data between themselves.</p>
600 *
601 * <p>The Activity class also provides an API for managing internal persistent state
602 * associated with an activity. This can be used, for example, to remember
603 * the user's preferred initial display in a calendar (day view or week view)
604 * or the user's default home page in a web browser.</p>
605 *
606 * <p>Activity persistent state is managed
607 * with the method {@link #getPreferences},
608 * allowing you to retrieve and
609 * modify a set of name/value pairs associated with the activity. To use
610 * preferences that are shared across multiple application components
611 * (activities, receivers, services, providers), you can use the underlying
612 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
613 * to retrieve a preferences
614 * object stored under a specific name.
615 * (Note that it is not possible to share settings data across application
616 * packages -- for that you will need a content provider.)</p>
617 *
618 * <p>Here is an excerpt from a calendar activity that stores the user's
619 * preferred view mode in its persistent settings:</p>
620 *
621 * <pre class="prettyprint">
622 * public class CalendarActivity extends Activity {
623 * ...
624 *
625 * static final int DAY_VIEW_MODE = 0;
626 * static final int WEEK_VIEW_MODE = 1;
627 *
628 * private SharedPreferences mPrefs;
629 * private int mCurViewMode;
630 *
631 * protected void onCreate(Bundle savedInstanceState) {
632 * super.onCreate(savedInstanceState);
633 *
634 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700635 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * }
637 *
638 * protected void onPause() {
639 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 * SharedPreferences.Editor ed = mPrefs.edit();
642 * ed.putInt("view_mode", mCurViewMode);
643 * ed.commit();
644 * }
645 * }
646 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700647 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * <a name="Permissions"></a>
649 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 * <p>The ability to start a particular Activity can be enforced when it is
652 * declared in its
653 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
654 * tag. By doing so, other applications will need to declare a corresponding
655 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
656 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800657 *
658 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
659 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
660 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
661 * Activity access to the specific URIs in the Intent. Access will remain
662 * until the Activity has finished (it will remain across the hosting
663 * process being killed and other temporary destruction). As of
664 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
665 * was already created and a new Intent is being delivered to
666 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
667 * to the existing ones it holds.
668 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
670 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700671 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 * <a name="ProcessLifecycle"></a>
673 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700674 *
kopriva4dddc632018-03-19 14:13:08 -0700675 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700677 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700679 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700681 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 * processes (the last ones) before it resorts to killing more important
683 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 * <ol>
686 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
687 * that the user is currently interacting with) is considered the most important.
688 * Its process will only be killed as a last resort, if it uses more memory
689 * than is available on the device. Generally at this point the device has
690 * reached a memory paging state, so this is required in order to keep the user
691 * interface responsive.
692 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700693 * but not in the foreground, such as one sitting behind a foreground dialog
694 * or next to other activities in multi-window mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 * is considered extremely important and will not be killed unless that is
696 * required to keep the foreground activity running.
697 * <li> <p>A <b>background activity</b> (an activity that is not visible to
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700698 * the user and has been stopped) is no longer critical, so the system may
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 * safely kill its process to reclaim memory for other foreground or
700 * visible processes. If its process needs to be killed, when the user navigates
701 * back to the activity (making it visible on the screen again), its
702 * {@link #onCreate} method will be called with the savedInstanceState it had previously
703 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
704 * state as the user last left it.
705 * <li> <p>An <b>empty process</b> is one hosting no activities or other
706 * application components (such as {@link Service} or
707 * {@link android.content.BroadcastReceiver} classes). These are killed very
708 * quickly by the system as memory becomes low. For this reason, any
709 * background operation you do outside of an activity must be executed in the
710 * context of an activity BroadcastReceiver or Service to ensure that the system
711 * knows it needs to keep your process around.
712 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700713 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * <p>Sometimes an Activity may need to do a long-running operation that exists
715 * independently of the activity lifecycle itself. An example may be a camera
716 * application that allows you to upload a picture to a web site. The upload
717 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700718 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 * should start a {@link Service} in which the upload takes place. This allows
720 * the system to properly prioritize your process (considering it to be more
721 * important than other non-visible applications) for the duration of the
722 * upload, independent of whether the original activity is paused, stopped,
723 * or finished.
724 */
725public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700726 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700728 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000729 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Lemecbf7f262019-04-17 13:57:59 -0700730 AutofillManager.AutofillClient, ContentCaptureManager.ContentCaptureClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700732 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
734 /** Standard activity result: operation canceled. */
735 public static final int RESULT_CANCELED = 0;
736 /** Standard activity result: operation succeeded. */
737 public static final int RESULT_OK = -1;
738 /** Start of user-defined activity results. */
739 public static final int RESULT_FIRST_USER = 1;
740
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700741 /** @hide Task isn't finished when activity is finished */
742 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700743 /**
744 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
745 * past behavior the task is also removed from recents.
746 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700747 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700748 /**
749 * @hide Task is finished along with the finishing activity, but it is not removed from
750 * recents.
751 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700752 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
753
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100754 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700755 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700756 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700757
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700758 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
760 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
761 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
762 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800763 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700764 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
765 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
Svetoslav970b59c2015-06-09 16:05:21 -0700767 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000768 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700769
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100770 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100771
Andrii Kuliand25680c2018-02-21 15:16:58 -0800772 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
773 private static final int LOG_AM_ON_START_CALLED = 30059;
774 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
775 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
776 private static final int LOG_AM_ON_STOP_CALLED = 30049;
777 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
778 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700779 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kulian86024012019-04-18 19:41:23 -0700780 private static final int LOG_AM_ON_TOP_RESUMED_GAINED_CALLED = 30064;
781 private static final int LOG_AM_ON_TOP_RESUMED_LOST_CALLED = 30065;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800782
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800783 private static class ManagedDialog {
784 Dialog mDialog;
785 Bundle mArgs;
786 }
787 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
789 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100790 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 private Instrumentation mInstrumentation;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100792 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 private IBinder mToken;
Sunny Goyald40c3452019-03-20 12:46:55 -0700794 private IBinder mAssistToken;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100795 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700796 private int mIdent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100797 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 /*package*/ String mEmbeddedID;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100799 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 private Application mApplication;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100801 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700802 /*package*/ Intent mIntent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100803 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800804 /*package*/ String mReferrer;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100805 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 private ComponentName mComponent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100807 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100809 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 /*package*/ ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100811 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 Activity mParent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100813 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 boolean mCalled;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100815 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600816 /*package*/ boolean mResumed;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100817 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700818 /*package*/ boolean mStopped;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100819 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 boolean mFinished;
821 boolean mStartedActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100822 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700823 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700824 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200825 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700826
827 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
828 * This only affects the client-side exception, the actual state check still happens in AMS. */
829 private boolean mCanEnterPictureInPicture = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500830 /** true if the activity is being destroyed in order to recreate it with a new configuration */
831 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100832 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 /*package*/ int mConfigChangeFlags;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100834 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100836 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700837 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700839 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
840 @Nullable private AutofillManager mAutofillManager;
841
Adam He43c06992019-04-15 15:41:49 -0700842 /** The content capture manager. Access via {@link #getContentCaptureManager()}. */
Felipe Lemeecb08be2018-11-27 15:48:47 -0800843 @Nullable private ContentCaptureManager mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800844
Ian Lake8a88cd62018-09-28 13:25:20 -0700845 private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
846 new ArrayList<Application.ActivityLifecycleCallbacks>();
Felipe Lemee348dc32018-11-05 12:35:29 -0800847
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700848 static final class NonConfigurationInstances {
849 Object activity;
850 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800851 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700852 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700853 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700854 }
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100855 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700856 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700857
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100858 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 private Window mWindow;
860
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100861 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 private WindowManager mWindowManager;
863 /*package*/ View mDecor = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100864 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 /*package*/ boolean mWindowAdded = false;
866 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood31755f92018-12-20 13:53:36 +0000867 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700869 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700870 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871
Mathew Inwood8c854f82018-09-14 12:35:36 +0100872 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Sunny Goyald40c3452019-03-20 12:46:55 -0700873 VoiceInteractor mVoiceInteractor;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700874
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100875 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 private CharSequence mTitle;
877 private int mTitleColor = 0;
878
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700879 // we must have a handler before the FragmentController is constructed
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100880 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700881 final Handler mHandler = new Handler();
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100882 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700883 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 private static final class ManagedCursor {
886 ManagedCursor(Cursor cursor) {
887 mCursor = cursor;
888 mReleased = false;
889 mUpdated = false;
890 }
891
892 private final Cursor mCursor;
893 private boolean mReleased;
894 private boolean mUpdated;
895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700897 @GuardedBy("mManagedCursors")
898 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
899
900 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100901 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700903 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100904 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700906
Craig Mautner5eda9b32013-07-02 11:58:16 -0700907 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700908 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700910 private SearchEvent mSearchEvent;
911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000913 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
915 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
916 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700917
Winsonb6403152016-02-23 13:32:09 -0800918 private ActivityManager.TaskDescription mTaskDescription =
919 new ActivityManager.TaskDescription();
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
922
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700923 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700924 private final Object mInstanceTracker = StrictMode.trackActivity(this);
925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700927
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100928 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700929 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700930 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
931 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
Svetoslavffb32b12015-10-15 16:54:00 -0700933 private boolean mHasCurrentPermissionsRequest;
934
Svet Ganov17db9dc2017-02-21 19:54:31 -0800935 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800936 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800937
Phil Weaver846cda932017-06-15 10:10:06 -0700938 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
939 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700940
Felipe Leme4753bb02017-03-22 20:24:00 -0700941 private AutofillPopupWindow mAutofillPopupWindow;
942
chaviwfeb2e1e2018-12-19 17:24:11 -0800943 /** @hide */
944 boolean mEnterAnimationComplete;
945
Hongwei Wangf0b5fb12019-07-25 15:14:47 -0700946 /** Track last dispatched multi-window and PiP mode to client, internal debug purpose **/
947 private Boolean mLastDispatchedIsInMultiWindowMode;
948 private Boolean mLastDispatchedIsInPictureInPictureMode;
949
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800950 private static native String getDlWarning();
951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 /** Return the intent that started this activity. */
953 public Intent getIntent() {
954 return mIntent;
955 }
956
RoboErik55011652014-07-09 15:05:53 -0700957 /**
958 * Change the intent returned by {@link #getIntent}. This holds a
959 * reference to the given intent; it does not copy it. Often used in
960 * conjunction with {@link #onNewIntent}.
961 *
962 * @param newIntent The new Intent object to return from getIntent
963 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * @see #getIntent
965 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700966 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 public void setIntent(Intent newIntent) {
968 mIntent = newIntent;
969 }
970
971 /** Return the application that owns this activity. */
972 public final Application getApplication() {
973 return mApplication;
974 }
975
976 /** Is this activity embedded inside of another activity? */
977 public final boolean isChild() {
978 return mParent != null;
979 }
RoboErik55011652014-07-09 15:05:53 -0700980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 /** Return the parent activity if this view is an embedded child. */
982 public final Activity getParent() {
983 return mParent;
984 }
985
986 /** Retrieve the window manager for showing custom windows. */
987 public WindowManager getWindowManager() {
988 return mWindowManager;
989 }
990
991 /**
992 * Retrieve the current {@link android.view.Window} for the activity.
993 * This can be used to directly access parts of the Window API that
994 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700995 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 * @return Window The current window, or null if the activity is not
997 * visual.
998 */
999 public Window getWindow() {
1000 return mWindow;
1001 }
1002
1003 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -08001004 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -08001005 *
1006 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -07001007 */
Ian Lake0a1feb82017-11-13 10:26:46 -08001008 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07001009 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001010 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -07001011 }
RoboErik55011652014-07-09 15:05:53 -07001012
Dianne Hackbornc8017682010-07-06 13:34:38 -07001013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 * Calls {@link android.view.Window#getCurrentFocus} on the
1015 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -07001016 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -07001018 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * @see #getWindow
1020 * @see android.view.Window#getCurrentFocus
1021 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001022 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 public View getCurrentFocus() {
1024 return mWindow != null ? mWindow.getCurrentFocus() : null;
1025 }
1026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 /**
Felipe Lemee348dc32018-11-05 12:35:29 -08001028 * (Creates, sets and) returns the autofill manager
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001029 *
1030 * @return The autofill manager
1031 */
1032 @NonNull private AutofillManager getAutofillManager() {
1033 if (mAutofillManager == null) {
1034 mAutofillManager = getSystemService(AutofillManager.class);
1035 }
1036
1037 return mAutofillManager;
1038 }
1039
Felipe Lemee348dc32018-11-05 12:35:29 -08001040 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -08001041 * (Creates, sets, and ) returns the content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001042 *
Felipe Lemeecb08be2018-11-27 15:48:47 -08001043 * @return The content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001044 */
Adam Heb66babb2019-01-08 15:43:53 -08001045 @Nullable private ContentCaptureManager getContentCaptureManager() {
1046 // ContextCapture disabled for system apps
Felipe Leme0c6b23d2019-01-15 17:02:49 -08001047 if (!UserHandle.isApp(myUid())) return null;
Felipe Lemeecb08be2018-11-27 15:48:47 -08001048 if (mContentCaptureManager == null) {
1049 mContentCaptureManager = getSystemService(ContentCaptureManager.class);
Felipe Lemee348dc32018-11-05 12:35:29 -08001050 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001051 return mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -08001052 }
1053
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001054 /** @hide */ private static final int CONTENT_CAPTURE_START = 1;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001055 /** @hide */ private static final int CONTENT_CAPTURE_RESUME = 2;
1056 /** @hide */ private static final int CONTENT_CAPTURE_PAUSE = 3;
1057 /** @hide */ private static final int CONTENT_CAPTURE_STOP = 4;
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001058
1059 /** @hide */
1060 @IntDef(prefix = { "CONTENT_CAPTURE_" }, value = {
1061 CONTENT_CAPTURE_START,
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001062 CONTENT_CAPTURE_RESUME,
1063 CONTENT_CAPTURE_PAUSE,
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001064 CONTENT_CAPTURE_STOP
1065 })
1066 @Retention(RetentionPolicy.SOURCE)
1067 @interface ContentCaptureNotificationType{}
1068
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001069 private String getContentCaptureTypeAsString(@ContentCaptureNotificationType int type) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001070 switch (type) {
1071 case CONTENT_CAPTURE_START:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001072 return "START";
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001073 case CONTENT_CAPTURE_RESUME:
1074 return "RESUME";
1075 case CONTENT_CAPTURE_PAUSE:
1076 return "PAUSE";
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001077 case CONTENT_CAPTURE_STOP:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001078 return "STOP";
Felipe Leme7a534082018-11-05 15:03:04 -08001079 default:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001080 return "UNKNOW-" + type;
1081 }
1082 }
1083
1084 private void notifyContentCaptureManagerIfNeeded(@ContentCaptureNotificationType int type) {
1085 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1086 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1087 "notifyContentCapture(" + getContentCaptureTypeAsString(type) + ") for "
1088 + mComponent.toShortString());
1089 }
1090 try {
1091 final ContentCaptureManager cm = getContentCaptureManager();
1092 if (cm == null) return;
1093
1094 switch (type) {
1095 case CONTENT_CAPTURE_START:
1096 //TODO(b/111276913): decide whether the InteractionSessionId should be
1097 // saved / restored in the activity bundle - probably not
Adam He43c06992019-04-15 15:41:49 -07001098 final Window window = getWindow();
1099 if (window != null) {
1100 cm.updateWindowAttributes(window.getAttributes());
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001101 }
Adam He43c06992019-04-15 15:41:49 -07001102 cm.onActivityCreated(mToken, getComponentName());
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001103 break;
1104 case CONTENT_CAPTURE_RESUME:
1105 cm.onActivityResumed();
1106 break;
1107 case CONTENT_CAPTURE_PAUSE:
1108 cm.onActivityPaused();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001109 break;
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001110 case CONTENT_CAPTURE_STOP:
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001111 cm.onActivityDestroyed();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001112 break;
1113 default:
1114 Log.wtf(TAG, "Invalid @ContentCaptureNotificationType: " + type);
1115 }
1116 } finally {
1117 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Felipe Leme7a534082018-11-05 15:03:04 -08001118 }
1119 }
1120
Felipe Lemebb567ae2017-10-04 09:56:21 -07001121 @Override
1122 protected void attachBaseContext(Context newBase) {
1123 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001124 if (newBase != null) {
1125 newBase.setAutofillClient(this);
Felipe Leme326f15a2019-02-19 09:42:24 -08001126 newBase.setContentCaptureOptions(getContentCaptureOptions());
Felipe Lemed247de82018-05-14 17:51:58 -07001127 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001128 }
1129
1130 /** @hide */
1131 @Override
1132 public final AutofillClient getAutofillClient() {
1133 return this;
1134 }
1135
Felipe Lemecbf7f262019-04-17 13:57:59 -07001136 /** @hide */
1137 @Override
1138 public final ContentCaptureClient getContentCaptureClient() {
1139 return this;
1140 }
1141
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001142 /**
Ian Lake8a88cd62018-09-28 13:25:20 -07001143 * Register an {@link Application.ActivityLifecycleCallbacks} instance that receives
1144 * lifecycle callbacks for only this Activity.
1145 * <p>
1146 * In relation to any
1147 * {@link Application#registerActivityLifecycleCallbacks Application registered callbacks},
1148 * the callbacks registered here will always occur nested within those callbacks. This means:
1149 * <ul>
1150 * <li>Pre events will first be sent to Application registered callbacks, then to callbacks
1151 * registered here.</li>
1152 * <li>{@link Application.ActivityLifecycleCallbacks#onActivityCreated(Activity, Bundle)},
1153 * {@link Application.ActivityLifecycleCallbacks#onActivityStarted(Activity)}, and
1154 * {@link Application.ActivityLifecycleCallbacks#onActivityResumed(Activity)} will
1155 * be sent first to Application registered callbacks, then to callbacks registered here.
1156 * For all other events, callbacks registered here will be sent first.</li>
1157 * <li>Post events will first be sent to callbacks registered here, then to
1158 * Application registered callbacks.</li>
1159 * </ul>
1160 * <p>
1161 * If multiple callbacks are registered here, they receive events in a first in (up through
1162 * {@link Application.ActivityLifecycleCallbacks#onActivityPostResumed}, last out
1163 * ordering.
1164 * <p>
1165 * It is strongly recommended to register this in the constructor of your Activity to ensure
1166 * you get all available callbacks. As this callback is associated with only this Activity,
1167 * it is not usually necessary to {@link #unregisterActivityLifecycleCallbacks unregister} it
1168 * unless you specifically do not want to receive further lifecycle callbacks.
1169 *
1170 * @param callback The callback instance to register
1171 */
1172 public void registerActivityLifecycleCallbacks(
1173 @NonNull Application.ActivityLifecycleCallbacks callback) {
1174 synchronized (mActivityLifecycleCallbacks) {
1175 mActivityLifecycleCallbacks.add(callback);
1176 }
1177 }
1178
1179 /**
1180 * Unregister an {@link Application.ActivityLifecycleCallbacks} previously registered
1181 * with {@link #registerActivityLifecycleCallbacks}. It will not receive any further
1182 * callbacks.
1183 *
1184 * @param callback The callback instance to unregister
1185 * @see #registerActivityLifecycleCallbacks
1186 */
1187 public void unregisterActivityLifecycleCallbacks(
1188 @NonNull Application.ActivityLifecycleCallbacks callback) {
1189 synchronized (mActivityLifecycleCallbacks) {
1190 mActivityLifecycleCallbacks.remove(callback);
1191 }
1192 }
1193
1194 private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
1195 getApplication().dispatchActivityPreCreated(this, savedInstanceState);
1196 Object[] callbacks = collectActivityLifecycleCallbacks();
1197 if (callbacks != null) {
1198 for (int i = 0; i < callbacks.length; i++) {
1199 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
1200 savedInstanceState);
1201 }
1202 }
1203 }
1204
1205 private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
1206 getApplication().dispatchActivityCreated(this, savedInstanceState);
1207 Object[] callbacks = collectActivityLifecycleCallbacks();
1208 if (callbacks != null) {
1209 for (int i = 0; i < callbacks.length; i++) {
1210 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
1211 savedInstanceState);
1212 }
1213 }
1214 }
1215
1216 private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
1217 Object[] callbacks = collectActivityLifecycleCallbacks();
1218 if (callbacks != null) {
1219 for (int i = 0; i < callbacks.length; i++) {
1220 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
1221 savedInstanceState);
1222 }
1223 }
1224 getApplication().dispatchActivityPostCreated(this, savedInstanceState);
1225 }
1226
1227 private void dispatchActivityPreStarted() {
1228 getApplication().dispatchActivityPreStarted(this);
1229 Object[] callbacks = collectActivityLifecycleCallbacks();
1230 if (callbacks != null) {
1231 for (int i = 0; i < callbacks.length; i++) {
1232 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(this);
1233 }
1234 }
1235 }
1236
1237 private void dispatchActivityStarted() {
1238 getApplication().dispatchActivityStarted(this);
1239 Object[] callbacks = collectActivityLifecycleCallbacks();
1240 if (callbacks != null) {
1241 for (int i = 0; i < callbacks.length; i++) {
1242 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
1243 }
1244 }
1245 }
1246
1247 private void dispatchActivityPostStarted() {
1248 Object[] callbacks = collectActivityLifecycleCallbacks();
1249 if (callbacks != null) {
1250 for (int i = 0; i < callbacks.length; i++) {
1251 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1252 .onActivityPostStarted(this);
1253 }
1254 }
1255 getApplication().dispatchActivityPostStarted(this);
1256 }
1257
1258 private void dispatchActivityPreResumed() {
1259 getApplication().dispatchActivityPreResumed(this);
1260 Object[] callbacks = collectActivityLifecycleCallbacks();
1261 if (callbacks != null) {
1262 for (int i = 0; i < callbacks.length; i++) {
1263 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(this);
1264 }
1265 }
1266 }
1267
1268 private void dispatchActivityResumed() {
1269 getApplication().dispatchActivityResumed(this);
1270 Object[] callbacks = collectActivityLifecycleCallbacks();
1271 if (callbacks != null) {
1272 for (int i = 0; i < callbacks.length; i++) {
1273 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
1274 }
1275 }
1276 }
1277
1278 private void dispatchActivityPostResumed() {
1279 Object[] callbacks = collectActivityLifecycleCallbacks();
1280 if (callbacks != null) {
1281 for (int i = 0; i < callbacks.length; i++) {
1282 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(this);
1283 }
1284 }
1285 getApplication().dispatchActivityPostResumed(this);
1286 }
1287
1288 private void dispatchActivityPrePaused() {
1289 getApplication().dispatchActivityPrePaused(this);
1290 Object[] callbacks = collectActivityLifecycleCallbacks();
1291 if (callbacks != null) {
1292 for (int i = callbacks.length - 1; i >= 0; i--) {
1293 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(this);
1294 }
1295 }
1296 }
1297
1298 private void dispatchActivityPaused() {
1299 Object[] callbacks = collectActivityLifecycleCallbacks();
1300 if (callbacks != null) {
1301 for (int i = callbacks.length - 1; i >= 0; i--) {
1302 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(this);
1303 }
1304 }
1305 getApplication().dispatchActivityPaused(this);
1306 }
1307
1308 private void dispatchActivityPostPaused() {
1309 Object[] callbacks = collectActivityLifecycleCallbacks();
1310 if (callbacks != null) {
1311 for (int i = callbacks.length - 1; i >= 0; i--) {
1312 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(this);
1313 }
1314 }
1315 getApplication().dispatchActivityPostPaused(this);
1316 }
1317
1318 private void dispatchActivityPreStopped() {
1319 getApplication().dispatchActivityPreStopped(this);
1320 Object[] callbacks = collectActivityLifecycleCallbacks();
1321 if (callbacks != null) {
1322 for (int i = callbacks.length - 1; i >= 0; i--) {
1323 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(this);
1324 }
1325 }
1326 }
1327
1328 private void dispatchActivityStopped() {
1329 Object[] callbacks = collectActivityLifecycleCallbacks();
1330 if (callbacks != null) {
1331 for (int i = callbacks.length - 1; i >= 0; i--) {
1332 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(this);
1333 }
1334 }
1335 getApplication().dispatchActivityStopped(this);
1336 }
1337
1338 private void dispatchActivityPostStopped() {
1339 Object[] callbacks = collectActivityLifecycleCallbacks();
1340 if (callbacks != null) {
1341 for (int i = callbacks.length - 1; i >= 0; i--) {
1342 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1343 .onActivityPostStopped(this);
1344 }
1345 }
1346 getApplication().dispatchActivityPostStopped(this);
1347 }
1348
1349 private void dispatchActivityPreSaveInstanceState(@NonNull Bundle outState) {
1350 getApplication().dispatchActivityPreSaveInstanceState(this, outState);
1351 Object[] callbacks = collectActivityLifecycleCallbacks();
1352 if (callbacks != null) {
1353 for (int i = callbacks.length - 1; i >= 0; i--) {
1354 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1355 .onActivityPreSaveInstanceState(this, outState);
1356 }
1357 }
1358 }
1359
1360 private void dispatchActivitySaveInstanceState(@NonNull Bundle outState) {
1361 Object[] callbacks = collectActivityLifecycleCallbacks();
1362 if (callbacks != null) {
1363 for (int i = callbacks.length - 1; i >= 0; i--) {
1364 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1365 .onActivitySaveInstanceState(this, outState);
1366 }
1367 }
1368 getApplication().dispatchActivitySaveInstanceState(this, outState);
1369 }
1370
1371 private void dispatchActivityPostSaveInstanceState(@NonNull Bundle outState) {
1372 Object[] callbacks = collectActivityLifecycleCallbacks();
1373 if (callbacks != null) {
1374 for (int i = callbacks.length - 1; i >= 0; i--) {
1375 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1376 .onActivityPostSaveInstanceState(this, outState);
1377 }
1378 }
1379 getApplication().dispatchActivityPostSaveInstanceState(this, outState);
1380 }
1381
1382 private void dispatchActivityPreDestroyed() {
1383 getApplication().dispatchActivityPreDestroyed(this);
1384 Object[] callbacks = collectActivityLifecycleCallbacks();
1385 if (callbacks != null) {
1386 for (int i = callbacks.length - 1; i >= 0; i--) {
1387 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1388 .onActivityPreDestroyed(this);
1389 }
1390 }
1391 }
1392
1393 private void dispatchActivityDestroyed() {
1394 Object[] callbacks = collectActivityLifecycleCallbacks();
1395 if (callbacks != null) {
1396 for (int i = callbacks.length - 1; i >= 0; i--) {
1397 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
1398 }
1399 }
1400 getApplication().dispatchActivityDestroyed(this);
1401 }
1402
1403 private void dispatchActivityPostDestroyed() {
1404 Object[] callbacks = collectActivityLifecycleCallbacks();
1405 if (callbacks != null) {
1406 for (int i = callbacks.length - 1; i >= 0; i--) {
1407 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1408 .onActivityPostDestroyed(this);
1409 }
1410 }
1411 getApplication().dispatchActivityPostDestroyed(this);
1412 }
1413
1414 private Object[] collectActivityLifecycleCallbacks() {
1415 Object[] callbacks = null;
1416 synchronized (mActivityLifecycleCallbacks) {
1417 if (mActivityLifecycleCallbacks.size() > 0) {
1418 callbacks = mActivityLifecycleCallbacks.toArray();
1419 }
1420 }
1421 return callbacks;
1422 }
1423
1424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 * Called when the activity is starting. This is where most initialization
1426 * should go: calling {@link #setContentView(int)} to inflate the
1427 * activity's UI, using {@link #findViewById} to programmatically interact
1428 * with widgets in the UI, calling
1429 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1430 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001433 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1434 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1435 * executing.
RoboErik55011652014-07-09 15:05:53 -07001436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 * <p><em>Derived classes must call through to the super class's
1438 * implementation of this method. If they do not, an exception will be
1439 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001440 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 * @param savedInstanceState If the activity is being re-initialized after
1442 * previously being shut down then this Bundle contains the data it most
1443 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 * @see #onStart
1446 * @see #onSaveInstanceState
1447 * @see #onRestoreInstanceState
1448 * @see #onPostCreate
1449 */
Tor Norbye83c68962015-03-10 20:55:31 -07001450 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001451 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001452 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001453 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001454
Dianne Hackborn2707d602010-07-09 18:01:20 -07001455 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001456 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001457 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001458 if (mActivityInfo.parentActivityName != null) {
1459 if (mActionBar == null) {
1460 mEnableDefaultActionBarUp = true;
1461 } else {
1462 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1463 }
1464 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001465 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001466 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001467 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1468 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001469
1470 if (mAutoFillResetNeeded) {
1471 getAutofillManager().onCreate(savedInstanceState);
1472 }
1473
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001474 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1475 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1476 ? mLastNonConfigurationInstances.fragments : null);
1477 }
1478 mFragments.dispatchCreate();
Ian Lake8a88cd62018-09-28 13:25:20 -07001479 dispatchActivityCreated(savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001480 if (mVoiceInteractor != null) {
1481 mVoiceInteractor.attachActivity(this);
1482 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001483 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 mCalled = true;
Felipe Lemee348dc32018-11-05 12:35:29 -08001485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487
1488 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001489 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001490 * the attribute {@link android.R.attr#persistableMode} set to
1491 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001492 *
1493 * @param savedInstanceState if the activity is being re-initialized after
1494 * previously being shut down then this Bundle contains the data it most
1495 * recently supplied in {@link #onSaveInstanceState}.
1496 * <b><i>Note: Otherwise it is null.</i></b>
1497 * @param persistentState if the activity is being re-initialized after
1498 * previously being shut down or powered off then this Bundle contains the data it most
1499 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1500 * <b><i>Note: Otherwise it is null.</i></b>
1501 *
1502 * @see #onCreate(android.os.Bundle)
1503 * @see #onStart
1504 * @see #onSaveInstanceState
1505 * @see #onRestoreInstanceState
1506 * @see #onPostCreate
1507 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001508 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001509 @Nullable PersistableBundle persistentState) {
1510 onCreate(savedInstanceState);
1511 }
1512
1513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * The hook for {@link ActivityThread} to restore the state of this activity.
1515 *
1516 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1517 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1518 *
1519 * @param savedInstanceState contains the saved state
1520 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001521 final void performRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 onRestoreInstanceState(savedInstanceState);
1523 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525
1526 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001527 * The hook for {@link ActivityThread} to restore the state of this activity.
1528 *
1529 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1530 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1531 *
1532 * @param savedInstanceState contains the saved state
1533 * @param persistentState contains the persistable saved state
1534 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001535 final void performRestoreInstanceState(@Nullable Bundle savedInstanceState,
1536 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001537 onRestoreInstanceState(savedInstanceState, persistentState);
1538 if (savedInstanceState != null) {
1539 restoreManagedDialogs(savedInstanceState);
1540 }
1541 }
1542
1543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 * This method is called after {@link #onStart} when the activity is
1545 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001546 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 * to restore their state, but it is sometimes convenient to do it here
1548 * after all of the initialization has been done or to allow subclasses to
1549 * decide whether to use your default implementation. The default
1550 * implementation of this method performs a restore of any view state that
1551 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * <p>This method is called between {@link #onStart} and
Kevin Hufnagle4fbcec12019-07-18 21:37:36 +00001554 * {@link #onPostCreate}. This method is called only when recreating
1555 * an activity; the method isn't invoked if {@link #onStart} is called for
1556 * any other reason.</p>
RoboErik55011652014-07-09 15:05:53 -07001557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001559 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * @see #onCreate
1561 * @see #onPostCreate
1562 * @see #onResume
1563 * @see #onSaveInstanceState
1564 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001565 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 if (mWindow != null) {
1567 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1568 if (windowState != null) {
1569 mWindow.restoreHierarchyState(windowState);
1570 }
1571 }
1572 }
Craig Mautnera0026042014-04-23 11:45:37 -07001573
1574 /**
1575 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001576 * created with the attribute {@link android.R.attr#persistableMode} set to
1577 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1578 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001579 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1580 *
1581 * <p>This method is called between {@link #onStart} and
1582 * {@link #onPostCreate}.
1583 *
1584 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1585 *
Jake Wharton63f4d892018-07-10 12:27:59 -04001586 * <p>At least one of {@code savedInstanceState} or {@code persistentState} will not be null.
1587 *
1588 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}
1589 * or null.
1590 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}
1591 * or null.
Craig Mautnera0026042014-04-23 11:45:37 -07001592 *
1593 * @see #onRestoreInstanceState(Bundle)
1594 * @see #onCreate
1595 * @see #onPostCreate
1596 * @see #onResume
1597 * @see #onSaveInstanceState
1598 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001599 public void onRestoreInstanceState(@Nullable Bundle savedInstanceState,
1600 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001601 if (savedInstanceState != null) {
1602 onRestoreInstanceState(savedInstanceState);
1603 }
1604 }
1605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 /**
1607 * Restore the state of any saved managed dialogs.
1608 *
1609 * @param savedInstanceState The bundle to restore from.
1610 */
1611 private void restoreManagedDialogs(Bundle savedInstanceState) {
1612 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1613 if (b == null) {
1614 return;
1615 }
1616
1617 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1618 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001619 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 for (int i = 0; i < numDialogs; i++) {
1621 final Integer dialogId = ids[i];
1622 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1623 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001624 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1625 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001626 final ManagedDialog md = new ManagedDialog();
1627 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1628 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1629 if (md.mDialog != null) {
1630 mManagedDialogs.put(dialogId, md);
1631 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1632 md.mDialog.onRestoreInstanceState(dialogState);
1633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 }
1635 }
1636 }
1637
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001638 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1639 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001640 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001641 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001642 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001643 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001644 return dialog;
1645 }
1646
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001647 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 return SAVED_DIALOG_KEY_PREFIX + key;
1649 }
1650
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001651 private static String savedDialogArgsKeyFor(int key) {
1652 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654
1655 /**
1656 * Called when activity start-up is complete (after {@link #onStart}
1657 * and {@link #onRestoreInstanceState} have been called). Applications will
1658 * generally not implement this method; it is intended for system
1659 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 * <p><em>Derived classes must call through to the super class's
1662 * implementation of this method. If they do not, an exception will be
1663 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001664 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 * @param savedInstanceState If the activity is being re-initialized after
1666 * previously being shut down then this Bundle contains the data it most
1667 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1668 * @see #onCreate
1669 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001670 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001671 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (!isChild()) {
1673 mTitleReady = true;
1674 onTitleChanged(getTitle(), getTitleColor());
1675 }
Winsonb6403152016-02-23 13:32:09 -08001676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 mCalled = true;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001678
1679 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_START);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681
1682 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001683 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001684 * created with the attribute {@link android.R.attr#persistableMode} set to
1685 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001686 *
1687 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1688 * @param persistentState The data caming from the PersistableBundle first
1689 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1690 *
1691 * @see #onCreate
1692 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001693 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001694 @Nullable PersistableBundle persistentState) {
1695 onPostCreate(savedInstanceState);
1696 }
1697
1698 /**
RoboErik55011652014-07-09 15:05:53 -07001699 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1700 * the activity had been stopped, but is now again being displayed to the
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001701 * user. It will usually be followed by {@link #onResume}. This is a good place to begin
1702 * drawing visual elements, running animations, etc.
1703 *
1704 * <p>You can call {@link #finish} from within this function, in
1705 * which case {@link #onStop} will be immediately called after {@link #onStart} without the
1706 * lifecycle transitions in-between ({@link #onResume}, {@link #onPause}, etc) executing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 *
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 #onCreate
1713 * @see #onStop
1714 * @see #onResume
1715 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001716 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001718 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001720
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001721 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001722
Ian Lake8a88cd62018-09-28 13:25:20 -07001723 dispatchActivityStarted();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001724
1725 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001726 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729
1730 /**
1731 * Called after {@link #onStop} when the current activity is being
1732 * re-displayed to the user (the user has navigated back to it). It will
1733 * be followed by {@link #onStart} and then {@link #onResume}.
1734 *
1735 * <p>For activities that are using raw {@link Cursor} objects (instead of
1736 * creating them through
1737 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1738 * this is usually the place
1739 * where the cursor should be requeried (because you had deactivated it in
1740 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001741 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 * <p><em>Derived classes must call through to the super class's
1743 * implementation of this method. If they do not, an exception will be
1744 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001745 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 * @see #onStop
1747 * @see #onStart
1748 * @see #onResume
1749 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001750 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 protected void onRestart() {
1752 mCalled = true;
1753 }
1754
1755 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001756 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1757 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1758 * to give the activity a hint that its state is no longer saved -- it will generally
1759 * be called after {@link #onSaveInstanceState} and prior to the activity being
1760 * resumed/started again.
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001761 *
1762 * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
1763 * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
1764 * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001765 */
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001766 @Deprecated
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001767 public void onStateNotSaved() {
1768 }
1769
1770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001772 * {@link #onPause}, for your activity to start interacting with the user. This is an indicator
1773 * that the activity became active and ready to receive input. It is on top of an activity stack
1774 * and visible to user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001776 * <p>On platform versions prior to {@link android.os.Build.VERSION_CODES#Q} this is also a good
1777 * place to try to open exclusive-access devices or to get access to singleton resources.
1778 * Starting with {@link android.os.Build.VERSION_CODES#Q} there can be multiple resumed
1779 * activities in the system simultaneously, so {@link #onTopResumedActivityChanged(boolean)}
1780 * should be used for that purpose instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 *
1782 * <p><em>Derived classes must call through to the super class's
1783 * implementation of this method. If they do not, an exception will be
1784 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001785 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 * @see #onRestoreInstanceState
1787 * @see #onRestart
1788 * @see #onPostResume
1789 * @see #onPause
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001790 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001792 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001794 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07001795 dispatchActivityResumed();
Sunny Goyal64643f62019-01-31 15:15:33 -08001796 mActivityTransitionState.onResume(this);
Felipe Lemece404982018-09-17 09:46:13 -07001797 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001798 if (mAutoFillResetNeeded) {
1799 if (!mAutoFillIgnoreFirstResumePause) {
1800 View focus = getCurrentFocus();
Ben Line55cd3e2018-12-07 20:26:37 +00001801 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
Dake Gu67decfa2017-12-27 11:48:08 -08001802 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1803 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1804 // window visibility after recreation is INVISIBLE in onResume() and next frame
1805 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1806 // So we cannot call View.notifyEnterOrExited() which will do nothing
1807 // when View.isVisibleToUser() is false.
1808 getAutofillManager().notifyViewEntered(focus);
1809 }
1810 }
1811 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001812
1813 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);
1814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 mCalled = true;
1816 }
1817
1818 /**
1819 * Called when activity resume is complete (after {@link #onResume} has
1820 * been called). Applications will generally not implement this method;
1821 * it is intended for system classes to do final setup after application
1822 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 * <p><em>Derived classes must call through to the super class's
1825 * implementation of this method. If they do not, an exception will be
1826 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 * @see #onResume
1829 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001830 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 protected void onPostResume() {
1832 final Window win = getWindow();
1833 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001834 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 mCalled = true;
1836 }
1837
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001838 /**
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001839 * Called when activity gets or loses the top resumed position in the system.
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001840 *
1841 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} multiple activities can be resumed
1842 * at the same time in multi-window and multi-display modes. This callback should be used
1843 * instead of {@link #onResume()} as an indication that the activity can try to open
1844 * exclusive-access devices like camera.</p>
1845 *
1846 * <p>It will always be delivered after the activity was resumed and before it is paused. In
1847 * some cases it might be skipped and activity can go straight from {@link #onResume()} to
1848 * {@link #onPause()} without receiving the top resumed state.</p>
1849 *
1850 * @param isTopResumedActivity {@code true} if it's the topmost resumed activity in the system,
1851 * {@code false} otherwise. A call with this as {@code true} will
1852 * always be followed by another one with {@code false}.
1853 *
1854 * @see #onResume()
1855 * @see #onPause()
1856 * @see #onWindowFocusChanged(boolean)
1857 */
1858 public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
1859 }
1860
Andrii Kulian86024012019-04-18 19:41:23 -07001861 final void performTopResumedActivityChanged(boolean isTopResumedActivity, String reason) {
1862 onTopResumedActivityChanged(isTopResumedActivity);
1863
1864 writeEventLog(isTopResumedActivity
1865 ? LOG_AM_ON_TOP_RESUMED_GAINED_CALLED : LOG_AM_ON_TOP_RESUMED_LOST_CALLED, reason);
1866 }
1867
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001868 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001869 if (mVoiceInteractor != null) {
Sunny Goyald40c3452019-03-20 12:46:55 -07001870 final Request[] requests = mVoiceInteractor.getActiveRequests();
1871 if (requests != null) {
1872 for (Request activeRequest : mVoiceInteractor.getActiveRequests()) {
1873 activeRequest.cancel();
1874 activeRequest.clear();
1875 }
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001876 }
1877 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001878 if (voiceInteractor == null) {
1879 mVoiceInteractor = null;
1880 } else {
1881 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1882 Looper.myLooper());
1883 }
1884 }
1885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001887 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001888 *
Phil Weaver846cda932017-06-15 10:10:06 -07001889 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001890 * will be unique.
1891 *
1892 * @return A ID that is unique in the activity
1893 *
1894 * {@hide}
1895 */
Felipe Leme42b97932018-02-20 13:04:31 -08001896 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001897 public int getNextAutofillId() {
1898 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1899 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001900 }
1901
Phil Weaver846cda932017-06-15 10:10:06 -07001902 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001903
Phil Weaver846cda932017-06-15 10:10:06 -07001904 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001905 }
1906
1907 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001908 * @hide
1909 */
1910 @Override
1911 public AutofillId autofillClientGetNextAutofillId() {
Felipe Leme305fd402018-09-11 18:20:42 +00001912 return new AutofillId(getNextAutofillId());
Felipe Leme42b97932018-02-20 13:04:31 -08001913 }
1914
1915 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001916 * Check whether this activity is running as part of a voice interaction with the user.
1917 * If true, it should perform its interaction with the user through the
1918 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1919 */
1920 public boolean isVoiceInteraction() {
1921 return mVoiceInteractor != null;
1922 }
1923
1924 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001925 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1926 * of a voice interaction. That is, returns true if this activity was directly
1927 * started by the voice interaction service as the initiation of a voice interaction.
1928 * Otherwise, for example if it was started by another activity while under voice
1929 * interaction, returns false.
1930 */
1931 public boolean isVoiceInteractionRoot() {
1932 try {
1933 return mVoiceInteractor != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001934 && ActivityTaskManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001935 } catch (RemoteException e) {
1936 }
1937 return false;
1938 }
1939
1940 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001941 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1942 * interact with this activity.
1943 */
1944 public VoiceInteractor getVoiceInteractor() {
1945 return mVoiceInteractor;
1946 }
1947
1948 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001949 * Queries whether the currently enabled voice interaction service supports returning
1950 * a voice interactor for use by the activity. This is valid only for the duration of the
1951 * activity.
1952 *
1953 * @return whether the current voice interaction service supports local voice interaction
1954 */
1955 public boolean isLocalVoiceInteractionSupported() {
1956 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001957 return ActivityTaskManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001958 } catch (RemoteException re) {
1959 }
1960 return false;
1961 }
1962
1963 /**
1964 * Starts a local voice interaction session. When ready,
1965 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1966 * to the registered voice interaction service.
1967 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1968 */
1969 public void startLocalVoiceInteraction(Bundle privateOptions) {
1970 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001971 ActivityTaskManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001972 } catch (RemoteException re) {
1973 }
1974 }
1975
1976 /**
1977 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1978 * voice interaction session being started. You can now retrieve a voice interactor using
1979 * {@link #getVoiceInteractor()}.
1980 */
1981 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001982 }
1983
1984 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001985 * Callback to indicate that the local voice interaction has stopped either
1986 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1987 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1988 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001989 */
1990 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001991 }
1992
1993 /**
1994 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001995 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1996 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001997 */
1998 public void stopLocalVoiceInteraction() {
1999 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002000 ActivityTaskManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002001 } catch (RemoteException re) {
2002 }
2003 }
2004
2005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 * This is called for activities that set launchMode to "singleTop" in
2007 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
2008 * flag when calling {@link #startActivity}. In either case, when the
2009 * activity is re-launched while at the top of the activity stack instead
2010 * of a new instance of the activity being started, onNewIntent() will be
2011 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07002012 * re-launch it.
2013 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002014 * <p>An activity can never receive a new intent in the resumed state. You can count on
2015 * {@link #onResume} being called after this method, though not necessarily immediately after
2016 * the completion this callback. If the activity was resumed, it will be paused and new intent
2017 * will be delivered, followed by {@link #onResume}. If the activity wasn't in the resumed
2018 * state, then new intent can be delivered immediately, with {@link #onResume()} called
2019 * sometime later when activity becomes active again.
RoboErik55011652014-07-09 15:05:53 -07002020 *
2021 * <p>Note that {@link #getIntent} still returns the original Intent. You
2022 * can use {@link #setIntent} to update it to this new Intent.
2023 *
2024 * @param intent The new intent that was started for the activity.
2025 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07002027 * @see #setIntent
2028 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 */
2030 protected void onNewIntent(Intent intent) {
2031 }
2032
2033 /**
2034 * The hook for {@link ActivityThread} to save the state of this activity.
2035 *
2036 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2037 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2038 *
2039 * @param outState The bundle to save the state to.
2040 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002041 final void performSaveInstanceState(@NonNull Bundle outState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002042 dispatchActivityPreSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 onSaveInstanceState(outState);
2044 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07002045 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002046 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002047 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002048 dispatchActivityPostSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
2050
2051 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002052 * The hook for {@link ActivityThread} to save the state of this activity.
2053 *
2054 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2055 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2056 *
2057 * @param outState The bundle to save the state to.
2058 * @param outPersistentState The bundle to save persistent state to.
2059 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002060 final void performSaveInstanceState(@NonNull Bundle outState,
2061 @NonNull PersistableBundle outPersistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002062 dispatchActivityPreSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002063 onSaveInstanceState(outState, outPersistentState);
2064 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002065 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002066 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
2067 ", " + outPersistentState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002068 dispatchActivityPostSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002069 }
2070
2071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 * Called to retrieve per-instance state from an activity before being killed
2073 * so that the state can be restored in {@link #onCreate} or
2074 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
2075 * will be passed to both).
2076 *
2077 * <p>This method is called before an activity may be killed so that when it
2078 * comes back some time in the future it can restore its state. For example,
2079 * if activity B is launched in front of activity A, and at some point activity
2080 * A is killed to reclaim resources, activity A will have a chance to save the
2081 * current state of its user interface via this method so that when the user
2082 * returns to activity A, the state of the user interface can be restored
2083 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
2084 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002085 * <p>Do not confuse this method with activity lifecycle callbacks such as {@link #onPause},
2086 * which is always called when the user no longer actively interacts with an activity, or
2087 * {@link #onStop} which is called when activity becomes invisible. One example of when
2088 * {@link #onPause} and {@link #onStop} is called and not this method is when a user navigates
2089 * back from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
2090 * on B because that particular instance will never be restored,
2091 * so the system avoids calling it. An example when {@link #onPause} is called and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
2093 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
2094 * killed during the lifetime of B since the state of the user interface of
2095 * A will stay intact.
2096 *
2097 * <p>The default implementation takes care of most of the UI per-instance
2098 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
2099 * view in the hierarchy that has an id, and by saving the id of the currently
2100 * focused view (all of which is restored by the default implementation of
2101 * {@link #onRestoreInstanceState}). If you override this method to save additional
2102 * information not captured by each individual view, you will likely want to
2103 * call through to the default implementation, otherwise be prepared to save
2104 * all of the state of each view yourself.
2105 *
Andrii Kulian391161f2018-01-29 10:50:02 -08002106 * <p>If called, this method will occur after {@link #onStop} for applications
2107 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
2108 * For applications targeting earlier platform versions this method will occur
2109 * before {@link #onStop} and there are no guarantees about whether it will
2110 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07002111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07002113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 * @see #onCreate
2115 * @see #onRestoreInstanceState
2116 * @see #onPause
2117 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002118 protected void onSaveInstanceState(@NonNull Bundle outState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002120
Phil Weaver846cda932017-06-15 10:10:06 -07002121 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002122 Parcelable p = mFragments.saveAllState();
2123 if (p != null) {
2124 outState.putParcelable(FRAGMENTS_TAG, p);
2125 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002126 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002127 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
2128 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002129 }
Ian Lake8a88cd62018-09-28 13:25:20 -07002130 dispatchActivitySaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
2132
2133 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002134 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07002135 * created with the attribute {@link android.R.attr#persistableMode} set to
2136 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
2137 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
2138 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07002139 *
2140 * @param outState Bundle in which to place your saved state.
2141 * @param outPersistentState State which will be saved across reboots.
2142 *
2143 * @see #onSaveInstanceState(Bundle)
2144 * @see #onCreate
2145 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
2146 * @see #onPause
2147 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002148 public void onSaveInstanceState(@NonNull Bundle outState,
2149 @NonNull PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07002150 onSaveInstanceState(outState);
2151 }
2152
2153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 * Save the state of any managed dialogs.
2155 *
2156 * @param outState place to store the saved state.
2157 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002158 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 private void saveManagedDialogs(Bundle outState) {
2160 if (mManagedDialogs == null) {
2161 return;
2162 }
2163
2164 final int numDialogs = mManagedDialogs.size();
2165 if (numDialogs == 0) {
2166 return;
2167 }
2168
2169 Bundle dialogState = new Bundle();
2170
2171 int[] ids = new int[mManagedDialogs.size()];
2172
2173 // save each dialog's bundle, gather the ids
2174 for (int i = 0; i < numDialogs; i++) {
2175 final int key = mManagedDialogs.keyAt(i);
2176 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002177 final ManagedDialog md = mManagedDialogs.valueAt(i);
2178 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
2179 if (md.mArgs != null) {
2180 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
2181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183
2184 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
2185 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
2186 }
2187
2188
2189 /**
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002190 * Called as part of the activity lifecycle when the user no longer actively interacts with the
2191 * activity, but it is still visible on screen. The counterpart to {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 *
2193 * <p>When activity B is launched in front of activity A, this callback will
2194 * be invoked on A. B will not be created until A's {@link #onPause} returns,
2195 * so be sure to not do anything lengthy here.
2196 *
2197 * <p>This callback is mostly used for saving any persistent state the
2198 * activity is editing, to present a "edit in place" model to the user and
2199 * making sure nothing is lost if there are not enough resources to start
2200 * the new activity without first killing this one. This is also a good
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002201 * place to stop things that consume a noticeable amount of CPU in order to
2202 * make the switch to the next activity as fast as possible.
RoboErik55011652014-07-09 15:05:53 -07002203 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002204 * <p>On platform versions prior to {@link android.os.Build.VERSION_CODES#Q} this is also a good
2205 * place to try to close exclusive-access devices or to release access to singleton resources.
2206 * Starting with {@link android.os.Build.VERSION_CODES#Q} there can be multiple resumed
2207 * activities in the system at the same time, so {@link #onTopResumedActivityChanged(boolean)}
2208 * should be used for that purpose instead.
RoboErik55011652014-07-09 15:05:53 -07002209 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002210 * <p>If an activity is launched on top, after receiving this call you will usually receive a
2211 * following call to {@link #onStop} (after the next activity has been resumed and displayed
2212 * above). However in some cases there will be a direct call back to {@link #onResume} without
2213 * going through the stopped state. An activity can also rest in paused state in some cases when
2214 * in multi-window mode, still visible to user.
RoboErik55011652014-07-09 15:05:53 -07002215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 * <p><em>Derived classes must call through to the super class's
2217 * implementation of this method. If they do not, an exception will be
2218 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002219 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 * @see #onResume
2221 * @see #onSaveInstanceState
2222 * @see #onStop
2223 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002224 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002226 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07002227 dispatchActivityPaused();
Dake Gu67decfa2017-12-27 11:48:08 -08002228 if (mAutoFillResetNeeded) {
2229 if (!mAutoFillIgnoreFirstResumePause) {
2230 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
2231 View focus = getCurrentFocus();
2232 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
2233 getAutofillManager().notifyViewExited(focus);
2234 }
2235 } else {
2236 // reset after first pause()
2237 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
2238 mAutoFillIgnoreFirstResumePause = false;
2239 }
2240 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002241
2242 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_PAUSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 mCalled = true;
2244 }
2245
2246 /**
2247 * Called as part of the activity lifecycle when an activity is about to go
2248 * into the background as the result of user choice. For example, when the
2249 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
2250 * when an incoming phone call causes the in-call Activity to be automatically
2251 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
2252 * the activity being interrupted. In cases when it is invoked, this method
2253 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07002254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 * <p>This callback and {@link #onUserInteraction} are intended to help
2256 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002257 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07002258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 * @see #onUserInteraction()
Louis Changc2e193a2019-01-09 15:33:57 +08002260 * @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 */
2262 protected void onUserLeaveHint() {
2263 }
RoboErik55011652014-07-09 15:05:53 -07002264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002266 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002268 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002270 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 }
2272
2273 /**
2274 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002275 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07002277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 * <p>The default implementation returns null, which will cause you to
2279 * inherit the description from the previous activity. If all activities
2280 * return null, generally the label of the top activity will be used as the
2281 * description.
RoboErik55011652014-07-09 15:05:53 -07002282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 * @return A description of what the user is doing. It should be short and
2284 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07002285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002287 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002289 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 public CharSequence onCreateDescription() {
2291 return null;
2292 }
2293
2294 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002295 * This is called when the user is requesting an assist, to build a full
2296 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
2297 * application. You can override this method to place into the bundle anything
2298 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002299 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002300 *
2301 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07002302 * been registered with {@link Application#registerOnProvideAssistDataListener
2303 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002304 */
2305 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002306 }
2307
2308 /**
2309 * This is called when the user is requesting an assist, to provide references
2310 * to content related to the current activity. Before being called, the
2311 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
2312 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
2313 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
2314 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
2315 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
2316 *
2317 * <p>Custom implementation may adjust the content intent to better reflect the top-level
2318 * context of the activity, and fill in its ClipData with additional content of
2319 * interest that the user is currently viewing. For example, an image gallery application
2320 * that has launched in to an activity allowing the user to swipe through pictures should
2321 * modify the intent to reference the current image they are looking it; such an
2322 * application when showing a list of pictures should add a ClipData that has
2323 * references to all of the pictures currently visible on screen.</p>
2324 *
2325 * @param outContent The assist content to return.
2326 */
2327 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002328 }
2329
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002330 /**
Sunny Goyald40c3452019-03-20 12:46:55 -07002331 * Returns the list of direct actions supported by the app.
2332 *
2333 * <p>You should return the list of actions that could be executed in the
2334 * current context, which is in the current state of the app. If the actions
2335 * that could be executed by the app changes you should report that via
2336 * calling {@link VoiceInteractor#notifyDirectActionsChanged()}.
2337 *
2338 * <p>To get the voice interactor you need to call {@link #getVoiceInteractor()}
Andrew Solovay7ad21702019-06-06 15:10:43 -07002339 * which would return non <code>null</code> only if there is an ongoing voice
Sunny Goyald40c3452019-03-20 12:46:55 -07002340 * interaction session. You an also detect when the voice interactor is no
2341 * longer valid because the voice interaction session that is backing is finished
2342 * by calling {@link VoiceInteractor#registerOnDestroyedCallback(Executor, Runnable)}.
2343 *
2344 * <p>This method will be called only after {@link #onStart()} is being called and
2345 * before {@link #onStop()} is being called.
2346 *
Svet Ganov3b6be082019-04-28 10:21:01 -07002347 * <p>You should pass to the callback the currently supported direct actions which
Andrew Solovay7ad21702019-06-06 15:10:43 -07002348 * cannot be <code>null</code> or contain <code>null</code> elements.
Svet Ganov3b6be082019-04-28 10:21:01 -07002349 *
2350 * <p>You should return the action list as soon as possible to ensure the consumer,
2351 * for example the assistant, is as responsive as possible which would improve user
2352 * experience of your app.
2353 *
2354 * @param cancellationSignal A signal to cancel the operation in progress.
2355 * @param callback The callback to send the action list. The actions list cannot
Svet Ganov07e5fb22019-04-30 11:07:09 -07002356 * contain <code>null</code> elements. You can call this on any thread.
Sunny Goyald40c3452019-03-20 12:46:55 -07002357 */
Svet Ganov3b6be082019-04-28 10:21:01 -07002358 public void onGetDirectActions(@NonNull CancellationSignal cancellationSignal,
2359 @NonNull Consumer<List<DirectAction>> callback) {
2360 callback.accept(Collections.emptyList());
Sunny Goyald40c3452019-03-20 12:46:55 -07002361 }
2362
2363 /**
2364 * This is called to perform an action previously defined by the app.
2365 * Apps also have access to {@link #getVoiceInteractor()} to follow up on the action.
2366 *
Svet Ganov07e5fb22019-04-30 11:07:09 -07002367 * @param actionId The ID for the action you previously reported via
2368 * {@link #onGetDirectActions(CancellationSignal, Consumer)}.
2369 * @param arguments Any additional arguments provided by the caller that are
2370 * specific to the given action.
Svet Ganov3b6be082019-04-28 10:21:01 -07002371 * @param cancellationSignal A signal to cancel the operation in progress.
Svet Ganov07e5fb22019-04-30 11:07:09 -07002372 * @param resultListener The callback to provide the result back to the caller.
2373 * You can call this on any thread. The result bundle is action specific.
Sunny Goyald40c3452019-03-20 12:46:55 -07002374 *
Svet Ganov3b6be082019-04-28 10:21:01 -07002375 * @see #onGetDirectActions(CancellationSignal, Consumer)
Sunny Goyald40c3452019-03-20 12:46:55 -07002376 */
2377 public void onPerformDirectAction(@NonNull String actionId,
Svet Ganov3b6be082019-04-28 10:21:01 -07002378 @NonNull Bundle arguments, @NonNull CancellationSignal cancellationSignal,
Sunny Goyald40c3452019-03-20 12:46:55 -07002379 @NonNull Consumer<Bundle> resultListener) { }
2380
2381 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01002382 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002383 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
2384 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01002385 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002386 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002387 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002388 sendBroadcastAsUser(intent, Process.myUserHandle());
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002389 }
2390
2391 /**
2392 * Dismiss the Keyboard Shortcuts screen.
2393 */
2394 public final void dismissKeyboardShortcutsHelper() {
2395 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002396 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002397 sendBroadcastAsUser(intent, Process.myUserHandle());
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002398 }
2399
Clara Bayarri75e09792015-07-29 16:20:40 +01002400 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00002401 public void onProvideKeyboardShortcuts(
2402 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01002403 if (menu == null) {
2404 return;
2405 }
2406 KeyboardShortcutGroup group = null;
2407 int menuSize = menu.size();
2408 for (int i = 0; i < menuSize; ++i) {
2409 final MenuItem item = menu.getItem(i);
2410 final CharSequence title = item.getTitle();
2411 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01002412 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01002413 if (title != null && alphaShortcut != MIN_VALUE) {
2414 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00002415 final int resource = mApplication.getApplicationInfo().labelRes;
2416 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01002417 }
2418 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01002419 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01002420 }
2421 }
2422 if (group != null) {
2423 data.add(group);
2424 }
2425 }
2426
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002427 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07002428 * Ask to have the current assistant shown to the user. This only works if the calling
2429 * activity is the current foreground activity. It is the same as calling
2430 * {@link android.service.voice.VoiceInteractionService#showSession
2431 * VoiceInteractionService.showSession} and requesting all of the possible context.
2432 * The receiver will always see
2433 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
2434 * @return Returns true if the assistant was successfully invoked, else false. For example
2435 * false will be returned if the caller is not the current top activity.
2436 */
2437 public boolean showAssist(Bundle args) {
2438 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002439 return ActivityTaskManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07002440 } catch (RemoteException e) {
2441 }
2442 return false;
2443 }
2444
2445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 * Called when you are no longer visible to the user. You will next
2447 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002448 * depending on later user activity. This is a good place to stop
2449 * refreshing UI, running animations and other visual things.
RoboErik55011652014-07-09 15:05:53 -07002450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * <p><em>Derived classes must call through to the super class's
2452 * implementation of this method. If they do not, an exception will be
2453 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 * @see #onRestart
2456 * @see #onResume
2457 * @see #onSaveInstanceState
2458 * @see #onDestroy
2459 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002460 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002462 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08002463 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07002464 mActivityTransitionState.onStop();
Ian Lake8a88cd62018-09-28 13:25:20 -07002465 dispatchActivityStopped();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002466 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07002468
Dake Gu67decfa2017-12-27 11:48:08 -08002469 if (mAutoFillResetNeeded) {
2470 getAutofillManager().onInvisibleForAutofill();
2471 }
2472
Felipe Lemec24a56a2017-08-03 14:27:57 -07002473 if (isFinishing()) {
2474 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08002475 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07002476 } else if (mIntent != null
2477 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
2478 // Activity was launched when user tapped a link in the Autofill Save UI - since
2479 // user launched another activity, the Save UI should not be restored when this
2480 // activity is finished.
2481 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
2482 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
2483 }
Felipe Lemebab851c2017-02-03 18:45:08 -08002484 }
chaviwfeb2e1e2018-12-19 17:24:11 -08002485 mEnterAnimationComplete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
2487
2488 /**
2489 * Perform any final cleanup before an activity is destroyed. This can
2490 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07002491 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 * this instance of the activity to save space. You can distinguish
2493 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07002494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 * <p><em>Note: do not count on this method being called as a place for
2496 * saving data! For example, if an activity is editing data in a content
2497 * provider, those edits should be committed in either {@link #onPause} or
2498 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
2499 * free resources like threads that are associated with an activity, so
2500 * that a destroyed activity does not leave such things around while the
2501 * rest of its application is still running. There are situations where
2502 * the system will simply kill the activity's hosting process without
2503 * calling this method (or any others) in it, so it should not be used to
2504 * do things that are intended to remain around after the process goes
2505 * away.
RoboErik55011652014-07-09 15:05:53 -07002506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * <p><em>Derived classes must call through to the super class's
2508 * implementation of this method. If they do not, an exception will be
2509 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002510 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 * @see #onPause
2512 * @see #onStop
2513 * @see #finish
2514 * @see #isFinishing
2515 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002516 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002518 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 mCalled = true;
2520
2521 // dismiss any dialogs we are managing.
2522 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 final int numDialogs = mManagedDialogs.size();
2524 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002525 final ManagedDialog md = mManagedDialogs.valueAt(i);
2526 if (md.mDialog.isShowing()) {
2527 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
2529 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002530 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532
2533 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002534 synchronized (mManagedCursors) {
2535 int numCursors = mManagedCursors.size();
2536 for (int i = 0; i < numCursors; i++) {
2537 ManagedCursor c = mManagedCursors.get(i);
2538 if (c != null) {
2539 c.mCursor.close();
2540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002542 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 }
Amith Yamasani49860442010-03-17 20:54:10 -07002544
2545 // Close any open search dialog
2546 if (mSearchManager != null) {
2547 mSearchManager.stopSearch();
2548 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002549
Chris Banes21b25772016-01-04 20:41:59 +00002550 if (mActionBar != null) {
2551 mActionBar.onDestroy();
2552 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002553
Ian Lake8a88cd62018-09-28 13:25:20 -07002554 dispatchActivityDestroyed();
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002555
2556 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_STOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
2558
2559 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002560 * Report to the system that your app is now fully drawn, purely for diagnostic
2561 * purposes (calling it does not impact the visible behavior of the activity).
2562 * This is only used to help instrument application launch times, so that the
2563 * app can report when it is fully in a usable state; without this, the only thing
2564 * the system itself can determine is the point at which the activity's window
2565 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002566 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002567 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002568 * entirely drawn your UI and populated with all of the significant data. You
2569 * can safely call this method any time after first launch as well, in which case
2570 * it will simply be ignored.
2571 */
2572 public void reportFullyDrawn() {
2573 if (mDoReportFullyDrawn) {
2574 mDoReportFullyDrawn = false;
2575 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002576 ActivityTaskManager.getService().reportActivityFullyDrawn(
2577 mToken, mRestoredFromBundle);
Mathieu Chartier63e14c12019-03-07 09:05:44 -08002578 VMRuntime.getRuntime().notifyStartupCompleted();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002579 } catch (RemoteException e) {
2580 }
2581 }
2582 }
2583
2584 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002585 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002586 * visa-versa. This method provides the same configuration that will be sent in the following
2587 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2588 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002589 * @see android.R.attr#resizeableActivity
2590 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002591 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002592 * @param newConfig The new configuration of the activity with the state
2593 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002594 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002595 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2596 // Left deliberately empty. There should be no side effects if a direct
2597 // subclass of Activity does not call super.
2598 onMultiWindowModeChanged(isInMultiWindowMode);
2599 }
2600
2601 /**
2602 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2603 * visa-versa.
2604 *
2605 * @see android.R.attr#resizeableActivity
2606 *
2607 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2608 *
2609 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2610 */
2611 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002612 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002613 // Left deliberately empty. There should be no side effects if a direct
2614 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002615 }
2616
2617 /**
2618 * Returns true if the activity is currently in multi-window mode.
2619 * @see android.R.attr#resizeableActivity
2620 *
2621 * @return True if the activity is in multi-window mode.
2622 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002623 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002624 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002625 return ActivityTaskManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002626 } catch (RemoteException e) {
2627 }
2628 return false;
2629 }
2630
2631 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002632 * Called by the system when the activity changes to and from picture-in-picture mode. This
2633 * method provides the same configuration that will be sent in the following
2634 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2635 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002636 * @see android.R.attr#supportsPictureInPicture
2637 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002638 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002639 * @param newConfig The new configuration of the activity with the state
2640 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002641 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002642 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2643 Configuration newConfig) {
2644 // Left deliberately empty. There should be no side effects if a direct
2645 // subclass of Activity does not call super.
2646 onPictureInPictureModeChanged(isInPictureInPictureMode);
2647 }
2648
2649 /**
2650 * Called by the system when the activity changes to and from picture-in-picture mode.
2651 *
2652 * @see android.R.attr#supportsPictureInPicture
2653 *
2654 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2655 *
2656 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2657 */
2658 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002659 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002660 // Left deliberately empty. There should be no side effects if a direct
2661 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002662 }
2663
2664 /**
2665 * Returns true if the activity is currently in picture-in-picture mode.
2666 * @see android.R.attr#supportsPictureInPicture
2667 *
2668 * @return True if the activity is in picture-in-picture mode.
2669 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002670 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002671 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002672 return ActivityTaskManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002673 } catch (RemoteException e) {
2674 }
2675 return false;
2676 }
2677
2678 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002679 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002680 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2681 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002682 *
Winson Chung709904f2017-04-25 11:00:48 -07002683 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002684 * @see android.R.attr#supportsPictureInPicture
2685 */
Winson Chung709904f2017-04-25 11:00:48 -07002686 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002687 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002688 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002689 }
2690
Jeff Sharkey000ce802017-04-29 13:13:27 -06002691 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002692 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002693 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002694 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002695 }
2696
2697 /**
Winson Chung709904f2017-04-25 11:00:48 -07002698 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2699 * set parameters in {@param params} will be combined with the parameters from prior calls to
2700 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2701 *
2702 * The system may disallow entering picture-in-picture in various cases, including when the
2703 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2704 *
2705 * @see android.R.attr#supportsPictureInPicture
2706 * @see PictureInPictureParams
2707 *
2708 * @param params non-null parameters to be combined with previously set parameters when entering
2709 * picture-in-picture.
2710 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002711 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002712 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002713 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002714 */
2715 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2716 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002717 if (!deviceSupportsPictureInPictureMode()) {
2718 return false;
2719 }
Winson Chung709904f2017-04-25 11:00:48 -07002720 if (params == null) {
2721 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2722 }
Winson Chung298f95b2017-08-10 15:57:18 -07002723 if (!mCanEnterPictureInPicture) {
2724 throw new IllegalStateException("Activity must be resumed to enter"
2725 + " picture-in-picture");
2726 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002727 return ActivityTaskManager.getService().enterPictureInPictureMode(mToken, params);
Winson Chung709904f2017-04-25 11:00:48 -07002728 } catch (RemoteException e) {
2729 return false;
2730 }
2731 }
2732
Jeff Sharkey000ce802017-04-29 13:13:27 -06002733 /** @removed */
2734 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002735 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002736 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002737 }
2738
2739 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002740 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2741 * {@link #enterPictureInPictureMode()} is called.
2742 *
Winson Chung709904f2017-04-25 11:00:48 -07002743 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002744 */
Winson Chung709904f2017-04-25 11:00:48 -07002745 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002746 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002747 if (!deviceSupportsPictureInPictureMode()) {
2748 return;
2749 }
Winson Chung709904f2017-04-25 11:00:48 -07002750 if (params == null) {
2751 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002752 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002753 ActivityTaskManager.getService().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002754 } catch (RemoteException e) {
2755 }
2756 }
2757
Winson Chung8802eac2017-04-17 14:21:44 -07002758 /**
Winson Chung709904f2017-04-25 11:00:48 -07002759 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2760 * user interacts with the activity currently in picture-in-picture mode. This number may change
2761 * if the global configuration changes (ie. if the device is plugged into an external display),
2762 * but will always be larger than three.
2763 */
2764 public int getMaxNumPictureInPictureActions() {
2765 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002766 return ActivityTaskManager.getService().getMaxNumPictureInPictureActions(mToken);
Winson Chung709904f2017-04-25 11:00:48 -07002767 } catch (RemoteException e) {
2768 return 0;
2769 }
2770 }
2771
Winson Chung80fa2d82018-01-25 13:58:20 -08002772 /**
2773 * @return Whether this device supports picture-in-picture.
2774 */
2775 private boolean deviceSupportsPictureInPictureMode() {
2776 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2777 }
2778
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002779 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002780 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002781 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002782 }
2783
2784 /**
2785 * Called by the system when the activity is moved from one display to another without
2786 * recreation. This means that this activity is declared to handle all changes to configuration
2787 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002788 * again.
2789 *
2790 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2791 * applied configuration actually changed. It is up to app developer to choose whether to handle
2792 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2793 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002794 *
2795 * <p>Use this callback to track changes to the displays if some activity functionality relies
2796 * on an association with some display properties.
2797 *
2798 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002799 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002800 *
2801 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002802 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002803 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002804 */
Artur Satayev5a525852019-10-31 15:15:50 +00002805 @UnsupportedAppUsage
Tiger Huangfc218452018-09-27 00:38:50 +08002806 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002807 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002808 }
2809
Winson Chungb5c41b72016-12-07 15:00:47 -08002810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 * Called by the system when the device configuration changes while your
2812 * activity is running. Note that this will <em>only</em> be called if
2813 * you have selected configurations you would like to handle with the
2814 * {@link android.R.attr#configChanges} attribute in your manifest. If
2815 * any configuration change occurs that is not selected to be reported
2816 * by that attribute, then instead of reporting it the system will stop
2817 * and restart the activity (to have it launched with the new
2818 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002819 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 * <p>At the time that this function has been called, your Resources
2821 * object will have been updated to return resource values matching the
2822 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 * @param newConfig The new device configuration.
2825 */
Ian Lake666a9652018-09-12 15:13:19 -07002826 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002827 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002829
Dianne Hackborn9d071802010-12-08 14:49:15 -08002830 mFragments.dispatchConfigurationChanged(newConfig);
2831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 if (mWindow != null) {
2833 // Pass the configuration changed event to the window
2834 mWindow.onConfigurationChanged(newConfig);
2835 }
Adam Powell45c0b192011-07-28 15:11:57 -07002836
2837 if (mActionBar != null) {
2838 // Do this last; the action bar will need to access
2839 // view changes from above.
2840 mActionBar.onConfigurationChanged(newConfig);
2841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
RoboErik55011652014-07-09 15:05:53 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 /**
2845 * If this activity is being destroyed because it can not handle a
2846 * configuration parameter being changed (and thus its
2847 * {@link #onConfigurationChanged(Configuration)} method is
2848 * <em>not</em> being called), then you can use this method to discover
2849 * the set of changes that have occurred while in the process of being
2850 * destroyed. Note that there is no guarantee that these will be
2851 * accurate (other changes could have happened at any time), so you should
2852 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002853 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 * @return Returns a bit field of the configuration parameters that are
2855 * changing, as defined by the {@link android.content.res.Configuration}
2856 * class.
2857 */
2858 public int getChangingConfigurations() {
2859 return mConfigChangeFlags;
2860 }
RoboErik55011652014-07-09 15:05:53 -07002861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 /**
2863 * Retrieve the non-configuration instance data that was previously
2864 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2865 * be available from the initial {@link #onCreate} and
2866 * {@link #onStart} calls to the new instance, allowing you to extract
2867 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002868 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 * <p>Note that the data you retrieve here should <em>only</em> be used
2870 * as an optimization for handling configuration changes. You should always
2871 * be able to handle getting a null pointer back, and an activity must
2872 * still be able to restore itself to its previous state (through the
2873 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2874 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002875 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002876 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002877 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002878 * available on older platforms through the Android support libraries.
2879 *
2880 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002882 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002884 return mLastNonConfigurationInstances != null
2885 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 }
RoboErik55011652014-07-09 15:05:53 -07002887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 /**
2889 * Called by the system, as part of destroying an
2890 * activity due to a configuration change, when it is known that a new
2891 * instance will immediately be created for the new configuration. You
2892 * can return any object you like here, including the activity instance
2893 * itself, which can later be retrieved by calling
2894 * {@link #getLastNonConfigurationInstance()} in the new activity
2895 * instance.
RoboErik55011652014-07-09 15:05:53 -07002896 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002897 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2898 * or later, consider instead using a {@link Fragment} with
2899 * {@link Fragment#setRetainInstance(boolean)
2900 * Fragment.setRetainInstance(boolean}.</em>
2901 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 * <p>This function is called purely as an optimization, and you must
2903 * not rely on it being called. When it is called, a number of guarantees
2904 * will be made to help optimize configuration switching:
2905 * <ul>
2906 * <li> The function will be called between {@link #onStop} and
2907 * {@link #onDestroy}.
2908 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002909 * created after this one's {@link #onDestroy()} is called. In particular,
2910 * <em>no</em> messages will be dispatched during this time (when the returned
2911 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 * <li> The object you return here will <em>always</em> be available from
2913 * the {@link #getLastNonConfigurationInstance()} method of the following
2914 * activity instance as described there.
2915 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002916 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 * <p>These guarantees are designed so that an activity can use this API
2918 * to propagate extensive state from the old to new activity instance, from
2919 * loaded bitmaps, to network connections, to evenly actively running
2920 * threads. Note that you should <em>not</em> propagate any data that
2921 * may change based on the configuration, including any data loaded from
2922 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002923 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002924 * <p>The guarantee of no message handling during the switch to the next
2925 * activity simplifies use with active objects. For example if your retained
2926 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2927 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2928 * not be called from the call here until you execute the next instance's
2929 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2930 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2931 * running in a separate thread.)
2932 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002933 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002934 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002935 * available on older platforms through the Android support libraries.
2936 *
2937 * @return any Object holding the desired state to propagate to the
2938 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 */
2940 public Object onRetainNonConfigurationInstance() {
2941 return null;
2942 }
RoboErik55011652014-07-09 15:05:53 -07002943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 /**
2945 * Retrieve the non-configuration instance data that was previously
2946 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2947 * be available from the initial {@link #onCreate} and
2948 * {@link #onStart} calls to the new instance, allowing you to extract
2949 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002950 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 * <p>Note that the data you retrieve here should <em>only</em> be used
2952 * as an optimization for handling configuration changes. You should always
2953 * be able to handle getting a null pointer back, and an activity must
2954 * still be able to restore itself to its previous state (through the
2955 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2956 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 * @return Returns the object previously returned by
2959 * {@link #onRetainNonConfigurationChildInstances()}
2960 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002961 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002962 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2963 return mLastNonConfigurationInstances != null
2964 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 }
RoboErik55011652014-07-09 15:05:53 -07002966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 /**
2968 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2969 * it should return either a mapping from child activity id strings to arbitrary objects,
2970 * or null. This method is intended to be used by Activity framework subclasses that control a
2971 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2972 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2973 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002974 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2976 return null;
2977 }
RoboErik55011652014-07-09 15:05:53 -07002978
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002979 NonConfigurationInstances retainNonConfigurationInstances() {
2980 Object activity = onRetainNonConfigurationInstance();
2981 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002982 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002983
2984 // We're already stopped but we've been asked to retain.
2985 // Our fragments are taken care of but we need to mark the loaders for retention.
2986 // In order to do this correctly we need to restart the loaders first before
2987 // handing them off to the next activity.
2988 mFragments.doLoaderStart();
2989 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002990 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002991
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002992 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002993 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002994 return null;
2995 }
RoboErik55011652014-07-09 15:05:53 -07002996
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002997 NonConfigurationInstances nci = new NonConfigurationInstances();
2998 nci.activity = activity;
2999 nci.children = children;
3000 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003001 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07003002 if (mVoiceInteractor != null) {
3003 mVoiceInteractor.retainInstance();
3004 nci.voiceInteractor = mVoiceInteractor;
3005 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003006 return nci;
3007 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003010 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08003012 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003014
3015 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003016 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003017 mCalled = true;
3018 mFragments.dispatchTrimMemory(level);
3019 }
3020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003022 * Return the FragmentManager for interacting with fragments associated
3023 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08003024 *
3025 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003026 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003027 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003028 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003029 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07003030 }
RoboErik55011652014-07-09 15:05:53 -07003031
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003032 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07003033 * Called when a Fragment is being attached to this activity, immediately
3034 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
3035 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08003036 *
3037 * @deprecated Use {@link
3038 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07003039 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003040 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07003041 public void onAttachFragment(Fragment fragment) {
3042 }
RoboErik55011652014-07-09 15:05:53 -07003043
Dianne Hackbornc8017682010-07-06 13:34:38 -07003044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 * Wrapper around
3046 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3047 * that gives the resulting {@link Cursor} to call
3048 * {@link #startManagingCursor} so that the activity will manage its
3049 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003050 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003051 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3052 * or later, consider instead using {@link LoaderManager} instead, available
3053 * via {@link #getLoaderManager()}.</em>
3054 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003055 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3056 * this method, because the activity will do that for you at the appropriate time. However, if
3057 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3058 * not</em> automatically close the cursor and, in that case, you must call
3059 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003060 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 * @param uri The URI of the content provider to query.
3062 * @param projection List of columns to return.
3063 * @param selection SQL WHERE clause.
3064 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003065 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003067 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3069 * @see #startManagingCursor
3070 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05003071 *
3072 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 */
Jason parks6ed50de2010-08-25 10:18:50 -05003074 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003075 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07003076 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3077 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
3079 if (c != null) {
3080 startManagingCursor(c);
3081 }
3082 return c;
3083 }
3084
3085 /**
3086 * Wrapper around
3087 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3088 * that gives the resulting {@link Cursor} to call
3089 * {@link #startManagingCursor} so that the activity will manage its
3090 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003091 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003092 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3093 * or later, consider instead using {@link LoaderManager} instead, available
3094 * via {@link #getLoaderManager()}.</em>
3095 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003096 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3097 * this method, because the activity will do that for you at the appropriate time. However, if
3098 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3099 * not</em> automatically close the cursor and, in that case, you must call
3100 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 * @param uri The URI of the content provider to query.
3103 * @param projection List of columns to return.
3104 * @param selection SQL WHERE clause.
3105 * @param selectionArgs The arguments to selection, if any ?s are pesent
3106 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3111 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05003112 *
3113 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 */
Jason parks6ed50de2010-08-25 10:18:50 -05003115 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07003116 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3117 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
3119 if (c != null) {
3120 startManagingCursor(c);
3121 }
3122 return c;
3123 }
3124
3125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 * This method allows the activity to take care of managing the given
3127 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
3128 * That is, when the activity is stopped it will automatically call
3129 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
3130 * it will call {@link Cursor#requery} for you. When the activity is
3131 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07003132 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003133 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3134 * or later, consider instead using {@link LoaderManager} instead, available
3135 * via {@link #getLoaderManager()}.</em>
3136 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003137 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
3138 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
3139 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
3140 * <em>will not</em> automatically close the cursor and, in that case, you must call
3141 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07003144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
3146 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003147 *
3148 * @deprecated Use the new {@link android.content.CursorLoader} class with
3149 * {@link LoaderManager} instead; this is also
3150 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 */
Jason parks6ed50de2010-08-25 10:18:50 -05003152 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 public void startManagingCursor(Cursor c) {
3154 synchronized (mManagedCursors) {
3155 mManagedCursors.add(new ManagedCursor(c));
3156 }
3157 }
3158
3159 /**
3160 * Given a Cursor that was previously given to
3161 * {@link #startManagingCursor}, stop the activity's management of that
3162 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003163 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003164 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003165 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003166 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003171 *
3172 * @deprecated Use the new {@link android.content.CursorLoader} class with
3173 * {@link LoaderManager} instead; this is also
3174 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 */
Jason parks6ed50de2010-08-25 10:18:50 -05003176 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 public void stopManagingCursor(Cursor c) {
3178 synchronized (mManagedCursors) {
3179 final int N = mManagedCursors.size();
3180 for (int i=0; i<N; i++) {
3181 ManagedCursor mc = mManagedCursors.get(i);
3182 if (mc.mCursor == c) {
3183 mManagedCursors.remove(i);
3184 break;
3185 }
3186 }
3187 }
3188 }
3189
3190 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003191 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3192 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003193 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003195 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003196 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 }
3199
3200 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003201 * Finds a view that was identified by the {@code android:id} XML attribute
3202 * that was processed in {@link #onCreate}.
3203 * <p>
3204 * <strong>Note:</strong> In most cases -- depending on compiler support --
3205 * the resulting view is automatically cast to the target class type. If
3206 * the target class type is unconstrained, an explicit cast may be
3207 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003209 * @param id the ID to search for
3210 * @return a view with given ID if found, or {@code null} otherwise
3211 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003212 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003214 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003215 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 return getWindow().findViewById(id);
3217 }
RoboErik55011652014-07-09 15:05:53 -07003218
Adam Powell33b97432010-04-20 10:01:14 -07003219 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003220 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3221 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3222 * no matching view in the hierarchy.
3223 * <p>
3224 * <strong>Note:</strong> In most cases -- depending on compiler support --
3225 * the resulting view is automatically cast to the target class type. If
3226 * the target class type is unconstrained, an explicit cast may be
3227 * necessary.
3228 *
3229 * @param id the ID to search for
3230 * @return a view with given ID
3231 * @see View#requireViewById(int)
3232 * @see Activity#findViewById(int)
3233 */
3234 @NonNull
3235 public final <T extends View> T requireViewById(@IdRes int id) {
3236 T view = findViewById(id);
3237 if (view == null) {
3238 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3239 }
3240 return view;
3241 }
3242
3243 /**
Adam Powell33b97432010-04-20 10:01:14 -07003244 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003245 *
Adam Powell33b97432010-04-20 10:01:14 -07003246 * @return The Activity's ActionBar, or null if it does not have one.
3247 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003248 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003249 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003250 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003251 return mActionBar;
3252 }
Adam Powelle43340c2014-03-17 19:10:43 -07003253
3254 /**
3255 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3256 * Activity window.
3257 *
3258 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3259 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3260 * a traditional window decor action bar. The toolbar's menu will be populated with the
3261 * Activity's options menu and the navigation button will be wired through the standard
3262 * {@link android.R.id#home home} menu select action.</p>
3263 *
3264 * <p>In order to use a Toolbar within the Activity's window content the application
3265 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3266 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003267 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003268 */
Adam Powell37780142014-06-01 13:31:00 -07003269 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003270 final ActionBar ab = getActionBar();
3271 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003272 throw new IllegalStateException("This Activity already has an action bar supplied " +
3273 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3274 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3275 }
Chris Banes21b25772016-01-04 20:41:59 +00003276
3277 // If we reach here then we're setting a new action bar
3278 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003279 mMenuInflater = null;
3280
Chris Banes21b25772016-01-04 20:41:59 +00003281 // If we have an action bar currently, destroy it
3282 if (ab != null) {
3283 ab.onDestroy();
3284 }
3285
Chris Banesc7d6c322016-01-27 14:09:16 +00003286 if (toolbar != null) {
3287 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3288 mActionBar = tbab;
3289 mWindow.setCallback(tbab.getWrappedWindowCallback());
3290 } else {
3291 mActionBar = null;
3292 // Re-set the original window callback since we may have already set a Toolbar wrapper
3293 mWindow.setCallback(this);
3294 }
3295
3296 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003297 }
RoboErik55011652014-07-09 15:05:53 -07003298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 /**
Adam Powell33b97432010-04-20 10:01:14 -07003300 * Creates a new ActionBar, locates the inflated ActionBarView,
3301 * initializes the ActionBar with the view, and sets mActionBar.
3302 */
Adam Powelle43340c2014-03-17 19:10:43 -07003303 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003304 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003305
3306 // Initializing the window decor can change window feature flags.
3307 // Make sure that we have the correct set before performing the test below.
3308 window.getDecorView();
3309
Adam Powell9b4c8042010-08-10 15:36:44 -07003310 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003311 return;
3312 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003313
Adam Powelle43340c2014-03-17 19:10:43 -07003314 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003315 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003316
3317 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3318 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003319 }
RoboErik55011652014-07-09 15:05:53 -07003320
Adam Powell33b97432010-04-20 10:01:14 -07003321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 * Set the activity content from a layout resource. The resource will be
3323 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003324 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003326 *
Romain Guy482b34a62011-01-20 10:59:28 -08003327 * @see #setContentView(android.view.View)
3328 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003330 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003332 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 }
3334
3335 /**
3336 * Set the activity content to an explicit view. This view is placed
3337 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003338 * view hierarchy. When calling this method, the layout parameters of the
3339 * specified view are ignored. Both the width and the height of the view are
3340 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3341 * your own layout parameters, invoke
3342 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3343 * instead.
RoboErik55011652014-07-09 15:05:53 -07003344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003346 *
3347 * @see #setContentView(int)
3348 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 */
3350 public void setContentView(View view) {
3351 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003352 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 }
3354
3355 /**
3356 * Set the activity content to an explicit view. This view is placed
3357 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003358 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003359 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 * @param view The desired content to display.
3361 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003362 *
3363 * @see #setContentView(android.view.View)
3364 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 */
3366 public void setContentView(View view, ViewGroup.LayoutParams params) {
3367 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003368 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 }
3370
3371 /**
3372 * Add an additional content view to the activity. Added after any existing
3373 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003374 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 * @param view The desired content to display.
3376 * @param params Layout parameters for the view.
3377 */
3378 public void addContentView(View view, ViewGroup.LayoutParams params) {
3379 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003380 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382
3383 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003384 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3385 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3386 *
3387 * <p>This method will return non-null after content has been initialized (e.g. by using
3388 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3389 *
3390 * @return This window's content TransitionManager or null if none is set.
3391 */
3392 public TransitionManager getContentTransitionManager() {
3393 return getWindow().getTransitionManager();
3394 }
3395
3396 /**
3397 * Set the {@link TransitionManager} to use for default transitions in this window.
3398 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3399 *
3400 * @param tm The TransitionManager to use for scene changes.
3401 */
3402 public void setContentTransitionManager(TransitionManager tm) {
3403 getWindow().setTransitionManager(tm);
3404 }
3405
3406 /**
3407 * Retrieve the {@link Scene} representing this window's current content.
3408 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3409 *
3410 * <p>This method will return null if the current content is not represented by a Scene.</p>
3411 *
3412 * @return Current Scene being shown or null
3413 */
3414 public Scene getContentScene() {
3415 return getWindow().getContentScene();
3416 }
3417
3418 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003419 * Sets whether this activity is finished when touched outside its window's
3420 * bounds.
3421 */
3422 public void setFinishOnTouchOutside(boolean finish) {
3423 mWindow.setCloseOnTouchOutside(finish);
3424 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003425
3426 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003427 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003428 DEFAULT_KEYS_DISABLE,
3429 DEFAULT_KEYS_DIALER,
3430 DEFAULT_KEYS_SHORTCUT,
3431 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003432 DEFAULT_KEYS_SEARCH_GLOBAL
3433 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003434 @Retention(RetentionPolicy.SOURCE)
3435 @interface DefaultKeyMode {}
3436
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3439 * keys.
RoboErik55011652014-07-09 15:05:53 -07003440 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 * @see #setDefaultKeyMode
3442 */
3443 static public final int DEFAULT_KEYS_DISABLE = 0;
3444 /**
3445 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3446 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 * @see #setDefaultKeyMode
3449 */
3450 static public final int DEFAULT_KEYS_DIALER = 1;
3451 /**
3452 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3453 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 * @see #setDefaultKeyMode
3458 */
3459 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3460 /**
3461 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3462 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003463 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 * @see #setDefaultKeyMode
3468 */
3469 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3470
3471 /**
3472 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3473 * will start a global search (typically web search, but some platforms may define alternate
3474 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 * @see #setDefaultKeyMode
3479 */
3480 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3481
3482 /**
3483 * Select the default key handling for this activity. This controls what
3484 * will happen to key events that are not otherwise handled. The default
3485 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3486 * floor. Other modes allow you to launch the dialer
3487 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3488 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003489 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003491 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 * <p>Note that the mode selected here does not impact the default
3493 * handling of system keys, such as the "back" and "menu" keys, and your
3494 * activity and its views always get a first chance to receive and handle
3495 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003496 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003498 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 * @see #onKeyDown
3500 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003501 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 // Some modes use a SpannableStringBuilder to track & dispatch input events
3505 // This list must remain in sync with the switch in onKeyDown()
3506 switch (mode) {
3507 case DEFAULT_KEYS_DISABLE:
3508 case DEFAULT_KEYS_SHORTCUT:
3509 mDefaultKeySsb = null; // not used in these modes
3510 break;
3511 case DEFAULT_KEYS_DIALER:
3512 case DEFAULT_KEYS_SEARCH_LOCAL:
3513 case DEFAULT_KEYS_SEARCH_GLOBAL:
3514 mDefaultKeySsb = new SpannableStringBuilder();
3515 Selection.setSelection(mDefaultKeySsb,0);
3516 break;
3517 default:
3518 throw new IllegalArgumentException();
3519 }
3520 }
3521
3522 /**
3523 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003524 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 * is inside a TextView will not trigger the event (unless it is a navigation
3526 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003527 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 * <p>If the focused view didn't want this event, this method is called.
3529 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003530 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3531 * by calling {@link #onBackPressed()}, though the behavior varies based
3532 * on the application compatibility mode: for
3533 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3534 * it will set up the dispatch to call {@link #onKeyUp} where the action
3535 * will be performed; for earlier applications, it will perform the
3536 * action immediately in on-down, as those versions of the platform
3537 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003538 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003539 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003540 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003543 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 * this event and it should continue to be propagated.
3545 * @see #onKeyUp
3546 * @see android.view.KeyEvent
3547 */
3548 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003549 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003550 if (getApplicationInfo().targetSdkVersion
3551 >= Build.VERSION_CODES.ECLAIR) {
3552 event.startTracking();
3553 } else {
3554 onBackPressed();
3555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 return true;
3557 }
RoboErik55011652014-07-09 15:05:53 -07003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3560 return false;
3561 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003562 Window w = getWindow();
3563 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3564 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3565 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003566 return true;
3567 }
3568 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003569 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3570 // Don't consume TAB here since it's used for navigation. Arrow keys
3571 // aren't considered "typing keys" so they already won't get consumed.
3572 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 } else {
3574 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3575 boolean clearSpannable = false;
3576 boolean handled;
3577 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3578 clearSpannable = true;
3579 handled = false;
3580 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003581 handled = TextKeyListener.getInstance().onKeyDown(
3582 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 if (handled && mDefaultKeySsb.length() > 0) {
3584 // something useable has been typed - dispatch it now.
3585
3586 final String str = mDefaultKeySsb.toString();
3587 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 switch (mDefaultKeyMode) {
3590 case DEFAULT_KEYS_DIALER:
3591 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3592 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003593 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 break;
3595 case DEFAULT_KEYS_SEARCH_LOCAL:
3596 startSearch(str, false, null, false);
3597 break;
3598 case DEFAULT_KEYS_SEARCH_GLOBAL:
3599 startSearch(str, false, null, true);
3600 break;
3601 }
3602 }
3603 }
3604 if (clearSpannable) {
3605 mDefaultKeySsb.clear();
3606 mDefaultKeySsb.clearSpans();
3607 Selection.setSelection(mDefaultKeySsb,0);
3608 }
3609 return handled;
3610 }
3611 }
3612
3613 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003614 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3615 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3616 * the event).
Siarhei Vishniakouccf973f2018-10-30 14:04:57 -07003617 *
3618 * To receive this callback, you must return true from onKeyDown for the current
3619 * event stream.
3620 *
3621 * @see KeyEvent.Callback#onKeyLongPress()
3622 * @see KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003623 */
3624 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3625 return false;
3626 }
3627
3628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003630 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 * is inside a TextView will not trigger the event (unless it is a navigation
3632 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003633 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003634 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3635 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003636 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003638 * further, or <code>false</code> to indicate that you have not handled
3639 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 * @see #onKeyDown
3641 * @see KeyEvent
3642 */
3643 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003644 if (getApplicationInfo().targetSdkVersion
3645 >= Build.VERSION_CODES.ECLAIR) {
3646 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3647 && !event.isCanceled()) {
3648 onBackPressed();
3649 return true;
3650 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 return false;
3653 }
3654
3655 /**
3656 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3657 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3658 * the event).
3659 */
3660 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3661 return false;
3662 }
RoboErik55011652014-07-09 15:05:53 -07003663
Mark Renouf1c25fc52019-09-09 13:13:43 -04003664 private static final class RequestFinishCallback extends IRequestFinishCallback.Stub {
3665 private final WeakReference<Activity> mActivityRef;
3666
3667 RequestFinishCallback(WeakReference<Activity> activityRef) {
3668 mActivityRef = activityRef;
3669 }
3670
3671 @Override
3672 public void requestFinish() {
3673 Activity activity = mActivityRef.get();
3674 if (activity != null) {
3675 activity.mHandler.post(activity::finishAfterTransition);
3676 }
3677 }
3678 }
3679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003681 * Called when the activity has detected the user's press of the back
3682 * key. The default implementation simply finishes the current activity,
3683 * but you can override this to do whatever you want.
3684 */
3685 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003686 if (mActionBar != null && mActionBar.collapseActionView()) {
3687 return;
3688 }
3689
George Mount27b0dc02017-02-21 10:24:09 -08003690 FragmentManager fragmentManager = mFragments.getFragmentManager();
3691
Mark Renouf446251d2019-04-26 10:22:41 -04003692 if (!fragmentManager.isStateSaved() && fragmentManager.popBackStackImmediate()) {
3693 return;
3694 }
3695 if (!isTaskRoot()) {
3696 // If the activity is not the root of the task, allow finish to proceed normally.
3697 finishAfterTransition();
3698 return;
3699 }
3700 try {
3701 // Inform activity task manager that the activity received a back press
3702 // while at the root of the task. This call allows ActivityTaskManager
3703 // to intercept or defer finishing.
3704 ActivityTaskManager.getService().onBackPressedOnTaskRoot(mToken,
Mark Renouf1c25fc52019-09-09 13:13:43 -04003705 new RequestFinishCallback(new WeakReference<>(this)));
Mark Renouf446251d2019-04-26 10:22:41 -04003706 } catch (RemoteException e) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003707 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003708 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003709 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003710
3711 /**
3712 * Called when a key shortcut event is not handled by any of the views in the Activity.
3713 * Override this method to implement global key shortcuts for the Activity.
3714 * Key shortcuts can also be implemented by setting the
3715 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3716 *
3717 * @param keyCode The value in event.getKeyCode().
3718 * @param event Description of the key event.
3719 * @return True if the key shortcut was handled.
3720 */
3721 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003722 // Let the Action Bar have a chance at handling the shortcut.
3723 ActionBar actionBar = getActionBar();
3724 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003725 }
3726
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 * Called when a touch screen event was not handled by any of the views
3729 * under it. This is most useful to process touch events that happen
3730 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003733 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 * @return Return true if you have consumed the event, false if you haven't.
3735 * The default implementation always returns false.
3736 */
3737 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003738 if (mWindow.shouldCloseOnTouch(this, event)) {
3739 finish();
3740 return true;
3741 }
RoboErik55011652014-07-09 15:05:53 -07003742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 return false;
3744 }
RoboErik55011652014-07-09 15:05:53 -07003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 /**
3747 * Called when the trackball was moved and not handled by any of the
3748 * views inside of the activity. So, for example, if the trackball moves
3749 * while focus is on a button, you will receive a call here because
3750 * buttons do not normally do anything with trackball events. The call
3751 * here happens <em>before</em> trackball movements are converted to
3752 * DPAD key events, which then get sent back to the view hierarchy, and
3753 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 * @return Return true if you have consumed the event, false if you haven't.
3758 * The default implementation always returns false.
3759 */
3760 public boolean onTrackballEvent(MotionEvent event) {
3761 return false;
3762 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003763
3764 /**
3765 * Called when a generic motion event was not handled by any of the
3766 * views inside of the activity.
3767 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003768 * Generic motion events describe joystick movements, mouse hovers, track pad
3769 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003770 * {@link MotionEvent#getSource() source} of the motion event specifies
3771 * the class of input that was received. Implementations of this method
3772 * must examine the bits in the source before processing the event.
3773 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003774 * </p><p>
3775 * Generic motion events with source class
3776 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3777 * are delivered to the view under the pointer. All other generic motion events are
3778 * delivered to the focused view.
3779 * </p><p>
3780 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3781 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003782 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003783 *
3784 * @param event The generic motion event being processed.
3785 *
3786 * @return Return true if you have consumed the event, false if you haven't.
3787 * The default implementation always returns false.
3788 */
3789 public boolean onGenericMotionEvent(MotionEvent event) {
3790 return false;
3791 }
3792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 /**
3794 * Called whenever a key, touch, or trackball event is dispatched to the
3795 * activity. Implement this method if you wish to know that the user has
3796 * interacted with the device in some way while your activity is running.
3797 * This callback and {@link #onUserLeaveHint} are intended to help
3798 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003799 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3802 * be accompanied by calls to {@link #onUserInteraction}. This
3803 * ensures that your activity will be told of relevant user activity such
3804 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003805 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 * <p>Note that this callback will be invoked for the touch down action
3807 * that begins a touch gesture, but may not be invoked for the touch-moved
3808 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003809 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 * @see #onUserLeaveHint()
3811 */
3812 public void onUserInteraction() {
3813 }
RoboErik55011652014-07-09 15:05:53 -07003814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3816 // Update window manager if: we have a view, that view is
3817 // attached to its parent (which will be a RootView), and
3818 // this activity is not embedded.
3819 if (mParent == null) {
3820 View decor = mDecor;
3821 if (decor != null && decor.getParent() != null) {
3822 getWindowManager().updateViewLayout(decor, params);
Adam He43c06992019-04-15 15:41:49 -07003823 if (mContentCaptureManager != null) {
3824 mContentCaptureManager.updateWindowAttributes(params);
3825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 }
3827 }
3828 }
3829
3830 public void onContentChanged() {
3831 }
3832
3833 /**
3834 * Called when the current {@link Window} of the activity gains or loses
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003835 * focus. This is the best indicator of whether this activity is the entity
3836 * with which the user actively interacts. The default implementation
3837 * clears the key tracking state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003838 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003839 * <p>Note that this provides information about global focus state, which
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003840 * is managed independently of activity lifecycle. As such, while focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 * changes will generally have some relation to lifecycle changes (an
3842 * activity that is stopped will not generally get window focus), you
3843 * should not rely on any particular order between the callbacks here and
3844 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003845 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003846 * <p>As a general rule, however, a foreground activity will have window
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 * focus... unless it has displayed other dialogs or popups that take
3848 * input focus, in which case the activity itself will not have focus
3849 * when the other windows have it. Likewise, the system may display
3850 * system-level windows (such as the status bar notification panel or
3851 * a system alert) which will temporarily take window input focus without
3852 * pausing the foreground activity.
3853 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003854 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} there can be
3855 * multiple resumed activities at the same time in multi-window mode, so
3856 * resumed state does not guarantee window focus even if there are no
3857 * overlays above.
3858 *
3859 * <p>If the intent is to know when an activity is the topmost active, the
3860 * one the user interacted with last among all activities but not including
3861 * non-activity windows like dialogs and popups, then
3862 * {@link #onTopResumedActivityChanged(boolean)} should be used. On platform
3863 * versions prior to {@link android.os.Build.VERSION_CODES#Q},
3864 * {@link #onResume} is the best indicator.
3865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 * @see #hasWindowFocus()
3869 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003870 * @see View#onWindowFocusChanged(boolean)
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003871 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 */
3873 public void onWindowFocusChanged(boolean hasFocus) {
3874 }
RoboErik55011652014-07-09 15:05:53 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003877 * Called when the main window associated with the activity has been
3878 * attached to the window manager.
3879 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3880 * for more information.
3881 * @see View#onAttachedToWindow
3882 */
3883 public void onAttachedToWindow() {
3884 }
RoboErik55011652014-07-09 15:05:53 -07003885
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003886 /**
3887 * Called when the main window associated with the activity has been
3888 * detached from the window manager.
3889 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3890 * for more information.
3891 * @see View#onDetachedFromWindow
3892 */
3893 public void onDetachedFromWindow() {
3894 }
RoboErik55011652014-07-09 15:05:53 -07003895
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003896 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 * Returns true if this activity's <em>main</em> window currently has window focus.
3898 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003901 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3903 */
3904 public boolean hasWindowFocus() {
3905 Window w = getWindow();
3906 if (w != null) {
3907 View d = w.getDecorView();
3908 if (d != null) {
3909 return d.hasWindowFocus();
3910 }
3911 }
3912 return false;
3913 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003914
3915 /**
3916 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003917 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003918 */
Adam Powell117b6952014-05-05 18:14:56 -07003919 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003920 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003921 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003922 if (suppressWindowTransition) {
3923 overridePendingTransition(0, 0);
3924 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003925 }
RoboErik55011652014-07-09 15:05:53 -07003926
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003927
Skuhnece2faa52015-08-11 10:36:38 -07003928 /**
Yunfan Chend967af82019-01-17 18:30:18 +09003929 * Moves the activity between {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode
3930 * and {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003931 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003932 * @hide
3933 */
3934 @Override
Yunfan Chend967af82019-01-17 18:30:18 +09003935 public void toggleFreeformWindowingMode() throws RemoteException {
3936 ActivityTaskManager.getService().toggleFreeformWindowingMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003937 }
3938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003940 * Puts the activity in picture-in-picture mode if the activity supports.
3941 * @see android.R.attr#supportsPictureInPicture
3942 * @hide
3943 */
3944 @Override
3945 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003946 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003947 enterPictureInPictureMode();
3948 }
3949 }
3950
3951 /**
RoboErik55011652014-07-09 15:05:53 -07003952 * Called to process key events. You can override this to intercept all
3953 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003955 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 * @return boolean Return true if this event was consumed.
3959 */
3960 public boolean dispatchKeyEvent(KeyEvent event) {
3961 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003962
3963 // Let action bars open menus in response to the menu key prioritized over
3964 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003965 final int keyCode = event.getKeyCode();
3966 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003967 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3968 return true;
3969 }
3970
Dianne Hackborn8d374262009-09-14 21:21:52 -07003971 Window win = getWindow();
3972 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 return true;
3974 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003975 View decor = mDecor;
3976 if (decor == null) decor = win.getDecorView();
3977 return event.dispatch(this, decor != null
3978 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 }
3980
3981 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003982 * Called to process a key shortcut event.
3983 * You can override this to intercept all key shortcut events before they are
3984 * dispatched to the window. Be sure to call this implementation for key shortcut
3985 * events that should be handled normally.
3986 *
3987 * @param event The key shortcut event.
3988 * @return True if this event was consumed.
3989 */
3990 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3991 onUserInteraction();
3992 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3993 return true;
3994 }
3995 return onKeyShortcut(event.getKeyCode(), event);
3996 }
3997
3998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 * Called to process touch screen events. You can override this to
4000 * intercept all touch screen events before they are dispatched to the
4001 * window. Be sure to call this implementation for touch screen events
4002 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07004005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 * @return boolean Return true if this event was consumed.
4007 */
4008 public boolean dispatchTouchEvent(MotionEvent ev) {
4009 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
4010 onUserInteraction();
4011 }
4012 if (getWindow().superDispatchTouchEvent(ev)) {
4013 return true;
4014 }
4015 return onTouchEvent(ev);
4016 }
RoboErik55011652014-07-09 15:05:53 -07004017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 /**
4019 * Called to process trackball events. You can override this to
4020 * intercept all trackball events before they are dispatched to the
4021 * window. Be sure to call this implementation for trackball events
4022 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004023 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07004025 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 * @return boolean Return true if this event was consumed.
4027 */
4028 public boolean dispatchTrackballEvent(MotionEvent ev) {
4029 onUserInteraction();
4030 if (getWindow().superDispatchTrackballEvent(ev)) {
4031 return true;
4032 }
4033 return onTrackballEvent(ev);
4034 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004035
Jeff Browncb1404e2011-01-15 18:14:15 -08004036 /**
4037 * Called to process generic motion events. You can override this to
4038 * intercept all generic motion events before they are dispatched to the
4039 * window. Be sure to call this implementation for generic motion events
4040 * that should be handled normally.
4041 *
4042 * @param ev The generic motion event.
4043 *
4044 * @return boolean Return true if this event was consumed.
4045 */
4046 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
4047 onUserInteraction();
4048 if (getWindow().superDispatchGenericMotionEvent(ev)) {
4049 return true;
4050 }
4051 return onGenericMotionEvent(ev);
4052 }
4053
svetoslavganov75986cf2009-05-14 22:28:01 -07004054 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
4055 event.setClassName(getClass().getName());
4056 event.setPackageName(getPackageName());
4057
4058 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08004059 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
4060 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07004061 event.setFullScreen(isFullScreen);
4062
4063 CharSequence title = getTitle();
4064 if (!TextUtils.isEmpty(title)) {
4065 event.getText().add(title);
4066 }
4067
4068 return true;
4069 }
4070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 /**
4072 * Default implementation of
4073 * {@link android.view.Window.Callback#onCreatePanelView}
4074 * for activities. This
4075 * simply returns null so that all panel sub-windows will have the default
4076 * menu behavior.
4077 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004078 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 public View onCreatePanelView(int featureId) {
4080 return null;
4081 }
4082
4083 /**
4084 * Default implementation of
4085 * {@link android.view.Window.Callback#onCreatePanelMenu}
4086 * for activities. This calls through to the new
4087 * {@link #onCreateOptionsMenu} method for the
4088 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4089 * so that subclasses of Activity don't need to deal with feature codes.
4090 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004091 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004093 boolean show = onCreateOptionsMenu(menu);
4094 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
4095 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
4097 return false;
4098 }
4099
4100 /**
4101 * Default implementation of
4102 * {@link android.view.Window.Callback#onPreparePanel}
4103 * for activities. This
4104 * calls through to the new {@link #onPrepareOptionsMenu} method for the
4105 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4106 * panel, so that subclasses of
4107 * Activity don't need to deal with feature codes.
4108 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004109 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
4110 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004112 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07004113 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 }
4115 return true;
4116 }
4117
4118 /**
4119 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07004120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 * @return The default implementation returns true.
4122 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004123 @Override
4124 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08004125 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07004126 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08004127 if (mActionBar != null) {
4128 mActionBar.dispatchMenuVisibilityChanged(true);
4129 } else {
4130 Log.e(TAG, "Tried to open action bar menu with no action bar");
4131 }
Adam Powell8515ee82010-11-30 14:09:55 -08004132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 return true;
4134 }
4135
4136 /**
4137 * Default implementation of
4138 * {@link android.view.Window.Callback#onMenuItemSelected}
4139 * for activities. This calls through to the new
4140 * {@link #onOptionsItemSelected} method for the
4141 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4142 * panel, so that subclasses of
4143 * Activity don't need to deal with feature codes.
4144 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004145 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09004146 CharSequence titleCondensed = item.getTitleCondensed();
4147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 switch (featureId) {
4149 case Window.FEATURE_OPTIONS_PANEL:
4150 // Put event logging here so it gets called even if subclass
4151 // doesn't call through to superclass's implmeentation of each
4152 // of these methods below
zobject686a8052012-12-14 21:11:08 +09004153 if(titleCondensed != null) {
4154 EventLog.writeEvent(50000, 0, titleCondensed.toString());
4155 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004156 if (onOptionsItemSelected(item)) {
4157 return true;
4158 }
Adam Powell04d58112012-04-09 10:22:12 -07004159 if (mFragments.dispatchOptionsItemSelected(item)) {
4160 return true;
4161 }
4162 if (item.getItemId() == android.R.id.home && mActionBar != null &&
4163 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
4164 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07004165 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07004166 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07004167 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07004168 }
Adam Powell04d58112012-04-09 10:22:12 -07004169 }
4170 return false;
RoboErik55011652014-07-09 15:05:53 -07004171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09004173 if(titleCondensed != null) {
4174 EventLog.writeEvent(50000, 1, titleCondensed.toString());
4175 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07004176 if (onContextItemSelected(item)) {
4177 return true;
4178 }
4179 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08004180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 default:
4182 return false;
4183 }
4184 }
RoboErik55011652014-07-09 15:05:53 -07004185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 /**
4187 * Default implementation of
4188 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
4189 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
4190 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4191 * so that subclasses of Activity don't need to deal with feature codes.
4192 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
4193 * {@link #onContextMenuClosed(Menu)} will be called.
4194 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004195 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 switch (featureId) {
4197 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004198 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 onOptionsMenuClosed(menu);
4200 break;
RoboErik55011652014-07-09 15:05:53 -07004201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 case Window.FEATURE_CONTEXT_MENU:
4203 onContextMenuClosed(menu);
4204 break;
Adam Powell8515ee82010-11-30 14:09:55 -08004205
4206 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07004207 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08004208 mActionBar.dispatchMenuVisibilityChanged(false);
4209 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
4211 }
4212
4213 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004214 * Declare that the options menu has changed, so should be recreated.
4215 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4216 * time it needs to be displayed.
4217 */
4218 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004219 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4220 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004221 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4222 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004223 }
RoboErik55011652014-07-09 15:05:53 -07004224
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 * Initialize the contents of the Activity's standard options menu. You
4227 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 * <p>This is only called once, the first time the options menu is
4230 * displayed. To update the menu every time it is displayed, see
4231 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004234 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4235 * they will be correctly ordered with application-defined menu items.
4236 * Deriving classes should always call through to the base implementation.
4237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 * <p>You can safely hold on to <var>menu</var> (and any items created
4239 * from it), making modifications to it as desired, until the next
4240 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 * <p>When you add items to the menu, you can implement the Activity's
4243 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004244 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 * @return You must return true for the menu to be displayed;
4248 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 * @see #onPrepareOptionsMenu
4251 * @see #onOptionsItemSelected
4252 */
4253 public boolean onCreateOptionsMenu(Menu menu) {
4254 if (mParent != null) {
4255 return mParent.onCreateOptionsMenu(menu);
4256 }
4257 return true;
4258 }
4259
4260 /**
4261 * Prepare the Screen's standard options menu to be displayed. This is
4262 * called right before the menu is shown, every time it is shown. You can
4263 * use this method to efficiently enable/disable items or otherwise
4264 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 * <p>The default implementation updates the system menu items based on the
4267 * activity's state. Deriving classes should always call through to the
4268 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 * @param menu The options menu as last shown or first initialized by
4271 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004272 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 * @return You must return true for the menu to be displayed;
4274 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 * @see #onCreateOptionsMenu
4277 */
4278 public boolean onPrepareOptionsMenu(Menu menu) {
4279 if (mParent != null) {
4280 return mParent.onPrepareOptionsMenu(menu);
4281 }
4282 return true;
4283 }
4284
4285 /**
4286 * This hook is called whenever an item in your options menu is selected.
4287 * The default implementation simply returns false to have the normal
4288 * processing happen (calling the item's Runnable or sending a message to
4289 * its Handler as appropriate). You can use this method for any items
4290 * for which you would like to do processing without those other
4291 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004292 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004294 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004297 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 * @return boolean Return false to allow normal menu processing to
4299 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004300 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 * @see #onCreateOptionsMenu
4302 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004303 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 if (mParent != null) {
4305 return mParent.onOptionsItemSelected(item);
4306 }
4307 return false;
4308 }
4309
4310 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004311 * This method is called whenever the user chooses to navigate Up within your application's
4312 * activity hierarchy from the action bar.
4313 *
4314 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4315 * was specified in the manifest for this activity or an activity-alias to it,
4316 * default Up navigation will be handled automatically. If any activity
4317 * along the parent chain requires extra Intent arguments, the Activity subclass
4318 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4319 * to supply those arguments.</p>
4320 *
Mark Lufa434852016-08-11 17:40:33 -07004321 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004322 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4323 * from the design guide for more information about navigating within your app.</p>
4324 *
4325 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4326 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4327 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4328 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4329 *
4330 * @return true if Up navigation completed successfully and this Activity was finished,
4331 * false otherwise.
4332 */
4333 public boolean onNavigateUp() {
4334 // Automatically handle hierarchical Up navigation if the proper
4335 // metadata is available.
4336 Intent upIntent = getParentActivityIntent();
4337 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004338 if (mActivityInfo.taskAffinity == null) {
4339 // Activities with a null affinity are special; they really shouldn't
4340 // specify a parent activity intent in the first place. Just finish
4341 // the current activity and call it a day.
4342 finish();
4343 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004344 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004345 onCreateNavigateUpTaskStack(b);
4346 onPrepareNavigateUpTaskStack(b);
4347 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004348
4349 // We can't finishAffinity if we have a result.
4350 // Fall back and simply finish the current activity instead.
4351 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4352 // Tell the developer what's going on to avoid hair-pulling.
4353 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4354 finish();
4355 } else {
4356 finishAffinity();
4357 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004358 } else {
4359 navigateUpTo(upIntent);
4360 }
4361 return true;
4362 }
4363 return false;
4364 }
4365
4366 /**
4367 * This is called when a child activity of this one attempts to navigate up.
4368 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4369 *
4370 * @param child The activity making the call.
4371 */
4372 public boolean onNavigateUpFromChild(Activity child) {
4373 return onNavigateUp();
4374 }
4375
4376 /**
4377 * Define the synthetic task stack that will be generated during Up navigation from
4378 * a different task.
4379 *
4380 * <p>The default implementation of this method adds the parent chain of this activity
4381 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4382 * may choose to override this method to construct the desired task stack in a different
4383 * way.</p>
4384 *
Adam Powellf0195952012-05-02 21:38:54 -07004385 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4386 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4387 * returned by {@link #getParentActivityIntent()}.</p>
4388 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004389 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4390 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4391 *
4392 * @param builder An empty TaskStackBuilder - the application should add intents representing
4393 * the desired task stack
4394 */
4395 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4396 builder.addParentStack(this);
4397 }
4398
4399 /**
4400 * Prepare the synthetic task stack that will be generated during Up navigation
4401 * from a different task.
4402 *
4403 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4404 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4405 * If any extra data should be added to these intents before launching the new task,
4406 * the application should override this method and add that data here.</p>
4407 *
4408 * @param builder A TaskStackBuilder that has been populated with Intents by
4409 * onCreateNavigateUpTaskStack.
4410 */
4411 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4412 }
4413
4414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 * This hook is called whenever the options menu is being closed (either by the user canceling
4416 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 * @param menu The options menu as last shown or first initialized by
4419 * onCreateOptionsMenu().
4420 */
4421 public void onOptionsMenuClosed(Menu menu) {
4422 if (mParent != null) {
4423 mParent.onOptionsMenuClosed(menu);
4424 }
4425 }
RoboErik55011652014-07-09 15:05:53 -07004426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 /**
4428 * Programmatically opens the options menu. If the options menu is already
4429 * open, this method does nothing.
4430 */
4431 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004432 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4433 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004434 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 }
RoboErik55011652014-07-09 15:05:53 -07004437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 /**
4439 * Progammatically closes the options menu. If the options menu is already
4440 * closed, this method does nothing.
4441 */
4442 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004443 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4444 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004445 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 }
4448
4449 /**
4450 * Called when a context menu for the {@code view} is about to be shown.
4451 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4452 * time the context menu is about to be shown and should be populated for
4453 * the view (or item inside the view for {@link AdapterView} subclasses,
4454 * this can be found in the {@code menuInfo})).
4455 * <p>
4456 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4457 * item has been selected.
4458 * <p>
4459 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 */
4462 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4463 }
4464
4465 /**
4466 * Registers a context menu to be shown for the given view (multiple views
4467 * can show the context menu). This method will set the
4468 * {@link OnCreateContextMenuListener} on the view to this activity, so
4469 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4470 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004471 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 * @see #unregisterForContextMenu(View)
4473 * @param view The view that should show a context menu.
4474 */
4475 public void registerForContextMenu(View view) {
4476 view.setOnCreateContextMenuListener(this);
4477 }
RoboErik55011652014-07-09 15:05:53 -07004478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 /**
4480 * Prevents a context menu to be shown for the given view. This method will remove the
4481 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004482 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 * @see #registerForContextMenu(View)
4484 * @param view The view that should stop showing a context menu.
4485 */
4486 public void unregisterForContextMenu(View view) {
4487 view.setOnCreateContextMenuListener(null);
4488 }
RoboErik55011652014-07-09 15:05:53 -07004489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 /**
4491 * Programmatically opens the context menu for a particular {@code view}.
4492 * The {@code view} should have been added via
4493 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 * @param view The view to show the context menu for.
4496 */
4497 public void openContextMenu(View view) {
4498 view.showContextMenu();
4499 }
RoboErik55011652014-07-09 15:05:53 -07004500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 /**
4502 * Programmatically closes the most recently opened context menu, if showing.
4503 */
4504 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004505 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4506 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 }
RoboErik55011652014-07-09 15:05:53 -07004509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 /**
4511 * This hook is called whenever an item in a context menu is selected. The
4512 * default implementation simply returns false to have the normal processing
4513 * happen (calling the item's Runnable or sending a message to its Handler
4514 * as appropriate). You can use this method for any items for which you
4515 * would like to do processing without those other facilities.
4516 * <p>
4517 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4518 * View that added this menu item.
4519 * <p>
4520 * Derived classes should call through to the base class for it to perform
4521 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 * @param item The context menu item that was selected.
4524 * @return boolean Return false to allow normal context menu processing to
4525 * proceed, true to consume it here.
4526 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004527 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 if (mParent != null) {
4529 return mParent.onContextItemSelected(item);
4530 }
4531 return false;
4532 }
4533
4534 /**
4535 * This hook is called whenever the context menu is being closed (either by
4536 * the user canceling the menu with the back/menu button, or when an item is
4537 * selected).
RoboErik55011652014-07-09 15:05:53 -07004538 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 * @param menu The context menu that is being closed.
4540 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004541 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 if (mParent != null) {
4543 mParent.onContextMenuClosed(menu);
4544 }
4545 }
4546
4547 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004548 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004550 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 protected Dialog onCreateDialog(int id) {
4552 return null;
4553 }
4554
4555 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004556 * Callback for creating dialogs that are managed (saved and restored) for you
4557 * by the activity. The default implementation calls through to
4558 * {@link #onCreateDialog(int)} for compatibility.
4559 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004560 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4561 * or later, consider instead using a {@link DialogFragment} instead.</em>
4562 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004563 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4564 * this method the first time, and hang onto it thereafter. Any dialog
4565 * that is created by this method will automatically be saved and restored
4566 * for you, including whether it is showing.
4567 *
4568 * <p>If you would like the activity to manage saving and restoring dialogs
4569 * for you, you should override this method and handle any ids that are
4570 * passed to {@link #showDialog}.
4571 *
4572 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4573 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4574 *
4575 * @param id The id of the dialog.
4576 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4577 * @return The dialog. If you return null, the dialog will not be created.
4578 *
4579 * @see #onPrepareDialog(int, Dialog, Bundle)
4580 * @see #showDialog(int, Bundle)
4581 * @see #dismissDialog(int)
4582 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004583 *
4584 * @deprecated Use the new {@link DialogFragment} class with
4585 * {@link FragmentManager} instead; this is also
4586 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004587 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004588 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004589 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004590 protected Dialog onCreateDialog(int id, Bundle args) {
4591 return onCreateDialog(id);
4592 }
4593
4594 /**
4595 * @deprecated Old no-arguments version of
4596 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4597 */
4598 @Deprecated
4599 protected void onPrepareDialog(int id, Dialog dialog) {
4600 dialog.setOwnerActivity(this);
4601 }
4602
4603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004605 * shown. The default implementation calls through to
4606 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004607 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 * <p>
4609 * Override this if you need to update a managed dialog based on the state
4610 * of the application each time it is shown. For example, a time picker
4611 * dialog might want to be updated with the current time. You should call
4612 * through to the superclass's implementation. The default implementation
4613 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615 * @param id The id of the managed dialog.
4616 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004617 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4618 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 * @see #showDialog(int)
4620 * @see #dismissDialog(int)
4621 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004622 *
4623 * @deprecated Use the new {@link DialogFragment} class with
4624 * {@link FragmentManager} instead; this is also
4625 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004627 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004628 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4629 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 }
4631
4632 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004633 * Simple version of {@link #showDialog(int, Bundle)} that does not
4634 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4635 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004636 *
4637 * @deprecated Use the new {@link DialogFragment} class with
4638 * {@link FragmentManager} instead; this is also
4639 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004640 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004641 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004642 public final void showDialog(int id) {
4643 showDialog(id, null);
4644 }
4645
4646 /**
4647 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 * will be made with the same id the first time this is called for a given
4649 * id. From thereafter, the dialog will be automatically saved and restored.
4650 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004651 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4652 * or later, consider instead using a {@link DialogFragment} instead.</em>
4653 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004654 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 * be made to provide an opportunity to do any timely preparation.
4656 *
4657 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004658 * @param args Arguments to pass through to the dialog. These will be saved
4659 * and restored for you. Note that if the dialog is already created,
4660 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4661 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004662 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004663 * @return Returns true if the Dialog was created; false is returned if
4664 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004665 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004666 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004667 * @see #onCreateDialog(int, Bundle)
4668 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 * @see #dismissDialog(int)
4670 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004671 *
4672 * @deprecated Use the new {@link DialogFragment} class with
4673 * {@link FragmentManager} instead; this is also
4674 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004676 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004677 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004679 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004681 ManagedDialog md = mManagedDialogs.get(id);
4682 if (md == null) {
4683 md = new ManagedDialog();
4684 md.mDialog = createDialog(id, null, args);
4685 if (md.mDialog == null) {
4686 return false;
4687 }
4688 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 }
RoboErik55011652014-07-09 15:05:53 -07004690
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004691 md.mArgs = args;
4692 onPrepareDialog(id, md.mDialog, args);
4693 md.mDialog.show();
4694 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 }
4696
4697 /**
4698 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4699 *
4700 * @param id The id of the managed dialog.
4701 *
4702 * @throws IllegalArgumentException if the id was not previously shown via
4703 * {@link #showDialog(int)}.
4704 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004705 * @see #onCreateDialog(int, Bundle)
4706 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 * @see #showDialog(int)
4708 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004709 *
4710 * @deprecated Use the new {@link DialogFragment} class with
4711 * {@link FragmentManager} instead; this is also
4712 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004714 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 public final void dismissDialog(int id) {
4716 if (mManagedDialogs == null) {
4717 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 }
RoboErik55011652014-07-09 15:05:53 -07004719
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004720 final ManagedDialog md = mManagedDialogs.get(id);
4721 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 throw missingDialog(id);
4723 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004724 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 }
4726
4727 /**
4728 * Creates an exception to throw if a user passed in a dialog id that is
4729 * unexpected.
4730 */
4731 private IllegalArgumentException missingDialog(int id) {
4732 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4733 + "shown via Activity#showDialog");
4734 }
4735
4736 /**
4737 * Removes any internal references to a dialog managed by this Activity.
4738 * If the dialog is showing, it will dismiss it as part of the clean up.
4739 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004740 * <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 -08004741 * want to avoid the overhead of saving and restoring it in the future.
4742 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004743 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4744 * will not throw an exception if you try to remove an ID that does not
4745 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004746 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 * @param id The id of the managed dialog.
4748 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004749 * @see #onCreateDialog(int, Bundle)
4750 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 * @see #showDialog(int)
4752 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004753 *
4754 * @deprecated Use the new {@link DialogFragment} class with
4755 * {@link FragmentManager} instead; this is also
4756 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004758 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004760 if (mManagedDialogs != null) {
4761 final ManagedDialog md = mManagedDialogs.get(id);
4762 if (md != null) {
4763 md.mDialog.dismiss();
4764 mManagedDialogs.remove(id);
4765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767 }
4768
4769 /**
4770 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004771 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004772 * <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 -07004773 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004774 * calling this function is the same as calling
4775 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4776 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004777 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 * <p>You can override this function to force global search, e.g. in response to a dedicated
4779 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004780 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004781 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4782 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4783 * return false and you must supply your own custom implementation if you want to support
4784 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004785 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004786 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004787 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4788 * not respond to search. The default implementation always returns {@code true}, except
4789 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004791 * @see android.app.SearchManager
4792 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004793 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4794 mSearchEvent = searchEvent;
4795 boolean result = onSearchRequested();
4796 mSearchEvent = null;
4797 return result;
4798 }
4799
4800 /**
4801 * @see #onSearchRequested(SearchEvent)
4802 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004804 final int uiMode = getResources().getConfiguration().uiMode
4805 & Configuration.UI_MODE_TYPE_MASK;
4806 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4807 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004808 startSearch(null, false, null, false);
4809 return true;
4810 } else {
4811 return false;
4812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 }
RoboErik55011652014-07-09 15:05:53 -07004814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004816 * During the onSearchRequested() callbacks, this function will return the
4817 * {@link SearchEvent} that triggered the callback, if it exists.
4818 *
4819 * @return SearchEvent The SearchEvent that triggered the {@link
4820 * #onSearchRequested} callback.
4821 */
4822 public final SearchEvent getSearchEvent() {
4823 return mSearchEvent;
4824 }
4825
4826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004828 *
4829 * <p>It is typically called from onSearchRequested(), either directly from
4830 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004832 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 * is to inject specific data such as context data, it is preferred to <i>override</i>
4834 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004835 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004836 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4837 * not supported.
4838 *
RoboErik55011652014-07-09 15:05:53 -07004839 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004841 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 * any further typing will replace it. This is useful for cases where an entire pre-formed
4843 * query is being inserted. If false, the selection point will be placed at the end of the
4844 * inserted query. This is useful when the inserted query is text that the user entered,
4845 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4846 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004847 * @param appSearchData An application can insert application-specific
4848 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 * searches. This data will be returned with SEARCH intent(s). Null if
4850 * no extra data is required.
4851 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004852 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004853 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004855 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004856 * @see android.app.SearchManager
4857 * @see #onSearchRequested
4858 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004859 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4860 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004861 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004862 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004863 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 }
4865
4866 /**
krosaend2d60142009-08-17 08:56:48 -07004867 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4868 * the search dialog. Made available for testing purposes.
4869 *
4870 * @param query The query to trigger. If empty, the request will be ignored.
4871 * @param appSearchData An application can insert application-specific
4872 * context here, in order to improve quality or specificity of its own
4873 * searches. This data will be returned with SEARCH intent(s). Null if
4874 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004875 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004876 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004877 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004878 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004879 }
4880
4881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 * Request that key events come to this activity. Use this if your
4883 * activity has no views with focus, but the activity still wants
4884 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004885 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 * @see android.view.Window#takeKeyEvents
4887 */
4888 public void takeKeyEvents(boolean get) {
4889 getWindow().takeKeyEvents(get);
4890 }
4891
4892 /**
4893 * Enable extended window features. This is a convenience for calling
4894 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004895 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 * @param featureId The desired feature as defined in
4897 * {@link android.view.Window}.
4898 * @return Returns true if the requested feature is supported and now
4899 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 * @see android.view.Window#requestFeature
4902 */
4903 public final boolean requestWindowFeature(int featureId) {
4904 return getWindow().requestFeature(featureId);
4905 }
4906
4907 /**
4908 * Convenience for calling
4909 * {@link android.view.Window#setFeatureDrawableResource}.
4910 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004911 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 getWindow().setFeatureDrawableResource(featureId, resId);
4913 }
4914
4915 /**
4916 * Convenience for calling
4917 * {@link android.view.Window#setFeatureDrawableUri}.
4918 */
4919 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4920 getWindow().setFeatureDrawableUri(featureId, uri);
4921 }
4922
4923 /**
4924 * Convenience for calling
4925 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4926 */
4927 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4928 getWindow().setFeatureDrawable(featureId, drawable);
4929 }
4930
4931 /**
4932 * Convenience for calling
4933 * {@link android.view.Window#setFeatureDrawableAlpha}.
4934 */
4935 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4936 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4937 }
4938
4939 /**
4940 * Convenience for calling
4941 * {@link android.view.Window#getLayoutInflater}.
4942 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004943 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 public LayoutInflater getLayoutInflater() {
4945 return getWindow().getLayoutInflater();
4946 }
4947
4948 /**
4949 * Returns a {@link MenuInflater} with this context.
4950 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004951 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004953 // Make sure that action views can get an appropriate theme.
4954 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004955 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004956 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004957 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004958 } else {
4959 mMenuInflater = new MenuInflater(this);
4960 }
4961 }
4962 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 }
4964
4965 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004966 public void setTheme(int resid) {
4967 super.setTheme(resid);
4968 mWindow.setTheme(resid);
4969 }
4970
4971 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004972 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004973 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 if (mParent == null) {
4975 super.onApplyThemeResource(theme, resid, first);
4976 } else {
4977 try {
4978 theme.setTo(mParent.getTheme());
4979 } catch (Exception e) {
4980 // Empty
4981 }
4982 theme.applyStyle(resid, false);
4983 }
Winson073a5262016-02-29 16:04:40 -08004984
4985 // Get the primary color and update the TaskDescription for this activity
4986 TypedArray a = theme.obtainStyledAttributes(
4987 com.android.internal.R.styleable.ActivityTaskDescription);
4988 if (mTaskDescription.getPrimaryColor() == 0) {
4989 int colorPrimary = a.getColor(
4990 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4991 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4992 mTaskDescription.setPrimaryColor(colorPrimary);
4993 }
4994 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004995
4996 int colorBackground = a.getColor(
4997 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4998 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4999 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08005000 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02005001
5002 final int statusBarColor = a.getColor(
5003 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
5004 if (statusBarColor != 0) {
5005 mTaskDescription.setStatusBarColor(statusBarColor);
5006 }
5007
5008 final int navigationBarColor = a.getColor(
5009 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
5010 if (navigationBarColor != 0) {
5011 mTaskDescription.setNavigationBarColor(navigationBarColor);
5012 }
5013
Adrian Roos4c864592019-04-10 14:47:57 +02005014 final int targetSdk = getApplicationInfo().targetSdkVersion;
5015 final boolean targetPreQ = targetSdk < Build.VERSION_CODES.Q;
5016 if (!targetPreQ) {
5017 mTaskDescription.setEnsureStatusBarContrastWhenTransparent(a.getBoolean(
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005018 R.styleable.ActivityTaskDescription_enforceStatusBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005019 false));
5020 mTaskDescription.setEnsureNavigationBarContrastWhenTransparent(a.getBoolean(
Adrian Roos6eb1ed32019-04-08 16:23:43 +02005021 R.styleable
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005022 .ActivityTaskDescription_enforceNavigationBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005023 true));
5024 }
5025
Winson073a5262016-02-29 16:04:40 -08005026 a.recycle();
5027 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00005028 }
5029
5030 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08005031 * Requests permissions to be granted to this application. These permissions
5032 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005033 * and they should have protection level {@link
5034 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
5035 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08005036 * <p>
5037 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
5038 * are granted at install time if requested in the manifest. Signature permissions
5039 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
5040 * install time if requested in the manifest and the signature of your app matches
5041 * the signature of the app declaring the permissions.
5042 * </p>
5043 * <p>
5044 * If your app does not have the requested permissions the user will be presented
5045 * with UI for accepting them. After the user has accepted or rejected the
5046 * requested permissions you will receive a callback on {@link
5047 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
5048 * permissions were granted or not.
5049 * </p>
5050 * <p>
5051 * Note that requesting a permission does not guarantee it will be granted and
5052 * your app should be able to run without having this permission.
5053 * </p>
5054 * <p>
5055 * This method may start an activity allowing the user to choose which permissions
5056 * to grant and which to reject. Hence, you should be prepared that your activity
5057 * may be paused and resumed. Further, granting some permissions may require
5058 * a restart of you application. In such a case, the system will recreate the
5059 * activity stack before delivering the result to {@link
5060 * #onRequestPermissionsResult(int, String[], int[])}.
5061 * </p>
5062 * <p>
5063 * When checking whether you have a permission you should use {@link
5064 * #checkSelfPermission(String)}.
5065 * </p>
5066 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07005067 * Calling this API for permissions already granted to your app would show UI
5068 * to the user to decide whether the app can still hold these permissions. This
5069 * can be useful if the way your app uses data guarded by the permissions
5070 * changes significantly.
5071 * </p>
5072 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07005073 * You cannot request a permission if your activity sets {@link
5074 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5075 * <code>true</code> because in this case the activity would not receive
5076 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
5077 * </p>
5078 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005079 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07005080 * RuntimePermissions</a> sample app demonstrates how to use this method to
5081 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08005082 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005083 *
Svet Ganovf66381c2016-02-18 20:02:36 -08005084 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08005085 * @param requestCode Application specific request code to match with a result
5086 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07005087 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08005088 *
Paul Miller77a0e132016-02-11 13:46:49 -08005089 * @throws IllegalArgumentException if requestCode is negative.
5090 *
Svetoslavc6d1c342015-02-26 14:44:43 -08005091 * @see #onRequestPermissionsResult(int, String[], int[])
5092 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07005093 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08005094 */
5095 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08005096 if (requestCode < 0) {
5097 throw new IllegalArgumentException("requestCode should be >= 0");
5098 }
Svetoslavffb32b12015-10-15 16:54:00 -07005099 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00005100 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07005101 // Dispatch the callback with empty arrays which means a cancellation.
5102 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
5103 return;
5104 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005105 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07005106 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07005107 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08005108 }
5109
5110 /**
5111 * Callback for the result from requesting permissions. This method
5112 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07005113 * <p>
5114 * <strong>Note:</strong> It is possible that the permissions request interaction
5115 * with the user is interrupted. In this case you will receive empty permissions
5116 * and results arrays which should be treated as a cancellation.
5117 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005118 *
5119 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
5120 * @param permissions The requested permissions. Never null.
5121 * @param grantResults The grant results for the corresponding permissions
5122 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
5123 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
5124 *
5125 * @see #requestPermissions(String[], int)
5126 */
5127 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
5128 @NonNull int[] grantResults) {
5129 /* callback - no nothing */
5130 }
5131
5132 /**
Svetoslav20770dd2015-05-29 15:43:04 -07005133 * Gets whether you should show UI with rationale for requesting a permission.
5134 * You should do this only if you do not have the permission and the context in
5135 * which the permission is requested does not clearly communicate to the user
5136 * what would be the benefit from granting this permission.
5137 * <p>
5138 * For example, if you write a camera app, requesting the camera permission
5139 * would be expected by the user and no rationale for why it is requested is
5140 * needed. If however, the app needs location for tagging photos then a non-tech
5141 * savvy user may wonder how location is related to taking photos. In this case
5142 * you may choose to show UI with rationale of requesting this permission.
5143 * </p>
5144 *
5145 * @param permission A permission your app wants to request.
5146 * @return Whether you can show permission rationale UI.
5147 *
5148 * @see #checkSelfPermission(String)
5149 * @see #requestPermissions(String[], int)
5150 * @see #onRequestPermissionsResult(int, String[], int[])
5151 */
Svetoslav9ce9a152015-06-02 17:59:31 -07005152 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07005153 return getPackageManager().shouldShowRequestPermissionRationale(permission);
5154 }
5155
5156 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005157 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
5158 * with no options.
5159 *
5160 * @param intent The intent to start.
5161 * @param requestCode If >= 0, this code will be returned in
5162 * onActivityResult() when the activity exits.
5163 *
5164 * @throws android.content.ActivityNotFoundException
5165 *
George Mount0a778ed2013-12-13 13:35:36 -08005166 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07005167 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005168 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005169 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005170 }
5171
5172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 * Launch an activity for which you would like a result when it finished.
5174 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08005175 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07005176 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07005178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 * <p>Note that this method should only be used with Intent protocols
5180 * that are defined to return a result. In other protocols (such as
5181 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
5182 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05005183 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
5184 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005185 *
RoboErik55011652014-07-09 15:05:53 -07005186 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08005188 * activity, then your window will not be displayed until a result is
5189 * returned back from the started activity. This is to avoid visible
5190 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 * <p>This method throws {@link android.content.ActivityNotFoundException}
5193 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005194 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 * @param intent The intent to start.
5196 * @param requestCode If >= 0, this code will be returned in
5197 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005198 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005199 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005200 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005203 *
George Mount0a778ed2013-12-13 13:35:36 -08005204 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005206 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
5207 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07005209 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 Instrumentation.ActivityResult ar =
5211 mInstrumentation.execStartActivity(
5212 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005213 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 if (ar != null) {
5215 mMainThread.sendActivityResult(
5216 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
5217 ar.getResultData());
5218 }
5219 if (requestCode >= 0) {
5220 // If this start is requesting a result, we can avoid making
5221 // the activity visible until the result is received. Setting
5222 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5223 // activity hidden during this time, to avoid flickering.
5224 // This can only be done when a result is requested because
5225 // that guarantees we will get information back when the
5226 // activity is finished, no matter what happens to it.
5227 mStartedActivity = true;
5228 }
Adam Powell14874662013-07-18 19:42:41 -07005229
George Mount41725de2015-04-09 08:23:05 -07005230 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005231 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005233 if (options != null) {
5234 mParent.startActivityFromChild(this, intent, requestCode, options);
5235 } else {
5236 // Note we want to go through this method for compatibility with
5237 // existing applications that may have overridden it.
5238 mParent.startActivityFromChild(this, intent, requestCode);
5239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
George Mount41725de2015-04-09 08:23:05 -07005241 }
5242
5243 /**
5244 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5245 *
5246 * @param options The ActivityOptions bundle used to start an Activity.
5247 */
5248 private void cancelInputsAndStartExitTransition(Bundle options) {
5249 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5250 if (decor != null) {
5251 decor.cancelPendingInputEvents();
5252 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005253 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005254 mActivityTransitionState.startExitOutTransition(this, options);
5255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 }
5257
Chet Haaseb64e777f2016-12-09 07:17:10 -08005258 /**
5259 * Returns whether there are any activity transitions currently running on this
5260 * activity. A return value of {@code true} can mean that either an enter or
5261 * exit transition is running, including whether the background of the activity
5262 * is animating as a part of that transition.
5263 *
5264 * @return true if a transition is currently running on this activity, false otherwise.
5265 */
5266 public boolean isActivityTransitionRunning() {
5267 return mActivityTransitionState.isTransitionRunning();
5268 }
5269
George Mount413739e2016-06-08 07:13:37 -07005270 private Bundle transferSpringboardActivityOptions(Bundle options) {
5271 if (options == null && (mWindow != null && !mWindow.isActive())) {
5272 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005273 if (activityOptions != null &&
5274 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005275 return activityOptions.toBundle();
5276 }
5277 }
5278 return options;
5279 }
5280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005282 * @hide Implement to provide correct calling token.
5283 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005284 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005285 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5286 startActivityForResultAsUser(intent, requestCode, null, user);
5287 }
5288
5289 /**
5290 * @hide Implement to provide correct calling token.
5291 */
5292 public void startActivityForResultAsUser(Intent intent, int requestCode,
5293 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005294 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5295 }
5296
5297 /**
5298 * @hide Implement to provide correct calling token.
5299 */
5300 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5301 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005302 if (mParent != null) {
5303 throw new RuntimeException("Can't be called from a child");
5304 }
George Mount413739e2016-06-08 07:13:37 -07005305 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005306 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005307 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005308 options, user);
5309 if (ar != null) {
5310 mMainThread.sendActivityResult(
5311 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5312 }
5313 if (requestCode >= 0) {
5314 // If this start is requesting a result, we can avoid making
5315 // the activity visible until the result is received. Setting
5316 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5317 // activity hidden during this time, to avoid flickering.
5318 // This can only be done when a result is requested because
5319 // that guarantees we will get information back when the
5320 // activity is finished, no matter what happens to it.
5321 mStartedActivity = true;
5322 }
5323
George Mount41725de2015-04-09 08:23:05 -07005324 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005325 }
5326
5327 /**
5328 * @hide Implement to provide correct calling token.
5329 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005330 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005331 public void startActivityAsUser(Intent intent, UserHandle user) {
5332 startActivityAsUser(intent, null, user);
5333 }
5334
5335 /**
5336 * @hide Implement to provide correct calling token.
5337 */
5338 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5339 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005340 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005341 }
George Mount413739e2016-06-08 07:13:37 -07005342 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005343 Instrumentation.ActivityResult ar =
5344 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005345 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005346 intent, -1, options, user);
5347 if (ar != null) {
5348 mMainThread.sendActivityResult(
5349 mToken, mEmbeddedID, -1, ar.getResultCode(),
5350 ar.getResultData());
5351 }
George Mount41725de2015-04-09 08:23:05 -07005352 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005353 }
5354
5355 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005356 * Start a new activity as if it was started by the activity that started our
5357 * current activity. This is for the resolver and chooser activities, which operate
5358 * as intermediaries that dispatch their intent to the target the user selects -- to
5359 * do this, they must perform all security checks including permission grants as if
5360 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005361 * @param intent The Intent to start.
5362 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005363 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005364 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5365 * caller it is doing the start is, is actually allowed to start the target activity.
5366 * If you set this to true, you must set an explicit component in the Intent and do any
5367 * appropriate security checks yourself.
5368 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005369 * @hide
5370 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005371 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005372 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005373 if (mParent != null) {
5374 throw new RuntimeException("Can't be called from a child");
5375 }
George Mount413739e2016-06-08 07:13:37 -07005376 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005377 Instrumentation.ActivityResult ar =
5378 mInstrumentation.execStartActivityAsCaller(
5379 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005380 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005381 if (ar != null) {
5382 mMainThread.sendActivityResult(
5383 mToken, mEmbeddedID, -1, ar.getResultCode(),
5384 ar.getResultData());
5385 }
George Mount41725de2015-04-09 08:23:05 -07005386 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005387 }
5388
5389 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005390 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5391 * Intent, int, int, int, Bundle)} with no options.
5392 *
5393 * @param intent The IntentSender to launch.
5394 * @param requestCode If >= 0, this code will be returned in
5395 * onActivityResult() when the activity exits.
5396 * @param fillInIntent If non-null, this will be provided as the
5397 * intent parameter to {@link IntentSender#sendIntent}.
5398 * @param flagsMask Intent flags in the original IntentSender that you
5399 * would like to change.
5400 * @param flagsValues Desired values for any bits set in
5401 * <var>flagsMask</var>
5402 * @param extraFlags Always set to 0.
5403 */
5404 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005405 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005406 throws IntentSender.SendIntentException {
5407 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5408 flagsValues, extraFlags, null);
5409 }
5410
5411 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005412 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005413 * to use a IntentSender to describe the activity to be started. If
5414 * the IntentSender is for an activity, that activity will be started
5415 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5416 * here; otherwise, its associated action will be executed (such as
5417 * sending a broadcast) as if you had called
5418 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005419 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005420 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005421 * @param requestCode If >= 0, this code will be returned in
5422 * onActivityResult() when the activity exits.
5423 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005424 * intent parameter to {@link IntentSender#sendIntent}.
5425 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005426 * would like to change.
5427 * @param flagsValues Desired values for any bits set in
5428 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005429 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005430 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005431 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005432 * Context.startActivity(Intent, Bundle)} for more details. If options
5433 * have also been supplied by the IntentSender, options given here will
5434 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005435 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005436 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005437 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005438 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005439 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005440 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5441 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005442 } else if (options != null) {
5443 mParent.startIntentSenderFromChild(this, intent, requestCode,
5444 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005445 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005446 // Note we want to go through this call for compatibility with
5447 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005448 mParent.startIntentSenderFromChild(this, intent, requestCode,
5449 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005450 }
5451 }
5452
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005453 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5454 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005455 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005456 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005457 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005458 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005459 String resolvedType = null;
5460 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005461 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005462 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005463 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5464 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005465 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005466 .startActivityIntentSender(mMainThread.getApplicationThread(),
5467 intent != null ? intent.getTarget() : null,
5468 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005469 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005470 requestCode, flagsMask, flagsValues, options);
5471 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005472 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005473 }
5474 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005475
5476 if (options != null) {
5477 // Only when the options are not null, as the intent can point to something other
5478 // than an Activity.
5479 cancelInputsAndStartExitTransition(options);
5480 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005481 } catch (RemoteException e) {
5482 }
5483 if (requestCode >= 0) {
5484 // If this start is requesting a result, we can avoid making
5485 // the activity visible until the result is received. Setting
5486 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5487 // activity hidden during this time, to avoid flickering.
5488 // This can only be done when a result is requested because
5489 // that guarantees we will get information back when the
5490 // activity is finished, no matter what happens to it.
5491 mStartedActivity = true;
5492 }
5493 }
5494
5495 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005496 * Same as {@link #startActivity(Intent, Bundle)} with no options
5497 * specified.
5498 *
5499 * @param intent The intent to start.
5500 *
5501 * @throws android.content.ActivityNotFoundException
5502 *
Mark Lufa434852016-08-11 17:40:33 -07005503 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005504 * @see #startActivityForResult
5505 */
5506 @Override
5507 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005508 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005509 }
5510
5511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 * Launch a new activity. You will not receive any information about when
5513 * the activity exits. This implementation overrides the base version,
5514 * providing information about
5515 * the activity performing the launch. Because of this additional
5516 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5517 * required; if not specified, the new activity will be added to the
5518 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005519 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 * <p>This method throws {@link android.content.ActivityNotFoundException}
5521 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005522 *
5523 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005524 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005525 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005526 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005527 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005529 *
Mark Lufa434852016-08-11 17:40:33 -07005530 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005531 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 */
5533 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005534 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005535 if (options != null) {
5536 startActivityForResult(intent, -1, options);
5537 } else {
5538 // Note we want to go through this call for compatibility with
5539 // applications that may have overridden the method.
5540 startActivityForResult(intent, -1);
5541 }
5542 }
5543
5544 /**
5545 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5546 * specified.
5547 *
5548 * @param intents The intents to start.
5549 *
5550 * @throws android.content.ActivityNotFoundException
5551 *
Mark Lufa434852016-08-11 17:40:33 -07005552 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005553 * @see #startActivityForResult
5554 */
5555 @Override
5556 public void startActivities(Intent[] intents) {
5557 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 }
5559
5560 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005561 * Launch a new activity. You will not receive any information about when
5562 * the activity exits. This implementation overrides the base version,
5563 * providing information about
5564 * the activity performing the launch. Because of this additional
5565 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5566 * required; if not specified, the new activity will be added to the
5567 * task of the caller.
5568 *
5569 * <p>This method throws {@link android.content.ActivityNotFoundException}
5570 * if there was no Activity found to run the given Intent.
5571 *
5572 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005573 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005574 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005575 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005576 *
5577 * @throws android.content.ActivityNotFoundException
5578 *
Mark Lufa434852016-08-11 17:40:33 -07005579 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005580 * @see #startActivityForResult
5581 */
5582 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005583 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005584 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005585 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005586 }
5587
5588 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005589 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5590 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005591 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005592 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005593 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005594 * intent parameter to {@link IntentSender#sendIntent}.
5595 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005596 * would like to change.
5597 * @param flagsValues Desired values for any bits set in
5598 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005599 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005600 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005601 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005602 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005603 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005604 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5605 extraFlags, null);
5606 }
5607
5608 /**
5609 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5610 * to start; see
5611 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5612 * for more information.
5613 *
5614 * @param intent The IntentSender to launch.
5615 * @param fillInIntent If non-null, this will be provided as the
5616 * intent parameter to {@link IntentSender#sendIntent}.
5617 * @param flagsMask Intent flags in the original IntentSender that you
5618 * would like to change.
5619 * @param flagsValues Desired values for any bits set in
5620 * <var>flagsMask</var>
5621 * @param extraFlags Always set to 0.
5622 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005623 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005624 * Context.startActivity(Intent, Bundle)} for more details. If options
5625 * have also been supplied by the IntentSender, options given here will
5626 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005627 */
5628 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005629 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005630 Bundle options) throws IntentSender.SendIntentException {
5631 if (options != null) {
5632 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5633 flagsValues, extraFlags, options);
5634 } else {
5635 // Note we want to go through this call for compatibility with
5636 // applications that may have overridden the method.
5637 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5638 flagsValues, extraFlags);
5639 }
5640 }
5641
5642 /**
5643 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5644 * with no options.
5645 *
5646 * @param intent The intent to start.
5647 * @param requestCode If >= 0, this code will be returned in
5648 * onActivityResult() when the activity exits, as described in
5649 * {@link #startActivityForResult}.
5650 *
5651 * @return If a new activity was launched then true is returned; otherwise
5652 * false is returned and you must handle the Intent yourself.
5653 *
5654 * @see #startActivity
5655 * @see #startActivityForResult
5656 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005657 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5658 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005659 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005660 }
5661
5662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663 * A special variation to launch an activity only if a new activity
5664 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005665 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005667 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005669 * and the activity
5670 * that handles <var>intent</var> is the same as your currently running
5671 * activity, then a new instance is not needed. In this case, instead of
5672 * the normal behavior of calling {@link #onNewIntent} this function will
5673 * return and you can handle the Intent yourself.
5674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 * <p>This function can only be called from a top-level activity; if it is
5676 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005677 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 * @param intent The intent to start.
5679 * @param requestCode If >= 0, this code will be returned in
5680 * onActivityResult() when the activity exits, as described in
5681 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005682 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005683 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005684 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005685 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 * @return If a new activity was launched then true is returned; otherwise
5687 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005688 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 * @see #startActivity
5690 * @see #startActivityForResult
5691 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005692 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5693 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005695 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005697 Uri referrer = onProvideReferrer();
5698 if (referrer != null) {
5699 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5700 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005701 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005702 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005703 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005704 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005705 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5706 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5707 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 } catch (RemoteException e) {
5709 // Empty
5710 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 if (requestCode >= 0) {
5715 // If this start is requesting a result, we can avoid making
5716 // the activity visible until the result is received. Setting
5717 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5718 // activity hidden during this time, to avoid flickering.
5719 // This can only be done when a result is requested because
5720 // that guarantees we will get information back when the
5721 // activity is finished, no matter what happens to it.
5722 mStartedActivity = true;
5723 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005724 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 }
5726
5727 throw new UnsupportedOperationException(
5728 "startActivityIfNeeded can only be called from a top-level activity");
5729 }
5730
5731 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005732 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5733 * no options.
5734 *
5735 * @param intent The intent to dispatch to the next activity. For
5736 * correct behavior, this must be the same as the Intent that started
5737 * your own activity; the only changes you can make are to the extras
5738 * inside of it.
5739 *
5740 * @return Returns a boolean indicating whether there was another Activity
5741 * to start: true if there was a next activity to start, false if there
5742 * wasn't. In general, if true is returned you will then want to call
5743 * finish() on yourself.
5744 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005745 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005746 return startNextMatchingActivity(intent, null);
5747 }
5748
5749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 * Special version of starting an activity, for use when you are replacing
5751 * other activity components. You can use this to hand the Intent off
5752 * to the next Activity that can handle it. You typically call this in
5753 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 * @param intent The intent to dispatch to the next activity. For
5756 * correct behavior, this must be the same as the Intent that started
5757 * your own activity; the only changes you can make are to the extras
5758 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005759 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005760 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005761 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005762 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 * @return Returns a boolean indicating whether there was another Activity
5764 * to start: true if there was a next activity to start, false if there
5765 * wasn't. In general, if true is returned you will then want to call
5766 * finish() on yourself.
5767 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005768 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5769 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 if (mParent == null) {
5771 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005772 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005773 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005774 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005775 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 } catch (RemoteException e) {
5777 // Empty
5778 }
5779 return false;
5780 }
5781
5782 throw new UnsupportedOperationException(
5783 "startNextMatchingActivity can only be called from a top-level activity");
5784 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005785
5786 /**
5787 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5788 * with no options.
5789 *
5790 * @param child The activity making the call.
5791 * @param intent The intent to start.
5792 * @param requestCode Reply request code. < 0 if reply is not requested.
5793 *
5794 * @throws android.content.ActivityNotFoundException
5795 *
5796 * @see #startActivity
5797 * @see #startActivityForResult
5798 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005799 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005800 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005801 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005802 }
5803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 /**
RoboErik55011652014-07-09 15:05:53 -07005805 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005807 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 * <p>This method throws {@link android.content.ActivityNotFoundException}
5809 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 * @param child The activity making the call.
5812 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005813 * @param requestCode Reply request code. < 0 if reply is not requested.
5814 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005815 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005816 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005819 *
5820 * @see #startActivity
5821 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005823 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005824 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005825 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826 Instrumentation.ActivityResult ar =
5827 mInstrumentation.execStartActivity(
5828 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005829 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 if (ar != null) {
5831 mMainThread.sendActivityResult(
5832 mToken, child.mEmbeddedID, requestCode,
5833 ar.getResultCode(), ar.getResultData());
5834 }
George Mount41725de2015-04-09 08:23:05 -07005835 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 }
5837
5838 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005839 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5840 * with no options.
5841 *
5842 * @param fragment The fragment making the call.
5843 * @param intent The intent to start.
5844 * @param requestCode Reply request code. < 0 if reply is not requested.
5845 *
5846 * @throws android.content.ActivityNotFoundException
5847 *
5848 * @see Fragment#startActivity
5849 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005850 *
5851 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5852 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005853 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005854 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005855 public void startActivityFromFragment(@NonNull Fragment fragment,
5856 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005857 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005858 }
5859
5860 /**
RoboErik55011652014-07-09 15:05:53 -07005861 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005862 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5863 * method.
RoboErik55011652014-07-09 15:05:53 -07005864 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005865 * <p>This method throws {@link android.content.ActivityNotFoundException}
5866 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005867 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005868 * @param fragment The fragment making the call.
5869 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005870 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005871 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005872 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005873 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005874 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005875 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005876 *
5877 * @see Fragment#startActivity
5878 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005879 *
5880 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5881 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005882 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005883 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005884 public void startActivityFromFragment(@NonNull Fragment fragment,
5885 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005886 startActivityForResult(fragment.mWho, intent, requestCode, options);
5887 }
5888
5889 /**
5890 * @hide
5891 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005892 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5893 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5894 UserHandle user) {
5895 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5896 }
5897
5898 /**
5899 * @hide
5900 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005901 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005902 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005903 public void startActivityForResult(
5904 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005905 Uri referrer = onProvideReferrer();
5906 if (referrer != null) {
5907 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5908 }
George Mount413739e2016-06-08 07:13:37 -07005909 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005910 Instrumentation.ActivityResult ar =
5911 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005912 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005913 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005914 if (ar != null) {
5915 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005916 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005917 ar.getResultCode(), ar.getResultData());
5918 }
George Mount41725de2015-04-09 08:23:05 -07005919 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005920 }
5921
5922 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005923 * @hide
5924 */
5925 @Override
5926 public boolean canStartActivityForResult() {
5927 return true;
5928 }
5929
5930 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005931 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5932 * int, Intent, int, int, int, Bundle)} with no options.
5933 */
5934 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5935 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5936 int extraFlags)
5937 throws IntentSender.SendIntentException {
5938 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5939 flagsMask, flagsValues, extraFlags, null);
5940 }
5941
5942 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005943 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005944 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005945 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005946 * for more information.
5947 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005948 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5949 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005950 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005951 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005952 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5953 flagsMask, flagsValues, options);
5954 }
5955
5956 /**
5957 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5958 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5959 * for more information.
5960 *
5961 * @hide
5962 */
5963 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5964 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5965 int extraFlags, @Nullable Bundle options)
5966 throws IntentSender.SendIntentException {
5967 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5968 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005969 }
5970
5971 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005972 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5973 * or {@link #finish} to specify an explicit transition animation to
5974 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005975 *
5976 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5977 * to using this with starting activities is to supply the desired animation
5978 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005979 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005980 * you to specify a custom animation even when starting an activity from
5981 * outside the context of the current top activity.
5982 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005983 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005984 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005985 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005986 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005987 */
5988 public void overridePendingTransition(int enterAnim, int exitAnim) {
5989 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005990 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005991 mToken, getPackageName(), enterAnim, exitAnim);
5992 } catch (RemoteException e) {
5993 }
5994 }
RoboErik55011652014-07-09 15:05:53 -07005995
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 * Call this to set the result that your activity will return to its
5998 * caller.
RoboErik55011652014-07-09 15:05:53 -07005999 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 * @param resultCode The result code to propagate back to the originating
6001 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07006002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 * @see #RESULT_CANCELED
6004 * @see #RESULT_OK
6005 * @see #RESULT_FIRST_USER
6006 * @see #setResult(int, Intent)
6007 */
6008 public final void setResult(int resultCode) {
6009 synchronized (this) {
6010 mResultCode = resultCode;
6011 mResultData = null;
6012 }
6013 }
6014
6015 /**
6016 * Call this to set the result that your activity will return to its
6017 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08006018 *
6019 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
6020 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
6021 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
6022 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
6023 * Activity receiving the result access to the specific URIs in the Intent.
6024 * Access will remain until the Activity has finished (it will remain across the hosting
6025 * process being killed and other temporary destruction) and will be added
6026 * to any existing set of URI permissions it already holds.
6027 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 * @param resultCode The result code to propagate back to the originating
6029 * activity, often RESULT_CANCELED or RESULT_OK
6030 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07006031 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 * @see #RESULT_CANCELED
6033 * @see #RESULT_OK
6034 * @see #RESULT_FIRST_USER
6035 * @see #setResult(int)
6036 */
6037 public final void setResult(int resultCode, Intent data) {
6038 synchronized (this) {
6039 mResultCode = resultCode;
6040 mResultData = data;
6041 }
6042 }
6043
6044 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006045 * Return information about who launched this activity. If the launching Intent
6046 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
6047 * that will be returned as-is; otherwise, if known, an
6048 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
6049 * package name that started the Intent will be returned. This may return null if no
6050 * referrer can be identified -- it is neither explicitly specified, nor is it known which
6051 * application package was involved.
6052 *
6053 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
6054 * return the referrer that submitted that new intent to the activity. Otherwise, it
6055 * always returns the referrer of the original Intent.</p>
6056 *
6057 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
6058 * referrer information, applications can spoof it.</p>
6059 */
6060 @Nullable
6061 public Uri getReferrer() {
6062 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07006063 try {
6064 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
6065 if (referrer != null) {
6066 return referrer;
6067 }
6068 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
6069 if (referrerName != null) {
6070 return Uri.parse(referrerName);
6071 }
6072 } catch (BadParcelableException e) {
6073 Log.w(TAG, "Cannot read referrer from intent;"
6074 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006075 }
6076 if (mReferrer != null) {
6077 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
6078 }
6079 return null;
6080 }
6081
6082 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07006083 * Override to generate the desired referrer for the content currently being shown
6084 * by the app. The default implementation returns null, meaning the referrer will simply
6085 * be the android-app: of the package name of this activity. Return a non-null Uri to
6086 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
6087 */
6088 public Uri onProvideReferrer() {
6089 return null;
6090 }
6091
6092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 * Return the name of the package that invoked this activity. This is who
6094 * the data in {@link #setResult setResult()} will be sent to. You can
6095 * use this information to validate that the recipient is allowed to
6096 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006097 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006098 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006099 * did not use the {@link #startActivityForResult}
6100 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07006101 * null.</p>
6102 *
6103 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
6104 * the result from this method was unstable. If the process hosting the calling
6105 * package was no longer running, it would return null instead of the proper package
6106 * name. You can use {@link #getCallingActivity()} and retrieve the package name
6107 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07006108 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 * @return The package of the activity that will receive your
6110 * reply, or null if none.
6111 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006112 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 public String getCallingPackage() {
6114 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006115 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 } catch (RemoteException e) {
6117 return null;
6118 }
6119 }
6120
6121 /**
6122 * Return the name of the activity that invoked this activity. This is
6123 * who the data in {@link #setResult setResult()} will be sent to. You
6124 * can use this information to validate that the recipient is allowed to
6125 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006126 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006127 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006128 * did not use the {@link #startActivityForResult}
6129 * form that includes a request code), then the calling package will be
6130 * null.
6131 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006132 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 * reply, or null if none.
6134 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006135 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006136 public ComponentName getCallingActivity() {
6137 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006138 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 } catch (RemoteException e) {
6140 return null;
6141 }
6142 }
6143
6144 /**
6145 * Control whether this activity's main window is visible. This is intended
6146 * only for the special case of an activity that is not going to show a
6147 * UI itself, but can't just finish prior to onResume() because it needs
6148 * to wait for a service binding or such. Setting this to false allows
6149 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07006150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 * <p>The default value for this is taken from the
6152 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
6153 */
6154 public void setVisible(boolean visible) {
6155 if (mVisibleFromClient != visible) {
6156 mVisibleFromClient = visible;
6157 if (mVisibleFromServer) {
6158 if (visible) makeVisible();
6159 else mDecor.setVisibility(View.INVISIBLE);
6160 }
6161 }
6162 }
RoboErik55011652014-07-09 15:05:53 -07006163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 void makeVisible() {
6165 if (!mWindowAdded) {
6166 ViewManager wm = getWindowManager();
6167 wm.addView(mDecor, getWindow().getAttributes());
6168 mWindowAdded = true;
6169 }
6170 mDecor.setVisibility(View.VISIBLE);
6171 }
RoboErik55011652014-07-09 15:05:53 -07006172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 /**
6174 * Check to see whether this activity is in the process of finishing,
6175 * either because you called {@link #finish} on it or someone else
6176 * has requested that it finished. This is often used in
6177 * {@link #onPause} to determine whether the activity is simply pausing or
6178 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07006179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07006181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 * @see #finish
6183 */
6184 public boolean isFinishing() {
6185 return mFinished;
6186 }
6187
6188 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006189 * Returns true if the final {@link #onDestroy()} call has been made
6190 * on the Activity, so this instance is now dead.
6191 */
6192 public boolean isDestroyed() {
6193 return mDestroyed;
6194 }
6195
6196 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006197 * Check to see whether this activity is in the process of being destroyed in order to be
6198 * recreated with a new configuration. This is often used in
6199 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
6200 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07006201 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006202 * @return If the activity is being torn down in order to be recreated with a new configuration,
6203 * returns true; else returns false.
6204 */
6205 public boolean isChangingConfigurations() {
6206 return mChangingConfigurations;
6207 }
6208
6209 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006210 * Cause this Activity to be recreated with a new instance. This results
6211 * in essentially the same flow as when the Activity is created due to
6212 * a configuration change -- the current instance will go through its
6213 * lifecycle to {@link #onDestroy} and a new instance then created after it.
6214 */
6215 public void recreate() {
6216 if (mParent != null) {
6217 throw new IllegalStateException("Can only be called on top-level activity");
6218 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07006219 if (Looper.myLooper() != mMainThread.getLooper()) {
6220 throw new IllegalStateException("Must be called from main thread");
6221 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006222 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006223 }
6224
6225 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006226 * Finishes the current activity and specifies whether to remove the task associated with this
6227 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006229 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006230 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 if (mParent == null) {
6232 int resultCode;
6233 Intent resultData;
6234 synchronized (this) {
6235 resultCode = mResultCode;
6236 resultData = mResultData;
6237 }
Joe Onorato43a17652011-04-06 19:22:23 -07006238 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006240 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006241 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006242 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006243 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006244 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 mFinished = true;
6246 }
6247 } catch (RemoteException e) {
6248 // Empty
6249 }
6250 } else {
6251 mParent.finishFromChild(this);
6252 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006253
6254 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6255 // be restored now.
6256 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
6257 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6258 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
6259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 }
6261
6262 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006263 * Call this when your activity is done and should be closed. The
6264 * ActivityResult is propagated back to whoever launched you via
6265 * onActivityResult().
6266 */
6267 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006268 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006269 }
6270
6271 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006272 * Finish this activity as well as all activities immediately below it
6273 * in the current task that have the same affinity. This is typically
6274 * used when an application can be launched on to another task (such as
6275 * from an ACTION_VIEW of a content type it understands) and the user
6276 * has used the up navigation to switch out of the current task and in
6277 * to its own task. In this case, if the user has navigated down into
6278 * any other activities of the second application, all of those should
6279 * be removed from the original task as part of the task switch.
6280 *
6281 * <p>Note that this finish does <em>not</em> allow you to deliver results
6282 * to the previous activity, and an exception will be thrown if you are trying
6283 * to do so.</p>
6284 */
6285 public void finishAffinity() {
6286 if (mParent != null) {
6287 throw new IllegalStateException("Can not be called from an embedded activity");
6288 }
6289 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6290 throw new IllegalStateException("Can not be called to deliver a result");
6291 }
6292 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006293 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006294 mFinished = true;
6295 }
6296 } catch (RemoteException e) {
6297 // Empty
6298 }
6299 }
6300
6301 /**
RoboErik55011652014-07-09 15:05:53 -07006302 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 * {@link #finish} method. The default implementation simply calls
6304 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 * @see #finish
6309 */
6310 public void finishFromChild(Activity child) {
6311 finish();
6312 }
6313
6314 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006315 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6316 * to reverse its exit Transition. When the exit Transition completes,
6317 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6318 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006319 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006320 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006321 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006322 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006323 finish();
6324 }
George Mountcb4b7d92014-02-25 10:47:55 -08006325 }
6326
6327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 * Force finish another activity that you had previously started with
6329 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 * @param requestCode The request code of the activity that you had
6332 * given to startActivityForResult(). If there are multiple
6333 * activities started with this request code, they
6334 * will all be finished.
6335 */
6336 public void finishActivity(int requestCode) {
6337 if (mParent == null) {
6338 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006339 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6341 } catch (RemoteException e) {
6342 // Empty
6343 }
6344 } else {
6345 mParent.finishActivityFromChild(this, requestCode);
6346 }
6347 }
6348
6349 /**
6350 * This is called when a child activity of this one calls its
6351 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006352 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 * @param child The activity making the call.
6354 * @param requestCode Request code that had been used to start the
6355 * activity.
6356 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006357 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006359 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6361 } catch (RemoteException e) {
6362 // Empty
6363 }
6364 }
6365
6366 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006367 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006368 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006369 */
6370 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006371 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006372 }
6373
6374 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006375 * Ask that the local app instance of this activity be released to free up its memory.
6376 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6377 * a new instance of the activity will later be re-created if needed due to the user
6378 * navigating back to it.
6379 *
6380 * @return Returns true if the activity was in a state that it has started the process
6381 * of destroying its current instance; returns false if for any reason this could not
6382 * be done: it is currently visible to the user, it is already being destroyed, it is
6383 * being finished, it hasn't yet saved its state, etc.
6384 */
6385 public boolean releaseInstance() {
6386 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006387 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006388 } catch (RemoteException e) {
6389 // Empty
6390 }
6391 return false;
6392 }
6393
6394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 * Called when an activity you launched exits, giving you the requestCode
6396 * you started it with, the resultCode it returned, and any additional
6397 * data from it. The <var>resultCode</var> will be
6398 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6399 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 * <p>You will receive this call immediately before onResume() when your
6402 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006403 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006404 * <p>This method is never invoked if your activity sets
6405 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6406 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006407 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 * @param requestCode The integer request code originally supplied to
6409 * startActivityForResult(), allowing you to identify who this
6410 * result came from.
6411 * @param resultCode The integer result code returned by the child activity
6412 * through its setResult().
6413 * @param data An Intent, which can return result data to the caller
6414 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 * @see #startActivityForResult
6417 * @see #createPendingResult
6418 * @see #setResult(int)
6419 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006420 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 }
6422
6423 /**
George Mount62ab9b72014-05-02 13:51:17 -07006424 * Called when an activity you launched with an activity transition exposes this
6425 * Activity through a returning activity transition, giving you the resultCode
6426 * and any additional data from it. This method will only be called if the activity
6427 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006428 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006429 *
6430 * <p>The purpose of this function is to let the called Activity send a hint about
6431 * its state so that this underlying Activity can prepare to be exposed. A call to
6432 * this method does not guarantee that the called Activity has or will be exiting soon.
6433 * It only indicates that it will expose this Activity's Window and it has
6434 * some data to pass to prepare it.</p>
6435 *
6436 * @param resultCode The integer result code returned by the child activity
6437 * through its setResult().
6438 * @param data An Intent, which can return result data to the caller
6439 * (various data can be attached to Intent "extras").
6440 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006441 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006442 }
6443
6444 /**
RoboErik55011652014-07-09 15:05:53 -07006445 * Create a new PendingIntent object which you can hand to others
6446 * for them to use to send result data back to your
6447 * {@link #onActivityResult} callback. The created object will be either
6448 * one-shot (becoming invalid after a result is sent back) or multiple
6449 * (allowing any number of results to be sent through it).
6450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 * @param requestCode Private request code for the sender that will be
6452 * associated with the result data when it is returned. The sender can not
6453 * modify this value, allowing you to identify incoming results.
6454 * @param data Default data to supply in the result, which may be modified
6455 * by the sender.
6456 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6457 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6458 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6459 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6460 * or any of the flags as supported by
6461 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6462 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 * @return Returns an existing or new PendingIntent matching the given
6465 * parameters. May return null only if
6466 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6467 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 * @see PendingIntent
6470 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006471 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6472 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 String packageName = getPackageName();
6474 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006475 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006477 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07006478 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07006480 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006481 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 return target != null ? new PendingIntent(target) : null;
6483 } catch (RemoteException e) {
6484 // Empty
6485 }
6486 return null;
6487 }
6488
6489 /**
6490 * Change the desired orientation of this activity. If the activity
6491 * is currently in the foreground or otherwise impacting the screen
6492 * orientation, the screen will immediately be changed (possibly causing
6493 * the activity to be restarted). Otherwise, this will be used the next
6494 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006495 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 * @param requestedOrientation An orientation constant as used in
6497 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6498 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006499 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 if (mParent == null) {
6501 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006502 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 mToken, requestedOrientation);
6504 } catch (RemoteException e) {
6505 // Empty
6506 }
6507 } else {
6508 mParent.setRequestedOrientation(requestedOrientation);
6509 }
6510 }
RoboErik55011652014-07-09 15:05:53 -07006511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 /**
6513 * Return the current requested orientation of the activity. This will
6514 * either be the orientation requested in its component's manifest, or
6515 * the last requested orientation given to
6516 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006517 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 * @return Returns an orientation constant as used in
6519 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6520 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006521 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 public int getRequestedOrientation() {
6523 if (mParent == null) {
6524 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006525 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 .getRequestedOrientation(mToken);
6527 } catch (RemoteException e) {
6528 // Empty
6529 }
6530 } else {
6531 return mParent.getRequestedOrientation();
6532 }
6533 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6534 }
RoboErik55011652014-07-09 15:05:53 -07006535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 /**
6537 * Return the identifier of the task this activity is in. This identifier
6538 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006539 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 * @return Task identifier, an opaque integer.
6541 */
6542 public int getTaskId() {
6543 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006544 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 } catch (RemoteException e) {
6546 return -1;
6547 }
6548 }
6549
6550 /**
6551 * Return whether this activity is the root of a task. The root is the
6552 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006553 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 * @return True if this is the root activity, else false.
6555 */
Winson Chung4d8681f2017-05-23 16:22:08 -07006556 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 public boolean isTaskRoot() {
6558 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006559 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 } catch (RemoteException e) {
6561 return false;
6562 }
6563 }
6564
6565 /**
6566 * Move the task containing this activity to the back of the activity
6567 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006568 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 * @param nonRoot If false then this only works if the activity is the root
6570 * of a task; if true it will work for any activity in
6571 * a task.
RoboErik55011652014-07-09 15:05:53 -07006572 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573 * @return If the task was moved (or it was already at the
6574 * back) true is returned, else false.
6575 */
6576 public boolean moveTaskToBack(boolean nonRoot) {
6577 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006578 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 } catch (RemoteException e) {
6580 // Empty
6581 }
6582 return false;
6583 }
6584
6585 /**
6586 * Returns class name for this activity with the package prefix removed.
6587 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 * @return The local class name.
6590 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006591 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 public String getLocalClassName() {
6593 final String pkg = getPackageName();
6594 final String cls = mComponent.getClassName();
6595 int packageLen = pkg.length();
6596 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6597 || cls.charAt(packageLen) != '.') {
6598 return cls;
6599 }
6600 return cls.substring(packageLen+1);
6601 }
RoboErik55011652014-07-09 15:05:53 -07006602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006604 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006605 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 * @return Returns the complete component name for this activity
6607 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006608 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 return mComponent;
6610 }
6611
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006612 /** @hide */
6613 @Override
6614 public final ComponentName autofillClientGetComponentName() {
6615 return getComponentName();
6616 }
6617
Felipe Lemecbf7f262019-04-17 13:57:59 -07006618 /** @hide */
6619 @Override
6620 public final ComponentName contentCaptureClientGetComponentName() {
6621 return getComponentName();
6622 }
6623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 /**
6625 * Retrieve a {@link SharedPreferences} object for accessing preferences
6626 * that are private to this activity. This simply calls the underlying
6627 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6628 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006629 *
6630 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006631 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 *
6633 * @return Returns the single SharedPreferences instance that can be used
6634 * to retrieve and modify the preference values.
6635 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006636 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 return getSharedPreferences(getLocalClassName(), mode);
6638 }
RoboErik55011652014-07-09 15:05:53 -07006639
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006640 private void ensureSearchManager() {
6641 if (mSearchManager != null) {
6642 return;
6643 }
RoboErik55011652014-07-09 15:05:53 -07006644
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006645 try {
6646 mSearchManager = new SearchManager(this, null);
6647 } catch (ServiceNotFoundException e) {
6648 throw new IllegalStateException(e);
6649 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006650 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006653 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654 if (getBaseContext() == null) {
6655 throw new IllegalStateException(
6656 "System services not available to Activities before onCreate()");
6657 }
6658
6659 if (WINDOW_SERVICE.equals(name)) {
6660 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006661 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006662 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006663 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 }
6665 return super.getSystemService(name);
6666 }
6667
6668 /**
6669 * Change the title associated with this activity. If this is a
6670 * top-level activity, the title for its window will change. If it
6671 * is an embedded activity, the parent can do whatever it wants
6672 * with it.
6673 */
6674 public void setTitle(CharSequence title) {
6675 mTitle = title;
6676 onTitleChanged(title, mTitleColor);
6677
6678 if (mParent != null) {
6679 mParent.onChildTitleChanged(this, title);
6680 }
6681 }
6682
6683 /**
6684 * Change the title associated with this activity. If this is a
6685 * top-level activity, the title for its window will change. If it
6686 * is an embedded activity, the parent can do whatever it wants
6687 * with it.
6688 */
6689 public void setTitle(int titleId) {
6690 setTitle(getText(titleId));
6691 }
6692
Alan Viverette2525d9c2013-11-15 14:42:19 -08006693 /**
6694 * Change the color of the title associated with this activity.
6695 * <p>
6696 * This method is deprecated starting in API Level 11 and replaced by action
6697 * bar styles. For information on styling the Action Bar, read the <a
6698 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6699 * guide.
6700 *
6701 * @deprecated Use action bar styles instead.
6702 */
6703 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 public void setTitleColor(int textColor) {
6705 mTitleColor = textColor;
6706 onTitleChanged(mTitle, textColor);
6707 }
6708
6709 public final CharSequence getTitle() {
6710 return mTitle;
6711 }
6712
6713 public final int getTitleColor() {
6714 return mTitleColor;
6715 }
6716
6717 protected void onTitleChanged(CharSequence title, int color) {
6718 if (mTitleReady) {
6719 final Window win = getWindow();
6720 if (win != null) {
6721 win.setTitle(title);
6722 if (color != 0) {
6723 win.setTitleColor(color);
6724 }
6725 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006726 if (mActionBar != null) {
6727 mActionBar.setWindowTitle(title);
6728 }
Adam Powella557fdc2014-08-21 18:05:53 -07006729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 }
6731
6732 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6733 }
6734
6735 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006736 * Sets information describing the task with this activity for presentation inside the Recents
6737 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6738 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6739 * for each property until a suitable value is found. For each task the taskDescription will be
6740 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006741 *
6742 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006743 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006744 *
Winson Chunga449dc02014-05-16 11:15:04 -07006745 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006746 */
Winson Chunga449dc02014-05-16 11:15:04 -07006747 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006748 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006749 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006750 // Scale the icon down to something reasonable if it is provided
6751 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6752 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6753 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6754 true);
6755 mTaskDescription.setIcon(icon);
6756 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006757 }
6758 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006759 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006760 } catch (RemoteException e) {
6761 }
6762 }
6763
6764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 * Sets the visibility of the progress bar in the title.
6766 * <p>
6767 * In order for the progress bar to be shown, the feature must be requested
6768 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006771 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006773 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 public final void setProgressBarVisibility(boolean visible) {
6775 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6776 Window.PROGRESS_VISIBILITY_OFF);
6777 }
6778
6779 /**
6780 * Sets the visibility of the indeterminate progress bar in the title.
6781 * <p>
6782 * In order for the progress bar to be shown, the feature must be requested
6783 * via {@link #requestWindowFeature(int)}.
6784 *
6785 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006786 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006788 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6790 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6791 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6792 }
RoboErik55011652014-07-09 15:05:53 -07006793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 /**
6795 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6796 * is always indeterminate).
6797 * <p>
6798 * In order for the progress bar to be shown, the feature must be requested
6799 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006802 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006804 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006805 public final void setProgressBarIndeterminate(boolean indeterminate) {
6806 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006807 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6808 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 }
RoboErik55011652014-07-09 15:05:53 -07006810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 /**
6812 * Sets the progress for the progress bars in the title.
6813 * <p>
6814 * In order for the progress bar to be shown, the feature must be requested
6815 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 * @param progress The progress for the progress bar. Valid ranges are from
6818 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6819 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006820 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006822 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 public final void setProgress(int progress) {
6824 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6825 }
RoboErik55011652014-07-09 15:05:53 -07006826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 /**
6828 * Sets the secondary progress for the progress bar in the title. This
6829 * progress is drawn between the primary progress (set via
6830 * {@link #setProgress(int)} and the background. It can be ideal for media
6831 * scenarios such as showing the buffering progress while the default
6832 * progress shows the play progress.
6833 * <p>
6834 * In order for the progress bar to be shown, the feature must be requested
6835 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006836 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6838 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006839 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006841 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 public final void setSecondaryProgress(int secondaryProgress) {
6843 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6844 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6845 }
6846
6847 /**
6848 * Suggests an audio stream whose volume should be changed by the hardware
6849 * volume controls.
6850 * <p>
6851 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006852 * Volume requests which are received while the Activity is in the
6853 * foreground will affect this stream.
6854 * <p>
6855 * It is not guaranteed that the hardware volume controls will always change
6856 * this stream's volume (for example, if a call is in progress, its stream's
6857 * volume may be changed instead). To reset back to the default, use
6858 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006861 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006862 */
6863 public final void setVolumeControlStream(int streamType) {
6864 getWindow().setVolumeControlStream(streamType);
6865 }
6866
6867 /**
6868 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006869 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006870 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 * @return The suggested audio stream type whose volume should be changed by
6872 * the hardware volume controls.
6873 * @see #setVolumeControlStream(int)
6874 */
6875 public final int getVolumeControlStream() {
6876 return getWindow().getVolumeControlStream();
6877 }
RoboErik55011652014-07-09 15:05:53 -07006878
6879 /**
6880 * Sets a {@link MediaController} to send media keys and volume changes to.
6881 * <p>
6882 * The controller will be tied to the window of this Activity. Media key and
6883 * volume events which are received while the Activity is in the foreground
6884 * will be forwarded to the controller and used to invoke transport controls
6885 * or adjust the volume. This may be used instead of or in addition to
6886 * {@link #setVolumeControlStream} to affect a specific session instead of a
6887 * specific stream.
6888 * <p>
6889 * It is not guaranteed that the hardware volume controls will always change
6890 * this session's volume (for example, if a call is in progress, its
6891 * stream's volume may be changed instead). To reset back to the default use
6892 * null as the controller.
6893 *
6894 * @param controller The controller for the session which should receive
6895 * media keys and volume changes.
6896 */
6897 public final void setMediaController(MediaController controller) {
6898 getWindow().setMediaController(controller);
6899 }
6900
6901 /**
6902 * Gets the controller which should be receiving media key and volume events
6903 * while this activity is in the foreground.
6904 *
6905 * @return The controller which should receive events.
6906 * @see #setMediaController(android.media.session.MediaController)
6907 */
6908 public final MediaController getMediaController() {
6909 return getWindow().getMediaController();
6910 }
6911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 /**
6913 * Runs the specified action on the UI thread. If the current thread is the UI
6914 * thread, then the action is executed immediately. If the current thread is
6915 * not the UI thread, the action is posted to the event queue of the UI thread.
6916 *
6917 * @param action the action to run on the UI thread
6918 */
6919 public final void runOnUiThread(Runnable action) {
6920 if (Thread.currentThread() != mUiThread) {
6921 mHandler.post(action);
6922 } else {
6923 action.run();
6924 }
6925 }
6926
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006927 /** @hide */
6928 @Override
6929 public final void autofillClientRunOnUiThread(Runnable action) {
6930 runOnUiThread(action);
6931 }
6932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006934 * Standard implementation of
6935 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6936 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006937 * This implementation does nothing and is for
6938 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6939 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6940 *
6941 * @see android.view.LayoutInflater#createView
6942 * @see android.view.Window#getLayoutInflater
6943 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006944 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07006945 public View onCreateView(@NonNull String name, @NonNull Context context,
6946 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006947 return null;
6948 }
6949
6950 /**
6951 * Standard implementation of
6952 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6953 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006954 * This implementation handles <fragment> tags to embed fragments inside
6955 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 *
6957 * @see android.view.LayoutInflater#createView
6958 * @see android.view.Window#getLayoutInflater
6959 */
Ian Lake0c807f42018-09-12 14:03:27 -07006960 @Nullable
6961 public View onCreateView(@Nullable View parent, @NonNull String name,
6962 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006963 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006964 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006965 }
RoboErik55011652014-07-09 15:05:53 -07006966
Adam Powell371a8092014-06-20 12:51:12 -07006967 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 }
6969
Daniel Sandler69a48172010-06-23 16:29:36 -04006970 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006971 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006972 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006973 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006974 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006975 * @param fd The raw file descriptor that the dump is being sent to.
6976 * @param writer The PrintWriter to which you should dump your state. This will be
6977 * closed for you after you return.
6978 * @param args additional arguments to the dump request.
6979 */
Ian Lake140fe122018-09-13 14:59:38 -07006980 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
6981 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006982 dumpInner(prefix, fd, writer, args);
6983 }
6984
Ian Lake140fe122018-09-13 14:59:38 -07006985 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
6986 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08006987 if (args != null && args.length > 0) {
6988 // Handle special cases
6989 switch (args[0]) {
6990 case "--autofill":
6991 dumpAutofillManager(prefix, writer);
6992 return;
Felipe Lemeb718e032018-12-10 09:44:23 -08006993 case "--contentcapture":
6994 dumpContentCaptureManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006995 return;
6996 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07006997 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006998 writer.print(prefix); writer.print("Local Activity ");
6999 writer.print(Integer.toHexString(System.identityHashCode(this)));
7000 writer.println(" State:");
7001 String innerPrefix = prefix + " ";
7002 writer.print(innerPrefix); writer.print("mResumed=");
7003 writer.print(mResumed); writer.print(" mStopped=");
7004 writer.print(mStopped); writer.print(" mFinished=");
7005 writer.println(mFinished);
Hongwei Wangf0b5fb12019-07-25 15:14:47 -07007006 writer.print(innerPrefix); writer.print("mLastDispatchedIsInMultiWindowMode=");
7007 writer.print(mLastDispatchedIsInMultiWindowMode);
7008 writer.print(" mLastDispatchedIsInPictureInPictureMode=");
7009 writer.println(mLastDispatchedIsInPictureInPictureMode);
Dianne Hackborn30d71892010-12-11 10:37:55 -08007010 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
7011 writer.println(mChangingConfigurations);
7012 writer.print(innerPrefix); writer.print("mCurrentConfig=");
7013 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07007014
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007015 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
7016 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07007017 if (mVoiceInteractor != null) {
7018 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
7019 }
Jeff Brown5182c782013-10-15 20:31:52 -07007020
Michael Wright5f48dc72013-11-01 12:42:49 -07007021 if (getWindow() != null &&
7022 getWindow().peekDecorView() != null &&
7023 getWindow().peekDecorView().getViewRootImpl() != null) {
7024 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
7025 }
Jeff Brown5182c782013-10-15 20:31:52 -07007026
7027 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07007028
Felipe Lemeb546ca72018-08-15 08:44:12 -07007029 dumpAutofillManager(prefix, writer);
Felipe Lemeb718e032018-12-10 09:44:23 -08007030 dumpContentCaptureManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07007031
7032 ResourcesManager.getInstance().dump(prefix, writer);
7033 }
7034
7035 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08007036 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07007037 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07007038 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08007039 writer.print(prefix); writer.print("Autofill Compat Mode: ");
7040 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07007041 } else {
7042 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07007043 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07007044 }
7045
Felipe Lemeb718e032018-12-10 09:44:23 -08007046 void dumpContentCaptureManager(String prefix, PrintWriter writer) {
7047 final ContentCaptureManager cm = getContentCaptureManager();
7048 if (cm != null) {
7049 cm.dump(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08007050 } else {
Felipe Lemeb718e032018-12-10 09:44:23 -08007051 writer.print(prefix); writer.println("No ContentCaptureManager");
Felipe Lemee348dc32018-11-05 12:35:29 -08007052 }
7053 }
7054
Dianne Hackborn625ac272010-09-17 18:29:22 -07007055 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007056 * Bit indicating that this activity is "immersive" and should not be
7057 * interrupted by notifications if possible.
7058 *
7059 * This value is initially set by the manifest property
7060 * <code>android:immersive</code> but may be changed at runtime by
7061 * {@link #setImmersive}.
7062 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007063 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04007064 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7065 */
7066 public boolean isImmersive() {
7067 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007068 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04007069 } catch (RemoteException e) {
7070 return false;
7071 }
7072 }
7073
7074 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07007075 * Indication of whether this is the highest level activity in this task. Can be used to
7076 * determine whether an activity launched by this activity was placed in the same task or
7077 * another task.
7078 *
7079 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07007080 */
Sunny Goyald85bed52018-09-25 12:01:01 -07007081 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07007082 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07007083 return false;
7084 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07007085 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007086 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07007087 } catch (RemoteException e) {
7088 return false;
7089 }
7090 }
7091
7092 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07007093 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
7094 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007095 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07007096 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007097 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
7098 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07007099 * This call has no effect on non-translucent activities or on activities with the
7100 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007101 *
George Mount62ab9b72014-05-02 13:51:17 -07007102 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7103 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007104 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007105 *
7106 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07007107 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007108 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007109 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07007110 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07007111 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007112 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007113 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
7114 }
Craig Mautner4addfc52013-06-25 08:05:45 -07007115 } catch (RemoteException e) {
7116 // pass
7117 }
7118 }
7119
7120 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007121 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
7122 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
7123 * <p>
7124 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
7125 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
7126 * be called indicating that it is safe to make this activity translucent again. Until
7127 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
7128 * behind the frontmost Activity will be indeterminate.
7129 * <p>
7130 * This call has no effect on non-translucent activities or on activities with the
7131 * {@link android.R.attr#windowIsFloating} attribute.
7132 *
7133 * @param callback the method to call when all visible Activities behind this one have been
7134 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07007135 * @param options activity options delivered to the activity below this one. The options
7136 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07007137 * @return <code>true</code> if Window was opaque and will become translucent or
7138 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007139 *
7140 * @see #convertFromTranslucent()
7141 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007142 *
7143 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007144 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007145 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07007146 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07007147 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007148 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007149 try {
7150 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007151 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007152 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07007153 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07007154 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007155 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007156 // Make callback return as though it timed out.
7157 mChangeCanvasToTranslucent = false;
7158 drawComplete = false;
7159 }
7160 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
7161 // Window is already translucent.
7162 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07007163 }
George Mount3cc716c2014-06-12 16:35:35 -07007164 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007165 }
7166
7167 /** @hide */
7168 void onTranslucentConversionComplete(boolean drawComplete) {
7169 if (mTranslucentCallback != null) {
7170 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
7171 mTranslucentCallback = null;
7172 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007173 if (mChangeCanvasToTranslucent) {
7174 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
7175 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007176 }
7177
Craig Mautnereb8abf72014-07-02 15:04:09 -07007178 /** @hide */
7179 public void onNewActivityOptions(ActivityOptions options) {
7180 mActivityTransitionState.setEnterActivityOptions(this, options);
7181 if (!mStopped) {
7182 mActivityTransitionState.enterReady(this);
7183 }
7184 }
7185
Craig Mautner5eda9b32013-07-02 11:58:16 -07007186 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07007187 * Retrieve the ActivityOptions passed in from the launching activity or passed back
7188 * from an activity launched by this activity in its call to {@link
7189 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
7190 *
7191 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
7192 * @hide
7193 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007194 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07007195 ActivityOptions getActivityOptions() {
7196 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007197 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007198 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07007199 } catch (RemoteException e) {
7200 }
7201 return null;
7202 }
7203
7204 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007205 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07007206 * this method anytime between the start of {@link #onResume()} and the return from
7207 * {@link #onPause()}. If this call is successful then the activity will remain visible after
7208 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
7209 *
7210 * <p>The actions of this call are reset each time that this activity is brought to the
7211 * front. That is, every time {@link #onResume()} is called the activity will be assumed
7212 * to not have requested visible behind. Therefore, if you want this activity to continue to
7213 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007214 *
7215 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
7216 * for dialog and translucent activities.
7217 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007218 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
7219 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
7220 *
7221 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07007222 * the next call to onResume.
7223 *
Bryce Leed59629e2017-04-18 14:35:29 -07007224 * @deprecated This method's functionality is no longer supported as of
7225 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7226 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007227 * @param visible true to notify the system that the activity wishes to be visible behind other
7228 * translucent activities, false to indicate otherwise. Resources must be
7229 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07007230 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007231 * @return the resulting visibiity state. If true the activity will remain visible beyond
7232 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7233 * then the activity may not count on being visible behind other translucent activities,
7234 * and must stop any media playback and release resources.
7235 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7236 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007237 *
Jose Limafcf70832014-08-27 23:09:05 -07007238 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007239 */
Bryce Leed59629e2017-04-18 14:35:29 -07007240 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007241 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007242 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007243 }
7244
7245 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007246 * Called when a translucent activity over this activity is becoming opaque or another
7247 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007248 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007249 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007250 * <p>When this method is called the activity has 500 msec to release any resources it may be
7251 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007252 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007253 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007254 * process. Otherwise {@link #onStop()} will be called following return.
7255 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007256 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007257 *
7258 * @deprecated This method's functionality is no longer supported as of
7259 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007260 */
Bryce Leed59629e2017-04-18 14:35:29 -07007261 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007262 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007263 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007264 mCalled = true;
7265 }
7266
7267 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007268 * Translucent activities may call this to determine if there is an activity below them that
7269 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007270 *
Bryce Leed59629e2017-04-18 14:35:29 -07007271 * @deprecated This method's functionality is no longer supported as of
7272 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7273 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007274 * @return true if an activity below is set to visible according to the most recent call to
7275 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007276 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007277 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007278 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007279 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007280 * @hide
7281 */
Bryce Leed59629e2017-04-18 14:35:29 -07007282 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007283 @SystemApi
7284 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007285 return false;
7286 }
7287
7288 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007289 * The topmost foreground activity will receive this call when the background visibility state
7290 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007291 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007292 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007293 * due to a background activity finishing itself.
7294 *
Bryce Leed59629e2017-04-18 14:35:29 -07007295 * @deprecated This method's functionality is no longer supported as of
7296 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7297 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007298 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007299 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007300 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007301 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007302 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007303 */
Bryce Leed59629e2017-04-18 14:35:29 -07007304 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007305 @SystemApi
7306 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007307 }
7308
7309 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007310 * Activities cannot draw during the period that their windows are animating in. In order
7311 * to know when it is safe to begin drawing they can override this method which will be
7312 * called when the entering animation has completed.
7313 */
7314 public void onEnterAnimationComplete() {
7315 }
7316
7317 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007318 * @hide
7319 */
7320 public void dispatchEnterAnimationComplete() {
chaviwfeb2e1e2018-12-19 17:24:11 -08007321 mEnterAnimationComplete = true;
7322 mInstrumentation.onEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007323 onEnterAnimationComplete();
7324 if (getWindow() != null && getWindow().getDecorView() != null) {
Felipe Leme01297692019-01-29 18:16:23 -08007325 View decorView = getWindow().getDecorView();
7326 decorView.getViewTreeObserver().dispatchOnEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007327 }
7328 }
7329
7330 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007331 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007332 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007333 * Note that changing this value will have no effect on the activity's
7334 * {@link android.content.pm.ActivityInfo} structure; that is, if
7335 * <code>android:immersive</code> is set to <code>true</code>
7336 * in the application's manifest entry for this activity, the {@link
7337 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7338 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7339 * FLAG_IMMERSIVE} bit set.
7340 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007341 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007342 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7343 */
7344 public void setImmersive(boolean i) {
7345 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007346 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007347 } catch (RemoteException e) {
7348 // pass
7349 }
7350 }
7351
Adam Powell6e346362010-07-23 10:18:23 -07007352 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007353 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007354 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007355 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7356 * while this Activity has user focus.</p>
7357 *
7358 * <p>It is recommended that applications additionally declare
7359 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7360 * transitions when switching between VR activities.</p>
7361 *
7362 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7363 * VR mode will not be started. Developers can handle this case as follows:</p>
7364 *
7365 * <pre>
7366 * String servicePackage = "com.whatever.app";
7367 * String serviceClass = "com.whatever.app.MyVrListenerService";
7368 *
7369 * // Name of the component of the VrListenerService to start.
7370 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7371 *
7372 * try {
7373 * setVrModeEnabled(true, myComponentName);
7374 * } catch (PackageManager.NameNotFoundException e) {
7375 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7376 * boolean isInstalled = false;
7377 * for (ApplicationInfo app : installed) {
7378 * if (app.packageName.equals(servicePackage)) {
7379 * isInstalled = true;
7380 * break;
7381 * }
7382 * }
7383 * if (isInstalled) {
7384 * // Package is installed, but not enabled in Settings. Let user enable it.
7385 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7386 * } else {
7387 * // Package is not installed. Send an intent to download this.
7388 * sentIntentToLaunchAppStore(servicePackage);
7389 * }
7390 * }
7391 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007392 *
7393 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007394 * @param requestedComponent the name of the component to use as a
7395 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7396 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007397 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7398 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7399 * not been enabled in user settings.
7400 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007401 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7402 * @see android.service.vr.VrListenerService
7403 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7404 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007405 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007406 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7407 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007408 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007409 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007410 != 0) {
7411 throw new PackageManager.NameNotFoundException(
7412 requestedComponent.flattenToString());
7413 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007414 } catch (RemoteException e) {
7415 // pass
7416 }
7417 }
7418
7419 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007420 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007421 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007422 * @param callback Callback that will manage lifecycle events for this action mode
7423 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007424 *
7425 * @see ActionMode
7426 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007427 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007428 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007429 return mWindow.getDecorView().startActionMode(callback);
7430 }
7431
Adam Powelldebf3be2010-11-15 18:58:48 -08007432 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007433 * Start an action mode of the given type.
7434 *
7435 * @param callback Callback that will manage lifecycle events for this action mode
7436 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7437 * @return The ActionMode that was started, or null if it was canceled
7438 *
7439 * @see ActionMode
7440 */
7441 @Nullable
7442 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7443 return mWindow.getDecorView().startActionMode(callback, type);
7444 }
7445
7446 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007447 * Give the Activity a chance to control the UI for an action mode requested
7448 * by the system.
7449 *
7450 * <p>Note: If you are looking for a notification callback that an action mode
7451 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7452 *
7453 * @param callback The callback that should control the new action mode
7454 * @return The new action mode, or <code>null</code> if the activity does not want to
7455 * provide special handling for this action mode. (It will be handled by the system.)
7456 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007457 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007458 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007459 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007460 // Only Primary ActionModes are represented in the ActionBar.
7461 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7462 initWindowDecorActionBar();
7463 if (mActionBar != null) {
7464 return mActionBar.startActionMode(callback);
7465 }
Adam Powell6e346362010-07-23 10:18:23 -07007466 }
7467 return null;
7468 }
7469
Adam Powelldebf3be2010-11-15 18:58:48 -08007470 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007471 * {@inheritDoc}
7472 */
7473 @Nullable
7474 @Override
7475 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7476 try {
7477 mActionModeTypeStarting = type;
7478 return onWindowStartingActionMode(callback);
7479 } finally {
7480 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7481 }
7482 }
7483
7484 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007485 * Notifies the Activity that an action mode has been started.
7486 * Activity subclasses overriding this method should call the superclass implementation.
7487 *
7488 * @param mode The new action mode.
7489 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007490 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007491 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007492 public void onActionModeStarted(ActionMode mode) {
7493 }
7494
7495 /**
7496 * Notifies the activity that an action mode has finished.
7497 * Activity subclasses overriding this method should call the superclass implementation.
7498 *
7499 * @param mode The action mode that just finished.
7500 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007501 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007502 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007503 public void onActionModeFinished(ActionMode mode) {
7504 }
7505
Adam Powelldd8fab22012-03-22 17:47:27 -07007506 /**
7507 * Returns true if the app should recreate the task when navigating 'up' from this activity
7508 * by using targetIntent.
7509 *
7510 * <p>If this method returns false the app can trivially call
7511 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7512 * up navigation. If this method returns false, the app should synthesize a new task stack
7513 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7514 *
7515 * @param targetIntent An intent representing the target destination for up navigation
7516 * @return true if navigating up should recreate a new task stack, false if the same task
7517 * should be used for the destination
7518 */
7519 public boolean shouldUpRecreateTask(Intent targetIntent) {
7520 try {
7521 PackageManager pm = getPackageManager();
7522 ComponentName cn = targetIntent.getComponent();
7523 if (cn == null) {
7524 cn = targetIntent.resolveActivity(pm);
7525 }
7526 ActivityInfo info = pm.getActivityInfo(cn, 0);
7527 if (info.taskAffinity == null) {
7528 return false;
7529 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007530 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007531 } catch (RemoteException e) {
7532 return false;
7533 } catch (NameNotFoundException e) {
7534 return false;
7535 }
7536 }
7537
7538 /**
7539 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7540 * in the process. If the activity indicated by upIntent already exists in the task's history,
7541 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007542 * finished.
7543 *
7544 * <p>If the indicated activity does not appear in the history stack, this will finish
7545 * each activity in this task until the root activity of the task is reached, resulting in
7546 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7547 * when an activity may be reached by a path not passing through a canonical parent
7548 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007549 *
7550 * <p>This method should be used when performing up navigation from within the same task
7551 * as the destination. If up navigation should cross tasks in some cases, see
7552 * {@link #shouldUpRecreateTask(Intent)}.</p>
7553 *
7554 * @param upIntent An intent representing the target destination for up navigation
7555 *
7556 * @return true if up navigation successfully reached the activity indicated by upIntent and
7557 * upIntent was delivered to it. false if an instance of the indicated activity could
7558 * not be found and this activity was simply finished normally.
7559 */
7560 public boolean navigateUpTo(Intent upIntent) {
7561 if (mParent == null) {
7562 ComponentName destInfo = upIntent.getComponent();
7563 if (destInfo == null) {
7564 destInfo = upIntent.resolveActivity(getPackageManager());
7565 if (destInfo == null) {
7566 return false;
7567 }
7568 upIntent = new Intent(upIntent);
7569 upIntent.setComponent(destInfo);
7570 }
7571 int resultCode;
7572 Intent resultData;
7573 synchronized (this) {
7574 resultCode = mResultCode;
7575 resultData = mResultData;
7576 }
7577 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007578 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007579 }
7580 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007581 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007582 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007583 resultCode, resultData);
7584 } catch (RemoteException e) {
7585 return false;
7586 }
7587 } else {
7588 return mParent.navigateUpToFromChild(this, upIntent);
7589 }
7590 }
7591
7592 /**
7593 * This is called when a child activity of this one calls its
7594 * {@link #navigateUpTo} method. The default implementation simply calls
7595 * navigateUpTo(upIntent) on this activity (the parent).
7596 *
7597 * @param child The activity making the call.
7598 * @param upIntent An intent representing the target destination for up navigation
7599 *
7600 * @return true if up navigation successfully reached the activity indicated by upIntent and
7601 * upIntent was delivered to it. false if an instance of the indicated activity could
7602 * not be found and this activity was simply finished normally.
7603 */
7604 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7605 return navigateUpTo(upIntent);
7606 }
7607
7608 /**
7609 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7610 * this activity's logical parent. The logical parent is named in the application's manifest
7611 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007612 * Activity subclasses may override this method to modify the Intent returned by
7613 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7614 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007615 *
Adam Powell04d58112012-04-09 10:22:12 -07007616 * @return a new Intent targeting the defined parent of this activity or null if
7617 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007618 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007619 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007620 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007621 final String parentName = mActivityInfo.parentActivityName;
7622 if (TextUtils.isEmpty(parentName)) {
7623 return null;
7624 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007625
7626 // If the parent itself has no parent, generate a main activity intent.
7627 final ComponentName target = new ComponentName(this, parentName);
7628 try {
7629 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7630 final String parentActivity = parentInfo.parentActivityName;
7631 final Intent parentIntent = parentActivity == null
7632 ? Intent.makeMainActivity(target)
7633 : new Intent().setComponent(target);
7634 return parentIntent;
7635 } catch (NameNotFoundException e) {
7636 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7637 "' in manifest");
7638 return null;
7639 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007640 }
7641
George Mount31a21722014-03-24 17:44:36 -07007642 /**
George Mount62ab9b72014-05-02 13:51:17 -07007643 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007644 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007645 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007646 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007647 *
George Mount65580562014-08-29 08:15:48 -07007648 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007649 */
George Mount65580562014-08-29 08:15:48 -07007650 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7651 if (callback == null) {
7652 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007653 }
George Mount65580562014-08-29 08:15:48 -07007654 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007655 }
7656
7657 /**
7658 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007659 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007660 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7661 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007662 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007663 *
George Mount65580562014-08-29 08:15:48 -07007664 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007665 */
George Mount65580562014-08-29 08:15:48 -07007666 public void setExitSharedElementCallback(SharedElementCallback callback) {
7667 if (callback == null) {
7668 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007669 }
George Mount65580562014-08-29 08:15:48 -07007670 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007671 }
7672
George Mount8c2614c2014-06-10 11:12:01 -07007673 /**
7674 * Postpone the entering activity transition when Activity was started with
7675 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7676 * android.util.Pair[])}.
7677 * <p>This method gives the Activity the ability to delay starting the entering and
7678 * shared element transitions until all data is loaded. Until then, the Activity won't
7679 * draw into its window, leaving the window transparent. This may also cause the
7680 * returning animation to be delayed until data is ready. This method should be
7681 * called in {@link #onCreate(android.os.Bundle)} or in
7682 * {@link #onActivityReenter(int, android.content.Intent)}.
7683 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7684 * start the transitions. If the Activity did not use
7685 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7686 * android.util.Pair[])}, then this method does nothing.</p>
7687 */
7688 public void postponeEnterTransition() {
7689 mActivityTransitionState.postponeEnterTransition();
7690 }
7691
7692 /**
7693 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7694 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7695 * to have your Activity start drawing.
7696 */
7697 public void startPostponedEnterTransition() {
7698 mActivityTransitionState.startPostponedEnterTransition();
7699 }
7700
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007701 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007702 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7703 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007704 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007705 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7706 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007707 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007708 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7709 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7710 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7711 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007712 }
7713 return null;
7714 }
7715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007717
Mathew Inwood8c854f82018-09-14 12:35:36 +01007718 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 final void setParent(Activity parent) {
7720 mParent = parent;
7721 }
7722
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007723 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007724 final void attach(Context context, ActivityThread aThread,
7725 Instrumentation instr, IBinder token, int ident,
7726 Application application, Intent intent, ActivityInfo info,
7727 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007728 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007729 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Sunny Goyald40c3452019-03-20 12:46:55 -07007730 Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 attachBaseContext(context);
7732
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007733 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007734
Andrii Kulian51c1b672017-04-07 18:39:32 -07007735 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007736 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007738 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007739 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7741 mWindow.setSoftInputMode(info.softInputMode);
7742 }
Adam Powell269248d2011-08-02 10:26:54 -07007743 if (info.uiOptions != 0) {
7744 mWindow.setUiOptions(info.uiOptions);
7745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 mMainThread = aThread;
7749 mInstrumentation = instr;
7750 mToken = token;
Sunny Goyald40c3452019-03-20 12:46:55 -07007751 mAssistToken = assistToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007752 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 mApplication = application;
7754 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007755 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 mComponent = intent.getComponent();
7757 mActivityInfo = info;
7758 mTitle = title;
7759 mParent = parent;
7760 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007761 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007762 if (voiceInteractor != null) {
7763 if (lastNonConfigurationInstances != null) {
7764 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7765 } else {
7766 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7767 Looper.myLooper());
7768 }
7769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770
Jeff Brown98365d72012-08-19 20:30:52 -07007771 mWindow.setWindowManager(
7772 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7773 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007774 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 if (mParent != null) {
7776 mWindow.setContainer(mParent.getWindow());
7777 }
7778 mWindowManager = mWindow.getWindowManager();
7779 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007780
7781 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007782
Felipe Lemea4f39cd2019-02-19 15:08:59 -08007783 setAutofillOptions(application.getAutofillOptions());
Felipe Leme326f15a2019-02-19 09:42:24 -08007784 setContentCaptureOptions(application.getContentCaptureOptions());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007785 }
7786
7787 private void enableAutofillCompatibilityIfNeeded() {
7788 if (isAutofillCompatibilityEnabled()) {
7789 final AutofillManager afm = getSystemService(AutofillManager.class);
7790 if (afm != null) {
7791 afm.enableCompatibilityMode();
7792 }
7793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 }
7795
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007796 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007797 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007798 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 return mParent != null ? mParent.getActivityToken() : mToken;
7800 }
7801
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007802 /** @hide */
Sunny Goyald40c3452019-03-20 12:46:55 -07007803 public final IBinder getAssistToken() {
7804 return mParent != null ? mParent.getAssistToken() : mAssistToken;
7805 }
7806
7807 /** @hide */
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007808 @VisibleForTesting
7809 public final ActivityThread getActivityThread() {
7810 return mMainThread;
7811 }
7812
Winson Chung298f95b2017-08-10 15:57:18 -07007813 final void performCreate(Bundle icicle) {
7814 performCreate(icicle, null);
7815 }
7816
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007817 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007818 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007819 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007820 mCanEnterPictureInPicture = true;
7821 restoreHasCurrentPermissionRequest(icicle);
7822 if (persistentState != null) {
7823 onCreate(icicle, persistentState);
7824 } else {
7825 onCreate(icicle);
7826 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007827 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007828 mActivityTransitionState.readState(icicle);
7829
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007830 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7831 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007832 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007833 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007834 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007835 }
George Mount0a778ed2013-12-13 13:35:36 -08007836
Ian Lake0b71b8e2018-09-13 13:56:26 -07007837 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07007838 mCanEnterPictureInPicture = true;
7839 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007840 }
7841
Andrii Kuliand25680c2018-02-21 15:16:58 -08007842 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007843 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07007844 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007845 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007847 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007849 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 if (!mCalled) {
7852 throw new SuperNotCalledException(
7853 "Activity " + mComponent.toShortString() +
7854 " did not call through to super.onStart()");
7855 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007856 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007857 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007858
Elliott Hughese14fa972019-10-02 11:35:53 -07007859 // Warn app developers if the dynamic linker logged anything during startup.
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007860 boolean isAppDebuggable =
7861 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Elliott Hughese14fa972019-10-02 11:35:53 -07007862 if (isAppDebuggable) {
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007863 String dlwarning = getDlWarning();
7864 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007865 String appName = getApplicationInfo().loadLabel(getPackageManager())
7866 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007867 String warning = "Detected problems with app native libraries\n" +
7868 "(please consult log for detail):\n" + dlwarning;
7869 if (isAppDebuggable) {
7870 new AlertDialog.Builder(this).
7871 setTitle(appName).
7872 setMessage(warning).
7873 setPositiveButton(android.R.string.ok, null).
7874 setCancelable(false).
7875 show();
7876 } else {
7877 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7878 }
7879 }
7880 }
7881
Tim Van Pattenddc43912018-12-18 17:47:52 -07007882 GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
7883
George Mount62ab9b72014-05-02 13:51:17 -07007884 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07007885 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 }
RoboErik55011652014-07-09 15:05:53 -07007887
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007888 /**
7889 * Restart the activity.
7890 * @param start Indicates whether the activity should also be started after restart.
7891 * The option to not start immediately is needed in case a transaction with
7892 * multiple lifecycle transitions is in progress.
7893 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007894 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007895 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007896 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007897
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007898 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007899 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007900 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7901 }
7902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 if (mStopped) {
7904 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007905
7906 synchronized (mManagedCursors) {
7907 final int N = mManagedCursors.size();
7908 for (int i=0; i<N; i++) {
7909 ManagedCursor mc = mManagedCursors.get(i);
7910 if (mc.mReleased || mc.mUpdated) {
7911 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007912 if (getApplicationInfo().targetSdkVersion
7913 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7914 throw new IllegalStateException(
7915 "trying to requery an already closed cursor "
7916 + mc.mCursor);
7917 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007918 }
7919 mc.mReleased = false;
7920 mc.mUpdated = false;
7921 }
7922 }
7923 }
7924
7925 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007926 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007927 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007928 if (!mCalled) {
7929 throw new SuperNotCalledException(
7930 "Activity " + mComponent.toShortString() +
7931 " did not call through to super.onRestart()");
7932 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007933 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007934 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 }
7937 }
RoboErik55011652014-07-09 15:05:53 -07007938
Andrii Kuliand25680c2018-02-21 15:16:58 -08007939 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007940 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007941 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007942
Dianne Hackborn445646c2010-06-25 15:52:59 -07007943 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007944
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007945 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007946
Dake Gu67decfa2017-12-27 11:48:08 -08007947 if (mAutoFillResetNeeded) {
7948 // When Activity is destroyed in paused state, and relaunch activity, there will be
7949 // extra onResume and onPause event, ignore the first onResume and onPause.
7950 // see ActivityThread.handleRelaunchActivity()
7951 mAutoFillIgnoreFirstResumePause = followedByPause;
7952 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7953 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7954 }
7955 }
7956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007958 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007960 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 if (!mCalled) {
7962 throw new SuperNotCalledException(
7963 "Activity " + mComponent.toShortString() +
7964 " did not call through to super.onResume()");
7965 }
7966
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007967 // invisible activities must be finished before onResume() completes
7968 if (!mVisibleFromClient && !mFinished) {
7969 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7970 if (getApplicationInfo().targetSdkVersion
7971 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7972 throw new IllegalStateException(
7973 "Activity " + mComponent.toShortString() +
7974 " did not call finish() prior to onResume() completing");
7975 }
7976 }
7977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007979 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007980
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007981 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007982 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 onPostResume();
7985 if (!mCalled) {
7986 throw new SuperNotCalledException(
7987 "Activity " + mComponent.toShortString() +
7988 " did not call through to super.onPostResume()");
7989 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007990 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 }
7992
7993 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007994 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007995 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007996 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007997 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007999 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08008000 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07008001 if (!mCalled && getApplicationInfo().targetSdkVersion
8002 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
8003 throw new SuperNotCalledException(
8004 "Activity " + mComponent.toShortString() +
8005 " did not call through to super.onPause()");
8006 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008007 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 }
RoboErik55011652014-07-09 15:05:53 -07008009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 final void performUserLeaving() {
8011 onUserInteraction();
8012 onUserLeaveHint();
8013 }
RoboErik55011652014-07-09 15:05:53 -07008014
Andrii Kuliand25680c2018-02-21 15:16:58 -08008015 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07008016 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008017 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00008018
Winson Chung298f95b2017-08-10 15:57:18 -07008019 // Disallow entering picture-in-picture after the activity has been stopped
8020 mCanEnterPictureInPicture = false;
8021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07008023 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 if (mWindow != null) {
8025 mWindow.closeAllPanels();
8026 }
8027
Chong Zhang7687f252016-02-26 12:03:33 -08008028 // If we're preserving the window, don't setStoppedState to true, since we
8029 // need the window started immediately again. Stopping the window will
8030 // destroys hardware resources and causes flicker.
8031 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07008032 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08008033 }
RoboErik55011652014-07-09 15:05:53 -07008034
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008035 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07008036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 mCalled = false;
8038 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08008039 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 if (!mCalled) {
8041 throw new SuperNotCalledException(
8042 "Activity " + mComponent.toShortString() +
8043 " did not call through to super.onStop()");
8044 }
RoboErik55011652014-07-09 15:05:53 -07008045
Makoto Onuki2f6a0182010-02-22 13:26:59 -08008046 synchronized (mManagedCursors) {
8047 final int N = mManagedCursors.size();
8048 for (int i=0; i<N; i++) {
8049 ManagedCursor mc = mManagedCursors.get(i);
8050 if (!mc.mReleased) {
8051 mc.mCursor.deactivate();
8052 mc.mReleased = true;
8053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 }
8055 }
George Mount0a778ed2013-12-13 13:35:36 -08008056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07008058 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 }
8060 mResumed = false;
8061 }
8062
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008063 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07008064 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07008065 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07008066 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008067 mFragments.dispatchDestroy();
8068 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08008069 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008070 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07008071 if (mVoiceInteractor != null) {
8072 mVoiceInteractor.detachActivity();
8073 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008074 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008075 }
George Mount0a778ed2013-12-13 13:35:36 -08008076
Winson Chung5af42fc2017-03-24 17:11:33 -07008077 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
8078 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008079 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008080 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
8081 + " " + newConfig);
8082 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008083 if (mWindow != null) {
8084 mWindow.onMultiWindowModeChanged();
8085 }
Hongwei Wangf0b5fb12019-07-25 15:14:47 -07008086 mLastDispatchedIsInMultiWindowMode = isInMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -07008087 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008088 }
8089
Winson Chung5af42fc2017-03-24 17:11:33 -07008090 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
8091 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008092 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008093 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
8094 + " " + newConfig);
8095 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07008096 if (mWindow != null) {
8097 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
8098 }
Hongwei Wangf0b5fb12019-07-25 15:14:47 -07008099 mLastDispatchedIsInPictureInPictureMode = isInPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -07008100 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008101 }
8102
George Mount0a778ed2013-12-13 13:35:36 -08008103 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06008104 * @hide
8105 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008106 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06008107 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008108 return mResumed;
8109 }
8110
Svetoslavffb32b12015-10-15 16:54:00 -07008111 private void storeHasCurrentPermissionRequest(Bundle bundle) {
8112 if (bundle != null && mHasCurrentPermissionsRequest) {
8113 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
8114 }
8115 }
8116
8117 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
8118 if (bundle != null) {
8119 mHasCurrentPermissionsRequest = bundle.getBoolean(
8120 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
8121 }
8122 }
8123
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008124 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07008125 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
8126 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07008127 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
8129 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07008130 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008132 onActivityResult(requestCode, resultCode, data);
8133 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
8134 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
8135 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08008136 dispatchRequestPermissionsResult(requestCode, data);
8137 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00008138 Fragment frag = mFragments.findFragmentByWho(who);
8139 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008140 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08008141 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07008142 }
Svetoslav970b59c2015-06-09 16:05:21 -07008143 } else if (who.startsWith("@android:view:")) {
8144 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
8145 getActivityToken());
8146 for (ViewRootImpl viewRoot : views) {
8147 if (viewRoot.getView() != null
8148 && viewRoot.getView().dispatchActivityResult(
8149 who, requestCode, resultCode, data)) {
8150 return;
8151 }
8152 }
Svet Ganov782043c2017-02-11 00:52:02 +00008153 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08008154 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08008155 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07008156 } else {
8157 Fragment frag = mFragments.findFragmentByWho(who);
8158 if (frag != null) {
8159 frag.onActivityResult(requestCode, resultCode, data);
8160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07008162 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07008164
Jason Monka57e5e02014-05-07 10:06:48 -04008165 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008166 * Request to put this activity in a mode where the user is locked to a restricted set of
8167 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04008168 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008169 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
8170 * for this component, the current task will be launched directly into LockTask mode. Only apps
8171 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
8172 * be launched while LockTask mode is active. The user will not be able to leave this mode
8173 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
8174 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04008175 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008176 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
8177 * system will prompt the user with a dialog requesting permission to use this mode.
8178 * The user can exit at any time through instructions shown on the request dialog. Calling
8179 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04008180 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008181 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
8182 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008183 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008184 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008185 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04008186 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008187 public void startLockTask() {
8188 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008189 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008190 } catch (RemoteException e) {
8191 }
8192 }
8193
Jason Monka57e5e02014-05-07 10:06:48 -04008194 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008195 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04008196 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008197 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
8198 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04008199 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008200 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
8201 * by this activity, then calling this method will not terminate the LockTask mode, but only
8202 * finish its own task. The device will remain in LockTask mode, until the activity which
8203 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
8204 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008205 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008206 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008207 * @see android.R.attr#lockTaskMode
8208 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04008209 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008210 public void stopLockTask() {
8211 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008212 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008213 } catch (RemoteException e) {
8214 }
8215 }
8216
Craig Mautner5eda9b32013-07-02 11:58:16 -07008217 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008218 * Shows the user the system defined message for telling the user how to exit
8219 * lock task mode. The task containing this activity must be in lock task mode at the time
8220 * of this call for the message to be displayed.
8221 */
8222 public void showLockTaskEscapeMessage() {
8223 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008224 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008225 } catch (RemoteException e) {
8226 }
8227 }
8228
8229 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07008230 * Check whether the caption on freeform windows is displayed directly on the content.
8231 *
8232 * @return True if caption is displayed on content, false if it pushes the content down.
8233 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08008234 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07008235 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07008236 */
8237 public boolean isOverlayWithDecorCaptionEnabled() {
8238 return mWindow.isOverlayWithDecorCaptionEnabled();
8239 }
8240
8241 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008242 * Set whether the caption should displayed directly on the content rather than push it down.
8243 *
8244 * This affects only freeform windows since they display the caption and only the main
8245 * window of the activity. The caption is used to drag the window around and also shows
8246 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008247 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008248 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008249 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8250 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008251 }
8252
8253 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008254 * Interface for informing a translucent {@link Activity} once all visible activities below it
8255 * have completed drawing. This is necessary only after an {@link Activity} has been made
8256 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8257 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008258 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8259 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008260 *
8261 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008262 */
Jose Lima14914852014-08-14 09:14:12 -07008263 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008264 public interface TranslucentConversionListener {
8265 /**
8266 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8267 * below the top one have been redrawn. Following this callback it is safe to make the top
8268 * Activity translucent because the underlying Activity has been drawn.
8269 *
8270 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8271 * occurred waiting for the Activity to complete drawing.
8272 *
8273 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008274 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008275 */
8276 public void onTranslucentConversionComplete(boolean drawComplete);
8277 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008278
8279 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008280 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008281 // If the package installer crashed we may have not data - best effort.
8282 String[] permissions = (data != null) ? data.getStringArrayExtra(
8283 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8284 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8285 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008286 onRequestPermissionsResult(requestCode, permissions, grantResults);
8287 }
8288
8289 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008290 Fragment fragment) {
8291 // If the package installer crashed we may have not data - best effort.
8292 String[] permissions = (data != null) ? data.getStringArrayExtra(
8293 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8294 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8295 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8296 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008297 }
8298
Svet Ganov782043c2017-02-11 00:52:02 +00008299 /** @hide */
8300 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008301 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008302 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00008303 try {
8304 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008305 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008306 } catch (IntentSender.SendIntentException e) {
8307 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8308 }
8309 }
8310
Svet Ganov17db9dc2017-02-21 19:54:31 -08008311 /** @hide */
8312 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008313 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008314 mAutoFillResetNeeded = true;
8315 }
8316
Felipe Leme4753bb02017-03-22 20:24:00 -07008317 /** @hide */
8318 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008319 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008320 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008321 final boolean wasShowing;
8322
8323 if (mAutofillPopupWindow == null) {
8324 wasShowing = false;
8325 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8326 } else {
8327 wasShowing = mAutofillPopupWindow.isShowing();
8328 }
Svet Ganov374cae12017-05-10 13:42:33 -07008329 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008330
8331 return !wasShowing && mAutofillPopupWindow.isShowing();
8332 }
8333
8334 /** @hide */
8335 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008336 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8337 @NonNull KeyEvent keyEvent) {
8338 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8339 if (rootImpl != null) {
8340 // dont care if anchorView is current focus, for example a custom view may only receive
8341 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8342 // might be inside parent of the custom view.
8343 rootImpl.dispatchKeyFromAutofill(keyEvent);
8344 }
8345 }
8346
8347 /** @hide */
8348 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008349 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008350 if (mAutofillPopupWindow == null) {
8351 return false;
8352 }
8353 mAutofillPopupWindow.dismiss();
8354 mAutofillPopupWindow = null;
8355 return true;
8356 }
8357
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008358 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008359 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008360 public final boolean autofillClientIsFillUiShowing() {
8361 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8362 }
8363
8364 /** @hide */
8365 @Override
8366 @NonNull
8367 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8368 @NonNull AutofillId[] autofillId) {
8369 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008370 final ArrayList<ViewRootImpl> roots =
8371 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8372
8373 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8374 final View rootView = roots.get(rootNum).getView();
8375
8376 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008377 final int viewCount = autofillId.length;
8378 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008379 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008380 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008381 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008382 }
8383 }
8384 }
8385 }
8386
8387 return views;
8388 }
8389
8390 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008391 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008392 @Nullable
8393 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008394 final ArrayList<ViewRootImpl> roots =
8395 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8396 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8397 final View rootView = roots.get(rootNum).getView();
8398
8399 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008400 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008401 if (view != null) {
8402 return view;
8403 }
8404 }
8405 }
8406
8407 return null;
8408 }
8409
8410 /** @hide */
8411 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008412 public final @NonNull boolean[] autofillClientGetViewVisibility(
8413 @NonNull AutofillId[] autofillIds) {
8414 final int autofillIdCount = autofillIds.length;
8415 final boolean[] visible = new boolean[autofillIdCount];
8416 for (int i = 0; i < autofillIdCount; i++) {
8417 final AutofillId autofillId = autofillIds[i];
8418 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8419 if (view != null) {
Felipe Lemea75333c2019-01-22 15:45:32 -08008420 if (!autofillId.isVirtualInt()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008421 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008422 } else {
Felipe Lemea75333c2019-01-22 15:45:32 -08008423 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildIntId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008424 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008425 }
8426 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008427 if (android.view.autofill.Helper.sVerbose) {
8428 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8429 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008430 return visible;
8431 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008432
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008433 /** @hide */
8434 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8435 int windowId) {
8436 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8437 .getRootViews(getActivityToken());
8438 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8439 final View rootView = roots.get(rootNum).getView();
8440 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8441 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8442 if (view != null) {
8443 return view;
8444 }
8445 }
8446 }
8447 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008448 }
8449
8450 /** @hide */
8451 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008452 public final @Nullable IBinder autofillClientGetActivityToken() {
8453 return getActivityToken();
8454 }
8455
8456 /** @hide */
8457 @Override
8458 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008459 return !mStopped;
8460 }
8461
Dake Gu67decfa2017-12-27 11:48:08 -08008462 /** @hide */
8463 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008464 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008465 return isAutofillCompatibilityEnabled();
8466 }
8467
8468 /** @hide */
8469 @Override
8470 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008471 return mAutoFillIgnoreFirstResumePause || !mResumed;
8472 }
8473
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008474 /**
8475 * If set to true, this indicates to the system that it should never take a
8476 * screenshot of the activity to be used as a representation while it is not in a started state.
8477 * <p>
8478 * Note that the system may use the window background of the theme instead to represent
8479 * the window when it is not running.
8480 * <p>
8481 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8482 * this only affects the behavior when the activity's screenshot would be used as a
8483 * representation when the activity is not in a started state, i.e. in Overview. The system may
8484 * still take screenshots of the activity in other contexts; for example, when the user takes a
8485 * screenshot of the entire screen, or when the active
8486 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8487 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8488 *
8489 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8490 * @hide
8491 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008492 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008493 public void setDisablePreviewScreenshots(boolean disable) {
8494 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008495 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008496 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008497 throw e.rethrowFromSystemServer();
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008498 }
8499 }
8500
chaviw59b98852017-06-13 12:05:44 -07008501 /**
koprivaa1a78482018-10-09 10:09:23 -07008502 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008503 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8504 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8505 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8506 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8507 *
8508 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8509 * {@code false} otherwise.
8510 * @see #setTurnScreenOn(boolean)
8511 * @see android.R.attr#turnScreenOn
8512 * @see android.R.attr#showWhenLocked
8513 */
8514 public void setShowWhenLocked(boolean showWhenLocked) {
8515 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008516 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008517 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008518 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008519 }
8520 }
8521
8522 /**
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008523 * Specifies whether this {@link Activity} should be shown on top of the lock screen whenever
8524 * the lockscreen is up and this activity has another activity behind it with the showWhenLock
8525 * attribute set. That is, this activity is only visible on the lock screen if there is another
8526 * activity with the showWhenLock attribute visible at the same time on the lock screen. A use
8527 * case for this is permission dialogs, that should only be visible on the lock screen if their
8528 * requesting activity is also visible. This value can be set as a manifest attribute using
8529 * android.R.attr#inheritShowWhenLocked.
8530 *
8531 * @param inheritShowWhenLocked {@code true} to show the {@link Activity} on top of the lock
8532 * screen when this activity has another activity behind it with
8533 * the showWhenLock attribute set; {@code false} otherwise.
8534 * @see #setShowWhenLocked(boolean)
Issei Suzuki7c09f0b2019-02-28 14:45:50 +01008535 * @see android.R.attr#inheritShowWhenLocked
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008536 */
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008537 public void setInheritShowWhenLocked(boolean inheritShowWhenLocked) {
8538 try {
8539 ActivityTaskManager.getService().setInheritShowWhenLocked(
8540 mToken, inheritShowWhenLocked);
8541 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008542 throw e.rethrowFromSystemServer();
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008543 }
8544 }
8545
8546 /**
chaviw59b98852017-06-13 12:05:44 -07008547 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8548 * Normally an activity will be transitioned to the stopped state if it is started while the
8549 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8550 * activity will be visible and resumed due to the screen coming on. The screen will not be
8551 * turned on if the activity won't be visible after the screen is turned on. This flag is
8552 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8553 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8554 * if this flag is set and the activity calls {@link
8555 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8556 * the screen will turn on.
8557 *
8558 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8559 *
8560 * @see #setShowWhenLocked(boolean)
8561 * @see android.R.attr#turnScreenOn
8562 * @see android.R.attr#showWhenLocked
8563 */
8564 public void setTurnScreenOn(boolean turnScreenOn) {
8565 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008566 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008567 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008568 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008569 }
8570 }
8571
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008572 /**
8573 * Registers remote animations per transition type for this activity.
8574 *
8575 * @param definition The remote animation definition that defines which transition whould run
8576 * which remote animation.
8577 * @hide
8578 */
8579 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008580 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008581 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8582 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008583 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008584 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008585 throw e.rethrowFromSystemServer();
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008586 }
8587 }
8588
Andrii Kuliand25680c2018-02-21 15:16:58 -08008589 /** Log a lifecycle event for current user id and component class. */
8590 private void writeEventLog(int event, String reason) {
8591 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
8592 reason);
8593 }
8594
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008595 class HostCallbacks extends FragmentHostCallback<Activity> {
8596 public HostCallbacks() {
8597 super(Activity.this /*activity*/);
8598 }
8599
8600 @Override
8601 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8602 Activity.this.dump(prefix, fd, writer, args);
8603 }
8604
8605 @Override
8606 public boolean onShouldSaveFragmentState(Fragment fragment) {
8607 return !isFinishing();
8608 }
8609
8610 @Override
8611 public LayoutInflater onGetLayoutInflater() {
8612 final LayoutInflater result = Activity.this.getLayoutInflater();
8613 if (onUseFragmentManagerInflaterFactory()) {
8614 return result.cloneInContext(Activity.this);
8615 }
8616 return result;
8617 }
8618
8619 @Override
8620 public boolean onUseFragmentManagerInflaterFactory() {
8621 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8622 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8623 }
8624
8625 @Override
8626 public Activity onGetHost() {
8627 return Activity.this;
8628 }
8629
8630 @Override
8631 public void onInvalidateOptionsMenu() {
8632 Activity.this.invalidateOptionsMenu();
8633 }
8634
8635 @Override
8636 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8637 Bundle options) {
8638 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8639 }
8640
8641 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008642 public void onStartActivityAsUserFromFragment(
8643 Fragment fragment, Intent intent, int requestCode, Bundle options,
8644 UserHandle user) {
8645 Activity.this.startActivityAsUserFromFragment(
8646 fragment, intent, requestCode, options, user);
8647 }
8648
8649 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008650 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8651 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8652 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8653 if (mParent == null) {
8654 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8655 flagsMask, flagsValues, options);
8656 } else if (options != null) {
8657 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
8658 fillInIntent, flagsMask, flagsValues, extraFlags, options);
8659 }
8660 }
8661
8662 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008663 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8664 int requestCode) {
8665 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8666 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8667 startActivityForResult(who, intent, requestCode, null);
8668 }
8669
8670 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008671 public boolean onHasWindowAnimations() {
8672 return getWindow() != null;
8673 }
8674
8675 @Override
8676 public int onGetWindowAnimations() {
8677 final Window w = getWindow();
8678 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8679 }
8680
Todd Kennedy434bd652015-05-04 12:29:50 -07008681 @Override
8682 public void onAttachFragment(Fragment fragment) {
8683 Activity.this.onAttachFragment(fragment);
8684 }
8685
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008686 @Nullable
8687 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008688 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008689 return Activity.this.findViewById(id);
8690 }
8691
8692 @Override
8693 public boolean onHasView() {
8694 final Window w = getWindow();
8695 return (w != null && w.peekDecorView() != null);
8696 }
8697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698}