blob: 27fac432a6028869327c74ce3f719403d39290ba [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 */
Tiger Huangfc218452018-09-27 00:38:50 +08002805 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002806 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002807 }
2808
Winson Chungb5c41b72016-12-07 15:00:47 -08002809 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 * Called by the system when the device configuration changes while your
2811 * activity is running. Note that this will <em>only</em> be called if
2812 * you have selected configurations you would like to handle with the
2813 * {@link android.R.attr#configChanges} attribute in your manifest. If
2814 * any configuration change occurs that is not selected to be reported
2815 * by that attribute, then instead of reporting it the system will stop
2816 * and restart the activity (to have it launched with the new
2817 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 * <p>At the time that this function has been called, your Resources
2820 * object will have been updated to return resource values matching the
2821 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 * @param newConfig The new device configuration.
2824 */
Ian Lake666a9652018-09-12 15:13:19 -07002825 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002826 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002828
Dianne Hackborn9d071802010-12-08 14:49:15 -08002829 mFragments.dispatchConfigurationChanged(newConfig);
2830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 if (mWindow != null) {
2832 // Pass the configuration changed event to the window
2833 mWindow.onConfigurationChanged(newConfig);
2834 }
Adam Powell45c0b192011-07-28 15:11:57 -07002835
2836 if (mActionBar != null) {
2837 // Do this last; the action bar will need to access
2838 // view changes from above.
2839 mActionBar.onConfigurationChanged(newConfig);
2840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 }
RoboErik55011652014-07-09 15:05:53 -07002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 /**
2844 * If this activity is being destroyed because it can not handle a
2845 * configuration parameter being changed (and thus its
2846 * {@link #onConfigurationChanged(Configuration)} method is
2847 * <em>not</em> being called), then you can use this method to discover
2848 * the set of changes that have occurred while in the process of being
2849 * destroyed. Note that there is no guarantee that these will be
2850 * accurate (other changes could have happened at any time), so you should
2851 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002852 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 * @return Returns a bit field of the configuration parameters that are
2854 * changing, as defined by the {@link android.content.res.Configuration}
2855 * class.
2856 */
2857 public int getChangingConfigurations() {
2858 return mConfigChangeFlags;
2859 }
RoboErik55011652014-07-09 15:05:53 -07002860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 /**
2862 * Retrieve the non-configuration instance data that was previously
2863 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2864 * be available from the initial {@link #onCreate} and
2865 * {@link #onStart} calls to the new instance, allowing you to extract
2866 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 * <p>Note that the data you retrieve here should <em>only</em> be used
2869 * as an optimization for handling configuration changes. You should always
2870 * be able to handle getting a null pointer back, and an activity must
2871 * still be able to restore itself to its previous state (through the
2872 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2873 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002874 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002875 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002876 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002877 * available on older platforms through the Android support libraries.
2878 *
2879 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002881 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002883 return mLastNonConfigurationInstances != null
2884 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
RoboErik55011652014-07-09 15:05:53 -07002886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 /**
2888 * Called by the system, as part of destroying an
2889 * activity due to a configuration change, when it is known that a new
2890 * instance will immediately be created for the new configuration. You
2891 * can return any object you like here, including the activity instance
2892 * itself, which can later be retrieved by calling
2893 * {@link #getLastNonConfigurationInstance()} in the new activity
2894 * instance.
RoboErik55011652014-07-09 15:05:53 -07002895 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002896 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2897 * or later, consider instead using a {@link Fragment} with
2898 * {@link Fragment#setRetainInstance(boolean)
2899 * Fragment.setRetainInstance(boolean}.</em>
2900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 * <p>This function is called purely as an optimization, and you must
2902 * not rely on it being called. When it is called, a number of guarantees
2903 * will be made to help optimize configuration switching:
2904 * <ul>
2905 * <li> The function will be called between {@link #onStop} and
2906 * {@link #onDestroy}.
2907 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002908 * created after this one's {@link #onDestroy()} is called. In particular,
2909 * <em>no</em> messages will be dispatched during this time (when the returned
2910 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 * <li> The object you return here will <em>always</em> be available from
2912 * the {@link #getLastNonConfigurationInstance()} method of the following
2913 * activity instance as described there.
2914 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002915 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 * <p>These guarantees are designed so that an activity can use this API
2917 * to propagate extensive state from the old to new activity instance, from
2918 * loaded bitmaps, to network connections, to evenly actively running
2919 * threads. Note that you should <em>not</em> propagate any data that
2920 * may change based on the configuration, including any data loaded from
2921 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002922 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002923 * <p>The guarantee of no message handling during the switch to the next
2924 * activity simplifies use with active objects. For example if your retained
2925 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2926 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2927 * not be called from the call here until you execute the next instance's
2928 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2929 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2930 * running in a separate thread.)
2931 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002932 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002933 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002934 * available on older platforms through the Android support libraries.
2935 *
2936 * @return any Object holding the desired state to propagate to the
2937 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 */
2939 public Object onRetainNonConfigurationInstance() {
2940 return null;
2941 }
RoboErik55011652014-07-09 15:05:53 -07002942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 /**
2944 * Retrieve the non-configuration instance data that was previously
2945 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2946 * be available from the initial {@link #onCreate} and
2947 * {@link #onStart} calls to the new instance, allowing you to extract
2948 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002949 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 * <p>Note that the data you retrieve here should <em>only</em> be used
2951 * as an optimization for handling configuration changes. You should always
2952 * be able to handle getting a null pointer back, and an activity must
2953 * still be able to restore itself to its previous state (through the
2954 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2955 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002956 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 * @return Returns the object previously returned by
2958 * {@link #onRetainNonConfigurationChildInstances()}
2959 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002960 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002961 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2962 return mLastNonConfigurationInstances != null
2963 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
RoboErik55011652014-07-09 15:05:53 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 /**
2967 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2968 * it should return either a mapping from child activity id strings to arbitrary objects,
2969 * or null. This method is intended to be used by Activity framework subclasses that control a
2970 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2971 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2972 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002973 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2975 return null;
2976 }
RoboErik55011652014-07-09 15:05:53 -07002977
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002978 NonConfigurationInstances retainNonConfigurationInstances() {
2979 Object activity = onRetainNonConfigurationInstance();
2980 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002981 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002982
2983 // We're already stopped but we've been asked to retain.
2984 // Our fragments are taken care of but we need to mark the loaders for retention.
2985 // In order to do this correctly we need to restart the loaders first before
2986 // handing them off to the next activity.
2987 mFragments.doLoaderStart();
2988 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002989 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002990
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002991 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002992 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002993 return null;
2994 }
RoboErik55011652014-07-09 15:05:53 -07002995
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002996 NonConfigurationInstances nci = new NonConfigurationInstances();
2997 nci.activity = activity;
2998 nci.children = children;
2999 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003000 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07003001 if (mVoiceInteractor != null) {
3002 mVoiceInteractor.retainInstance();
3003 nci.voiceInteractor = mVoiceInteractor;
3004 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003005 return nci;
3006 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003009 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08003011 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003013
3014 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003015 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003016 mCalled = true;
3017 mFragments.dispatchTrimMemory(level);
3018 }
3019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003021 * Return the FragmentManager for interacting with fragments associated
3022 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08003023 *
3024 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003025 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003026 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003027 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003028 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07003029 }
RoboErik55011652014-07-09 15:05:53 -07003030
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003031 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07003032 * Called when a Fragment is being attached to this activity, immediately
3033 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
3034 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08003035 *
3036 * @deprecated Use {@link
3037 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07003038 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003039 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07003040 public void onAttachFragment(Fragment fragment) {
3041 }
RoboErik55011652014-07-09 15:05:53 -07003042
Dianne Hackbornc8017682010-07-06 13:34:38 -07003043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 * Wrapper around
3045 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3046 * that gives the resulting {@link Cursor} to call
3047 * {@link #startManagingCursor} so that the activity will manage its
3048 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003049 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003050 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3051 * or later, consider instead using {@link LoaderManager} instead, available
3052 * via {@link #getLoaderManager()}.</em>
3053 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003054 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3055 * this method, because the activity will do that for you at the appropriate time. However, if
3056 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3057 * not</em> automatically close the cursor and, in that case, you must call
3058 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003059 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 * @param uri The URI of the content provider to query.
3061 * @param projection List of columns to return.
3062 * @param selection SQL WHERE clause.
3063 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003064 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003066 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3068 * @see #startManagingCursor
3069 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05003070 *
3071 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 */
Jason parks6ed50de2010-08-25 10:18:50 -05003073 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003074 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07003075 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3076 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
3078 if (c != null) {
3079 startManagingCursor(c);
3080 }
3081 return c;
3082 }
3083
3084 /**
3085 * Wrapper around
3086 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3087 * that gives the resulting {@link Cursor} to call
3088 * {@link #startManagingCursor} so that the activity will manage its
3089 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003090 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003091 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3092 * or later, consider instead using {@link LoaderManager} instead, available
3093 * via {@link #getLoaderManager()}.</em>
3094 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003095 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3096 * this method, because the activity will do that for you at the appropriate time. However, if
3097 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3098 * not</em> automatically close the cursor and, in that case, you must call
3099 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003100 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 * @param uri The URI of the content provider to query.
3102 * @param projection List of columns to return.
3103 * @param selection SQL WHERE clause.
3104 * @param selectionArgs The arguments to selection, if any ?s are pesent
3105 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003108 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3110 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05003111 *
3112 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 */
Jason parks6ed50de2010-08-25 10:18:50 -05003114 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07003115 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3116 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
3118 if (c != null) {
3119 startManagingCursor(c);
3120 }
3121 return c;
3122 }
3123
3124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 * This method allows the activity to take care of managing the given
3126 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
3127 * That is, when the activity is stopped it will automatically call
3128 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
3129 * it will call {@link Cursor#requery} for you. When the activity is
3130 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07003131 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003132 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3133 * or later, consider instead using {@link LoaderManager} instead, available
3134 * via {@link #getLoaderManager()}.</em>
3135 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003136 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
3137 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
3138 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
3139 * <em>will not</em> automatically close the cursor and, in that case, you must call
3140 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07003143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
3145 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003146 *
3147 * @deprecated Use the new {@link android.content.CursorLoader} class with
3148 * {@link LoaderManager} instead; this is also
3149 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 */
Jason parks6ed50de2010-08-25 10:18:50 -05003151 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 public void startManagingCursor(Cursor c) {
3153 synchronized (mManagedCursors) {
3154 mManagedCursors.add(new ManagedCursor(c));
3155 }
3156 }
3157
3158 /**
3159 * Given a Cursor that was previously given to
3160 * {@link #startManagingCursor}, stop the activity's management of that
3161 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003162 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003163 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003164 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003165 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003170 *
3171 * @deprecated Use the new {@link android.content.CursorLoader} class with
3172 * {@link LoaderManager} instead; this is also
3173 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 */
Jason parks6ed50de2010-08-25 10:18:50 -05003175 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 public void stopManagingCursor(Cursor c) {
3177 synchronized (mManagedCursors) {
3178 final int N = mManagedCursors.size();
3179 for (int i=0; i<N; i++) {
3180 ManagedCursor mc = mManagedCursors.get(i);
3181 if (mc.mCursor == c) {
3182 mManagedCursors.remove(i);
3183 break;
3184 }
3185 }
3186 }
3187 }
3188
3189 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003190 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3191 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003192 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003194 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003195 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
3198
3199 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003200 * Finds a view that was identified by the {@code android:id} XML attribute
3201 * that was processed in {@link #onCreate}.
3202 * <p>
3203 * <strong>Note:</strong> In most cases -- depending on compiler support --
3204 * the resulting view is automatically cast to the target class type. If
3205 * the target class type is unconstrained, an explicit cast may be
3206 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003208 * @param id the ID to search for
3209 * @return a view with given ID if found, or {@code null} otherwise
3210 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003211 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003213 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003214 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 return getWindow().findViewById(id);
3216 }
RoboErik55011652014-07-09 15:05:53 -07003217
Adam Powell33b97432010-04-20 10:01:14 -07003218 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003219 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3220 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3221 * no matching view in the hierarchy.
3222 * <p>
3223 * <strong>Note:</strong> In most cases -- depending on compiler support --
3224 * the resulting view is automatically cast to the target class type. If
3225 * the target class type is unconstrained, an explicit cast may be
3226 * necessary.
3227 *
3228 * @param id the ID to search for
3229 * @return a view with given ID
3230 * @see View#requireViewById(int)
3231 * @see Activity#findViewById(int)
3232 */
3233 @NonNull
3234 public final <T extends View> T requireViewById(@IdRes int id) {
3235 T view = findViewById(id);
3236 if (view == null) {
3237 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3238 }
3239 return view;
3240 }
3241
3242 /**
Adam Powell33b97432010-04-20 10:01:14 -07003243 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003244 *
Adam Powell33b97432010-04-20 10:01:14 -07003245 * @return The Activity's ActionBar, or null if it does not have one.
3246 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003247 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003248 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003249 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003250 return mActionBar;
3251 }
Adam Powelle43340c2014-03-17 19:10:43 -07003252
3253 /**
3254 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3255 * Activity window.
3256 *
3257 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3258 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3259 * a traditional window decor action bar. The toolbar's menu will be populated with the
3260 * Activity's options menu and the navigation button will be wired through the standard
3261 * {@link android.R.id#home home} menu select action.</p>
3262 *
3263 * <p>In order to use a Toolbar within the Activity's window content the application
3264 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3265 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003266 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003267 */
Adam Powell37780142014-06-01 13:31:00 -07003268 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003269 final ActionBar ab = getActionBar();
3270 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003271 throw new IllegalStateException("This Activity already has an action bar supplied " +
3272 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3273 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3274 }
Chris Banes21b25772016-01-04 20:41:59 +00003275
3276 // If we reach here then we're setting a new action bar
3277 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003278 mMenuInflater = null;
3279
Chris Banes21b25772016-01-04 20:41:59 +00003280 // If we have an action bar currently, destroy it
3281 if (ab != null) {
3282 ab.onDestroy();
3283 }
3284
Chris Banesc7d6c322016-01-27 14:09:16 +00003285 if (toolbar != null) {
3286 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3287 mActionBar = tbab;
3288 mWindow.setCallback(tbab.getWrappedWindowCallback());
3289 } else {
3290 mActionBar = null;
3291 // Re-set the original window callback since we may have already set a Toolbar wrapper
3292 mWindow.setCallback(this);
3293 }
3294
3295 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003296 }
RoboErik55011652014-07-09 15:05:53 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 /**
Adam Powell33b97432010-04-20 10:01:14 -07003299 * Creates a new ActionBar, locates the inflated ActionBarView,
3300 * initializes the ActionBar with the view, and sets mActionBar.
3301 */
Adam Powelle43340c2014-03-17 19:10:43 -07003302 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003303 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003304
3305 // Initializing the window decor can change window feature flags.
3306 // Make sure that we have the correct set before performing the test below.
3307 window.getDecorView();
3308
Adam Powell9b4c8042010-08-10 15:36:44 -07003309 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003310 return;
3311 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003312
Adam Powelle43340c2014-03-17 19:10:43 -07003313 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003314 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003315
3316 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3317 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003318 }
RoboErik55011652014-07-09 15:05:53 -07003319
Adam Powell33b97432010-04-20 10:01:14 -07003320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 * Set the activity content from a layout resource. The resource will be
3322 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003323 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003325 *
Romain Guy482b34a62011-01-20 10:59:28 -08003326 * @see #setContentView(android.view.View)
3327 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003329 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003331 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 }
3333
3334 /**
3335 * Set the activity content to an explicit view. This view is placed
3336 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003337 * view hierarchy. When calling this method, the layout parameters of the
3338 * specified view are ignored. Both the width and the height of the view are
3339 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3340 * your own layout parameters, invoke
3341 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3342 * instead.
RoboErik55011652014-07-09 15:05:53 -07003343 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003345 *
3346 * @see #setContentView(int)
3347 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 */
3349 public void setContentView(View view) {
3350 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003351 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
3353
3354 /**
3355 * Set the activity content to an explicit view. This view is placed
3356 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003357 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 * @param view The desired content to display.
3360 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003361 *
3362 * @see #setContentView(android.view.View)
3363 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 */
3365 public void setContentView(View view, ViewGroup.LayoutParams params) {
3366 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003367 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 }
3369
3370 /**
3371 * Add an additional content view to the activity. Added after any existing
3372 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 * @param view The desired content to display.
3375 * @param params Layout parameters for the view.
3376 */
3377 public void addContentView(View view, ViewGroup.LayoutParams params) {
3378 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003379 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 }
3381
3382 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003383 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3384 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3385 *
3386 * <p>This method will return non-null after content has been initialized (e.g. by using
3387 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3388 *
3389 * @return This window's content TransitionManager or null if none is set.
3390 */
3391 public TransitionManager getContentTransitionManager() {
3392 return getWindow().getTransitionManager();
3393 }
3394
3395 /**
3396 * Set the {@link TransitionManager} to use for default transitions in this window.
3397 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3398 *
3399 * @param tm The TransitionManager to use for scene changes.
3400 */
3401 public void setContentTransitionManager(TransitionManager tm) {
3402 getWindow().setTransitionManager(tm);
3403 }
3404
3405 /**
3406 * Retrieve the {@link Scene} representing this window's current content.
3407 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3408 *
3409 * <p>This method will return null if the current content is not represented by a Scene.</p>
3410 *
3411 * @return Current Scene being shown or null
3412 */
3413 public Scene getContentScene() {
3414 return getWindow().getContentScene();
3415 }
3416
3417 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003418 * Sets whether this activity is finished when touched outside its window's
3419 * bounds.
3420 */
3421 public void setFinishOnTouchOutside(boolean finish) {
3422 mWindow.setCloseOnTouchOutside(finish);
3423 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003424
3425 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003426 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003427 DEFAULT_KEYS_DISABLE,
3428 DEFAULT_KEYS_DIALER,
3429 DEFAULT_KEYS_SHORTCUT,
3430 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003431 DEFAULT_KEYS_SEARCH_GLOBAL
3432 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003433 @Retention(RetentionPolicy.SOURCE)
3434 @interface DefaultKeyMode {}
3435
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003436 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3438 * keys.
RoboErik55011652014-07-09 15:05:53 -07003439 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 * @see #setDefaultKeyMode
3441 */
3442 static public final int DEFAULT_KEYS_DISABLE = 0;
3443 /**
3444 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3445 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003446 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 * @see #setDefaultKeyMode
3448 */
3449 static public final int DEFAULT_KEYS_DIALER = 1;
3450 /**
3451 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3452 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 * @see #setDefaultKeyMode
3457 */
3458 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3459 /**
3460 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3461 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003462 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 * @see #setDefaultKeyMode
3467 */
3468 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3469
3470 /**
3471 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3472 * will start a global search (typically web search, but some platforms may define alternate
3473 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003474 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 * @see #setDefaultKeyMode
3478 */
3479 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3480
3481 /**
3482 * Select the default key handling for this activity. This controls what
3483 * will happen to key events that are not otherwise handled. The default
3484 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3485 * floor. Other modes allow you to launch the dialer
3486 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3487 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003488 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003490 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 * <p>Note that the mode selected here does not impact the default
3492 * handling of system keys, such as the "back" and "menu" keys, and your
3493 * activity and its views always get a first chance to receive and handle
3494 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003495 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003497 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 * @see #onKeyDown
3499 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003500 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 // Some modes use a SpannableStringBuilder to track & dispatch input events
3504 // This list must remain in sync with the switch in onKeyDown()
3505 switch (mode) {
3506 case DEFAULT_KEYS_DISABLE:
3507 case DEFAULT_KEYS_SHORTCUT:
3508 mDefaultKeySsb = null; // not used in these modes
3509 break;
3510 case DEFAULT_KEYS_DIALER:
3511 case DEFAULT_KEYS_SEARCH_LOCAL:
3512 case DEFAULT_KEYS_SEARCH_GLOBAL:
3513 mDefaultKeySsb = new SpannableStringBuilder();
3514 Selection.setSelection(mDefaultKeySsb,0);
3515 break;
3516 default:
3517 throw new IllegalArgumentException();
3518 }
3519 }
3520
3521 /**
3522 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003523 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 * is inside a TextView will not trigger the event (unless it is a navigation
3525 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003526 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 * <p>If the focused view didn't want this event, this method is called.
3528 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003529 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3530 * by calling {@link #onBackPressed()}, though the behavior varies based
3531 * on the application compatibility mode: for
3532 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3533 * it will set up the dispatch to call {@link #onKeyUp} where the action
3534 * will be performed; for earlier applications, it will perform the
3535 * action immediately in on-down, as those versions of the platform
3536 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003537 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003538 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003539 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003542 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 * this event and it should continue to be propagated.
3544 * @see #onKeyUp
3545 * @see android.view.KeyEvent
3546 */
3547 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003548 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003549 if (getApplicationInfo().targetSdkVersion
3550 >= Build.VERSION_CODES.ECLAIR) {
3551 event.startTracking();
3552 } else {
3553 onBackPressed();
3554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 return true;
3556 }
RoboErik55011652014-07-09 15:05:53 -07003557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3559 return false;
3560 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003561 Window w = getWindow();
3562 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3563 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3564 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003565 return true;
3566 }
3567 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003568 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3569 // Don't consume TAB here since it's used for navigation. Arrow keys
3570 // aren't considered "typing keys" so they already won't get consumed.
3571 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 } else {
3573 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3574 boolean clearSpannable = false;
3575 boolean handled;
3576 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3577 clearSpannable = true;
3578 handled = false;
3579 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003580 handled = TextKeyListener.getInstance().onKeyDown(
3581 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 if (handled && mDefaultKeySsb.length() > 0) {
3583 // something useable has been typed - dispatch it now.
3584
3585 final String str = mDefaultKeySsb.toString();
3586 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 switch (mDefaultKeyMode) {
3589 case DEFAULT_KEYS_DIALER:
3590 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3591 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003592 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 break;
3594 case DEFAULT_KEYS_SEARCH_LOCAL:
3595 startSearch(str, false, null, false);
3596 break;
3597 case DEFAULT_KEYS_SEARCH_GLOBAL:
3598 startSearch(str, false, null, true);
3599 break;
3600 }
3601 }
3602 }
3603 if (clearSpannable) {
3604 mDefaultKeySsb.clear();
3605 mDefaultKeySsb.clearSpans();
3606 Selection.setSelection(mDefaultKeySsb,0);
3607 }
3608 return handled;
3609 }
3610 }
3611
3612 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003613 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3614 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3615 * the event).
Siarhei Vishniakouccf973f2018-10-30 14:04:57 -07003616 *
3617 * To receive this callback, you must return true from onKeyDown for the current
3618 * event stream.
3619 *
3620 * @see KeyEvent.Callback#onKeyLongPress()
3621 * @see KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003622 */
3623 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3624 return false;
3625 }
3626
3627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003629 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 * is inside a TextView will not trigger the event (unless it is a navigation
3631 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003632 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003633 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3634 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003635 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003637 * further, or <code>false</code> to indicate that you have not handled
3638 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 * @see #onKeyDown
3640 * @see KeyEvent
3641 */
3642 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003643 if (getApplicationInfo().targetSdkVersion
3644 >= Build.VERSION_CODES.ECLAIR) {
3645 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3646 && !event.isCanceled()) {
3647 onBackPressed();
3648 return true;
3649 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 return false;
3652 }
3653
3654 /**
3655 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3656 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3657 * the event).
3658 */
3659 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3660 return false;
3661 }
RoboErik55011652014-07-09 15:05:53 -07003662
Mark Renouf1c25fc52019-09-09 13:13:43 -04003663 private static final class RequestFinishCallback extends IRequestFinishCallback.Stub {
3664 private final WeakReference<Activity> mActivityRef;
3665
3666 RequestFinishCallback(WeakReference<Activity> activityRef) {
3667 mActivityRef = activityRef;
3668 }
3669
3670 @Override
3671 public void requestFinish() {
3672 Activity activity = mActivityRef.get();
3673 if (activity != null) {
3674 activity.mHandler.post(activity::finishAfterTransition);
3675 }
3676 }
3677 }
3678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003680 * Called when the activity has detected the user's press of the back
3681 * key. The default implementation simply finishes the current activity,
3682 * but you can override this to do whatever you want.
3683 */
3684 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003685 if (mActionBar != null && mActionBar.collapseActionView()) {
3686 return;
3687 }
3688
George Mount27b0dc02017-02-21 10:24:09 -08003689 FragmentManager fragmentManager = mFragments.getFragmentManager();
3690
Mark Renouf446251d2019-04-26 10:22:41 -04003691 if (!fragmentManager.isStateSaved() && fragmentManager.popBackStackImmediate()) {
3692 return;
3693 }
3694 if (!isTaskRoot()) {
3695 // If the activity is not the root of the task, allow finish to proceed normally.
3696 finishAfterTransition();
3697 return;
3698 }
3699 try {
3700 // Inform activity task manager that the activity received a back press
3701 // while at the root of the task. This call allows ActivityTaskManager
3702 // to intercept or defer finishing.
3703 ActivityTaskManager.getService().onBackPressedOnTaskRoot(mToken,
Mark Renouf1c25fc52019-09-09 13:13:43 -04003704 new RequestFinishCallback(new WeakReference<>(this)));
Mark Renouf446251d2019-04-26 10:22:41 -04003705 } catch (RemoteException e) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003706 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003707 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003708 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003709
3710 /**
3711 * Called when a key shortcut event is not handled by any of the views in the Activity.
3712 * Override this method to implement global key shortcuts for the Activity.
3713 * Key shortcuts can also be implemented by setting the
3714 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3715 *
3716 * @param keyCode The value in event.getKeyCode().
3717 * @param event Description of the key event.
3718 * @return True if the key shortcut was handled.
3719 */
3720 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003721 // Let the Action Bar have a chance at handling the shortcut.
3722 ActionBar actionBar = getActionBar();
3723 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003724 }
3725
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 * Called when a touch screen event was not handled by any of the views
3728 * under it. This is most useful to process touch events that happen
3729 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 * @return Return true if you have consumed the event, false if you haven't.
3734 * The default implementation always returns false.
3735 */
3736 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003737 if (mWindow.shouldCloseOnTouch(this, event)) {
3738 finish();
3739 return true;
3740 }
RoboErik55011652014-07-09 15:05:53 -07003741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 return false;
3743 }
RoboErik55011652014-07-09 15:05:53 -07003744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 /**
3746 * Called when the trackball was moved and not handled by any of the
3747 * views inside of the activity. So, for example, if the trackball moves
3748 * while focus is on a button, you will receive a call here because
3749 * buttons do not normally do anything with trackball events. The call
3750 * here happens <em>before</em> trackball movements are converted to
3751 * DPAD key events, which then get sent back to the view hierarchy, and
3752 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003755 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 * @return Return true if you have consumed the event, false if you haven't.
3757 * The default implementation always returns false.
3758 */
3759 public boolean onTrackballEvent(MotionEvent event) {
3760 return false;
3761 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003762
3763 /**
3764 * Called when a generic motion event was not handled by any of the
3765 * views inside of the activity.
3766 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003767 * Generic motion events describe joystick movements, mouse hovers, track pad
3768 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003769 * {@link MotionEvent#getSource() source} of the motion event specifies
3770 * the class of input that was received. Implementations of this method
3771 * must examine the bits in the source before processing the event.
3772 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003773 * </p><p>
3774 * Generic motion events with source class
3775 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3776 * are delivered to the view under the pointer. All other generic motion events are
3777 * delivered to the focused view.
3778 * </p><p>
3779 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3780 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003781 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003782 *
3783 * @param event The generic motion event being processed.
3784 *
3785 * @return Return true if you have consumed the event, false if you haven't.
3786 * The default implementation always returns false.
3787 */
3788 public boolean onGenericMotionEvent(MotionEvent event) {
3789 return false;
3790 }
3791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 /**
3793 * Called whenever a key, touch, or trackball event is dispatched to the
3794 * activity. Implement this method if you wish to know that the user has
3795 * interacted with the device in some way while your activity is running.
3796 * This callback and {@link #onUserLeaveHint} are intended to help
3797 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003798 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3801 * be accompanied by calls to {@link #onUserInteraction}. This
3802 * ensures that your activity will be told of relevant user activity such
3803 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003804 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 * <p>Note that this callback will be invoked for the touch down action
3806 * that begins a touch gesture, but may not be invoked for the touch-moved
3807 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003808 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 * @see #onUserLeaveHint()
3810 */
3811 public void onUserInteraction() {
3812 }
RoboErik55011652014-07-09 15:05:53 -07003813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3815 // Update window manager if: we have a view, that view is
3816 // attached to its parent (which will be a RootView), and
3817 // this activity is not embedded.
3818 if (mParent == null) {
3819 View decor = mDecor;
3820 if (decor != null && decor.getParent() != null) {
3821 getWindowManager().updateViewLayout(decor, params);
Adam He43c06992019-04-15 15:41:49 -07003822 if (mContentCaptureManager != null) {
3823 mContentCaptureManager.updateWindowAttributes(params);
3824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 }
3826 }
3827 }
3828
3829 public void onContentChanged() {
3830 }
3831
3832 /**
3833 * Called when the current {@link Window} of the activity gains or loses
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003834 * focus. This is the best indicator of whether this activity is the entity
3835 * with which the user actively interacts. The default implementation
3836 * clears the key tracking state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003837 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003838 * <p>Note that this provides information about global focus state, which
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003839 * is managed independently of activity lifecycle. As such, while focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 * changes will generally have some relation to lifecycle changes (an
3841 * activity that is stopped will not generally get window focus), you
3842 * should not rely on any particular order between the callbacks here and
3843 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003844 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003845 * <p>As a general rule, however, a foreground activity will have window
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 * focus... unless it has displayed other dialogs or popups that take
3847 * input focus, in which case the activity itself will not have focus
3848 * when the other windows have it. Likewise, the system may display
3849 * system-level windows (such as the status bar notification panel or
3850 * a system alert) which will temporarily take window input focus without
3851 * pausing the foreground activity.
3852 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003853 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} there can be
3854 * multiple resumed activities at the same time in multi-window mode, so
3855 * resumed state does not guarantee window focus even if there are no
3856 * overlays above.
3857 *
3858 * <p>If the intent is to know when an activity is the topmost active, the
3859 * one the user interacted with last among all activities but not including
3860 * non-activity windows like dialogs and popups, then
3861 * {@link #onTopResumedActivityChanged(boolean)} should be used. On platform
3862 * versions prior to {@link android.os.Build.VERSION_CODES#Q},
3863 * {@link #onResume} is the best indicator.
3864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003866 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 * @see #hasWindowFocus()
3868 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003869 * @see View#onWindowFocusChanged(boolean)
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003870 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 */
3872 public void onWindowFocusChanged(boolean hasFocus) {
3873 }
RoboErik55011652014-07-09 15:05:53 -07003874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003876 * Called when the main window associated with the activity has been
3877 * attached to the window manager.
3878 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3879 * for more information.
3880 * @see View#onAttachedToWindow
3881 */
3882 public void onAttachedToWindow() {
3883 }
RoboErik55011652014-07-09 15:05:53 -07003884
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003885 /**
3886 * Called when the main window associated with the activity has been
3887 * detached from the window manager.
3888 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3889 * for more information.
3890 * @see View#onDetachedFromWindow
3891 */
3892 public void onDetachedFromWindow() {
3893 }
RoboErik55011652014-07-09 15:05:53 -07003894
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 * Returns true if this activity's <em>main</em> window currently has window focus.
3897 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3902 */
3903 public boolean hasWindowFocus() {
3904 Window w = getWindow();
3905 if (w != null) {
3906 View d = w.getDecorView();
3907 if (d != null) {
3908 return d.hasWindowFocus();
3909 }
3910 }
3911 return false;
3912 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003913
3914 /**
3915 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003916 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003917 */
Adam Powell117b6952014-05-05 18:14:56 -07003918 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003919 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003920 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003921 if (suppressWindowTransition) {
3922 overridePendingTransition(0, 0);
3923 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003924 }
RoboErik55011652014-07-09 15:05:53 -07003925
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003926
Skuhnece2faa52015-08-11 10:36:38 -07003927 /**
Yunfan Chend967af82019-01-17 18:30:18 +09003928 * Moves the activity between {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode
3929 * and {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003930 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003931 * @hide
3932 */
3933 @Override
Yunfan Chend967af82019-01-17 18:30:18 +09003934 public void toggleFreeformWindowingMode() throws RemoteException {
3935 ActivityTaskManager.getService().toggleFreeformWindowingMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003936 }
3937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003939 * Puts the activity in picture-in-picture mode if the activity supports.
3940 * @see android.R.attr#supportsPictureInPicture
3941 * @hide
3942 */
3943 @Override
3944 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003945 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003946 enterPictureInPictureMode();
3947 }
3948 }
3949
3950 /**
RoboErik55011652014-07-09 15:05:53 -07003951 * Called to process key events. You can override this to intercept all
3952 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003954 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003956 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 * @return boolean Return true if this event was consumed.
3958 */
3959 public boolean dispatchKeyEvent(KeyEvent event) {
3960 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003961
3962 // Let action bars open menus in response to the menu key prioritized over
3963 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003964 final int keyCode = event.getKeyCode();
3965 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003966 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3967 return true;
3968 }
3969
Dianne Hackborn8d374262009-09-14 21:21:52 -07003970 Window win = getWindow();
3971 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 return true;
3973 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003974 View decor = mDecor;
3975 if (decor == null) decor = win.getDecorView();
3976 return event.dispatch(this, decor != null
3977 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 }
3979
3980 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003981 * Called to process a key shortcut event.
3982 * You can override this to intercept all key shortcut events before they are
3983 * dispatched to the window. Be sure to call this implementation for key shortcut
3984 * events that should be handled normally.
3985 *
3986 * @param event The key shortcut event.
3987 * @return True if this event was consumed.
3988 */
3989 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3990 onUserInteraction();
3991 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3992 return true;
3993 }
3994 return onKeyShortcut(event.getKeyCode(), event);
3995 }
3996
3997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 * Called to process touch screen events. You can override this to
3999 * intercept all touch screen events before they are dispatched to the
4000 * window. Be sure to call this implementation for touch screen events
4001 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07004004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 * @return boolean Return true if this event was consumed.
4006 */
4007 public boolean dispatchTouchEvent(MotionEvent ev) {
4008 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
4009 onUserInteraction();
4010 }
4011 if (getWindow().superDispatchTouchEvent(ev)) {
4012 return true;
4013 }
4014 return onTouchEvent(ev);
4015 }
RoboErik55011652014-07-09 15:05:53 -07004016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 /**
4018 * Called to process trackball events. You can override this to
4019 * intercept all trackball events before they are dispatched to the
4020 * window. Be sure to call this implementation for trackball events
4021 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004022 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07004024 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 * @return boolean Return true if this event was consumed.
4026 */
4027 public boolean dispatchTrackballEvent(MotionEvent ev) {
4028 onUserInteraction();
4029 if (getWindow().superDispatchTrackballEvent(ev)) {
4030 return true;
4031 }
4032 return onTrackballEvent(ev);
4033 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004034
Jeff Browncb1404e2011-01-15 18:14:15 -08004035 /**
4036 * Called to process generic motion events. You can override this to
4037 * intercept all generic motion events before they are dispatched to the
4038 * window. Be sure to call this implementation for generic motion events
4039 * that should be handled normally.
4040 *
4041 * @param ev The generic motion event.
4042 *
4043 * @return boolean Return true if this event was consumed.
4044 */
4045 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
4046 onUserInteraction();
4047 if (getWindow().superDispatchGenericMotionEvent(ev)) {
4048 return true;
4049 }
4050 return onGenericMotionEvent(ev);
4051 }
4052
svetoslavganov75986cf2009-05-14 22:28:01 -07004053 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
4054 event.setClassName(getClass().getName());
4055 event.setPackageName(getPackageName());
4056
4057 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08004058 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
4059 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07004060 event.setFullScreen(isFullScreen);
4061
4062 CharSequence title = getTitle();
4063 if (!TextUtils.isEmpty(title)) {
4064 event.getText().add(title);
4065 }
4066
4067 return true;
4068 }
4069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 /**
4071 * Default implementation of
4072 * {@link android.view.Window.Callback#onCreatePanelView}
4073 * for activities. This
4074 * simply returns null so that all panel sub-windows will have the default
4075 * menu behavior.
4076 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004077 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 public View onCreatePanelView(int featureId) {
4079 return null;
4080 }
4081
4082 /**
4083 * Default implementation of
4084 * {@link android.view.Window.Callback#onCreatePanelMenu}
4085 * for activities. This calls through to the new
4086 * {@link #onCreateOptionsMenu} method for the
4087 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4088 * so that subclasses of Activity don't need to deal with feature codes.
4089 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004090 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004092 boolean show = onCreateOptionsMenu(menu);
4093 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
4094 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
4096 return false;
4097 }
4098
4099 /**
4100 * Default implementation of
4101 * {@link android.view.Window.Callback#onPreparePanel}
4102 * for activities. This
4103 * calls through to the new {@link #onPrepareOptionsMenu} method for the
4104 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4105 * panel, so that subclasses of
4106 * Activity don't need to deal with feature codes.
4107 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004108 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
4109 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004111 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07004112 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 }
4114 return true;
4115 }
4116
4117 /**
4118 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07004119 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 * @return The default implementation returns true.
4121 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004122 @Override
4123 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08004124 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07004125 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08004126 if (mActionBar != null) {
4127 mActionBar.dispatchMenuVisibilityChanged(true);
4128 } else {
4129 Log.e(TAG, "Tried to open action bar menu with no action bar");
4130 }
Adam Powell8515ee82010-11-30 14:09:55 -08004131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 return true;
4133 }
4134
4135 /**
4136 * Default implementation of
4137 * {@link android.view.Window.Callback#onMenuItemSelected}
4138 * for activities. This calls through to the new
4139 * {@link #onOptionsItemSelected} method for the
4140 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4141 * panel, so that subclasses of
4142 * Activity don't need to deal with feature codes.
4143 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004144 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09004145 CharSequence titleCondensed = item.getTitleCondensed();
4146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 switch (featureId) {
4148 case Window.FEATURE_OPTIONS_PANEL:
4149 // Put event logging here so it gets called even if subclass
4150 // doesn't call through to superclass's implmeentation of each
4151 // of these methods below
zobject686a8052012-12-14 21:11:08 +09004152 if(titleCondensed != null) {
4153 EventLog.writeEvent(50000, 0, titleCondensed.toString());
4154 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004155 if (onOptionsItemSelected(item)) {
4156 return true;
4157 }
Adam Powell04d58112012-04-09 10:22:12 -07004158 if (mFragments.dispatchOptionsItemSelected(item)) {
4159 return true;
4160 }
4161 if (item.getItemId() == android.R.id.home && mActionBar != null &&
4162 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
4163 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07004164 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07004165 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07004166 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07004167 }
Adam Powell04d58112012-04-09 10:22:12 -07004168 }
4169 return false;
RoboErik55011652014-07-09 15:05:53 -07004170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09004172 if(titleCondensed != null) {
4173 EventLog.writeEvent(50000, 1, titleCondensed.toString());
4174 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07004175 if (onContextItemSelected(item)) {
4176 return true;
4177 }
4178 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08004179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 default:
4181 return false;
4182 }
4183 }
RoboErik55011652014-07-09 15:05:53 -07004184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 /**
4186 * Default implementation of
4187 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
4188 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
4189 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4190 * so that subclasses of Activity don't need to deal with feature codes.
4191 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
4192 * {@link #onContextMenuClosed(Menu)} will be called.
4193 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004194 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 switch (featureId) {
4196 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004197 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 onOptionsMenuClosed(menu);
4199 break;
RoboErik55011652014-07-09 15:05:53 -07004200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 case Window.FEATURE_CONTEXT_MENU:
4202 onContextMenuClosed(menu);
4203 break;
Adam Powell8515ee82010-11-30 14:09:55 -08004204
4205 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07004206 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08004207 mActionBar.dispatchMenuVisibilityChanged(false);
4208 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 }
4210 }
4211
4212 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004213 * Declare that the options menu has changed, so should be recreated.
4214 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4215 * time it needs to be displayed.
4216 */
4217 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004218 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4219 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004220 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4221 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004222 }
RoboErik55011652014-07-09 15:05:53 -07004223
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 * Initialize the contents of the Activity's standard options menu. You
4226 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004227 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 * <p>This is only called once, the first time the options menu is
4229 * displayed. To update the menu every time it is displayed, see
4230 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004233 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4234 * they will be correctly ordered with application-defined menu items.
4235 * Deriving classes should always call through to the base implementation.
4236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 * <p>You can safely hold on to <var>menu</var> (and any items created
4238 * from it), making modifications to it as desired, until the next
4239 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 * <p>When you add items to the menu, you can implement the Activity's
4242 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 * @return You must return true for the menu to be displayed;
4247 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 * @see #onPrepareOptionsMenu
4250 * @see #onOptionsItemSelected
4251 */
4252 public boolean onCreateOptionsMenu(Menu menu) {
4253 if (mParent != null) {
4254 return mParent.onCreateOptionsMenu(menu);
4255 }
4256 return true;
4257 }
4258
4259 /**
4260 * Prepare the Screen's standard options menu to be displayed. This is
4261 * called right before the menu is shown, every time it is shown. You can
4262 * use this method to efficiently enable/disable items or otherwise
4263 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 * <p>The default implementation updates the system menu items based on the
4266 * activity's state. Deriving classes should always call through to the
4267 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 * @param menu The options menu as last shown or first initialized by
4270 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004271 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 * @return You must return true for the menu to be displayed;
4273 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 * @see #onCreateOptionsMenu
4276 */
4277 public boolean onPrepareOptionsMenu(Menu menu) {
4278 if (mParent != null) {
4279 return mParent.onPrepareOptionsMenu(menu);
4280 }
4281 return true;
4282 }
4283
4284 /**
4285 * This hook is called whenever an item in your options menu is selected.
4286 * The default implementation simply returns false to have the normal
4287 * processing happen (calling the item's Runnable or sending a message to
4288 * its Handler as appropriate). You can use this method for any items
4289 * for which you would like to do processing without those other
4290 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004291 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004293 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004294 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 * @return boolean Return false to allow normal menu processing to
4298 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 * @see #onCreateOptionsMenu
4301 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004302 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 if (mParent != null) {
4304 return mParent.onOptionsItemSelected(item);
4305 }
4306 return false;
4307 }
4308
4309 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004310 * This method is called whenever the user chooses to navigate Up within your application's
4311 * activity hierarchy from the action bar.
4312 *
4313 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4314 * was specified in the manifest for this activity or an activity-alias to it,
4315 * default Up navigation will be handled automatically. If any activity
4316 * along the parent chain requires extra Intent arguments, the Activity subclass
4317 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4318 * to supply those arguments.</p>
4319 *
Mark Lufa434852016-08-11 17:40:33 -07004320 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004321 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4322 * from the design guide for more information about navigating within your app.</p>
4323 *
4324 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4325 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4326 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4327 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4328 *
4329 * @return true if Up navigation completed successfully and this Activity was finished,
4330 * false otherwise.
4331 */
4332 public boolean onNavigateUp() {
4333 // Automatically handle hierarchical Up navigation if the proper
4334 // metadata is available.
4335 Intent upIntent = getParentActivityIntent();
4336 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004337 if (mActivityInfo.taskAffinity == null) {
4338 // Activities with a null affinity are special; they really shouldn't
4339 // specify a parent activity intent in the first place. Just finish
4340 // the current activity and call it a day.
4341 finish();
4342 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004343 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004344 onCreateNavigateUpTaskStack(b);
4345 onPrepareNavigateUpTaskStack(b);
4346 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004347
4348 // We can't finishAffinity if we have a result.
4349 // Fall back and simply finish the current activity instead.
4350 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4351 // Tell the developer what's going on to avoid hair-pulling.
4352 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4353 finish();
4354 } else {
4355 finishAffinity();
4356 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004357 } else {
4358 navigateUpTo(upIntent);
4359 }
4360 return true;
4361 }
4362 return false;
4363 }
4364
4365 /**
4366 * This is called when a child activity of this one attempts to navigate up.
4367 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4368 *
4369 * @param child The activity making the call.
4370 */
4371 public boolean onNavigateUpFromChild(Activity child) {
4372 return onNavigateUp();
4373 }
4374
4375 /**
4376 * Define the synthetic task stack that will be generated during Up navigation from
4377 * a different task.
4378 *
4379 * <p>The default implementation of this method adds the parent chain of this activity
4380 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4381 * may choose to override this method to construct the desired task stack in a different
4382 * way.</p>
4383 *
Adam Powellf0195952012-05-02 21:38:54 -07004384 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4385 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4386 * returned by {@link #getParentActivityIntent()}.</p>
4387 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004388 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4389 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4390 *
4391 * @param builder An empty TaskStackBuilder - the application should add intents representing
4392 * the desired task stack
4393 */
4394 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4395 builder.addParentStack(this);
4396 }
4397
4398 /**
4399 * Prepare the synthetic task stack that will be generated during Up navigation
4400 * from a different task.
4401 *
4402 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4403 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4404 * If any extra data should be added to these intents before launching the new task,
4405 * the application should override this method and add that data here.</p>
4406 *
4407 * @param builder A TaskStackBuilder that has been populated with Intents by
4408 * onCreateNavigateUpTaskStack.
4409 */
4410 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4411 }
4412
4413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 * This hook is called whenever the options menu is being closed (either by the user canceling
4415 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004416 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 * @param menu The options menu as last shown or first initialized by
4418 * onCreateOptionsMenu().
4419 */
4420 public void onOptionsMenuClosed(Menu menu) {
4421 if (mParent != null) {
4422 mParent.onOptionsMenuClosed(menu);
4423 }
4424 }
RoboErik55011652014-07-09 15:05:53 -07004425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 /**
4427 * Programmatically opens the options menu. If the options menu is already
4428 * open, this method does nothing.
4429 */
4430 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004431 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4432 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004433 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 }
RoboErik55011652014-07-09 15:05:53 -07004436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 /**
4438 * Progammatically closes the options menu. If the options menu is already
4439 * closed, this method does nothing.
4440 */
4441 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004442 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4443 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004444 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 }
4447
4448 /**
4449 * Called when a context menu for the {@code view} is about to be shown.
4450 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4451 * time the context menu is about to be shown and should be populated for
4452 * the view (or item inside the view for {@link AdapterView} subclasses,
4453 * this can be found in the {@code menuInfo})).
4454 * <p>
4455 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4456 * item has been selected.
4457 * <p>
4458 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004459 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 */
4461 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4462 }
4463
4464 /**
4465 * Registers a context menu to be shown for the given view (multiple views
4466 * can show the context menu). This method will set the
4467 * {@link OnCreateContextMenuListener} on the view to this activity, so
4468 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4469 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004470 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 * @see #unregisterForContextMenu(View)
4472 * @param view The view that should show a context menu.
4473 */
4474 public void registerForContextMenu(View view) {
4475 view.setOnCreateContextMenuListener(this);
4476 }
RoboErik55011652014-07-09 15:05:53 -07004477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 /**
4479 * Prevents a context menu to be shown for the given view. This method will remove the
4480 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 * @see #registerForContextMenu(View)
4483 * @param view The view that should stop showing a context menu.
4484 */
4485 public void unregisterForContextMenu(View view) {
4486 view.setOnCreateContextMenuListener(null);
4487 }
RoboErik55011652014-07-09 15:05:53 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 /**
4490 * Programmatically opens the context menu for a particular {@code view}.
4491 * The {@code view} should have been added via
4492 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004493 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 * @param view The view to show the context menu for.
4495 */
4496 public void openContextMenu(View view) {
4497 view.showContextMenu();
4498 }
RoboErik55011652014-07-09 15:05:53 -07004499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 /**
4501 * Programmatically closes the most recently opened context menu, if showing.
4502 */
4503 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004504 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4505 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 }
RoboErik55011652014-07-09 15:05:53 -07004508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 /**
4510 * This hook is called whenever an item in a context menu is selected. The
4511 * default implementation simply returns false to have the normal processing
4512 * happen (calling the item's Runnable or sending a message to its Handler
4513 * as appropriate). You can use this method for any items for which you
4514 * would like to do processing without those other facilities.
4515 * <p>
4516 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4517 * View that added this menu item.
4518 * <p>
4519 * Derived classes should call through to the base class for it to perform
4520 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 * @param item The context menu item that was selected.
4523 * @return boolean Return false to allow normal context menu processing to
4524 * proceed, true to consume it here.
4525 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004526 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 if (mParent != null) {
4528 return mParent.onContextItemSelected(item);
4529 }
4530 return false;
4531 }
4532
4533 /**
4534 * This hook is called whenever the context menu is being closed (either by
4535 * the user canceling the menu with the back/menu button, or when an item is
4536 * selected).
RoboErik55011652014-07-09 15:05:53 -07004537 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 * @param menu The context menu that is being closed.
4539 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004540 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 if (mParent != null) {
4542 mParent.onContextMenuClosed(menu);
4543 }
4544 }
4545
4546 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004547 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004549 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 protected Dialog onCreateDialog(int id) {
4551 return null;
4552 }
4553
4554 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004555 * Callback for creating dialogs that are managed (saved and restored) for you
4556 * by the activity. The default implementation calls through to
4557 * {@link #onCreateDialog(int)} for compatibility.
4558 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004559 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4560 * or later, consider instead using a {@link DialogFragment} instead.</em>
4561 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004562 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4563 * this method the first time, and hang onto it thereafter. Any dialog
4564 * that is created by this method will automatically be saved and restored
4565 * for you, including whether it is showing.
4566 *
4567 * <p>If you would like the activity to manage saving and restoring dialogs
4568 * for you, you should override this method and handle any ids that are
4569 * passed to {@link #showDialog}.
4570 *
4571 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4572 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4573 *
4574 * @param id The id of the dialog.
4575 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4576 * @return The dialog. If you return null, the dialog will not be created.
4577 *
4578 * @see #onPrepareDialog(int, Dialog, Bundle)
4579 * @see #showDialog(int, Bundle)
4580 * @see #dismissDialog(int)
4581 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004582 *
4583 * @deprecated Use the new {@link DialogFragment} class with
4584 * {@link FragmentManager} instead; this is also
4585 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004586 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004587 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004588 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004589 protected Dialog onCreateDialog(int id, Bundle args) {
4590 return onCreateDialog(id);
4591 }
4592
4593 /**
4594 * @deprecated Old no-arguments version of
4595 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4596 */
4597 @Deprecated
4598 protected void onPrepareDialog(int id, Dialog dialog) {
4599 dialog.setOwnerActivity(this);
4600 }
4601
4602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004604 * shown. The default implementation calls through to
4605 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004606 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 * <p>
4608 * Override this if you need to update a managed dialog based on the state
4609 * of the application each time it is shown. For example, a time picker
4610 * dialog might want to be updated with the current time. You should call
4611 * through to the superclass's implementation. The default implementation
4612 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 * @param id The id of the managed dialog.
4615 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004616 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4617 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 * @see #showDialog(int)
4619 * @see #dismissDialog(int)
4620 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004621 *
4622 * @deprecated Use the new {@link DialogFragment} class with
4623 * {@link FragmentManager} instead; this is also
4624 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004626 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004627 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4628 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 }
4630
4631 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004632 * Simple version of {@link #showDialog(int, Bundle)} that does not
4633 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4634 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004635 *
4636 * @deprecated Use the new {@link DialogFragment} class with
4637 * {@link FragmentManager} instead; this is also
4638 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004639 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004640 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004641 public final void showDialog(int id) {
4642 showDialog(id, null);
4643 }
4644
4645 /**
4646 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 * will be made with the same id the first time this is called for a given
4648 * id. From thereafter, the dialog will be automatically saved and restored.
4649 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004650 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4651 * or later, consider instead using a {@link DialogFragment} instead.</em>
4652 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004653 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 * be made to provide an opportunity to do any timely preparation.
4655 *
4656 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004657 * @param args Arguments to pass through to the dialog. These will be saved
4658 * and restored for you. Note that if the dialog is already created,
4659 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4660 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004661 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004662 * @return Returns true if the Dialog was created; false is returned if
4663 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004664 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004665 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004666 * @see #onCreateDialog(int, Bundle)
4667 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 * @see #dismissDialog(int)
4669 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004670 *
4671 * @deprecated Use the new {@link DialogFragment} class with
4672 * {@link FragmentManager} instead; this is also
4673 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004675 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004676 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004678 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004679 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004680 ManagedDialog md = mManagedDialogs.get(id);
4681 if (md == null) {
4682 md = new ManagedDialog();
4683 md.mDialog = createDialog(id, null, args);
4684 if (md.mDialog == null) {
4685 return false;
4686 }
4687 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 }
RoboErik55011652014-07-09 15:05:53 -07004689
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004690 md.mArgs = args;
4691 onPrepareDialog(id, md.mDialog, args);
4692 md.mDialog.show();
4693 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 }
4695
4696 /**
4697 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4698 *
4699 * @param id The id of the managed dialog.
4700 *
4701 * @throws IllegalArgumentException if the id was not previously shown via
4702 * {@link #showDialog(int)}.
4703 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004704 * @see #onCreateDialog(int, Bundle)
4705 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 * @see #showDialog(int)
4707 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004708 *
4709 * @deprecated Use the new {@link DialogFragment} class with
4710 * {@link FragmentManager} instead; this is also
4711 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004713 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 public final void dismissDialog(int id) {
4715 if (mManagedDialogs == null) {
4716 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 }
RoboErik55011652014-07-09 15:05:53 -07004718
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004719 final ManagedDialog md = mManagedDialogs.get(id);
4720 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 throw missingDialog(id);
4722 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004723 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
4725
4726 /**
4727 * Creates an exception to throw if a user passed in a dialog id that is
4728 * unexpected.
4729 */
4730 private IllegalArgumentException missingDialog(int id) {
4731 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4732 + "shown via Activity#showDialog");
4733 }
4734
4735 /**
4736 * Removes any internal references to a dialog managed by this Activity.
4737 * If the dialog is showing, it will dismiss it as part of the clean up.
4738 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004739 * <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 -08004740 * want to avoid the overhead of saving and restoring it in the future.
4741 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004742 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4743 * will not throw an exception if you try to remove an ID that does not
4744 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004745 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 * @param id The id of the managed dialog.
4747 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004748 * @see #onCreateDialog(int, Bundle)
4749 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 * @see #showDialog(int)
4751 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004752 *
4753 * @deprecated Use the new {@link DialogFragment} class with
4754 * {@link FragmentManager} instead; this is also
4755 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004756 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004757 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004759 if (mManagedDialogs != null) {
4760 final ManagedDialog md = mManagedDialogs.get(id);
4761 if (md != null) {
4762 md.mDialog.dismiss();
4763 mManagedDialogs.remove(id);
4764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 }
4767
4768 /**
4769 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004770 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004771 * <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 -07004772 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004773 * calling this function is the same as calling
4774 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4775 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004776 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 * <p>You can override this function to force global search, e.g. in response to a dedicated
4778 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004779 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004780 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4781 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4782 * return false and you must supply your own custom implementation if you want to support
4783 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004784 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004785 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004786 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4787 * not respond to search. The default implementation always returns {@code true}, except
4788 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 * @see android.app.SearchManager
4791 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004792 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4793 mSearchEvent = searchEvent;
4794 boolean result = onSearchRequested();
4795 mSearchEvent = null;
4796 return result;
4797 }
4798
4799 /**
4800 * @see #onSearchRequested(SearchEvent)
4801 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004803 final int uiMode = getResources().getConfiguration().uiMode
4804 & Configuration.UI_MODE_TYPE_MASK;
4805 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4806 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004807 startSearch(null, false, null, false);
4808 return true;
4809 } else {
4810 return false;
4811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
RoboErik55011652014-07-09 15:05:53 -07004813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004815 * During the onSearchRequested() callbacks, this function will return the
4816 * {@link SearchEvent} that triggered the callback, if it exists.
4817 *
4818 * @return SearchEvent The SearchEvent that triggered the {@link
4819 * #onSearchRequested} callback.
4820 */
4821 public final SearchEvent getSearchEvent() {
4822 return mSearchEvent;
4823 }
4824
4825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004827 *
4828 * <p>It is typically called from onSearchRequested(), either directly from
4829 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004831 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 * is to inject specific data such as context data, it is preferred to <i>override</i>
4833 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004834 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004835 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4836 * not supported.
4837 *
RoboErik55011652014-07-09 15:05:53 -07004838 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004840 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 * any further typing will replace it. This is useful for cases where an entire pre-formed
4842 * query is being inserted. If false, the selection point will be placed at the end of the
4843 * inserted query. This is useful when the inserted query is text that the user entered,
4844 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4845 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004846 * @param appSearchData An application can insert application-specific
4847 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 * searches. This data will be returned with SEARCH intent(s). Null if
4849 * no extra data is required.
4850 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004851 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004852 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 * @see android.app.SearchManager
4856 * @see #onSearchRequested
4857 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004858 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4859 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004860 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004861 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004862 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 }
4864
4865 /**
krosaend2d60142009-08-17 08:56:48 -07004866 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4867 * the search dialog. Made available for testing purposes.
4868 *
4869 * @param query The query to trigger. If empty, the request will be ignored.
4870 * @param appSearchData An application can insert application-specific
4871 * context here, in order to improve quality or specificity of its own
4872 * searches. This data will be returned with SEARCH intent(s). Null if
4873 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004874 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004875 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004876 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004877 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004878 }
4879
4880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 * Request that key events come to this activity. Use this if your
4882 * activity has no views with focus, but the activity still wants
4883 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004884 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 * @see android.view.Window#takeKeyEvents
4886 */
4887 public void takeKeyEvents(boolean get) {
4888 getWindow().takeKeyEvents(get);
4889 }
4890
4891 /**
4892 * Enable extended window features. This is a convenience for calling
4893 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004894 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 * @param featureId The desired feature as defined in
4896 * {@link android.view.Window}.
4897 * @return Returns true if the requested feature is supported and now
4898 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004900 * @see android.view.Window#requestFeature
4901 */
4902 public final boolean requestWindowFeature(int featureId) {
4903 return getWindow().requestFeature(featureId);
4904 }
4905
4906 /**
4907 * Convenience for calling
4908 * {@link android.view.Window#setFeatureDrawableResource}.
4909 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004910 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 getWindow().setFeatureDrawableResource(featureId, resId);
4912 }
4913
4914 /**
4915 * Convenience for calling
4916 * {@link android.view.Window#setFeatureDrawableUri}.
4917 */
4918 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4919 getWindow().setFeatureDrawableUri(featureId, uri);
4920 }
4921
4922 /**
4923 * Convenience for calling
4924 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4925 */
4926 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4927 getWindow().setFeatureDrawable(featureId, drawable);
4928 }
4929
4930 /**
4931 * Convenience for calling
4932 * {@link android.view.Window#setFeatureDrawableAlpha}.
4933 */
4934 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4935 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4936 }
4937
4938 /**
4939 * Convenience for calling
4940 * {@link android.view.Window#getLayoutInflater}.
4941 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004942 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 public LayoutInflater getLayoutInflater() {
4944 return getWindow().getLayoutInflater();
4945 }
4946
4947 /**
4948 * Returns a {@link MenuInflater} with this context.
4949 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004950 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004952 // Make sure that action views can get an appropriate theme.
4953 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004954 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004955 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004956 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004957 } else {
4958 mMenuInflater = new MenuInflater(this);
4959 }
4960 }
4961 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 }
4963
4964 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004965 public void setTheme(int resid) {
4966 super.setTheme(resid);
4967 mWindow.setTheme(resid);
4968 }
4969
4970 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004971 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004972 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 if (mParent == null) {
4974 super.onApplyThemeResource(theme, resid, first);
4975 } else {
4976 try {
4977 theme.setTo(mParent.getTheme());
4978 } catch (Exception e) {
4979 // Empty
4980 }
4981 theme.applyStyle(resid, false);
4982 }
Winson073a5262016-02-29 16:04:40 -08004983
4984 // Get the primary color and update the TaskDescription for this activity
4985 TypedArray a = theme.obtainStyledAttributes(
4986 com.android.internal.R.styleable.ActivityTaskDescription);
4987 if (mTaskDescription.getPrimaryColor() == 0) {
4988 int colorPrimary = a.getColor(
4989 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4990 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4991 mTaskDescription.setPrimaryColor(colorPrimary);
4992 }
4993 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004994
4995 int colorBackground = a.getColor(
4996 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4997 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4998 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004999 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02005000
5001 final int statusBarColor = a.getColor(
5002 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
5003 if (statusBarColor != 0) {
5004 mTaskDescription.setStatusBarColor(statusBarColor);
5005 }
5006
5007 final int navigationBarColor = a.getColor(
5008 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
5009 if (navigationBarColor != 0) {
5010 mTaskDescription.setNavigationBarColor(navigationBarColor);
5011 }
5012
Adrian Roos4c864592019-04-10 14:47:57 +02005013 final int targetSdk = getApplicationInfo().targetSdkVersion;
5014 final boolean targetPreQ = targetSdk < Build.VERSION_CODES.Q;
5015 if (!targetPreQ) {
5016 mTaskDescription.setEnsureStatusBarContrastWhenTransparent(a.getBoolean(
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005017 R.styleable.ActivityTaskDescription_enforceStatusBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005018 false));
5019 mTaskDescription.setEnsureNavigationBarContrastWhenTransparent(a.getBoolean(
Adrian Roos6eb1ed32019-04-08 16:23:43 +02005020 R.styleable
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005021 .ActivityTaskDescription_enforceNavigationBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005022 true));
5023 }
5024
Winson073a5262016-02-29 16:04:40 -08005025 a.recycle();
5026 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00005027 }
5028
5029 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08005030 * Requests permissions to be granted to this application. These permissions
5031 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005032 * and they should have protection level {@link
5033 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
5034 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08005035 * <p>
5036 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
5037 * are granted at install time if requested in the manifest. Signature permissions
5038 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
5039 * install time if requested in the manifest and the signature of your app matches
5040 * the signature of the app declaring the permissions.
5041 * </p>
5042 * <p>
5043 * If your app does not have the requested permissions the user will be presented
5044 * with UI for accepting them. After the user has accepted or rejected the
5045 * requested permissions you will receive a callback on {@link
5046 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
5047 * permissions were granted or not.
5048 * </p>
5049 * <p>
5050 * Note that requesting a permission does not guarantee it will be granted and
5051 * your app should be able to run without having this permission.
5052 * </p>
5053 * <p>
5054 * This method may start an activity allowing the user to choose which permissions
5055 * to grant and which to reject. Hence, you should be prepared that your activity
5056 * may be paused and resumed. Further, granting some permissions may require
5057 * a restart of you application. In such a case, the system will recreate the
5058 * activity stack before delivering the result to {@link
5059 * #onRequestPermissionsResult(int, String[], int[])}.
5060 * </p>
5061 * <p>
5062 * When checking whether you have a permission you should use {@link
5063 * #checkSelfPermission(String)}.
5064 * </p>
5065 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07005066 * Calling this API for permissions already granted to your app would show UI
5067 * to the user to decide whether the app can still hold these permissions. This
5068 * can be useful if the way your app uses data guarded by the permissions
5069 * changes significantly.
5070 * </p>
5071 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07005072 * You cannot request a permission if your activity sets {@link
5073 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5074 * <code>true</code> because in this case the activity would not receive
5075 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
5076 * </p>
5077 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005078 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07005079 * RuntimePermissions</a> sample app demonstrates how to use this method to
5080 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08005081 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005082 *
Svet Ganovf66381c2016-02-18 20:02:36 -08005083 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08005084 * @param requestCode Application specific request code to match with a result
5085 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07005086 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08005087 *
Paul Miller77a0e132016-02-11 13:46:49 -08005088 * @throws IllegalArgumentException if requestCode is negative.
5089 *
Svetoslavc6d1c342015-02-26 14:44:43 -08005090 * @see #onRequestPermissionsResult(int, String[], int[])
5091 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07005092 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08005093 */
5094 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08005095 if (requestCode < 0) {
5096 throw new IllegalArgumentException("requestCode should be >= 0");
5097 }
Svetoslavffb32b12015-10-15 16:54:00 -07005098 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00005099 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07005100 // Dispatch the callback with empty arrays which means a cancellation.
5101 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
5102 return;
5103 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005104 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07005105 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07005106 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08005107 }
5108
5109 /**
5110 * Callback for the result from requesting permissions. This method
5111 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07005112 * <p>
5113 * <strong>Note:</strong> It is possible that the permissions request interaction
5114 * with the user is interrupted. In this case you will receive empty permissions
5115 * and results arrays which should be treated as a cancellation.
5116 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005117 *
5118 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
5119 * @param permissions The requested permissions. Never null.
5120 * @param grantResults The grant results for the corresponding permissions
5121 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
5122 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
5123 *
5124 * @see #requestPermissions(String[], int)
5125 */
5126 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
5127 @NonNull int[] grantResults) {
5128 /* callback - no nothing */
5129 }
5130
5131 /**
Svetoslav20770dd2015-05-29 15:43:04 -07005132 * Gets whether you should show UI with rationale for requesting a permission.
5133 * You should do this only if you do not have the permission and the context in
5134 * which the permission is requested does not clearly communicate to the user
5135 * what would be the benefit from granting this permission.
5136 * <p>
5137 * For example, if you write a camera app, requesting the camera permission
5138 * would be expected by the user and no rationale for why it is requested is
5139 * needed. If however, the app needs location for tagging photos then a non-tech
5140 * savvy user may wonder how location is related to taking photos. In this case
5141 * you may choose to show UI with rationale of requesting this permission.
5142 * </p>
5143 *
5144 * @param permission A permission your app wants to request.
5145 * @return Whether you can show permission rationale UI.
5146 *
5147 * @see #checkSelfPermission(String)
5148 * @see #requestPermissions(String[], int)
5149 * @see #onRequestPermissionsResult(int, String[], int[])
5150 */
Svetoslav9ce9a152015-06-02 17:59:31 -07005151 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07005152 return getPackageManager().shouldShowRequestPermissionRationale(permission);
5153 }
5154
5155 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005156 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
5157 * with no options.
5158 *
5159 * @param intent The intent to start.
5160 * @param requestCode If >= 0, this code will be returned in
5161 * onActivityResult() when the activity exits.
5162 *
5163 * @throws android.content.ActivityNotFoundException
5164 *
George Mount0a778ed2013-12-13 13:35:36 -08005165 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07005166 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005167 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005168 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005169 }
5170
5171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 * Launch an activity for which you would like a result when it finished.
5173 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08005174 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07005175 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07005177 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 * <p>Note that this method should only be used with Intent protocols
5179 * that are defined to return a result. In other protocols (such as
5180 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
5181 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05005182 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
5183 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005184 *
RoboErik55011652014-07-09 15:05:53 -07005185 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08005187 * activity, then your window will not be displayed until a result is
5188 * returned back from the started activity. This is to avoid visible
5189 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 * <p>This method throws {@link android.content.ActivityNotFoundException}
5192 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 * @param intent The intent to start.
5195 * @param requestCode If >= 0, this code will be returned in
5196 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005197 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005198 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005199 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005202 *
George Mount0a778ed2013-12-13 13:35:36 -08005203 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005205 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
5206 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07005208 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 Instrumentation.ActivityResult ar =
5210 mInstrumentation.execStartActivity(
5211 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005212 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 if (ar != null) {
5214 mMainThread.sendActivityResult(
5215 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
5216 ar.getResultData());
5217 }
5218 if (requestCode >= 0) {
5219 // If this start is requesting a result, we can avoid making
5220 // the activity visible until the result is received. Setting
5221 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5222 // activity hidden during this time, to avoid flickering.
5223 // This can only be done when a result is requested because
5224 // that guarantees we will get information back when the
5225 // activity is finished, no matter what happens to it.
5226 mStartedActivity = true;
5227 }
Adam Powell14874662013-07-18 19:42:41 -07005228
George Mount41725de2015-04-09 08:23:05 -07005229 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005230 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005232 if (options != null) {
5233 mParent.startActivityFromChild(this, intent, requestCode, options);
5234 } else {
5235 // Note we want to go through this method for compatibility with
5236 // existing applications that may have overridden it.
5237 mParent.startActivityFromChild(this, intent, requestCode);
5238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 }
George Mount41725de2015-04-09 08:23:05 -07005240 }
5241
5242 /**
5243 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5244 *
5245 * @param options The ActivityOptions bundle used to start an Activity.
5246 */
5247 private void cancelInputsAndStartExitTransition(Bundle options) {
5248 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5249 if (decor != null) {
5250 decor.cancelPendingInputEvents();
5251 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005252 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005253 mActivityTransitionState.startExitOutTransition(this, options);
5254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 }
5256
Chet Haaseb64e777f2016-12-09 07:17:10 -08005257 /**
5258 * Returns whether there are any activity transitions currently running on this
5259 * activity. A return value of {@code true} can mean that either an enter or
5260 * exit transition is running, including whether the background of the activity
5261 * is animating as a part of that transition.
5262 *
5263 * @return true if a transition is currently running on this activity, false otherwise.
5264 */
5265 public boolean isActivityTransitionRunning() {
5266 return mActivityTransitionState.isTransitionRunning();
5267 }
5268
George Mount413739e2016-06-08 07:13:37 -07005269 private Bundle transferSpringboardActivityOptions(Bundle options) {
5270 if (options == null && (mWindow != null && !mWindow.isActive())) {
5271 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005272 if (activityOptions != null &&
5273 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005274 return activityOptions.toBundle();
5275 }
5276 }
5277 return options;
5278 }
5279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005281 * @hide Implement to provide correct calling token.
5282 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005283 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005284 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5285 startActivityForResultAsUser(intent, requestCode, null, user);
5286 }
5287
5288 /**
5289 * @hide Implement to provide correct calling token.
5290 */
5291 public void startActivityForResultAsUser(Intent intent, int requestCode,
5292 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005293 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5294 }
5295
5296 /**
5297 * @hide Implement to provide correct calling token.
5298 */
5299 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5300 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005301 if (mParent != null) {
5302 throw new RuntimeException("Can't be called from a child");
5303 }
George Mount413739e2016-06-08 07:13:37 -07005304 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005305 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005306 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005307 options, user);
5308 if (ar != null) {
5309 mMainThread.sendActivityResult(
5310 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5311 }
5312 if (requestCode >= 0) {
5313 // If this start is requesting a result, we can avoid making
5314 // the activity visible until the result is received. Setting
5315 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5316 // activity hidden during this time, to avoid flickering.
5317 // This can only be done when a result is requested because
5318 // that guarantees we will get information back when the
5319 // activity is finished, no matter what happens to it.
5320 mStartedActivity = true;
5321 }
5322
George Mount41725de2015-04-09 08:23:05 -07005323 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005324 }
5325
5326 /**
5327 * @hide Implement to provide correct calling token.
5328 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005329 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005330 public void startActivityAsUser(Intent intent, UserHandle user) {
5331 startActivityAsUser(intent, null, user);
5332 }
5333
5334 /**
5335 * @hide Implement to provide correct calling token.
5336 */
5337 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5338 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005339 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005340 }
George Mount413739e2016-06-08 07:13:37 -07005341 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005342 Instrumentation.ActivityResult ar =
5343 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005344 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005345 intent, -1, options, user);
5346 if (ar != null) {
5347 mMainThread.sendActivityResult(
5348 mToken, mEmbeddedID, -1, ar.getResultCode(),
5349 ar.getResultData());
5350 }
George Mount41725de2015-04-09 08:23:05 -07005351 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005352 }
5353
5354 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005355 * Start a new activity as if it was started by the activity that started our
5356 * current activity. This is for the resolver and chooser activities, which operate
5357 * as intermediaries that dispatch their intent to the target the user selects -- to
5358 * do this, they must perform all security checks including permission grants as if
5359 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005360 * @param intent The Intent to start.
5361 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005362 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005363 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5364 * caller it is doing the start is, is actually allowed to start the target activity.
5365 * If you set this to true, you must set an explicit component in the Intent and do any
5366 * appropriate security checks yourself.
5367 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005368 * @hide
5369 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005370 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005371 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005372 if (mParent != null) {
5373 throw new RuntimeException("Can't be called from a child");
5374 }
George Mount413739e2016-06-08 07:13:37 -07005375 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005376 Instrumentation.ActivityResult ar =
5377 mInstrumentation.execStartActivityAsCaller(
5378 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005379 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005380 if (ar != null) {
5381 mMainThread.sendActivityResult(
5382 mToken, mEmbeddedID, -1, ar.getResultCode(),
5383 ar.getResultData());
5384 }
George Mount41725de2015-04-09 08:23:05 -07005385 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005386 }
5387
5388 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005389 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5390 * Intent, int, int, int, Bundle)} with no options.
5391 *
5392 * @param intent The IntentSender to launch.
5393 * @param requestCode If >= 0, this code will be returned in
5394 * onActivityResult() when the activity exits.
5395 * @param fillInIntent If non-null, this will be provided as the
5396 * intent parameter to {@link IntentSender#sendIntent}.
5397 * @param flagsMask Intent flags in the original IntentSender that you
5398 * would like to change.
5399 * @param flagsValues Desired values for any bits set in
5400 * <var>flagsMask</var>
5401 * @param extraFlags Always set to 0.
5402 */
5403 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005404 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005405 throws IntentSender.SendIntentException {
5406 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5407 flagsValues, extraFlags, null);
5408 }
5409
5410 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005411 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005412 * to use a IntentSender to describe the activity to be started. If
5413 * the IntentSender is for an activity, that activity will be started
5414 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5415 * here; otherwise, its associated action will be executed (such as
5416 * sending a broadcast) as if you had called
5417 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005418 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005419 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005420 * @param requestCode If >= 0, this code will be returned in
5421 * onActivityResult() when the activity exits.
5422 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005423 * intent parameter to {@link IntentSender#sendIntent}.
5424 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005425 * would like to change.
5426 * @param flagsValues Desired values for any bits set in
5427 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005428 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005429 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005430 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005431 * Context.startActivity(Intent, Bundle)} for more details. If options
5432 * have also been supplied by the IntentSender, options given here will
5433 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005434 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005435 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005436 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005437 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005438 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005439 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5440 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005441 } else if (options != null) {
5442 mParent.startIntentSenderFromChild(this, intent, requestCode,
5443 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005444 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005445 // Note we want to go through this call for compatibility with
5446 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005447 mParent.startIntentSenderFromChild(this, intent, requestCode,
5448 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005449 }
5450 }
5451
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005452 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5453 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005454 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005455 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005456 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005457 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005458 String resolvedType = null;
5459 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005460 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005461 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005462 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5463 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005464 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005465 .startActivityIntentSender(mMainThread.getApplicationThread(),
5466 intent != null ? intent.getTarget() : null,
5467 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005468 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005469 requestCode, flagsMask, flagsValues, options);
5470 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005471 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005472 }
5473 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005474
5475 if (options != null) {
5476 // Only when the options are not null, as the intent can point to something other
5477 // than an Activity.
5478 cancelInputsAndStartExitTransition(options);
5479 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005480 } catch (RemoteException e) {
5481 }
5482 if (requestCode >= 0) {
5483 // If this start is requesting a result, we can avoid making
5484 // the activity visible until the result is received. Setting
5485 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5486 // activity hidden during this time, to avoid flickering.
5487 // This can only be done when a result is requested because
5488 // that guarantees we will get information back when the
5489 // activity is finished, no matter what happens to it.
5490 mStartedActivity = true;
5491 }
5492 }
5493
5494 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005495 * Same as {@link #startActivity(Intent, Bundle)} with no options
5496 * specified.
5497 *
5498 * @param intent The intent to start.
5499 *
5500 * @throws android.content.ActivityNotFoundException
5501 *
Mark Lufa434852016-08-11 17:40:33 -07005502 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005503 * @see #startActivityForResult
5504 */
5505 @Override
5506 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005507 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005508 }
5509
5510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005511 * Launch a new activity. You will not receive any information about when
5512 * the activity exits. This implementation overrides the base version,
5513 * providing information about
5514 * the activity performing the launch. Because of this additional
5515 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5516 * required; if not specified, the new activity will be added to the
5517 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005518 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 * <p>This method throws {@link android.content.ActivityNotFoundException}
5520 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005521 *
5522 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005523 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005524 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005525 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005526 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005528 *
Mark Lufa434852016-08-11 17:40:33 -07005529 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005530 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 */
5532 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005533 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005534 if (options != null) {
5535 startActivityForResult(intent, -1, options);
5536 } else {
5537 // Note we want to go through this call for compatibility with
5538 // applications that may have overridden the method.
5539 startActivityForResult(intent, -1);
5540 }
5541 }
5542
5543 /**
5544 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5545 * specified.
5546 *
5547 * @param intents The intents to start.
5548 *
5549 * @throws android.content.ActivityNotFoundException
5550 *
Mark Lufa434852016-08-11 17:40:33 -07005551 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005552 * @see #startActivityForResult
5553 */
5554 @Override
5555 public void startActivities(Intent[] intents) {
5556 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 }
5558
5559 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005560 * Launch a new activity. You will not receive any information about when
5561 * the activity exits. This implementation overrides the base version,
5562 * providing information about
5563 * the activity performing the launch. Because of this additional
5564 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5565 * required; if not specified, the new activity will be added to the
5566 * task of the caller.
5567 *
5568 * <p>This method throws {@link android.content.ActivityNotFoundException}
5569 * if there was no Activity found to run the given Intent.
5570 *
5571 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005572 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005573 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005574 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005575 *
5576 * @throws android.content.ActivityNotFoundException
5577 *
Mark Lufa434852016-08-11 17:40:33 -07005578 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005579 * @see #startActivityForResult
5580 */
5581 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005582 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005583 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005584 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005585 }
5586
5587 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005588 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5589 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005590 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005591 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005592 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005593 * intent parameter to {@link IntentSender#sendIntent}.
5594 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005595 * would like to change.
5596 * @param flagsValues Desired values for any bits set in
5597 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005598 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005599 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005600 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005601 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005602 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005603 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5604 extraFlags, null);
5605 }
5606
5607 /**
5608 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5609 * to start; see
5610 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5611 * for more information.
5612 *
5613 * @param intent The IntentSender to launch.
5614 * @param fillInIntent If non-null, this will be provided as the
5615 * intent parameter to {@link IntentSender#sendIntent}.
5616 * @param flagsMask Intent flags in the original IntentSender that you
5617 * would like to change.
5618 * @param flagsValues Desired values for any bits set in
5619 * <var>flagsMask</var>
5620 * @param extraFlags Always set to 0.
5621 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005622 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005623 * Context.startActivity(Intent, Bundle)} for more details. If options
5624 * have also been supplied by the IntentSender, options given here will
5625 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005626 */
5627 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005628 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005629 Bundle options) throws IntentSender.SendIntentException {
5630 if (options != null) {
5631 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5632 flagsValues, extraFlags, options);
5633 } else {
5634 // Note we want to go through this call for compatibility with
5635 // applications that may have overridden the method.
5636 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5637 flagsValues, extraFlags);
5638 }
5639 }
5640
5641 /**
5642 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5643 * with no options.
5644 *
5645 * @param intent The intent to start.
5646 * @param requestCode If >= 0, this code will be returned in
5647 * onActivityResult() when the activity exits, as described in
5648 * {@link #startActivityForResult}.
5649 *
5650 * @return If a new activity was launched then true is returned; otherwise
5651 * false is returned and you must handle the Intent yourself.
5652 *
5653 * @see #startActivity
5654 * @see #startActivityForResult
5655 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005656 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5657 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005658 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005659 }
5660
5661 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 * A special variation to launch an activity only if a new activity
5663 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005664 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005666 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005668 * and the activity
5669 * that handles <var>intent</var> is the same as your currently running
5670 * activity, then a new instance is not needed. In this case, instead of
5671 * the normal behavior of calling {@link #onNewIntent} this function will
5672 * return and you can handle the Intent yourself.
5673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 * <p>This function can only be called from a top-level activity; if it is
5675 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 * @param intent The intent to start.
5678 * @param requestCode If >= 0, this code will be returned in
5679 * onActivityResult() when the activity exits, as described in
5680 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005681 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005682 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005683 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 * @return If a new activity was launched then true is returned; otherwise
5686 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005687 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 * @see #startActivity
5689 * @see #startActivityForResult
5690 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005691 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5692 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005694 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005696 Uri referrer = onProvideReferrer();
5697 if (referrer != null) {
5698 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5699 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005700 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005701 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005702 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005703 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005704 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5705 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5706 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 } catch (RemoteException e) {
5708 // Empty
5709 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005711 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 if (requestCode >= 0) {
5714 // If this start is requesting a result, we can avoid making
5715 // the activity visible until the result is received. Setting
5716 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5717 // activity hidden during this time, to avoid flickering.
5718 // This can only be done when a result is requested because
5719 // that guarantees we will get information back when the
5720 // activity is finished, no matter what happens to it.
5721 mStartedActivity = true;
5722 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005723 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 }
5725
5726 throw new UnsupportedOperationException(
5727 "startActivityIfNeeded can only be called from a top-level activity");
5728 }
5729
5730 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005731 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5732 * no options.
5733 *
5734 * @param intent The intent to dispatch to the next activity. For
5735 * correct behavior, this must be the same as the Intent that started
5736 * your own activity; the only changes you can make are to the extras
5737 * inside of it.
5738 *
5739 * @return Returns a boolean indicating whether there was another Activity
5740 * to start: true if there was a next activity to start, false if there
5741 * wasn't. In general, if true is returned you will then want to call
5742 * finish() on yourself.
5743 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005744 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005745 return startNextMatchingActivity(intent, null);
5746 }
5747
5748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 * Special version of starting an activity, for use when you are replacing
5750 * other activity components. You can use this to hand the Intent off
5751 * to the next Activity that can handle it. You typically call this in
5752 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 * @param intent The intent to dispatch to the next activity. For
5755 * correct behavior, this must be the same as the Intent that started
5756 * your own activity; the only changes you can make are to the extras
5757 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005758 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005759 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005760 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 * @return Returns a boolean indicating whether there was another Activity
5763 * to start: true if there was a next activity to start, false if there
5764 * wasn't. In general, if true is returned you will then want to call
5765 * finish() on yourself.
5766 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005767 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5768 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 if (mParent == null) {
5770 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005771 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005772 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005773 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005774 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 } catch (RemoteException e) {
5776 // Empty
5777 }
5778 return false;
5779 }
5780
5781 throw new UnsupportedOperationException(
5782 "startNextMatchingActivity can only be called from a top-level activity");
5783 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005784
5785 /**
5786 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5787 * with no options.
5788 *
5789 * @param child The activity making the call.
5790 * @param intent The intent to start.
5791 * @param requestCode Reply request code. < 0 if reply is not requested.
5792 *
5793 * @throws android.content.ActivityNotFoundException
5794 *
5795 * @see #startActivity
5796 * @see #startActivityForResult
5797 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005798 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005799 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005800 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005801 }
5802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 /**
RoboErik55011652014-07-09 15:05:53 -07005804 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 * <p>This method throws {@link android.content.ActivityNotFoundException}
5808 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005809 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 * @param child The activity making the call.
5811 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005812 * @param requestCode Reply request code. < 0 if reply is not requested.
5813 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005814 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005815 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005818 *
5819 * @see #startActivity
5820 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005822 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005823 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005824 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 Instrumentation.ActivityResult ar =
5826 mInstrumentation.execStartActivity(
5827 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005828 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 if (ar != null) {
5830 mMainThread.sendActivityResult(
5831 mToken, child.mEmbeddedID, requestCode,
5832 ar.getResultCode(), ar.getResultData());
5833 }
George Mount41725de2015-04-09 08:23:05 -07005834 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 }
5836
5837 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005838 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5839 * with no options.
5840 *
5841 * @param fragment The fragment making the call.
5842 * @param intent The intent to start.
5843 * @param requestCode Reply request code. < 0 if reply is not requested.
5844 *
5845 * @throws android.content.ActivityNotFoundException
5846 *
5847 * @see Fragment#startActivity
5848 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005849 *
5850 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5851 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005852 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005853 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005854 public void startActivityFromFragment(@NonNull Fragment fragment,
5855 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005856 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005857 }
5858
5859 /**
RoboErik55011652014-07-09 15:05:53 -07005860 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005861 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5862 * method.
RoboErik55011652014-07-09 15:05:53 -07005863 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005864 * <p>This method throws {@link android.content.ActivityNotFoundException}
5865 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005866 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005867 * @param fragment The fragment making the call.
5868 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005869 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005870 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005871 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005872 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005873 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005874 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005875 *
5876 * @see Fragment#startActivity
5877 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005878 *
5879 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5880 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005881 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005882 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005883 public void startActivityFromFragment(@NonNull Fragment fragment,
5884 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005885 startActivityForResult(fragment.mWho, intent, requestCode, options);
5886 }
5887
5888 /**
5889 * @hide
5890 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005891 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5892 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5893 UserHandle user) {
5894 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5895 }
5896
5897 /**
5898 * @hide
5899 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005900 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005901 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005902 public void startActivityForResult(
5903 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005904 Uri referrer = onProvideReferrer();
5905 if (referrer != null) {
5906 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5907 }
George Mount413739e2016-06-08 07:13:37 -07005908 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005909 Instrumentation.ActivityResult ar =
5910 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005911 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005912 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005913 if (ar != null) {
5914 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005915 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005916 ar.getResultCode(), ar.getResultData());
5917 }
George Mount41725de2015-04-09 08:23:05 -07005918 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005919 }
5920
5921 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005922 * @hide
5923 */
5924 @Override
5925 public boolean canStartActivityForResult() {
5926 return true;
5927 }
5928
5929 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005930 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5931 * int, Intent, int, int, int, Bundle)} with no options.
5932 */
5933 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5934 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5935 int extraFlags)
5936 throws IntentSender.SendIntentException {
5937 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5938 flagsMask, flagsValues, extraFlags, null);
5939 }
5940
5941 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005942 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005943 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005944 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005945 * for more information.
5946 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005947 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5948 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005949 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005950 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005951 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5952 flagsMask, flagsValues, options);
5953 }
5954
5955 /**
5956 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5957 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5958 * for more information.
5959 *
5960 * @hide
5961 */
5962 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5963 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5964 int extraFlags, @Nullable Bundle options)
5965 throws IntentSender.SendIntentException {
5966 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5967 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005968 }
5969
5970 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005971 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5972 * or {@link #finish} to specify an explicit transition animation to
5973 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005974 *
5975 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5976 * to using this with starting activities is to supply the desired animation
5977 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005978 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005979 * you to specify a custom animation even when starting an activity from
5980 * outside the context of the current top activity.
5981 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005982 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005983 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005984 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005985 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005986 */
5987 public void overridePendingTransition(int enterAnim, int exitAnim) {
5988 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005989 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005990 mToken, getPackageName(), enterAnim, exitAnim);
5991 } catch (RemoteException e) {
5992 }
5993 }
RoboErik55011652014-07-09 15:05:53 -07005994
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 * Call this to set the result that your activity will return to its
5997 * caller.
RoboErik55011652014-07-09 15:05:53 -07005998 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 * @param resultCode The result code to propagate back to the originating
6000 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07006001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 * @see #RESULT_CANCELED
6003 * @see #RESULT_OK
6004 * @see #RESULT_FIRST_USER
6005 * @see #setResult(int, Intent)
6006 */
6007 public final void setResult(int resultCode) {
6008 synchronized (this) {
6009 mResultCode = resultCode;
6010 mResultData = null;
6011 }
6012 }
6013
6014 /**
6015 * Call this to set the result that your activity will return to its
6016 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08006017 *
6018 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
6019 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
6020 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
6021 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
6022 * Activity receiving the result access to the specific URIs in the Intent.
6023 * Access will remain until the Activity has finished (it will remain across the hosting
6024 * process being killed and other temporary destruction) and will be added
6025 * to any existing set of URI permissions it already holds.
6026 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 * @param resultCode The result code to propagate back to the originating
6028 * activity, often RESULT_CANCELED or RESULT_OK
6029 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07006030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 * @see #RESULT_CANCELED
6032 * @see #RESULT_OK
6033 * @see #RESULT_FIRST_USER
6034 * @see #setResult(int)
6035 */
6036 public final void setResult(int resultCode, Intent data) {
6037 synchronized (this) {
6038 mResultCode = resultCode;
6039 mResultData = data;
6040 }
6041 }
6042
6043 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006044 * Return information about who launched this activity. If the launching Intent
6045 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
6046 * that will be returned as-is; otherwise, if known, an
6047 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
6048 * package name that started the Intent will be returned. This may return null if no
6049 * referrer can be identified -- it is neither explicitly specified, nor is it known which
6050 * application package was involved.
6051 *
6052 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
6053 * return the referrer that submitted that new intent to the activity. Otherwise, it
6054 * always returns the referrer of the original Intent.</p>
6055 *
6056 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
6057 * referrer information, applications can spoof it.</p>
6058 */
6059 @Nullable
6060 public Uri getReferrer() {
6061 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07006062 try {
6063 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
6064 if (referrer != null) {
6065 return referrer;
6066 }
6067 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
6068 if (referrerName != null) {
6069 return Uri.parse(referrerName);
6070 }
6071 } catch (BadParcelableException e) {
6072 Log.w(TAG, "Cannot read referrer from intent;"
6073 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006074 }
6075 if (mReferrer != null) {
6076 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
6077 }
6078 return null;
6079 }
6080
6081 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07006082 * Override to generate the desired referrer for the content currently being shown
6083 * by the app. The default implementation returns null, meaning the referrer will simply
6084 * be the android-app: of the package name of this activity. Return a non-null Uri to
6085 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
6086 */
6087 public Uri onProvideReferrer() {
6088 return null;
6089 }
6090
6091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 * Return the name of the package that invoked this activity. This is who
6093 * the data in {@link #setResult setResult()} will be sent to. You can
6094 * use this information to validate that the recipient is allowed to
6095 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006096 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006097 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006098 * did not use the {@link #startActivityForResult}
6099 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07006100 * null.</p>
6101 *
6102 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
6103 * the result from this method was unstable. If the process hosting the calling
6104 * package was no longer running, it would return null instead of the proper package
6105 * name. You can use {@link #getCallingActivity()} and retrieve the package name
6106 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07006107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 * @return The package of the activity that will receive your
6109 * reply, or null if none.
6110 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006111 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 public String getCallingPackage() {
6113 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006114 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 } catch (RemoteException e) {
6116 return null;
6117 }
6118 }
6119
6120 /**
6121 * Return the name of the activity that invoked this activity. This is
6122 * who the data in {@link #setResult setResult()} will be sent to. You
6123 * can use this information to validate that the recipient is allowed to
6124 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006125 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006126 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006127 * did not use the {@link #startActivityForResult}
6128 * form that includes a request code), then the calling package will be
6129 * null.
6130 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006131 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 * reply, or null if none.
6133 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006134 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 public ComponentName getCallingActivity() {
6136 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006137 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 } catch (RemoteException e) {
6139 return null;
6140 }
6141 }
6142
6143 /**
6144 * Control whether this activity's main window is visible. This is intended
6145 * only for the special case of an activity that is not going to show a
6146 * UI itself, but can't just finish prior to onResume() because it needs
6147 * to wait for a service binding or such. Setting this to false allows
6148 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07006149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 * <p>The default value for this is taken from the
6151 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
6152 */
6153 public void setVisible(boolean visible) {
6154 if (mVisibleFromClient != visible) {
6155 mVisibleFromClient = visible;
6156 if (mVisibleFromServer) {
6157 if (visible) makeVisible();
6158 else mDecor.setVisibility(View.INVISIBLE);
6159 }
6160 }
6161 }
RoboErik55011652014-07-09 15:05:53 -07006162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 void makeVisible() {
6164 if (!mWindowAdded) {
6165 ViewManager wm = getWindowManager();
6166 wm.addView(mDecor, getWindow().getAttributes());
6167 mWindowAdded = true;
6168 }
6169 mDecor.setVisibility(View.VISIBLE);
6170 }
RoboErik55011652014-07-09 15:05:53 -07006171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 /**
6173 * Check to see whether this activity is in the process of finishing,
6174 * either because you called {@link #finish} on it or someone else
6175 * has requested that it finished. This is often used in
6176 * {@link #onPause} to determine whether the activity is simply pausing or
6177 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07006178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07006180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 * @see #finish
6182 */
6183 public boolean isFinishing() {
6184 return mFinished;
6185 }
6186
6187 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006188 * Returns true if the final {@link #onDestroy()} call has been made
6189 * on the Activity, so this instance is now dead.
6190 */
6191 public boolean isDestroyed() {
6192 return mDestroyed;
6193 }
6194
6195 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006196 * Check to see whether this activity is in the process of being destroyed in order to be
6197 * recreated with a new configuration. This is often used in
6198 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
6199 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07006200 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006201 * @return If the activity is being torn down in order to be recreated with a new configuration,
6202 * returns true; else returns false.
6203 */
6204 public boolean isChangingConfigurations() {
6205 return mChangingConfigurations;
6206 }
6207
6208 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006209 * Cause this Activity to be recreated with a new instance. This results
6210 * in essentially the same flow as when the Activity is created due to
6211 * a configuration change -- the current instance will go through its
6212 * lifecycle to {@link #onDestroy} and a new instance then created after it.
6213 */
6214 public void recreate() {
6215 if (mParent != null) {
6216 throw new IllegalStateException("Can only be called on top-level activity");
6217 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07006218 if (Looper.myLooper() != mMainThread.getLooper()) {
6219 throw new IllegalStateException("Must be called from main thread");
6220 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006221 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006222 }
6223
6224 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006225 * Finishes the current activity and specifies whether to remove the task associated with this
6226 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006228 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006229 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 if (mParent == null) {
6231 int resultCode;
6232 Intent resultData;
6233 synchronized (this) {
6234 resultCode = mResultCode;
6235 resultData = mResultData;
6236 }
Joe Onorato43a17652011-04-06 19:22:23 -07006237 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006239 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006240 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006241 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006242 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006243 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 mFinished = true;
6245 }
6246 } catch (RemoteException e) {
6247 // Empty
6248 }
6249 } else {
6250 mParent.finishFromChild(this);
6251 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006252
6253 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6254 // be restored now.
6255 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
6256 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6257 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
6258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 }
6260
6261 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006262 * Call this when your activity is done and should be closed. The
6263 * ActivityResult is propagated back to whoever launched you via
6264 * onActivityResult().
6265 */
6266 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006267 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006268 }
6269
6270 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006271 * Finish this activity as well as all activities immediately below it
6272 * in the current task that have the same affinity. This is typically
6273 * used when an application can be launched on to another task (such as
6274 * from an ACTION_VIEW of a content type it understands) and the user
6275 * has used the up navigation to switch out of the current task and in
6276 * to its own task. In this case, if the user has navigated down into
6277 * any other activities of the second application, all of those should
6278 * be removed from the original task as part of the task switch.
6279 *
6280 * <p>Note that this finish does <em>not</em> allow you to deliver results
6281 * to the previous activity, and an exception will be thrown if you are trying
6282 * to do so.</p>
6283 */
6284 public void finishAffinity() {
6285 if (mParent != null) {
6286 throw new IllegalStateException("Can not be called from an embedded activity");
6287 }
6288 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6289 throw new IllegalStateException("Can not be called to deliver a result");
6290 }
6291 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006292 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006293 mFinished = true;
6294 }
6295 } catch (RemoteException e) {
6296 // Empty
6297 }
6298 }
6299
6300 /**
RoboErik55011652014-07-09 15:05:53 -07006301 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 * {@link #finish} method. The default implementation simply calls
6303 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006304 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 * @see #finish
6308 */
6309 public void finishFromChild(Activity child) {
6310 finish();
6311 }
6312
6313 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006314 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6315 * to reverse its exit Transition. When the exit Transition completes,
6316 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6317 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006318 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006319 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006320 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006321 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006322 finish();
6323 }
George Mountcb4b7d92014-02-25 10:47:55 -08006324 }
6325
6326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 * Force finish another activity that you had previously started with
6328 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 * @param requestCode The request code of the activity that you had
6331 * given to startActivityForResult(). If there are multiple
6332 * activities started with this request code, they
6333 * will all be finished.
6334 */
6335 public void finishActivity(int requestCode) {
6336 if (mParent == null) {
6337 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006338 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6340 } catch (RemoteException e) {
6341 // Empty
6342 }
6343 } else {
6344 mParent.finishActivityFromChild(this, requestCode);
6345 }
6346 }
6347
6348 /**
6349 * This is called when a child activity of this one calls its
6350 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 * @param child The activity making the call.
6353 * @param requestCode Request code that had been used to start the
6354 * activity.
6355 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006356 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006358 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6360 } catch (RemoteException e) {
6361 // Empty
6362 }
6363 }
6364
6365 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006366 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006367 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006368 */
6369 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006370 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006371 }
6372
6373 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006374 * Ask that the local app instance of this activity be released to free up its memory.
6375 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6376 * a new instance of the activity will later be re-created if needed due to the user
6377 * navigating back to it.
6378 *
6379 * @return Returns true if the activity was in a state that it has started the process
6380 * of destroying its current instance; returns false if for any reason this could not
6381 * be done: it is currently visible to the user, it is already being destroyed, it is
6382 * being finished, it hasn't yet saved its state, etc.
6383 */
6384 public boolean releaseInstance() {
6385 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006386 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006387 } catch (RemoteException e) {
6388 // Empty
6389 }
6390 return false;
6391 }
6392
6393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 * Called when an activity you launched exits, giving you the requestCode
6395 * you started it with, the resultCode it returned, and any additional
6396 * data from it. The <var>resultCode</var> will be
6397 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6398 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 * <p>You will receive this call immediately before onResume() when your
6401 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006402 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006403 * <p>This method is never invoked if your activity sets
6404 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6405 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 * @param requestCode The integer request code originally supplied to
6408 * startActivityForResult(), allowing you to identify who this
6409 * result came from.
6410 * @param resultCode The integer result code returned by the child activity
6411 * through its setResult().
6412 * @param data An Intent, which can return result data to the caller
6413 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 * @see #startActivityForResult
6416 * @see #createPendingResult
6417 * @see #setResult(int)
6418 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006419 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 }
6421
6422 /**
George Mount62ab9b72014-05-02 13:51:17 -07006423 * Called when an activity you launched with an activity transition exposes this
6424 * Activity through a returning activity transition, giving you the resultCode
6425 * and any additional data from it. This method will only be called if the activity
6426 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006427 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006428 *
6429 * <p>The purpose of this function is to let the called Activity send a hint about
6430 * its state so that this underlying Activity can prepare to be exposed. A call to
6431 * this method does not guarantee that the called Activity has or will be exiting soon.
6432 * It only indicates that it will expose this Activity's Window and it has
6433 * some data to pass to prepare it.</p>
6434 *
6435 * @param resultCode The integer result code returned by the child activity
6436 * through its setResult().
6437 * @param data An Intent, which can return result data to the caller
6438 * (various data can be attached to Intent "extras").
6439 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006440 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006441 }
6442
6443 /**
RoboErik55011652014-07-09 15:05:53 -07006444 * Create a new PendingIntent object which you can hand to others
6445 * for them to use to send result data back to your
6446 * {@link #onActivityResult} callback. The created object will be either
6447 * one-shot (becoming invalid after a result is sent back) or multiple
6448 * (allowing any number of results to be sent through it).
6449 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450 * @param requestCode Private request code for the sender that will be
6451 * associated with the result data when it is returned. The sender can not
6452 * modify this value, allowing you to identify incoming results.
6453 * @param data Default data to supply in the result, which may be modified
6454 * by the sender.
6455 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6456 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6457 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6458 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6459 * or any of the flags as supported by
6460 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6461 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 * @return Returns an existing or new PendingIntent matching the given
6464 * parameters. May return null only if
6465 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6466 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006467 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 * @see PendingIntent
6469 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006470 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6471 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 String packageName = getPackageName();
6473 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006474 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006476 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07006477 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07006479 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006480 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006481 return target != null ? new PendingIntent(target) : null;
6482 } catch (RemoteException e) {
6483 // Empty
6484 }
6485 return null;
6486 }
6487
6488 /**
6489 * Change the desired orientation of this activity. If the activity
6490 * is currently in the foreground or otherwise impacting the screen
6491 * orientation, the screen will immediately be changed (possibly causing
6492 * the activity to be restarted). Otherwise, this will be used the next
6493 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 * @param requestedOrientation An orientation constant as used in
6496 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6497 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006498 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 if (mParent == null) {
6500 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006501 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 mToken, requestedOrientation);
6503 } catch (RemoteException e) {
6504 // Empty
6505 }
6506 } else {
6507 mParent.setRequestedOrientation(requestedOrientation);
6508 }
6509 }
RoboErik55011652014-07-09 15:05:53 -07006510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 /**
6512 * Return the current requested orientation of the activity. This will
6513 * either be the orientation requested in its component's manifest, or
6514 * the last requested orientation given to
6515 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 * @return Returns an orientation constant as used in
6518 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6519 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006520 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 public int getRequestedOrientation() {
6522 if (mParent == null) {
6523 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006524 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 .getRequestedOrientation(mToken);
6526 } catch (RemoteException e) {
6527 // Empty
6528 }
6529 } else {
6530 return mParent.getRequestedOrientation();
6531 }
6532 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6533 }
RoboErik55011652014-07-09 15:05:53 -07006534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 /**
6536 * Return the identifier of the task this activity is in. This identifier
6537 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006538 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 * @return Task identifier, an opaque integer.
6540 */
6541 public int getTaskId() {
6542 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006543 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 } catch (RemoteException e) {
6545 return -1;
6546 }
6547 }
6548
6549 /**
6550 * Return whether this activity is the root of a task. The root is the
6551 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 * @return True if this is the root activity, else false.
6554 */
Winson Chung4d8681f2017-05-23 16:22:08 -07006555 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 public boolean isTaskRoot() {
6557 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006558 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 } catch (RemoteException e) {
6560 return false;
6561 }
6562 }
6563
6564 /**
6565 * Move the task containing this activity to the back of the activity
6566 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006567 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006568 * @param nonRoot If false then this only works if the activity is the root
6569 * of a task; if true it will work for any activity in
6570 * a task.
RoboErik55011652014-07-09 15:05:53 -07006571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006572 * @return If the task was moved (or it was already at the
6573 * back) true is returned, else false.
6574 */
6575 public boolean moveTaskToBack(boolean nonRoot) {
6576 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006577 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 } catch (RemoteException e) {
6579 // Empty
6580 }
6581 return false;
6582 }
6583
6584 /**
6585 * Returns class name for this activity with the package prefix removed.
6586 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 * @return The local class name.
6589 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006590 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 public String getLocalClassName() {
6592 final String pkg = getPackageName();
6593 final String cls = mComponent.getClassName();
6594 int packageLen = pkg.length();
6595 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6596 || cls.charAt(packageLen) != '.') {
6597 return cls;
6598 }
6599 return cls.substring(packageLen+1);
6600 }
RoboErik55011652014-07-09 15:05:53 -07006601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006603 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006604 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006605 * @return Returns the complete component name for this activity
6606 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006607 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 return mComponent;
6609 }
6610
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006611 /** @hide */
6612 @Override
6613 public final ComponentName autofillClientGetComponentName() {
6614 return getComponentName();
6615 }
6616
Felipe Lemecbf7f262019-04-17 13:57:59 -07006617 /** @hide */
6618 @Override
6619 public final ComponentName contentCaptureClientGetComponentName() {
6620 return getComponentName();
6621 }
6622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 /**
6624 * Retrieve a {@link SharedPreferences} object for accessing preferences
6625 * that are private to this activity. This simply calls the underlying
6626 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6627 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006628 *
6629 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006630 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 *
6632 * @return Returns the single SharedPreferences instance that can be used
6633 * to retrieve and modify the preference values.
6634 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006635 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 return getSharedPreferences(getLocalClassName(), mode);
6637 }
RoboErik55011652014-07-09 15:05:53 -07006638
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006639 private void ensureSearchManager() {
6640 if (mSearchManager != null) {
6641 return;
6642 }
RoboErik55011652014-07-09 15:05:53 -07006643
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006644 try {
6645 mSearchManager = new SearchManager(this, null);
6646 } catch (ServiceNotFoundException e) {
6647 throw new IllegalStateException(e);
6648 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006649 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006652 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 if (getBaseContext() == null) {
6654 throw new IllegalStateException(
6655 "System services not available to Activities before onCreate()");
6656 }
6657
6658 if (WINDOW_SERVICE.equals(name)) {
6659 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006660 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006661 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006662 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 }
6664 return super.getSystemService(name);
6665 }
6666
6667 /**
6668 * Change the title associated with this activity. If this is a
6669 * top-level activity, the title for its window will change. If it
6670 * is an embedded activity, the parent can do whatever it wants
6671 * with it.
6672 */
6673 public void setTitle(CharSequence title) {
6674 mTitle = title;
6675 onTitleChanged(title, mTitleColor);
6676
6677 if (mParent != null) {
6678 mParent.onChildTitleChanged(this, title);
6679 }
6680 }
6681
6682 /**
6683 * Change the title associated with this activity. If this is a
6684 * top-level activity, the title for its window will change. If it
6685 * is an embedded activity, the parent can do whatever it wants
6686 * with it.
6687 */
6688 public void setTitle(int titleId) {
6689 setTitle(getText(titleId));
6690 }
6691
Alan Viverette2525d9c2013-11-15 14:42:19 -08006692 /**
6693 * Change the color of the title associated with this activity.
6694 * <p>
6695 * This method is deprecated starting in API Level 11 and replaced by action
6696 * bar styles. For information on styling the Action Bar, read the <a
6697 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6698 * guide.
6699 *
6700 * @deprecated Use action bar styles instead.
6701 */
6702 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 public void setTitleColor(int textColor) {
6704 mTitleColor = textColor;
6705 onTitleChanged(mTitle, textColor);
6706 }
6707
6708 public final CharSequence getTitle() {
6709 return mTitle;
6710 }
6711
6712 public final int getTitleColor() {
6713 return mTitleColor;
6714 }
6715
6716 protected void onTitleChanged(CharSequence title, int color) {
6717 if (mTitleReady) {
6718 final Window win = getWindow();
6719 if (win != null) {
6720 win.setTitle(title);
6721 if (color != 0) {
6722 win.setTitleColor(color);
6723 }
6724 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006725 if (mActionBar != null) {
6726 mActionBar.setWindowTitle(title);
6727 }
Adam Powella557fdc2014-08-21 18:05:53 -07006728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 }
6730
6731 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6732 }
6733
6734 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006735 * Sets information describing the task with this activity for presentation inside the Recents
6736 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6737 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6738 * for each property until a suitable value is found. For each task the taskDescription will be
6739 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006740 *
6741 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006742 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006743 *
Winson Chunga449dc02014-05-16 11:15:04 -07006744 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006745 */
Winson Chunga449dc02014-05-16 11:15:04 -07006746 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006747 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006748 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006749 // Scale the icon down to something reasonable if it is provided
6750 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6751 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6752 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6753 true);
6754 mTaskDescription.setIcon(icon);
6755 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006756 }
6757 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006758 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006759 } catch (RemoteException e) {
6760 }
6761 }
6762
6763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 * Sets the visibility of the progress bar in the title.
6765 * <p>
6766 * In order for the progress bar to be shown, the feature must be requested
6767 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006768 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006770 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006772 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 public final void setProgressBarVisibility(boolean visible) {
6774 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6775 Window.PROGRESS_VISIBILITY_OFF);
6776 }
6777
6778 /**
6779 * Sets the visibility of the indeterminate progress bar in the title.
6780 * <p>
6781 * In order for the progress bar to be shown, the feature must be requested
6782 * via {@link #requestWindowFeature(int)}.
6783 *
6784 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006785 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006787 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6789 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6790 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6791 }
RoboErik55011652014-07-09 15:05:53 -07006792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 /**
6794 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6795 * is always indeterminate).
6796 * <p>
6797 * In order for the progress bar to be shown, the feature must be requested
6798 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006801 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006803 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006804 public final void setProgressBarIndeterminate(boolean indeterminate) {
6805 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006806 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6807 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 }
RoboErik55011652014-07-09 15:05:53 -07006809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 /**
6811 * Sets the progress for the progress bars in the title.
6812 * <p>
6813 * In order for the progress bar to be shown, the feature must be requested
6814 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006815 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816 * @param progress The progress for the progress bar. Valid ranges are from
6817 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6818 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006819 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006821 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 public final void setProgress(int progress) {
6823 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6824 }
RoboErik55011652014-07-09 15:05:53 -07006825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 /**
6827 * Sets the secondary progress for the progress bar in the title. This
6828 * progress is drawn between the primary progress (set via
6829 * {@link #setProgress(int)} and the background. It can be ideal for media
6830 * scenarios such as showing the buffering progress while the default
6831 * progress shows the play progress.
6832 * <p>
6833 * In order for the progress bar to be shown, the feature must be requested
6834 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006835 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6837 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006838 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006840 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 public final void setSecondaryProgress(int secondaryProgress) {
6842 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6843 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6844 }
6845
6846 /**
6847 * Suggests an audio stream whose volume should be changed by the hardware
6848 * volume controls.
6849 * <p>
6850 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006851 * Volume requests which are received while the Activity is in the
6852 * foreground will affect this stream.
6853 * <p>
6854 * It is not guaranteed that the hardware volume controls will always change
6855 * this stream's volume (for example, if a call is in progress, its stream's
6856 * volume may be changed instead). To reset back to the default, use
6857 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006860 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 */
6862 public final void setVolumeControlStream(int streamType) {
6863 getWindow().setVolumeControlStream(streamType);
6864 }
6865
6866 /**
6867 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006868 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006869 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 * @return The suggested audio stream type whose volume should be changed by
6871 * the hardware volume controls.
6872 * @see #setVolumeControlStream(int)
6873 */
6874 public final int getVolumeControlStream() {
6875 return getWindow().getVolumeControlStream();
6876 }
RoboErik55011652014-07-09 15:05:53 -07006877
6878 /**
6879 * Sets a {@link MediaController} to send media keys and volume changes to.
6880 * <p>
6881 * The controller will be tied to the window of this Activity. Media key and
6882 * volume events which are received while the Activity is in the foreground
6883 * will be forwarded to the controller and used to invoke transport controls
6884 * or adjust the volume. This may be used instead of or in addition to
6885 * {@link #setVolumeControlStream} to affect a specific session instead of a
6886 * specific stream.
6887 * <p>
6888 * It is not guaranteed that the hardware volume controls will always change
6889 * this session's volume (for example, if a call is in progress, its
6890 * stream's volume may be changed instead). To reset back to the default use
6891 * null as the controller.
6892 *
6893 * @param controller The controller for the session which should receive
6894 * media keys and volume changes.
6895 */
6896 public final void setMediaController(MediaController controller) {
6897 getWindow().setMediaController(controller);
6898 }
6899
6900 /**
6901 * Gets the controller which should be receiving media key and volume events
6902 * while this activity is in the foreground.
6903 *
6904 * @return The controller which should receive events.
6905 * @see #setMediaController(android.media.session.MediaController)
6906 */
6907 public final MediaController getMediaController() {
6908 return getWindow().getMediaController();
6909 }
6910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 /**
6912 * Runs the specified action on the UI thread. If the current thread is the UI
6913 * thread, then the action is executed immediately. If the current thread is
6914 * not the UI thread, the action is posted to the event queue of the UI thread.
6915 *
6916 * @param action the action to run on the UI thread
6917 */
6918 public final void runOnUiThread(Runnable action) {
6919 if (Thread.currentThread() != mUiThread) {
6920 mHandler.post(action);
6921 } else {
6922 action.run();
6923 }
6924 }
6925
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006926 /** @hide */
6927 @Override
6928 public final void autofillClientRunOnUiThread(Runnable action) {
6929 runOnUiThread(action);
6930 }
6931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006933 * Standard implementation of
6934 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6935 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006936 * This implementation does nothing and is for
6937 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6938 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6939 *
6940 * @see android.view.LayoutInflater#createView
6941 * @see android.view.Window#getLayoutInflater
6942 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006943 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07006944 public View onCreateView(@NonNull String name, @NonNull Context context,
6945 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006946 return null;
6947 }
6948
6949 /**
6950 * Standard implementation of
6951 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6952 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006953 * This implementation handles <fragment> tags to embed fragments inside
6954 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 *
6956 * @see android.view.LayoutInflater#createView
6957 * @see android.view.Window#getLayoutInflater
6958 */
Ian Lake0c807f42018-09-12 14:03:27 -07006959 @Nullable
6960 public View onCreateView(@Nullable View parent, @NonNull String name,
6961 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006962 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006963 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006964 }
RoboErik55011652014-07-09 15:05:53 -07006965
Adam Powell371a8092014-06-20 12:51:12 -07006966 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 }
6968
Daniel Sandler69a48172010-06-23 16:29:36 -04006969 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006970 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006971 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006972 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006973 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006974 * @param fd The raw file descriptor that the dump is being sent to.
6975 * @param writer The PrintWriter to which you should dump your state. This will be
6976 * closed for you after you return.
6977 * @param args additional arguments to the dump request.
6978 */
Ian Lake140fe122018-09-13 14:59:38 -07006979 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
6980 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006981 dumpInner(prefix, fd, writer, args);
6982 }
6983
Ian Lake140fe122018-09-13 14:59:38 -07006984 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
6985 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08006986 if (args != null && args.length > 0) {
6987 // Handle special cases
6988 switch (args[0]) {
6989 case "--autofill":
6990 dumpAutofillManager(prefix, writer);
6991 return;
Felipe Lemeb718e032018-12-10 09:44:23 -08006992 case "--contentcapture":
6993 dumpContentCaptureManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006994 return;
6995 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07006996 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006997 writer.print(prefix); writer.print("Local Activity ");
6998 writer.print(Integer.toHexString(System.identityHashCode(this)));
6999 writer.println(" State:");
7000 String innerPrefix = prefix + " ";
7001 writer.print(innerPrefix); writer.print("mResumed=");
7002 writer.print(mResumed); writer.print(" mStopped=");
7003 writer.print(mStopped); writer.print(" mFinished=");
7004 writer.println(mFinished);
Hongwei Wangf0b5fb12019-07-25 15:14:47 -07007005 writer.print(innerPrefix); writer.print("mLastDispatchedIsInMultiWindowMode=");
7006 writer.print(mLastDispatchedIsInMultiWindowMode);
7007 writer.print(" mLastDispatchedIsInPictureInPictureMode=");
7008 writer.println(mLastDispatchedIsInPictureInPictureMode);
Dianne Hackborn30d71892010-12-11 10:37:55 -08007009 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
7010 writer.println(mChangingConfigurations);
7011 writer.print(innerPrefix); writer.print("mCurrentConfig=");
7012 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07007013
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007014 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
7015 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07007016 if (mVoiceInteractor != null) {
7017 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
7018 }
Jeff Brown5182c782013-10-15 20:31:52 -07007019
Michael Wright5f48dc72013-11-01 12:42:49 -07007020 if (getWindow() != null &&
7021 getWindow().peekDecorView() != null &&
7022 getWindow().peekDecorView().getViewRootImpl() != null) {
7023 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
7024 }
Jeff Brown5182c782013-10-15 20:31:52 -07007025
7026 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07007027
Felipe Lemeb546ca72018-08-15 08:44:12 -07007028 dumpAutofillManager(prefix, writer);
Felipe Lemeb718e032018-12-10 09:44:23 -08007029 dumpContentCaptureManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07007030
7031 ResourcesManager.getInstance().dump(prefix, writer);
7032 }
7033
7034 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08007035 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07007036 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07007037 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08007038 writer.print(prefix); writer.print("Autofill Compat Mode: ");
7039 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07007040 } else {
7041 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07007042 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07007043 }
7044
Felipe Lemeb718e032018-12-10 09:44:23 -08007045 void dumpContentCaptureManager(String prefix, PrintWriter writer) {
7046 final ContentCaptureManager cm = getContentCaptureManager();
7047 if (cm != null) {
7048 cm.dump(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08007049 } else {
Felipe Lemeb718e032018-12-10 09:44:23 -08007050 writer.print(prefix); writer.println("No ContentCaptureManager");
Felipe Lemee348dc32018-11-05 12:35:29 -08007051 }
7052 }
7053
Dianne Hackborn625ac272010-09-17 18:29:22 -07007054 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007055 * Bit indicating that this activity is "immersive" and should not be
7056 * interrupted by notifications if possible.
7057 *
7058 * This value is initially set by the manifest property
7059 * <code>android:immersive</code> but may be changed at runtime by
7060 * {@link #setImmersive}.
7061 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007062 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04007063 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7064 */
7065 public boolean isImmersive() {
7066 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007067 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04007068 } catch (RemoteException e) {
7069 return false;
7070 }
7071 }
7072
7073 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07007074 * Indication of whether this is the highest level activity in this task. Can be used to
7075 * determine whether an activity launched by this activity was placed in the same task or
7076 * another task.
7077 *
7078 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07007079 */
Sunny Goyald85bed52018-09-25 12:01:01 -07007080 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07007081 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07007082 return false;
7083 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07007084 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007085 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07007086 } catch (RemoteException e) {
7087 return false;
7088 }
7089 }
7090
7091 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07007092 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
7093 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007094 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07007095 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007096 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
7097 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07007098 * This call has no effect on non-translucent activities or on activities with the
7099 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007100 *
George Mount62ab9b72014-05-02 13:51:17 -07007101 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7102 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007103 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007104 *
7105 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07007106 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007107 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007108 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07007109 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07007110 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007111 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007112 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
7113 }
Craig Mautner4addfc52013-06-25 08:05:45 -07007114 } catch (RemoteException e) {
7115 // pass
7116 }
7117 }
7118
7119 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007120 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
7121 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
7122 * <p>
7123 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
7124 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
7125 * be called indicating that it is safe to make this activity translucent again. Until
7126 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
7127 * behind the frontmost Activity will be indeterminate.
7128 * <p>
7129 * This call has no effect on non-translucent activities or on activities with the
7130 * {@link android.R.attr#windowIsFloating} attribute.
7131 *
7132 * @param callback the method to call when all visible Activities behind this one have been
7133 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07007134 * @param options activity options delivered to the activity below this one. The options
7135 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07007136 * @return <code>true</code> if Window was opaque and will become translucent or
7137 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007138 *
7139 * @see #convertFromTranslucent()
7140 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007141 *
7142 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007143 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007144 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07007145 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07007146 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007147 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007148 try {
7149 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007150 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007151 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07007152 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07007153 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007154 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007155 // Make callback return as though it timed out.
7156 mChangeCanvasToTranslucent = false;
7157 drawComplete = false;
7158 }
7159 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
7160 // Window is already translucent.
7161 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07007162 }
George Mount3cc716c2014-06-12 16:35:35 -07007163 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007164 }
7165
7166 /** @hide */
7167 void onTranslucentConversionComplete(boolean drawComplete) {
7168 if (mTranslucentCallback != null) {
7169 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
7170 mTranslucentCallback = null;
7171 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007172 if (mChangeCanvasToTranslucent) {
7173 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
7174 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007175 }
7176
Craig Mautnereb8abf72014-07-02 15:04:09 -07007177 /** @hide */
7178 public void onNewActivityOptions(ActivityOptions options) {
7179 mActivityTransitionState.setEnterActivityOptions(this, options);
7180 if (!mStopped) {
7181 mActivityTransitionState.enterReady(this);
7182 }
7183 }
7184
Craig Mautner5eda9b32013-07-02 11:58:16 -07007185 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07007186 * Retrieve the ActivityOptions passed in from the launching activity or passed back
7187 * from an activity launched by this activity in its call to {@link
7188 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
7189 *
7190 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
7191 * @hide
7192 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007193 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07007194 ActivityOptions getActivityOptions() {
7195 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007196 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007197 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07007198 } catch (RemoteException e) {
7199 }
7200 return null;
7201 }
7202
7203 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007204 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07007205 * this method anytime between the start of {@link #onResume()} and the return from
7206 * {@link #onPause()}. If this call is successful then the activity will remain visible after
7207 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
7208 *
7209 * <p>The actions of this call are reset each time that this activity is brought to the
7210 * front. That is, every time {@link #onResume()} is called the activity will be assumed
7211 * to not have requested visible behind. Therefore, if you want this activity to continue to
7212 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007213 *
7214 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
7215 * for dialog and translucent activities.
7216 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007217 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
7218 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
7219 *
7220 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07007221 * the next call to onResume.
7222 *
Bryce Leed59629e2017-04-18 14:35:29 -07007223 * @deprecated This method's functionality is no longer supported as of
7224 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7225 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007226 * @param visible true to notify the system that the activity wishes to be visible behind other
7227 * translucent activities, false to indicate otherwise. Resources must be
7228 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07007229 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007230 * @return the resulting visibiity state. If true the activity will remain visible beyond
7231 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7232 * then the activity may not count on being visible behind other translucent activities,
7233 * and must stop any media playback and release resources.
7234 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7235 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007236 *
Jose Limafcf70832014-08-27 23:09:05 -07007237 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007238 */
Bryce Leed59629e2017-04-18 14:35:29 -07007239 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007240 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007241 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007242 }
7243
7244 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007245 * Called when a translucent activity over this activity is becoming opaque or another
7246 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007247 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007248 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007249 * <p>When this method is called the activity has 500 msec to release any resources it may be
7250 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007251 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007252 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007253 * process. Otherwise {@link #onStop()} will be called following return.
7254 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007255 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007256 *
7257 * @deprecated This method's functionality is no longer supported as of
7258 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007259 */
Bryce Leed59629e2017-04-18 14:35:29 -07007260 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007261 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007262 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007263 mCalled = true;
7264 }
7265
7266 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007267 * Translucent activities may call this to determine if there is an activity below them that
7268 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007269 *
Bryce Leed59629e2017-04-18 14:35:29 -07007270 * @deprecated This method's functionality is no longer supported as of
7271 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7272 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007273 * @return true if an activity below is set to visible according to the most recent call to
7274 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007275 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007276 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007277 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007278 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007279 * @hide
7280 */
Bryce Leed59629e2017-04-18 14:35:29 -07007281 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007282 @SystemApi
7283 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007284 return false;
7285 }
7286
7287 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007288 * The topmost foreground activity will receive this call when the background visibility state
7289 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007290 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007291 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007292 * due to a background activity finishing itself.
7293 *
Bryce Leed59629e2017-04-18 14:35:29 -07007294 * @deprecated This method's functionality is no longer supported as of
7295 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7296 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007297 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007298 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007299 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007300 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007301 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007302 */
Bryce Leed59629e2017-04-18 14:35:29 -07007303 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007304 @SystemApi
7305 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007306 }
7307
7308 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007309 * Activities cannot draw during the period that their windows are animating in. In order
7310 * to know when it is safe to begin drawing they can override this method which will be
7311 * called when the entering animation has completed.
7312 */
7313 public void onEnterAnimationComplete() {
7314 }
7315
7316 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007317 * @hide
7318 */
7319 public void dispatchEnterAnimationComplete() {
chaviwfeb2e1e2018-12-19 17:24:11 -08007320 mEnterAnimationComplete = true;
7321 mInstrumentation.onEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007322 onEnterAnimationComplete();
7323 if (getWindow() != null && getWindow().getDecorView() != null) {
Felipe Leme01297692019-01-29 18:16:23 -08007324 View decorView = getWindow().getDecorView();
7325 decorView.getViewTreeObserver().dispatchOnEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007326 }
7327 }
7328
7329 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007330 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007331 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007332 * Note that changing this value will have no effect on the activity's
7333 * {@link android.content.pm.ActivityInfo} structure; that is, if
7334 * <code>android:immersive</code> is set to <code>true</code>
7335 * in the application's manifest entry for this activity, the {@link
7336 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7337 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7338 * FLAG_IMMERSIVE} bit set.
7339 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007340 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007341 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7342 */
7343 public void setImmersive(boolean i) {
7344 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007345 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007346 } catch (RemoteException e) {
7347 // pass
7348 }
7349 }
7350
Adam Powell6e346362010-07-23 10:18:23 -07007351 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007352 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007353 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007354 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7355 * while this Activity has user focus.</p>
7356 *
7357 * <p>It is recommended that applications additionally declare
7358 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7359 * transitions when switching between VR activities.</p>
7360 *
7361 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7362 * VR mode will not be started. Developers can handle this case as follows:</p>
7363 *
7364 * <pre>
7365 * String servicePackage = "com.whatever.app";
7366 * String serviceClass = "com.whatever.app.MyVrListenerService";
7367 *
7368 * // Name of the component of the VrListenerService to start.
7369 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7370 *
7371 * try {
7372 * setVrModeEnabled(true, myComponentName);
7373 * } catch (PackageManager.NameNotFoundException e) {
7374 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7375 * boolean isInstalled = false;
7376 * for (ApplicationInfo app : installed) {
7377 * if (app.packageName.equals(servicePackage)) {
7378 * isInstalled = true;
7379 * break;
7380 * }
7381 * }
7382 * if (isInstalled) {
7383 * // Package is installed, but not enabled in Settings. Let user enable it.
7384 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7385 * } else {
7386 * // Package is not installed. Send an intent to download this.
7387 * sentIntentToLaunchAppStore(servicePackage);
7388 * }
7389 * }
7390 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007391 *
7392 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007393 * @param requestedComponent the name of the component to use as a
7394 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7395 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007396 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7397 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7398 * not been enabled in user settings.
7399 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007400 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7401 * @see android.service.vr.VrListenerService
7402 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7403 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007404 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007405 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7406 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007407 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007408 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007409 != 0) {
7410 throw new PackageManager.NameNotFoundException(
7411 requestedComponent.flattenToString());
7412 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007413 } catch (RemoteException e) {
7414 // pass
7415 }
7416 }
7417
7418 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007419 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007420 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007421 * @param callback Callback that will manage lifecycle events for this action mode
7422 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007423 *
7424 * @see ActionMode
7425 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007426 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007427 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007428 return mWindow.getDecorView().startActionMode(callback);
7429 }
7430
Adam Powelldebf3be2010-11-15 18:58:48 -08007431 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007432 * Start an action mode of the given type.
7433 *
7434 * @param callback Callback that will manage lifecycle events for this action mode
7435 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7436 * @return The ActionMode that was started, or null if it was canceled
7437 *
7438 * @see ActionMode
7439 */
7440 @Nullable
7441 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7442 return mWindow.getDecorView().startActionMode(callback, type);
7443 }
7444
7445 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007446 * Give the Activity a chance to control the UI for an action mode requested
7447 * by the system.
7448 *
7449 * <p>Note: If you are looking for a notification callback that an action mode
7450 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7451 *
7452 * @param callback The callback that should control the new action mode
7453 * @return The new action mode, or <code>null</code> if the activity does not want to
7454 * provide special handling for this action mode. (It will be handled by the system.)
7455 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007456 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007457 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007458 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007459 // Only Primary ActionModes are represented in the ActionBar.
7460 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7461 initWindowDecorActionBar();
7462 if (mActionBar != null) {
7463 return mActionBar.startActionMode(callback);
7464 }
Adam Powell6e346362010-07-23 10:18:23 -07007465 }
7466 return null;
7467 }
7468
Adam Powelldebf3be2010-11-15 18:58:48 -08007469 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007470 * {@inheritDoc}
7471 */
7472 @Nullable
7473 @Override
7474 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7475 try {
7476 mActionModeTypeStarting = type;
7477 return onWindowStartingActionMode(callback);
7478 } finally {
7479 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7480 }
7481 }
7482
7483 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007484 * Notifies the Activity that an action mode has been started.
7485 * Activity subclasses overriding this method should call the superclass implementation.
7486 *
7487 * @param mode The new action mode.
7488 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007489 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007490 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007491 public void onActionModeStarted(ActionMode mode) {
7492 }
7493
7494 /**
7495 * Notifies the activity that an action mode has finished.
7496 * Activity subclasses overriding this method should call the superclass implementation.
7497 *
7498 * @param mode The action mode that just finished.
7499 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007500 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007501 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007502 public void onActionModeFinished(ActionMode mode) {
7503 }
7504
Adam Powelldd8fab22012-03-22 17:47:27 -07007505 /**
7506 * Returns true if the app should recreate the task when navigating 'up' from this activity
7507 * by using targetIntent.
7508 *
7509 * <p>If this method returns false the app can trivially call
7510 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7511 * up navigation. If this method returns false, the app should synthesize a new task stack
7512 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7513 *
7514 * @param targetIntent An intent representing the target destination for up navigation
7515 * @return true if navigating up should recreate a new task stack, false if the same task
7516 * should be used for the destination
7517 */
7518 public boolean shouldUpRecreateTask(Intent targetIntent) {
7519 try {
7520 PackageManager pm = getPackageManager();
7521 ComponentName cn = targetIntent.getComponent();
7522 if (cn == null) {
7523 cn = targetIntent.resolveActivity(pm);
7524 }
7525 ActivityInfo info = pm.getActivityInfo(cn, 0);
7526 if (info.taskAffinity == null) {
7527 return false;
7528 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007529 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007530 } catch (RemoteException e) {
7531 return false;
7532 } catch (NameNotFoundException e) {
7533 return false;
7534 }
7535 }
7536
7537 /**
7538 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7539 * in the process. If the activity indicated by upIntent already exists in the task's history,
7540 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007541 * finished.
7542 *
7543 * <p>If the indicated activity does not appear in the history stack, this will finish
7544 * each activity in this task until the root activity of the task is reached, resulting in
7545 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7546 * when an activity may be reached by a path not passing through a canonical parent
7547 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007548 *
7549 * <p>This method should be used when performing up navigation from within the same task
7550 * as the destination. If up navigation should cross tasks in some cases, see
7551 * {@link #shouldUpRecreateTask(Intent)}.</p>
7552 *
7553 * @param upIntent An intent representing the target destination for up navigation
7554 *
7555 * @return true if up navigation successfully reached the activity indicated by upIntent and
7556 * upIntent was delivered to it. false if an instance of the indicated activity could
7557 * not be found and this activity was simply finished normally.
7558 */
7559 public boolean navigateUpTo(Intent upIntent) {
7560 if (mParent == null) {
7561 ComponentName destInfo = upIntent.getComponent();
7562 if (destInfo == null) {
7563 destInfo = upIntent.resolveActivity(getPackageManager());
7564 if (destInfo == null) {
7565 return false;
7566 }
7567 upIntent = new Intent(upIntent);
7568 upIntent.setComponent(destInfo);
7569 }
7570 int resultCode;
7571 Intent resultData;
7572 synchronized (this) {
7573 resultCode = mResultCode;
7574 resultData = mResultData;
7575 }
7576 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007577 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007578 }
7579 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007580 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007581 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007582 resultCode, resultData);
7583 } catch (RemoteException e) {
7584 return false;
7585 }
7586 } else {
7587 return mParent.navigateUpToFromChild(this, upIntent);
7588 }
7589 }
7590
7591 /**
7592 * This is called when a child activity of this one calls its
7593 * {@link #navigateUpTo} method. The default implementation simply calls
7594 * navigateUpTo(upIntent) on this activity (the parent).
7595 *
7596 * @param child The activity making the call.
7597 * @param upIntent An intent representing the target destination for up navigation
7598 *
7599 * @return true if up navigation successfully reached the activity indicated by upIntent and
7600 * upIntent was delivered to it. false if an instance of the indicated activity could
7601 * not be found and this activity was simply finished normally.
7602 */
7603 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7604 return navigateUpTo(upIntent);
7605 }
7606
7607 /**
7608 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7609 * this activity's logical parent. The logical parent is named in the application's manifest
7610 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007611 * Activity subclasses may override this method to modify the Intent returned by
7612 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7613 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007614 *
Adam Powell04d58112012-04-09 10:22:12 -07007615 * @return a new Intent targeting the defined parent of this activity or null if
7616 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007617 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007618 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007619 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007620 final String parentName = mActivityInfo.parentActivityName;
7621 if (TextUtils.isEmpty(parentName)) {
7622 return null;
7623 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007624
7625 // If the parent itself has no parent, generate a main activity intent.
7626 final ComponentName target = new ComponentName(this, parentName);
7627 try {
7628 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7629 final String parentActivity = parentInfo.parentActivityName;
7630 final Intent parentIntent = parentActivity == null
7631 ? Intent.makeMainActivity(target)
7632 : new Intent().setComponent(target);
7633 return parentIntent;
7634 } catch (NameNotFoundException e) {
7635 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7636 "' in manifest");
7637 return null;
7638 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007639 }
7640
George Mount31a21722014-03-24 17:44:36 -07007641 /**
George Mount62ab9b72014-05-02 13:51:17 -07007642 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007643 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007644 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007645 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007646 *
George Mount65580562014-08-29 08:15:48 -07007647 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007648 */
George Mount65580562014-08-29 08:15:48 -07007649 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7650 if (callback == null) {
7651 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007652 }
George Mount65580562014-08-29 08:15:48 -07007653 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007654 }
7655
7656 /**
7657 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007658 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007659 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7660 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007661 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007662 *
George Mount65580562014-08-29 08:15:48 -07007663 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007664 */
George Mount65580562014-08-29 08:15:48 -07007665 public void setExitSharedElementCallback(SharedElementCallback callback) {
7666 if (callback == null) {
7667 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007668 }
George Mount65580562014-08-29 08:15:48 -07007669 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007670 }
7671
George Mount8c2614c2014-06-10 11:12:01 -07007672 /**
7673 * Postpone the entering activity transition when Activity was started with
7674 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7675 * android.util.Pair[])}.
7676 * <p>This method gives the Activity the ability to delay starting the entering and
7677 * shared element transitions until all data is loaded. Until then, the Activity won't
7678 * draw into its window, leaving the window transparent. This may also cause the
7679 * returning animation to be delayed until data is ready. This method should be
7680 * called in {@link #onCreate(android.os.Bundle)} or in
7681 * {@link #onActivityReenter(int, android.content.Intent)}.
7682 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7683 * start the transitions. If the Activity did not use
7684 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7685 * android.util.Pair[])}, then this method does nothing.</p>
7686 */
7687 public void postponeEnterTransition() {
7688 mActivityTransitionState.postponeEnterTransition();
7689 }
7690
7691 /**
7692 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7693 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7694 * to have your Activity start drawing.
7695 */
7696 public void startPostponedEnterTransition() {
7697 mActivityTransitionState.startPostponedEnterTransition();
7698 }
7699
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007700 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007701 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7702 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007703 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007704 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7705 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007706 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007707 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7708 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7709 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7710 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007711 }
7712 return null;
7713 }
7714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007716
Mathew Inwood8c854f82018-09-14 12:35:36 +01007717 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 final void setParent(Activity parent) {
7719 mParent = parent;
7720 }
7721
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007722 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007723 final void attach(Context context, ActivityThread aThread,
7724 Instrumentation instr, IBinder token, int ident,
7725 Application application, Intent intent, ActivityInfo info,
7726 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007727 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007728 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Sunny Goyald40c3452019-03-20 12:46:55 -07007729 Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 attachBaseContext(context);
7731
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007732 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007733
Andrii Kulian51c1b672017-04-07 18:39:32 -07007734 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007735 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007737 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007738 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7740 mWindow.setSoftInputMode(info.softInputMode);
7741 }
Adam Powell269248d2011-08-02 10:26:54 -07007742 if (info.uiOptions != 0) {
7743 mWindow.setUiOptions(info.uiOptions);
7744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 mMainThread = aThread;
7748 mInstrumentation = instr;
7749 mToken = token;
Sunny Goyald40c3452019-03-20 12:46:55 -07007750 mAssistToken = assistToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007751 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007752 mApplication = application;
7753 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007754 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 mComponent = intent.getComponent();
7756 mActivityInfo = info;
7757 mTitle = title;
7758 mParent = parent;
7759 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007760 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007761 if (voiceInteractor != null) {
7762 if (lastNonConfigurationInstances != null) {
7763 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7764 } else {
7765 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7766 Looper.myLooper());
7767 }
7768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769
Jeff Brown98365d72012-08-19 20:30:52 -07007770 mWindow.setWindowManager(
7771 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7772 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007773 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 if (mParent != null) {
7775 mWindow.setContainer(mParent.getWindow());
7776 }
7777 mWindowManager = mWindow.getWindowManager();
7778 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007779
7780 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007781
Felipe Lemea4f39cd2019-02-19 15:08:59 -08007782 setAutofillOptions(application.getAutofillOptions());
Felipe Leme326f15a2019-02-19 09:42:24 -08007783 setContentCaptureOptions(application.getContentCaptureOptions());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007784 }
7785
7786 private void enableAutofillCompatibilityIfNeeded() {
7787 if (isAutofillCompatibilityEnabled()) {
7788 final AutofillManager afm = getSystemService(AutofillManager.class);
7789 if (afm != null) {
7790 afm.enableCompatibilityMode();
7791 }
7792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 }
7794
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007795 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007796 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007797 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 return mParent != null ? mParent.getActivityToken() : mToken;
7799 }
7800
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007801 /** @hide */
Sunny Goyald40c3452019-03-20 12:46:55 -07007802 public final IBinder getAssistToken() {
7803 return mParent != null ? mParent.getAssistToken() : mAssistToken;
7804 }
7805
7806 /** @hide */
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007807 @VisibleForTesting
7808 public final ActivityThread getActivityThread() {
7809 return mMainThread;
7810 }
7811
Winson Chung298f95b2017-08-10 15:57:18 -07007812 final void performCreate(Bundle icicle) {
7813 performCreate(icicle, null);
7814 }
7815
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007816 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007817 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007818 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007819 mCanEnterPictureInPicture = true;
7820 restoreHasCurrentPermissionRequest(icicle);
7821 if (persistentState != null) {
7822 onCreate(icicle, persistentState);
7823 } else {
7824 onCreate(icicle);
7825 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007826 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007827 mActivityTransitionState.readState(icicle);
7828
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007829 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7830 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007831 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007832 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007833 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007834 }
George Mount0a778ed2013-12-13 13:35:36 -08007835
Ian Lake0b71b8e2018-09-13 13:56:26 -07007836 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07007837 mCanEnterPictureInPicture = true;
7838 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007839 }
7840
Andrii Kuliand25680c2018-02-21 15:16:58 -08007841 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007842 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07007843 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007844 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007846 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007848 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 if (!mCalled) {
7851 throw new SuperNotCalledException(
7852 "Activity " + mComponent.toShortString() +
7853 " did not call through to super.onStart()");
7854 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007855 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007856 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007857
Elliott Hughese14fa972019-10-02 11:35:53 -07007858 // Warn app developers if the dynamic linker logged anything during startup.
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007859 boolean isAppDebuggable =
7860 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Elliott Hughese14fa972019-10-02 11:35:53 -07007861 if (isAppDebuggable) {
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007862 String dlwarning = getDlWarning();
7863 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007864 String appName = getApplicationInfo().loadLabel(getPackageManager())
7865 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007866 String warning = "Detected problems with app native libraries\n" +
7867 "(please consult log for detail):\n" + dlwarning;
7868 if (isAppDebuggable) {
7869 new AlertDialog.Builder(this).
7870 setTitle(appName).
7871 setMessage(warning).
7872 setPositiveButton(android.R.string.ok, null).
7873 setCancelable(false).
7874 show();
7875 } else {
7876 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7877 }
7878 }
7879 }
7880
Tim Van Pattenddc43912018-12-18 17:47:52 -07007881 GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
7882
George Mount62ab9b72014-05-02 13:51:17 -07007883 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07007884 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 }
RoboErik55011652014-07-09 15:05:53 -07007886
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007887 /**
7888 * Restart the activity.
7889 * @param start Indicates whether the activity should also be started after restart.
7890 * The option to not start immediately is needed in case a transaction with
7891 * multiple lifecycle transitions is in progress.
7892 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007893 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007894 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007895 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007896
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007897 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007898 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007899 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7900 }
7901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 if (mStopped) {
7903 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007904
7905 synchronized (mManagedCursors) {
7906 final int N = mManagedCursors.size();
7907 for (int i=0; i<N; i++) {
7908 ManagedCursor mc = mManagedCursors.get(i);
7909 if (mc.mReleased || mc.mUpdated) {
7910 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007911 if (getApplicationInfo().targetSdkVersion
7912 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7913 throw new IllegalStateException(
7914 "trying to requery an already closed cursor "
7915 + mc.mCursor);
7916 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007917 }
7918 mc.mReleased = false;
7919 mc.mUpdated = false;
7920 }
7921 }
7922 }
7923
7924 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007926 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927 if (!mCalled) {
7928 throw new SuperNotCalledException(
7929 "Activity " + mComponent.toShortString() +
7930 " did not call through to super.onRestart()");
7931 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007932 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007933 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 }
7936 }
RoboErik55011652014-07-09 15:05:53 -07007937
Andrii Kuliand25680c2018-02-21 15:16:58 -08007938 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007939 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007940 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007941
Dianne Hackborn445646c2010-06-25 15:52:59 -07007942 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007943
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007944 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007945
Dake Gu67decfa2017-12-27 11:48:08 -08007946 if (mAutoFillResetNeeded) {
7947 // When Activity is destroyed in paused state, and relaunch activity, there will be
7948 // extra onResume and onPause event, ignore the first onResume and onPause.
7949 // see ActivityThread.handleRelaunchActivity()
7950 mAutoFillIgnoreFirstResumePause = followedByPause;
7951 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7952 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7953 }
7954 }
7955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007957 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007959 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007960 if (!mCalled) {
7961 throw new SuperNotCalledException(
7962 "Activity " + mComponent.toShortString() +
7963 " did not call through to super.onResume()");
7964 }
7965
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007966 // invisible activities must be finished before onResume() completes
7967 if (!mVisibleFromClient && !mFinished) {
7968 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7969 if (getApplicationInfo().targetSdkVersion
7970 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7971 throw new IllegalStateException(
7972 "Activity " + mComponent.toShortString() +
7973 " did not call finish() prior to onResume() completing");
7974 }
7975 }
7976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007979
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007980 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007981 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 onPostResume();
7984 if (!mCalled) {
7985 throw new SuperNotCalledException(
7986 "Activity " + mComponent.toShortString() +
7987 " did not call through to super.onPostResume()");
7988 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007989 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 }
7991
7992 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007993 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007994 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007995 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007996 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007998 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007999 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07008000 if (!mCalled && getApplicationInfo().targetSdkVersion
8001 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
8002 throw new SuperNotCalledException(
8003 "Activity " + mComponent.toShortString() +
8004 " did not call through to super.onPause()");
8005 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008006 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 }
RoboErik55011652014-07-09 15:05:53 -07008008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008009 final void performUserLeaving() {
8010 onUserInteraction();
8011 onUserLeaveHint();
8012 }
RoboErik55011652014-07-09 15:05:53 -07008013
Andrii Kuliand25680c2018-02-21 15:16:58 -08008014 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07008015 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008016 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00008017
Winson Chung298f95b2017-08-10 15:57:18 -07008018 // Disallow entering picture-in-picture after the activity has been stopped
8019 mCanEnterPictureInPicture = false;
8020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07008022 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 if (mWindow != null) {
8024 mWindow.closeAllPanels();
8025 }
8026
Chong Zhang7687f252016-02-26 12:03:33 -08008027 // If we're preserving the window, don't setStoppedState to true, since we
8028 // need the window started immediately again. Stopping the window will
8029 // destroys hardware resources and causes flicker.
8030 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07008031 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08008032 }
RoboErik55011652014-07-09 15:05:53 -07008033
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008034 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07008035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008036 mCalled = false;
8037 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08008038 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008039 if (!mCalled) {
8040 throw new SuperNotCalledException(
8041 "Activity " + mComponent.toShortString() +
8042 " did not call through to super.onStop()");
8043 }
RoboErik55011652014-07-09 15:05:53 -07008044
Makoto Onuki2f6a0182010-02-22 13:26:59 -08008045 synchronized (mManagedCursors) {
8046 final int N = mManagedCursors.size();
8047 for (int i=0; i<N; i++) {
8048 ManagedCursor mc = mManagedCursors.get(i);
8049 if (!mc.mReleased) {
8050 mc.mCursor.deactivate();
8051 mc.mReleased = true;
8052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008053 }
8054 }
George Mount0a778ed2013-12-13 13:35:36 -08008055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07008057 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 }
8059 mResumed = false;
8060 }
8061
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008062 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07008063 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07008064 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07008065 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008066 mFragments.dispatchDestroy();
8067 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08008068 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008069 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07008070 if (mVoiceInteractor != null) {
8071 mVoiceInteractor.detachActivity();
8072 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008073 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008074 }
George Mount0a778ed2013-12-13 13:35:36 -08008075
Winson Chung5af42fc2017-03-24 17:11:33 -07008076 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
8077 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008078 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008079 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
8080 + " " + newConfig);
8081 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008082 if (mWindow != null) {
8083 mWindow.onMultiWindowModeChanged();
8084 }
Hongwei Wangf0b5fb12019-07-25 15:14:47 -07008085 mLastDispatchedIsInMultiWindowMode = isInMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -07008086 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008087 }
8088
Winson Chung5af42fc2017-03-24 17:11:33 -07008089 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
8090 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008091 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008092 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
8093 + " " + newConfig);
8094 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07008095 if (mWindow != null) {
8096 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
8097 }
Hongwei Wangf0b5fb12019-07-25 15:14:47 -07008098 mLastDispatchedIsInPictureInPictureMode = isInPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -07008099 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008100 }
8101
George Mount0a778ed2013-12-13 13:35:36 -08008102 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06008103 * @hide
8104 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008105 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06008106 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 return mResumed;
8108 }
8109
Svetoslavffb32b12015-10-15 16:54:00 -07008110 private void storeHasCurrentPermissionRequest(Bundle bundle) {
8111 if (bundle != null && mHasCurrentPermissionsRequest) {
8112 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
8113 }
8114 }
8115
8116 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
8117 if (bundle != null) {
8118 mHasCurrentPermissionsRequest = bundle.getBoolean(
8119 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
8120 }
8121 }
8122
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008123 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07008124 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
8125 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07008126 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
8128 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07008129 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008131 onActivityResult(requestCode, resultCode, data);
8132 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
8133 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
8134 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08008135 dispatchRequestPermissionsResult(requestCode, data);
8136 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00008137 Fragment frag = mFragments.findFragmentByWho(who);
8138 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008139 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08008140 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07008141 }
Svetoslav970b59c2015-06-09 16:05:21 -07008142 } else if (who.startsWith("@android:view:")) {
8143 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
8144 getActivityToken());
8145 for (ViewRootImpl viewRoot : views) {
8146 if (viewRoot.getView() != null
8147 && viewRoot.getView().dispatchActivityResult(
8148 who, requestCode, resultCode, data)) {
8149 return;
8150 }
8151 }
Svet Ganov782043c2017-02-11 00:52:02 +00008152 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08008153 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08008154 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07008155 } else {
8156 Fragment frag = mFragments.findFragmentByWho(who);
8157 if (frag != null) {
8158 frag.onActivityResult(requestCode, resultCode, data);
8159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07008161 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07008163
Jason Monka57e5e02014-05-07 10:06:48 -04008164 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008165 * Request to put this activity in a mode where the user is locked to a restricted set of
8166 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04008167 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008168 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
8169 * for this component, the current task will be launched directly into LockTask mode. Only apps
8170 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
8171 * be launched while LockTask mode is active. The user will not be able to leave this mode
8172 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
8173 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04008174 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008175 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
8176 * system will prompt the user with a dialog requesting permission to use this mode.
8177 * The user can exit at any time through instructions shown on the request dialog. Calling
8178 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04008179 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008180 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
8181 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008182 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008183 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008184 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04008185 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008186 public void startLockTask() {
8187 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008188 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008189 } catch (RemoteException e) {
8190 }
8191 }
8192
Jason Monka57e5e02014-05-07 10:06:48 -04008193 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008194 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04008195 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008196 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
8197 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04008198 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008199 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
8200 * by this activity, then calling this method will not terminate the LockTask mode, but only
8201 * finish its own task. The device will remain in LockTask mode, until the activity which
8202 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
8203 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008204 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008205 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008206 * @see android.R.attr#lockTaskMode
8207 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04008208 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008209 public void stopLockTask() {
8210 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008211 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008212 } catch (RemoteException e) {
8213 }
8214 }
8215
Craig Mautner5eda9b32013-07-02 11:58:16 -07008216 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008217 * Shows the user the system defined message for telling the user how to exit
8218 * lock task mode. The task containing this activity must be in lock task mode at the time
8219 * of this call for the message to be displayed.
8220 */
8221 public void showLockTaskEscapeMessage() {
8222 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008223 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008224 } catch (RemoteException e) {
8225 }
8226 }
8227
8228 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07008229 * Check whether the caption on freeform windows is displayed directly on the content.
8230 *
8231 * @return True if caption is displayed on content, false if it pushes the content down.
8232 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08008233 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07008234 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07008235 */
8236 public boolean isOverlayWithDecorCaptionEnabled() {
8237 return mWindow.isOverlayWithDecorCaptionEnabled();
8238 }
8239
8240 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008241 * Set whether the caption should displayed directly on the content rather than push it down.
8242 *
8243 * This affects only freeform windows since they display the caption and only the main
8244 * window of the activity. The caption is used to drag the window around and also shows
8245 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008246 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008247 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008248 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8249 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008250 }
8251
8252 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008253 * Interface for informing a translucent {@link Activity} once all visible activities below it
8254 * have completed drawing. This is necessary only after an {@link Activity} has been made
8255 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8256 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008257 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8258 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008259 *
8260 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008261 */
Jose Lima14914852014-08-14 09:14:12 -07008262 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008263 public interface TranslucentConversionListener {
8264 /**
8265 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8266 * below the top one have been redrawn. Following this callback it is safe to make the top
8267 * Activity translucent because the underlying Activity has been drawn.
8268 *
8269 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8270 * occurred waiting for the Activity to complete drawing.
8271 *
8272 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008273 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008274 */
8275 public void onTranslucentConversionComplete(boolean drawComplete);
8276 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008277
8278 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008279 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008280 // If the package installer crashed we may have not data - best effort.
8281 String[] permissions = (data != null) ? data.getStringArrayExtra(
8282 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8283 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8284 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008285 onRequestPermissionsResult(requestCode, permissions, grantResults);
8286 }
8287
8288 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008289 Fragment fragment) {
8290 // If the package installer crashed we may have not data - best effort.
8291 String[] permissions = (data != null) ? data.getStringArrayExtra(
8292 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8293 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8294 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8295 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008296 }
8297
Svet Ganov782043c2017-02-11 00:52:02 +00008298 /** @hide */
8299 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008300 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008301 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00008302 try {
8303 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008304 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008305 } catch (IntentSender.SendIntentException e) {
8306 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8307 }
8308 }
8309
Svet Ganov17db9dc2017-02-21 19:54:31 -08008310 /** @hide */
8311 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008312 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008313 mAutoFillResetNeeded = true;
8314 }
8315
Felipe Leme4753bb02017-03-22 20:24:00 -07008316 /** @hide */
8317 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008318 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008319 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008320 final boolean wasShowing;
8321
8322 if (mAutofillPopupWindow == null) {
8323 wasShowing = false;
8324 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8325 } else {
8326 wasShowing = mAutofillPopupWindow.isShowing();
8327 }
Svet Ganov374cae12017-05-10 13:42:33 -07008328 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008329
8330 return !wasShowing && mAutofillPopupWindow.isShowing();
8331 }
8332
8333 /** @hide */
8334 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008335 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8336 @NonNull KeyEvent keyEvent) {
8337 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8338 if (rootImpl != null) {
8339 // dont care if anchorView is current focus, for example a custom view may only receive
8340 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8341 // might be inside parent of the custom view.
8342 rootImpl.dispatchKeyFromAutofill(keyEvent);
8343 }
8344 }
8345
8346 /** @hide */
8347 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008348 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008349 if (mAutofillPopupWindow == null) {
8350 return false;
8351 }
8352 mAutofillPopupWindow.dismiss();
8353 mAutofillPopupWindow = null;
8354 return true;
8355 }
8356
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008357 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008358 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008359 public final boolean autofillClientIsFillUiShowing() {
8360 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8361 }
8362
8363 /** @hide */
8364 @Override
8365 @NonNull
8366 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8367 @NonNull AutofillId[] autofillId) {
8368 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008369 final ArrayList<ViewRootImpl> roots =
8370 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8371
8372 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8373 final View rootView = roots.get(rootNum).getView();
8374
8375 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008376 final int viewCount = autofillId.length;
8377 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008378 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008379 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008380 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008381 }
8382 }
8383 }
8384 }
8385
8386 return views;
8387 }
8388
8389 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008390 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008391 @Nullable
8392 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008393 final ArrayList<ViewRootImpl> roots =
8394 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8395 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8396 final View rootView = roots.get(rootNum).getView();
8397
8398 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008399 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008400 if (view != null) {
8401 return view;
8402 }
8403 }
8404 }
8405
8406 return null;
8407 }
8408
8409 /** @hide */
8410 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008411 public final @NonNull boolean[] autofillClientGetViewVisibility(
8412 @NonNull AutofillId[] autofillIds) {
8413 final int autofillIdCount = autofillIds.length;
8414 final boolean[] visible = new boolean[autofillIdCount];
8415 for (int i = 0; i < autofillIdCount; i++) {
8416 final AutofillId autofillId = autofillIds[i];
8417 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8418 if (view != null) {
Felipe Lemea75333c2019-01-22 15:45:32 -08008419 if (!autofillId.isVirtualInt()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008420 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008421 } else {
Felipe Lemea75333c2019-01-22 15:45:32 -08008422 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildIntId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008423 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008424 }
8425 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008426 if (android.view.autofill.Helper.sVerbose) {
8427 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8428 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008429 return visible;
8430 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008431
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008432 /** @hide */
8433 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8434 int windowId) {
8435 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8436 .getRootViews(getActivityToken());
8437 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8438 final View rootView = roots.get(rootNum).getView();
8439 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8440 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8441 if (view != null) {
8442 return view;
8443 }
8444 }
8445 }
8446 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008447 }
8448
8449 /** @hide */
8450 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008451 public final @Nullable IBinder autofillClientGetActivityToken() {
8452 return getActivityToken();
8453 }
8454
8455 /** @hide */
8456 @Override
8457 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008458 return !mStopped;
8459 }
8460
Dake Gu67decfa2017-12-27 11:48:08 -08008461 /** @hide */
8462 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008463 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008464 return isAutofillCompatibilityEnabled();
8465 }
8466
8467 /** @hide */
8468 @Override
8469 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008470 return mAutoFillIgnoreFirstResumePause || !mResumed;
8471 }
8472
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008473 /**
8474 * If set to true, this indicates to the system that it should never take a
8475 * screenshot of the activity to be used as a representation while it is not in a started state.
8476 * <p>
8477 * Note that the system may use the window background of the theme instead to represent
8478 * the window when it is not running.
8479 * <p>
8480 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8481 * this only affects the behavior when the activity's screenshot would be used as a
8482 * representation when the activity is not in a started state, i.e. in Overview. The system may
8483 * still take screenshots of the activity in other contexts; for example, when the user takes a
8484 * screenshot of the entire screen, or when the active
8485 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8486 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8487 *
8488 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8489 * @hide
8490 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008491 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008492 public void setDisablePreviewScreenshots(boolean disable) {
8493 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008494 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008495 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008496 throw e.rethrowFromSystemServer();
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008497 }
8498 }
8499
chaviw59b98852017-06-13 12:05:44 -07008500 /**
koprivaa1a78482018-10-09 10:09:23 -07008501 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008502 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8503 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8504 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8505 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8506 *
8507 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8508 * {@code false} otherwise.
8509 * @see #setTurnScreenOn(boolean)
8510 * @see android.R.attr#turnScreenOn
8511 * @see android.R.attr#showWhenLocked
8512 */
8513 public void setShowWhenLocked(boolean showWhenLocked) {
8514 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008515 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008516 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008517 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008518 }
8519 }
8520
8521 /**
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008522 * Specifies whether this {@link Activity} should be shown on top of the lock screen whenever
8523 * the lockscreen is up and this activity has another activity behind it with the showWhenLock
8524 * attribute set. That is, this activity is only visible on the lock screen if there is another
8525 * activity with the showWhenLock attribute visible at the same time on the lock screen. A use
8526 * case for this is permission dialogs, that should only be visible on the lock screen if their
8527 * requesting activity is also visible. This value can be set as a manifest attribute using
8528 * android.R.attr#inheritShowWhenLocked.
8529 *
8530 * @param inheritShowWhenLocked {@code true} to show the {@link Activity} on top of the lock
8531 * screen when this activity has another activity behind it with
8532 * the showWhenLock attribute set; {@code false} otherwise.
8533 * @see #setShowWhenLocked(boolean)
Issei Suzuki7c09f0b2019-02-28 14:45:50 +01008534 * @see android.R.attr#inheritShowWhenLocked
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008535 */
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008536 public void setInheritShowWhenLocked(boolean inheritShowWhenLocked) {
8537 try {
8538 ActivityTaskManager.getService().setInheritShowWhenLocked(
8539 mToken, inheritShowWhenLocked);
8540 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008541 throw e.rethrowFromSystemServer();
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008542 }
8543 }
8544
8545 /**
chaviw59b98852017-06-13 12:05:44 -07008546 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8547 * Normally an activity will be transitioned to the stopped state if it is started while the
8548 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8549 * activity will be visible and resumed due to the screen coming on. The screen will not be
8550 * turned on if the activity won't be visible after the screen is turned on. This flag is
8551 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8552 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8553 * if this flag is set and the activity calls {@link
8554 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8555 * the screen will turn on.
8556 *
8557 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8558 *
8559 * @see #setShowWhenLocked(boolean)
8560 * @see android.R.attr#turnScreenOn
8561 * @see android.R.attr#showWhenLocked
8562 */
8563 public void setTurnScreenOn(boolean turnScreenOn) {
8564 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008565 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008566 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008567 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008568 }
8569 }
8570
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008571 /**
8572 * Registers remote animations per transition type for this activity.
8573 *
8574 * @param definition The remote animation definition that defines which transition whould run
8575 * which remote animation.
8576 * @hide
8577 */
8578 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008579 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008580 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8581 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008582 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008583 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008584 throw e.rethrowFromSystemServer();
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008585 }
8586 }
8587
Andrii Kuliand25680c2018-02-21 15:16:58 -08008588 /** Log a lifecycle event for current user id and component class. */
8589 private void writeEventLog(int event, String reason) {
8590 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
8591 reason);
8592 }
8593
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008594 class HostCallbacks extends FragmentHostCallback<Activity> {
8595 public HostCallbacks() {
8596 super(Activity.this /*activity*/);
8597 }
8598
8599 @Override
8600 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8601 Activity.this.dump(prefix, fd, writer, args);
8602 }
8603
8604 @Override
8605 public boolean onShouldSaveFragmentState(Fragment fragment) {
8606 return !isFinishing();
8607 }
8608
8609 @Override
8610 public LayoutInflater onGetLayoutInflater() {
8611 final LayoutInflater result = Activity.this.getLayoutInflater();
8612 if (onUseFragmentManagerInflaterFactory()) {
8613 return result.cloneInContext(Activity.this);
8614 }
8615 return result;
8616 }
8617
8618 @Override
8619 public boolean onUseFragmentManagerInflaterFactory() {
8620 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8621 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8622 }
8623
8624 @Override
8625 public Activity onGetHost() {
8626 return Activity.this;
8627 }
8628
8629 @Override
8630 public void onInvalidateOptionsMenu() {
8631 Activity.this.invalidateOptionsMenu();
8632 }
8633
8634 @Override
8635 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8636 Bundle options) {
8637 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8638 }
8639
8640 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008641 public void onStartActivityAsUserFromFragment(
8642 Fragment fragment, Intent intent, int requestCode, Bundle options,
8643 UserHandle user) {
8644 Activity.this.startActivityAsUserFromFragment(
8645 fragment, intent, requestCode, options, user);
8646 }
8647
8648 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008649 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8650 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8651 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8652 if (mParent == null) {
8653 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8654 flagsMask, flagsValues, options);
8655 } else if (options != null) {
8656 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
8657 fillInIntent, flagsMask, flagsValues, extraFlags, options);
8658 }
8659 }
8660
8661 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008662 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8663 int requestCode) {
8664 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8665 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8666 startActivityForResult(who, intent, requestCode, null);
8667 }
8668
8669 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008670 public boolean onHasWindowAnimations() {
8671 return getWindow() != null;
8672 }
8673
8674 @Override
8675 public int onGetWindowAnimations() {
8676 final Window w = getWindow();
8677 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8678 }
8679
Todd Kennedy434bd652015-05-04 12:29:50 -07008680 @Override
8681 public void onAttachFragment(Fragment fragment) {
8682 Activity.this.onAttachFragment(fragment);
8683 }
8684
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008685 @Nullable
8686 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008687 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008688 return Activity.this.findViewById(id);
8689 }
8690
8691 @Override
8692 public boolean onHasView() {
8693 final Window w = getWindow();
8694 return (w != null && w.peekDecorView() != null);
8695 }
8696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697}