blob: 6480a6abeb785e94139e604d3ae7c365671fa89c [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;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070036import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040037import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070038import android.app.assist.AssistContent;
Artur Satayevc895b1b2019-12-10 17:47:51 +000039import android.compat.annotation.UnsupportedAppUsage;
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;
Yuliya Kamatkova48c38072020-01-09 15:43:25 -080048import android.content.LocusId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.SharedPreferences;
50import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080051import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070052import android.content.pm.PackageManager;
53import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.res.Configuration;
55import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070056import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.database.Cursor;
58import android.graphics.Bitmap;
59import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080060import android.graphics.Color;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070061import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.graphics.drawable.Drawable;
Winson Chungd6722032020-02-18 15:16:08 -080063import android.graphics.drawable.Icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070065import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070067import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070068import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Bundle;
Sunny Goyald40c3452019-03-20 12:46:55 -070070import android.os.CancellationSignal;
Tim Van Pattenddc43912018-12-18 17:47:52 -070071import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Handler;
73import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080074import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070075import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000076import android.os.PersistableBundle;
Nicholas Sauer28348ed742019-02-15 11:58:32 -080077import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070078import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060079import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080080import android.os.StrictMode;
Felipe Lemeb337e1c2019-01-18 13:06:17 -080081import android.os.Trace;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070082import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.text.Selection;
84import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070085import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000087import android.transition.Scene;
88import android.transition.TransitionManager;
89import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.util.EventLog;
92import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070093import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070094import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000096import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070097import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070099import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700101import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +0000102import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +0100104import android.view.KeyboardShortcutGroup;
105import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.LayoutInflater;
107import android.view.Menu;
108import android.view.MenuInflater;
109import android.view.MenuItem;
110import android.view.MotionEvent;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100111import android.view.RemoteAnimationDefinition;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700112import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700114import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700116import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000118import android.view.ViewRootImpl;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700119import android.view.ViewRootImpl.ActivityConfigCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000121import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700123import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700124import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov24c90452017-12-27 15:17:14 -0800125import android.view.autofill.AutofillId;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700126import android.view.autofill.AutofillManager;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700127import android.view.autofill.AutofillManager.AutofillClient;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700128import android.view.autofill.AutofillPopupWindow;
129import android.view.autofill.IAutofillWindowPresenter;
Yuliya Kamatkovab5d87a82020-02-13 16:00:52 -0800130import android.view.contentcapture.ContentCaptureContext;
Felipe Leme749b8892018-12-03 16:30:30 -0800131import android.view.contentcapture.ContentCaptureManager;
Felipe Lemecbf7f262019-04-17 13:57:59 -0700132import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800134import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000135import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100136
Adrian Roos4c864592019-04-10 14:47:57 +0200137import com.android.internal.R;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700138import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800139import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700140import com.android.internal.app.IVoiceInteractor;
141import com.android.internal.app.ToolbarActionBar;
142import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700143import com.android.internal.policy.PhoneWindow;
144
Mathieu Chartier63e14c12019-03-07 09:05:44 -0800145import dalvik.system.VMRuntime;
146
Dianne Hackborn625ac272010-09-17 18:29:22 -0700147import java.io.FileDescriptor;
148import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700149import java.lang.annotation.Retention;
150import java.lang.annotation.RetentionPolicy;
Mark Renouf1c25fc52019-09-09 13:13:43 -0400151import java.lang.ref.WeakReference;
Adam Powell6e346362010-07-23 10:18:23 -0700152import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800153import java.util.Arrays;
Sunny Goyald40c3452019-03-20 12:46:55 -0700154import java.util.Collections;
Adam Powell6e346362010-07-23 10:18:23 -0700155import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700156import java.util.List;
Sunny Goyald40c3452019-03-20 12:46:55 -0700157import java.util.concurrent.Executor;
158import java.util.function.Consumer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Bryce Leee83f34cd2017-10-31 19:50:54 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161/**
162 * An activity is a single, focused thing that the user can do. Almost all
163 * activities interact with the user, so the Activity class takes care of
164 * creating a window for you in which you can place your UI with
165 * {@link #setContentView}. While activities are often presented to the user
166 * as full-screen windows, they can also be used in other ways: as floating
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700167 * windows (via a theme with {@link android.R.attr#windowIsFloating} set),
168 * <a href="https://developer.android.com/guide/topics/ui/multi-window">
169 * Multi-Window mode</a> or embedded into other windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 *
171 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <ul>
174 * <li> {@link #onCreate} is where you initialize your activity. Most
175 * importantly, here you will usually call {@link #setContentView(int)}
176 * with a layout resource defining your UI, and using {@link #findViewById}
177 * to retrieve the widgets in that UI that you need to interact with
178 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700179 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700180 * <li> {@link #onPause} is where you deal with the user pausing active
181 * interaction with the activity. Any changes made by the user should at
182 * this point be committed (usually to the
183 * {@link android.content.ContentProvider} holding the data). In this
184 * state the activity is still visible on screen.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * </ul>
186 *
187 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
188 * activity classes must have a corresponding
189 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
190 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <p>Topics covered here:
193 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700194 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
196 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
197 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
198 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
199 * <li><a href="#Permissions">Permissions</a>
200 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
201 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700202 *
203 * <div class="special reference">
204 * <h3>Developer Guides</h3>
205 * <p>The Activity class is an important part of an application's overall lifecycle,
206 * and the way activities are launched and put together is a fundamental
207 * part of the platform's application model. For a detailed perspective on the structure of an
208 * Android application and how activities behave, please read the
209 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700210 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700211 * developer guides.</p>
212 *
213 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700214 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700215 * developer guide.</p>
216 * </div>
217 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700218 * <a name="Fragments"></a>
219 * <h3>Fragments</h3>
220 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800221 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
222 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700223 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800224 * screens, and help scale their application between small and large screens.</p>
225 *
226 * <p>For more information about using fragments, read the
227 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <a name="ActivityLifecycle"></a>
230 * <h3>Activity Lifecycle</h3>
231 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700232 * <p>Activities in the system are managed as
233 * <a href="https://developer.android.com/guide/components/activities/tasks-and-back-stack">
234 * activity stacks</a>. When a new activity is started, it is usually placed on the top of the
235 * current stack and becomes the running activity -- the previous activity always remains
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * below it in the stack, and will not come to the foreground again until
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700237 * the new activity exits. There can be one or multiple activity stacks visible
238 * on screen.</p>
RoboErik55011652014-07-09 15:05:53 -0700239 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * <p>An activity has essentially four states:</p>
241 * <ul>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700242 * <li>If an activity is in the foreground of the screen (at the highest position of the topmost
243 * stack), it is <em>active</em> or <em>running</em>. This is usually the activity that the
244 * user is currently interacting with.</li>
245 * <li>If an activity has lost focus but is still presented to the user, it is <em>visible</em>.
246 * It is possible if a new non-full-sized or transparent activity has focus on top of your
247 * activity, another activity has higher position in multi-window mode, or the activity
248 * itself is not focusable in current windowing mode. Such activity is completely alive (it
249 * maintains all state and member information and remains attached to the window manager).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * <li>If an activity is completely obscured by another activity,
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700251 * it is <em>stopped</em> or <em>hidden</em>. It still retains all state and member
252 * information, however, it is no longer visible to the user so its window is hidden
253 * and it will often be killed by the system when memory is needed elsewhere.</li>
254 * <li>The system can drop the activity from memory by either asking it to finish,
255 * or simply killing its process, making it <em>destroyed</em>. When it is displayed again
256 * 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 -0800257 * </ul>
258 *
259 * <p>The following diagram shows the important state paths of an Activity.
260 * The square rectangles represent callback methods you can implement to
261 * perform operations when the Activity moves between states. The colored
262 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * <p><img src="../../../images/activity_lifecycle.png"
265 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * <p>There are three key loops you may be interested in monitoring within your
268 * activity:
RoboErik55011652014-07-09 15:05:53 -0700269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * <ul>
271 * <li>The <b>entire lifetime</b> of an activity happens between the first call
272 * to {@link android.app.Activity#onCreate} through to a single final call
273 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
274 * of "global" state in onCreate(), and release all remaining resources in
275 * onDestroy(). For example, if it has a thread running in the background
276 * to download data from the network, it may create that thread in onCreate()
277 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * <li>The <b>visible lifetime</b> of an activity happens between a call to
280 * {@link android.app.Activity#onStart} until a corresponding call to
281 * {@link android.app.Activity#onStop}. During this time the user can see the
282 * activity on-screen, though it may not be in the foreground and interacting
283 * with the user. Between these two methods you can maintain resources that
284 * are needed to show the activity to the user. For example, you can register
285 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900286 * that impact your UI, and unregister it in onStop() when the user no
287 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * can be called multiple times, as the activity becomes visible and hidden
289 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
292 * {@link android.app.Activity#onResume} until a corresponding call to
293 * {@link android.app.Activity#onPause}. During this time the activity is
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700294 * in visible, active and interacting with the user. An activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 * can frequently go between the resumed and paused states -- for example when
296 * the device goes to sleep, when an activity result is delivered, when a new
297 * intent is delivered -- so the code in these methods should be fairly
298 * lightweight.
299 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700300 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 * <p>The entire lifecycle of an activity is defined by the following
302 * Activity methods. All of these are hooks that you can override
303 * to do appropriate work when the activity changes state. All
304 * activities will implement {@link android.app.Activity#onCreate}
305 * to do their initial setup; many will also implement
306 * {@link android.app.Activity#onPause} to commit changes to data and
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700307 * prepare to pause interacting with the user, and {@link android.app.Activity#onStop}
308 * to handle no longer being visible on screen. You should always
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 * call up to your superclass when implementing these methods.</p>
310 *
311 * </p>
312 * <pre class="prettyprint">
313 * public class Activity extends ApplicationContext {
314 * protected void onCreate(Bundle savedInstanceState);
315 *
316 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * protected void onRestart();
319 *
320 * protected void onResume();
321 *
322 * protected void onPause();
323 *
324 * protected void onStop();
325 *
326 * protected void onDestroy();
327 * }
328 * </pre>
329 *
330 * <p>In general the movement through an activity's lifecycle looks like
331 * this:</p>
332 *
333 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
334 * <colgroup align="left" span="3" />
335 * <colgroup align="left" />
336 * <colgroup align="center" />
337 * <colgroup align="center" />
338 *
339 * <thead>
340 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
341 * </thead>
342 *
343 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700344 * <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 -0800345 * <td>Called when the activity is first created.
346 * This is where you should do all of your normal static set up:
347 * create views, bind data to lists, etc. This method also
348 * provides you with a Bundle containing the activity's previously
349 * frozen state, if there was one.
350 * <p>Always followed by <code>onStart()</code>.</td>
351 * <td align="center">No</td>
352 * <td align="center"><code>onStart()</code></td>
353 * </tr>
354 *
355 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700356 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * <td>Called after your activity has been stopped, prior to it being
358 * started again.
359 * <p>Always followed by <code>onStart()</code></td>
360 * <td align="center">No</td>
361 * <td align="center"><code>onStart()</code></td>
362 * </tr>
363 *
smain@google.com10e29b82016-06-23 12:09:30 -0700364 * <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 -0800365 * <td>Called when the activity is becoming visible to the user.
366 * <p>Followed by <code>onResume()</code> if the activity comes
367 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
368 * <td align="center">No</td>
369 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
370 * </tr>
371 *
372 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700373 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * <td>Called when the activity will start
375 * interacting with the user. At this point your activity is at
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700376 * the top of its activity stack, with user input going to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * <p>Always followed by <code>onPause()</code>.</td>
378 * <td align="center">No</td>
379 * <td align="center"><code>onPause()</code></td>
380 * </tr>
381 *
smain@google.com10e29b82016-06-23 12:09:30 -0700382 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700383 * <td>Called when the activity loses foreground state, is no longer focusable or before
384 * transition to stopped/hidden or destroyed state. The activity is still visible to
385 * user, so it's recommended to keep it visually active and continue updating the UI.
386 * Implementations of this method must be very quick because
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * the next activity will not be resumed until this method returns.
388 * <p>Followed by either <code>onResume()</code> if the activity
389 * returns back to the front, or <code>onStop()</code> if it becomes
390 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800391 * <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 -0800392 * <td align="center"><code>onResume()</code> or<br>
393 * <code>onStop()</code></td>
394 * </tr>
395 *
smain@google.com10e29b82016-06-23 12:09:30 -0700396 * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</td>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700397 * <td>Called when the activity is no longer visible to the user. This may happen either
398 * because a new activity is being started on top, an existing one is being brought in
399 * front of this one, or this one is being destroyed. This is typically used to stop
400 * animations and refreshing the UI, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * <p>Followed by either <code>onRestart()</code> if
402 * this activity is coming back to interact with the user, or
403 * <code>onDestroy()</code> if this activity is going away.</td>
404 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
405 * <td align="center"><code>onRestart()</code> or<br>
406 * <code>onDestroy()</code></td>
407 * </tr>
408 *
smain@google.com10e29b82016-06-23 12:09:30 -0700409 * <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 -0800410 * <td>The final call you receive before your
411 * activity is destroyed. This can happen either because the
412 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700413 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * instance of the activity to save space. You can distinguish
415 * between these two scenarios with the {@link
416 * Activity#isFinishing} method.</td>
417 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
418 * <td align="center"><em>nothing</em></td>
419 * </tr>
420 * </tbody>
421 * </table>
422 *
423 * <p>Note the "Killable" column in the above table -- for those methods that
424 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800425 * 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 -0800426 * of its code being executed. Because of this, you should use the
427 * {@link #onPause} method to write any persistent data (such as user edits)
428 * to storage. In addition, the method
429 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
430 * in such a background state, allowing you to save away any dynamic instance
431 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700432 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
434 * section for more information on how the lifecycle of a process is tied
435 * to the activities it is hosting. Note that it is important to save
436 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800437 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * be called in every situation as described in its documentation.</p>
439 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800440 * <p class="note">Be aware that these semantics will change slightly between
441 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
442 * vs. those targeting prior platforms. Starting with Honeycomb, an application
443 * is not in the killable state until its {@link #onStop} has returned. This
444 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700445 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800446 * wait until {@link #onStop()} to save persistent state.</p>
447 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800448 * <p class="note">For applications targeting platforms starting with
449 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
450 * will always be called after {@link #onStop}, so an application may safely
451 * perform fragment transactions in {@link #onStop} and will be able to save
452 * persistent state later.</p>
453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * <p>For those methods that are not marked as being killable, the activity's
455 * process will not be killed by the system starting from the time the method
456 * is called and continuing after it returns. Thus an activity is in the killable
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700457 * state, for example, between after <code>onStop()</code> to the start of
458 * <code>onResume()</code>. Keep in mind that under extreme memory pressure the
459 * system can kill the application process at any time.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 *
461 * <a name="ConfigurationChanges"></a>
462 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 * <p>If the configuration of the device (as defined by the
465 * {@link Configuration Resources.Configuration} class) changes,
466 * then anything displaying a user interface will need to update to match that
467 * configuration. Because Activity is the primary mechanism for interacting
468 * with the user, it includes special support for handling configuration
469 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700470 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 * <p>Unless you specify otherwise, a configuration change (such as a change
472 * in screen orientation, language, input devices, etc) will cause your
473 * current activity to be <em>destroyed</em>, going through the normal activity
474 * lifecycle process of {@link #onPause},
475 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
476 * had been in the foreground or visible to the user, once {@link #onDestroy} is
477 * called in that instance then a new instance of the activity will be
478 * created, with whatever savedInstanceState the previous instance had generated
479 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700480 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * <p>This is done because any application resource,
482 * including layout files, can change based on any configuration value. Thus
483 * the only safe way to handle a configuration change is to re-retrieve all
484 * resources, including layouts, drawables, and strings. Because activities
485 * must already know how to save their state and re-create themselves from
486 * that state, this is a convenient way to have an activity restart itself
487 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 * <p>In some special cases, you may want to bypass restarting of your
490 * activity based on one or more types of configuration changes. This is
491 * done with the {@link android.R.attr#configChanges android:configChanges}
492 * attribute in its manifest. For any types of configuration changes you say
493 * that you handle there, you will receive a call to your current activity's
494 * {@link #onConfigurationChanged} method instead of being restarted. If
495 * a configuration change involves any that you do not handle, however, the
496 * activity will still be restarted and {@link #onConfigurationChanged}
497 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700498 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 * <a name="StartingActivities"></a>
500 * <h3>Starting Activities and Getting Results</h3>
501 *
502 * <p>The {@link android.app.Activity#startActivity}
503 * method is used to start a
504 * new activity, which will be placed at the top of the activity stack. It
505 * takes a single argument, an {@link android.content.Intent Intent},
506 * which describes the activity
507 * to be executed.</p>
508 *
509 * <p>Sometimes you want to get a result back from an activity when it
510 * ends. For example, you may start an activity that lets the user pick
511 * a person in a list of contacts; when it ends, it returns the person
512 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700513 * {@link android.app.Activity#startActivityForResult(Intent, int)}
514 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700516 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 *
518 * <p>When an activity exits, it can call
519 * {@link android.app.Activity#setResult(int)}
520 * to return data back to its parent. It must always supply a result code,
521 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
522 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
523 * return back an Intent containing any additional data it wants. All of this
524 * information appears back on the
525 * parent's <code>Activity.onActivityResult()</code>, along with the integer
526 * identifier it originally supplied.</p>
527 *
528 * <p>If a child activity fails for any reason (such as crashing), the parent
529 * activity will receive a result with the code RESULT_CANCELED.</p>
530 *
531 * <pre class="prettyprint">
532 * public class MyActivity extends Activity {
533 * ...
534 *
535 * static final int PICK_CONTACT_REQUEST = 0;
536 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700537 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
539 * // When the user center presses, let them pick a contact.
540 * startActivityForResult(
541 * new Intent(Intent.ACTION_PICK,
542 * new Uri("content://contacts")),
543 * PICK_CONTACT_REQUEST);
544 * return true;
545 * }
546 * return false;
547 * }
548 *
549 * protected void onActivityResult(int requestCode, int resultCode,
550 * Intent data) {
551 * if (requestCode == PICK_CONTACT_REQUEST) {
552 * if (resultCode == RESULT_OK) {
553 * // A contact was picked. Here we will just display it
554 * // to the user.
555 * startActivity(new Intent(Intent.ACTION_VIEW, data));
556 * }
557 * }
558 * }
559 * }
560 * </pre>
561 *
562 * <a name="SavingPersistentState"></a>
563 * <h3>Saving Persistent State</h3>
564 *
koprivafc3949e2018-09-17 11:35:13 -0700565 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 * will deal with: shared document-like data (typically stored in a SQLite
567 * database using a {@linkplain android.content.ContentProvider content provider})
568 * and internal state such as user preferences.</p>
569 *
koprivafc3949e2018-09-17 11:35:13 -0700570 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * "edit in place" user model. That is, any edits a user makes are effectively
572 * made immediately without requiring an additional confirmation step.
573 * Supporting this model is generally a simple matter of following two rules:</p>
574 *
575 * <ul>
576 * <li> <p>When creating a new document, the backing database entry or file for
577 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700578 * 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 -0800579 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700580 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
582 * commit to the backing content provider or file any changes the user
583 * has made. This ensures that those changes will be seen by any other
584 * activity that is about to run. You will probably want to commit
585 * your data even more aggressively at key times during your
586 * activity's lifecycle: for example before starting a new
587 * activity, before finishing your own activity, when the user
588 * switches between input fields, etc.</p>
589 * </ul>
590 *
591 * <p>This model is designed to prevent data loss when a user is navigating
592 * between activities, and allows the system to safely kill an activity (because
593 * system resources are needed somewhere else) at any time after it has been
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700594 * stopped (or paused on platform versions before {@link android.os.Build.VERSION_CODES#HONEYCOMB}).
595 * 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 -0800596 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800597 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
599 *
600 * <p>See the {@linkplain android.content.ContentProvider content package} for
601 * more information about content providers. These are a key aspect of how
602 * different activities invoke and propagate data between themselves.</p>
603 *
604 * <p>The Activity class also provides an API for managing internal persistent state
605 * associated with an activity. This can be used, for example, to remember
606 * the user's preferred initial display in a calendar (day view or week view)
607 * or the user's default home page in a web browser.</p>
608 *
609 * <p>Activity persistent state is managed
610 * with the method {@link #getPreferences},
611 * allowing you to retrieve and
612 * modify a set of name/value pairs associated with the activity. To use
613 * preferences that are shared across multiple application components
614 * (activities, receivers, services, providers), you can use the underlying
615 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
616 * to retrieve a preferences
617 * object stored under a specific name.
618 * (Note that it is not possible to share settings data across application
619 * packages -- for that you will need a content provider.)</p>
620 *
621 * <p>Here is an excerpt from a calendar activity that stores the user's
622 * preferred view mode in its persistent settings:</p>
623 *
624 * <pre class="prettyprint">
625 * public class CalendarActivity extends Activity {
626 * ...
627 *
628 * static final int DAY_VIEW_MODE = 0;
629 * static final int WEEK_VIEW_MODE = 1;
630 *
631 * private SharedPreferences mPrefs;
632 * private int mCurViewMode;
633 *
634 * protected void onCreate(Bundle savedInstanceState) {
635 * super.onCreate(savedInstanceState);
636 *
637 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700638 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * }
640 *
641 * protected void onPause() {
642 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 * SharedPreferences.Editor ed = mPrefs.edit();
645 * ed.putInt("view_mode", mCurViewMode);
646 * ed.commit();
647 * }
648 * }
649 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 * <a name="Permissions"></a>
652 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * <p>The ability to start a particular Activity can be enforced when it is
655 * declared in its
656 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
657 * tag. By doing so, other applications will need to declare a corresponding
658 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
659 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800660 *
661 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
662 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
663 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
664 * Activity access to the specific URIs in the Intent. Access will remain
665 * until the Activity has finished (it will remain across the hosting
666 * process being killed and other temporary destruction). As of
667 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
668 * was already created and a new Intent is being delivered to
669 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
670 * to the existing ones it holds.
671 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
673 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 * <a name="ProcessLifecycle"></a>
676 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700677 *
kopriva4dddc632018-03-19 14:13:08 -0700678 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700680 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700682 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700684 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 * processes (the last ones) before it resorts to killing more important
686 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700687 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 * <ol>
689 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
690 * that the user is currently interacting with) is considered the most important.
691 * Its process will only be killed as a last resort, if it uses more memory
692 * than is available on the device. Generally at this point the device has
693 * reached a memory paging state, so this is required in order to keep the user
694 * interface responsive.
695 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700696 * but not in the foreground, such as one sitting behind a foreground dialog
697 * or next to other activities in multi-window mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 * is considered extremely important and will not be killed unless that is
699 * required to keep the foreground activity running.
700 * <li> <p>A <b>background activity</b> (an activity that is not visible to
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700701 * the user and has been stopped) is no longer critical, so the system may
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 * safely kill its process to reclaim memory for other foreground or
703 * visible processes. If its process needs to be killed, when the user navigates
704 * back to the activity (making it visible on the screen again), its
705 * {@link #onCreate} method will be called with the savedInstanceState it had previously
706 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
707 * state as the user last left it.
708 * <li> <p>An <b>empty process</b> is one hosting no activities or other
709 * application components (such as {@link Service} or
710 * {@link android.content.BroadcastReceiver} classes). These are killed very
711 * quickly by the system as memory becomes low. For this reason, any
712 * background operation you do outside of an activity must be executed in the
713 * context of an activity BroadcastReceiver or Service to ensure that the system
714 * knows it needs to keep your process around.
715 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 * <p>Sometimes an Activity may need to do a long-running operation that exists
718 * independently of the activity lifecycle itself. An example may be a camera
719 * application that allows you to upload a picture to a web site. The upload
720 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700721 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 * should start a {@link Service} in which the upload takes place. This allows
723 * the system to properly prioritize your process (considering it to be more
724 * important than other non-visible applications) for the duration of the
725 * upload, independent of whether the original activity is paused, stopped,
726 * or finished.
727 */
728public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700729 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700731 OnCreateContextMenuListener, ComponentCallbacks2,
Mathew Inwoodd6973862019-12-05 10:15:19 +0000732 Window.OnWindowDismissedCallback,
Felipe Lemecbf7f262019-04-17 13:57:59 -0700733 AutofillManager.AutofillClient, ContentCaptureManager.ContentCaptureClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700735 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
737 /** Standard activity result: operation canceled. */
738 public static final int RESULT_CANCELED = 0;
739 /** Standard activity result: operation succeeded. */
740 public static final int RESULT_OK = -1;
741 /** Start of user-defined activity results. */
742 public static final int RESULT_FIRST_USER = 1;
743
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700744 /** @hide Task isn't finished when activity is finished */
745 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700746 /**
747 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
748 * past behavior the task is also removed from recents.
749 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700750 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700751 /**
752 * @hide Task is finished along with the finishing activity, but it is not removed from
753 * recents.
754 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700755 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
756
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100757 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700758 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700759 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700760
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700761 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
763 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
764 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
765 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800766 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700767 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
768 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
Svetoslav970b59c2015-06-09 16:05:21 -0700770 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000771 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100772 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100773
Winson Chungf2b41772019-11-06 15:00:48 -0800774 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
775 private static final int LOG_AM_ON_START_CALLED = 30059;
776 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
777 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
778 private static final int LOG_AM_ON_STOP_CALLED = 30049;
779 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
780 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
781 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
782 private static final int LOG_AM_ON_TOP_RESUMED_GAINED_CALLED = 30064;
783 private static final int LOG_AM_ON_TOP_RESUMED_LOST_CALLED = 30065;
784
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800785 private static class ManagedDialog {
786 Dialog mDialog;
787 Bundle mArgs;
788 }
789 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790
791 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100792 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 private Instrumentation mInstrumentation;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100794 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 private IBinder mToken;
Sunny Goyald40c3452019-03-20 12:46:55 -0700796 private IBinder mAssistToken;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100797 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700798 private int mIdent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100799 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 /*package*/ String mEmbeddedID;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100801 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 private Application mApplication;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100803 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700804 /*package*/ Intent mIntent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100805 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800806 /*package*/ String mReferrer;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100807 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 private ComponentName mComponent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100809 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100811 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 /*package*/ ActivityThread mMainThread;
Charles Chen5c0943d2019-07-30 17:21:47 +0800813 @UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q,
814 publicAlternatives = "Use {@code androidx.fragment.app.Fragment} and "
815 + "{@code androidx.fragment.app.FragmentManager} instead")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 Activity mParent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100817 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 boolean mCalled;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100819 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600820 /*package*/ boolean mResumed;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100821 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700822 /*package*/ boolean mStopped;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100823 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 boolean mFinished;
825 boolean mStartedActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100826 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700827 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700828 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200829 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700830
831 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
832 * This only affects the client-side exception, the actual state check still happens in AMS. */
833 private boolean mCanEnterPictureInPicture = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500834 /** true if the activity is being destroyed in order to recreate it with a new configuration */
835 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100836 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 /*package*/ int mConfigChangeFlags;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100838 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100840 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700841 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700843 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
844 @Nullable private AutofillManager mAutofillManager;
845
Adam He43c06992019-04-15 15:41:49 -0700846 /** The content capture manager. Access via {@link #getContentCaptureManager()}. */
Felipe Lemeecb08be2018-11-27 15:48:47 -0800847 @Nullable private ContentCaptureManager mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800848
Ian Lake8a88cd62018-09-28 13:25:20 -0700849 private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
850 new ArrayList<Application.ActivityLifecycleCallbacks>();
Felipe Lemee348dc32018-11-05 12:35:29 -0800851
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700852 static final class NonConfigurationInstances {
853 Object activity;
854 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800855 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700856 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700857 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700858 }
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100859 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700860 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700861
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100862 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 private Window mWindow;
864
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100865 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 private WindowManager mWindowManager;
867 /*package*/ View mDecor = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100868 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 /*package*/ boolean mWindowAdded = false;
870 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood31755f92018-12-20 13:53:36 +0000871 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700873 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700874 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875
Mathew Inwood8c854f82018-09-14 12:35:36 +0100876 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Sunny Goyald40c3452019-03-20 12:46:55 -0700877 VoiceInteractor mVoiceInteractor;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700878
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100879 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 private CharSequence mTitle;
881 private int mTitleColor = 0;
882
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700883 // we must have a handler before the FragmentController is constructed
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100884 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700885 final Handler mHandler = new Handler();
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100886 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700887 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 private static final class ManagedCursor {
890 ManagedCursor(Cursor cursor) {
891 mCursor = cursor;
892 mReleased = false;
893 mUpdated = false;
894 }
895
896 private final Cursor mCursor;
897 private boolean mReleased;
898 private boolean mUpdated;
899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700901 @GuardedBy("mManagedCursors")
902 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
903
904 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100905 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700907 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100908 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700910
Craig Mautner5eda9b32013-07-02 11:58:16 -0700911 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700912 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700914 private SearchEvent mSearchEvent;
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000917 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
919 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
920 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700921
Winsonb6403152016-02-23 13:32:09 -0800922 private ActivityManager.TaskDescription mTaskDescription =
923 new ActivityManager.TaskDescription();
924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
926
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700927 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700928 private final Object mInstanceTracker = StrictMode.trackActivity(this);
929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700931
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100932 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700933 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700934 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
935 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
Svetoslavffb32b12015-10-15 16:54:00 -0700937 private boolean mHasCurrentPermissionsRequest;
938
Svet Ganov17db9dc2017-02-21 19:54:31 -0800939 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800940 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800941
Phil Weaver846cda932017-06-15 10:10:06 -0700942 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
943 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700944
Felipe Leme4753bb02017-03-22 20:24:00 -0700945 private AutofillPopupWindow mAutofillPopupWindow;
946
chaviwfeb2e1e2018-12-19 17:24:11 -0800947 /** @hide */
948 boolean mEnterAnimationComplete;
949
Hongwei Wang2cebfd52019-07-25 15:14:47 -0700950 /** Track last dispatched multi-window and PiP mode to client, internal debug purpose **/
951 private Boolean mLastDispatchedIsInMultiWindowMode;
952 private Boolean mLastDispatchedIsInPictureInPictureMode;
953
Mathew Inwoodd6973862019-12-05 10:15:19 +0000954 private final WindowControllerCallback mWindowControllerCallback =
955 new WindowControllerCallback() {
956 /**
957 * Moves the activity between {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing
958 * mode and {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
959 *
960 * @hide
961 */
962 @Override
963 public void toggleFreeformWindowingMode() throws RemoteException {
964 ActivityTaskManager.getService().toggleFreeformWindowingMode(mToken);
965 }
966
967 /**
968 * Puts the activity in picture-in-picture mode if the activity supports.
969 * @see android.R.attr#supportsPictureInPicture
970 * @hide
971 */
972 @Override
973 public void enterPictureInPictureModeIfPossible() {
974 if (mActivityInfo.supportsPictureInPicture()) {
975 enterPictureInPictureMode();
976 }
977 }
978
979 @Override
980 public boolean isTaskRoot() {
981 try {
982 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
983 } catch (RemoteException e) {
984 return false;
985 }
986 }
987
988 /**
989 * Update the forced status bar color.
990 * @hide
991 */
992 @Override
993 public void updateStatusBarColor(int color) {
994 mTaskDescription.setStatusBarColor(color);
995 setTaskDescription(mTaskDescription);
996 }
997
998 /**
999 * Update the forced navigation bar color.
1000 * @hide
1001 */
1002 @Override
1003 public void updateNavigationBarColor(int color) {
1004 mTaskDescription.setNavigationBarColor(color);
1005 setTaskDescription(mTaskDescription);
1006 }
1007
1008 };
1009
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08001010 private static native String getDlWarning();
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 /** Return the intent that started this activity. */
1013 public Intent getIntent() {
1014 return mIntent;
1015 }
1016
RoboErik55011652014-07-09 15:05:53 -07001017 /**
1018 * Change the intent returned by {@link #getIntent}. This holds a
1019 * reference to the given intent; it does not copy it. Often used in
1020 * conjunction with {@link #onNewIntent}.
1021 *
1022 * @param newIntent The new Intent object to return from getIntent
1023 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * @see #getIntent
1025 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -07001026 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 public void setIntent(Intent newIntent) {
1028 mIntent = newIntent;
1029 }
1030
Yuliya Kamatkova48c38072020-01-09 15:43:25 -08001031 /**
1032 * Sets the {@link android.content.LocusId} for this activity. The locus id
1033 * helps identify different instances of the same {@code Activity} class.
1034 * <p> For example, a locus id based on a specific conversation could be set on a
1035 * conversation app's chat {@code Activity}. The system can then use this locus id
1036 * along with app's contents to provide ranking signals in various UI surfaces
1037 * including sharing, notifications, shortcuts and so on.
1038 * <p> It is recommended to set the same locus id in the shortcut's locus id using
1039 * {@link android.content.pm.ShortcutInfo.Builder#setLocusId(android.content.LocusId)
1040 * setLocusId}
1041 * so that the system can learn appropriate ranking signals linking the activity's
1042 * locus id with the matching shortcut.
1043 *
1044 * @param locusId a unique, stable id that identifies this {@code Activity} instance from
1045 * others. This can be linked to a shortcut using
1046 * {@link android.content.pm.ShortcutInfo.Builder#setLocusId(android.content.LocusId)
1047 * setLocusId} with the same locus id string.
1048 * @param bundle extras set or updated as part of this locus context. This may help provide
1049 * additional metadata such as URLs, conversation participants specific to this
1050 * {@code Activity}'s context.
1051 *
1052 * @see android.view.contentcapture.ContentCaptureManager
1053 * @see android.view.contentcapture.ContentCaptureContext
1054 */
1055 public void setLocusContext(@Nullable LocusId locusId, @Nullable Bundle bundle) {
1056 try {
1057 ActivityManager.getService().setActivityLocusContext(mComponent, locusId, mToken);
1058 } catch (RemoteException re) {
1059 re.rethrowFromSystemServer();
1060 }
Yuliya Kamatkovab5d87a82020-02-13 16:00:52 -08001061 // If locusId is not null pass it to the Content Capture.
1062 if (locusId != null) {
1063 setLocusContextToContentCapture(locusId, bundle);
1064 }
Yuliya Kamatkova48c38072020-01-09 15:43:25 -08001065 }
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /** Return the application that owns this activity. */
1068 public final Application getApplication() {
1069 return mApplication;
1070 }
1071
1072 /** Is this activity embedded inside of another activity? */
1073 public final boolean isChild() {
1074 return mParent != null;
1075 }
RoboErik55011652014-07-09 15:05:53 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 /** Return the parent activity if this view is an embedded child. */
1078 public final Activity getParent() {
1079 return mParent;
1080 }
1081
1082 /** Retrieve the window manager for showing custom windows. */
1083 public WindowManager getWindowManager() {
1084 return mWindowManager;
1085 }
1086
1087 /**
1088 * Retrieve the current {@link android.view.Window} for the activity.
1089 * This can be used to directly access parts of the Window API that
1090 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -07001091 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 * @return Window The current window, or null if the activity is not
1093 * visual.
1094 */
1095 public Window getWindow() {
1096 return mWindow;
1097 }
1098
1099 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -08001100 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -08001101 *
1102 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -07001103 */
Ian Lake0a1feb82017-11-13 10:26:46 -08001104 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07001105 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001106 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -07001107 }
RoboErik55011652014-07-09 15:05:53 -07001108
Dianne Hackbornc8017682010-07-06 13:34:38 -07001109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 * Calls {@link android.view.Window#getCurrentFocus} on the
1111 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -07001112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -07001114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 * @see #getWindow
1116 * @see android.view.Window#getCurrentFocus
1117 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001118 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 public View getCurrentFocus() {
1120 return mWindow != null ? mWindow.getCurrentFocus() : null;
1121 }
1122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
Felipe Lemee348dc32018-11-05 12:35:29 -08001124 * (Creates, sets and) returns the autofill manager
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001125 *
1126 * @return The autofill manager
1127 */
1128 @NonNull private AutofillManager getAutofillManager() {
1129 if (mAutofillManager == null) {
1130 mAutofillManager = getSystemService(AutofillManager.class);
1131 }
1132
1133 return mAutofillManager;
1134 }
1135
Felipe Lemee348dc32018-11-05 12:35:29 -08001136 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -08001137 * (Creates, sets, and ) returns the content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001138 *
Felipe Lemeecb08be2018-11-27 15:48:47 -08001139 * @return The content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001140 */
Adam Heb66babb2019-01-08 15:43:53 -08001141 @Nullable private ContentCaptureManager getContentCaptureManager() {
1142 // ContextCapture disabled for system apps
Felipe Leme0c6b23d2019-01-15 17:02:49 -08001143 if (!UserHandle.isApp(myUid())) return null;
Felipe Lemeecb08be2018-11-27 15:48:47 -08001144 if (mContentCaptureManager == null) {
1145 mContentCaptureManager = getSystemService(ContentCaptureManager.class);
Felipe Lemee348dc32018-11-05 12:35:29 -08001146 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001147 return mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -08001148 }
1149
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001150 /** @hide */ private static final int CONTENT_CAPTURE_START = 1;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001151 /** @hide */ private static final int CONTENT_CAPTURE_RESUME = 2;
1152 /** @hide */ private static final int CONTENT_CAPTURE_PAUSE = 3;
1153 /** @hide */ private static final int CONTENT_CAPTURE_STOP = 4;
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001154
1155 /** @hide */
1156 @IntDef(prefix = { "CONTENT_CAPTURE_" }, value = {
1157 CONTENT_CAPTURE_START,
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001158 CONTENT_CAPTURE_RESUME,
1159 CONTENT_CAPTURE_PAUSE,
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001160 CONTENT_CAPTURE_STOP
1161 })
1162 @Retention(RetentionPolicy.SOURCE)
1163 @interface ContentCaptureNotificationType{}
1164
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001165 private String getContentCaptureTypeAsString(@ContentCaptureNotificationType int type) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001166 switch (type) {
1167 case CONTENT_CAPTURE_START:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001168 return "START";
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001169 case CONTENT_CAPTURE_RESUME:
1170 return "RESUME";
1171 case CONTENT_CAPTURE_PAUSE:
1172 return "PAUSE";
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001173 case CONTENT_CAPTURE_STOP:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001174 return "STOP";
Felipe Leme7a534082018-11-05 15:03:04 -08001175 default:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001176 return "UNKNOW-" + type;
1177 }
1178 }
1179
1180 private void notifyContentCaptureManagerIfNeeded(@ContentCaptureNotificationType int type) {
1181 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1182 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1183 "notifyContentCapture(" + getContentCaptureTypeAsString(type) + ") for "
1184 + mComponent.toShortString());
1185 }
1186 try {
1187 final ContentCaptureManager cm = getContentCaptureManager();
1188 if (cm == null) return;
1189
1190 switch (type) {
1191 case CONTENT_CAPTURE_START:
1192 //TODO(b/111276913): decide whether the InteractionSessionId should be
1193 // saved / restored in the activity bundle - probably not
Adam He43c06992019-04-15 15:41:49 -07001194 final Window window = getWindow();
1195 if (window != null) {
1196 cm.updateWindowAttributes(window.getAttributes());
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001197 }
Adam He43c06992019-04-15 15:41:49 -07001198 cm.onActivityCreated(mToken, getComponentName());
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001199 break;
1200 case CONTENT_CAPTURE_RESUME:
1201 cm.onActivityResumed();
1202 break;
1203 case CONTENT_CAPTURE_PAUSE:
1204 cm.onActivityPaused();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001205 break;
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001206 case CONTENT_CAPTURE_STOP:
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001207 cm.onActivityDestroyed();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001208 break;
1209 default:
1210 Log.wtf(TAG, "Invalid @ContentCaptureNotificationType: " + type);
1211 }
1212 } finally {
1213 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Felipe Leme7a534082018-11-05 15:03:04 -08001214 }
1215 }
1216
Yuliya Kamatkovab5d87a82020-02-13 16:00:52 -08001217 private void setLocusContextToContentCapture(LocusId locusId, @Nullable Bundle bundle) {
1218 final ContentCaptureManager cm = getContentCaptureManager();
1219 if (cm == null) return;
1220
1221 ContentCaptureContext.Builder contentCaptureContextBuilder =
1222 new ContentCaptureContext.Builder(locusId);
1223 if (bundle != null) {
1224 contentCaptureContextBuilder.setExtras(bundle);
1225 }
1226 cm.getMainContentCaptureSession().setContentCaptureContext(
1227 contentCaptureContextBuilder.build());
1228 }
1229
Felipe Lemebb567ae2017-10-04 09:56:21 -07001230 @Override
1231 protected void attachBaseContext(Context newBase) {
1232 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001233 if (newBase != null) {
1234 newBase.setAutofillClient(this);
Felipe Leme326f15a2019-02-19 09:42:24 -08001235 newBase.setContentCaptureOptions(getContentCaptureOptions());
Felipe Lemed247de82018-05-14 17:51:58 -07001236 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001237 }
1238
1239 /** @hide */
1240 @Override
1241 public final AutofillClient getAutofillClient() {
1242 return this;
1243 }
1244
Felipe Lemecbf7f262019-04-17 13:57:59 -07001245 /** @hide */
1246 @Override
1247 public final ContentCaptureClient getContentCaptureClient() {
1248 return this;
1249 }
1250
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001251 /**
Ian Lake8a88cd62018-09-28 13:25:20 -07001252 * Register an {@link Application.ActivityLifecycleCallbacks} instance that receives
1253 * lifecycle callbacks for only this Activity.
1254 * <p>
1255 * In relation to any
1256 * {@link Application#registerActivityLifecycleCallbacks Application registered callbacks},
1257 * the callbacks registered here will always occur nested within those callbacks. This means:
1258 * <ul>
1259 * <li>Pre events will first be sent to Application registered callbacks, then to callbacks
1260 * registered here.</li>
1261 * <li>{@link Application.ActivityLifecycleCallbacks#onActivityCreated(Activity, Bundle)},
1262 * {@link Application.ActivityLifecycleCallbacks#onActivityStarted(Activity)}, and
1263 * {@link Application.ActivityLifecycleCallbacks#onActivityResumed(Activity)} will
1264 * be sent first to Application registered callbacks, then to callbacks registered here.
1265 * For all other events, callbacks registered here will be sent first.</li>
1266 * <li>Post events will first be sent to callbacks registered here, then to
1267 * Application registered callbacks.</li>
1268 * </ul>
1269 * <p>
1270 * If multiple callbacks are registered here, they receive events in a first in (up through
1271 * {@link Application.ActivityLifecycleCallbacks#onActivityPostResumed}, last out
1272 * ordering.
1273 * <p>
1274 * It is strongly recommended to register this in the constructor of your Activity to ensure
1275 * you get all available callbacks. As this callback is associated with only this Activity,
1276 * it is not usually necessary to {@link #unregisterActivityLifecycleCallbacks unregister} it
1277 * unless you specifically do not want to receive further lifecycle callbacks.
1278 *
1279 * @param callback The callback instance to register
1280 */
1281 public void registerActivityLifecycleCallbacks(
1282 @NonNull Application.ActivityLifecycleCallbacks callback) {
1283 synchronized (mActivityLifecycleCallbacks) {
1284 mActivityLifecycleCallbacks.add(callback);
1285 }
1286 }
1287
1288 /**
1289 * Unregister an {@link Application.ActivityLifecycleCallbacks} previously registered
1290 * with {@link #registerActivityLifecycleCallbacks}. It will not receive any further
1291 * callbacks.
1292 *
1293 * @param callback The callback instance to unregister
1294 * @see #registerActivityLifecycleCallbacks
1295 */
1296 public void unregisterActivityLifecycleCallbacks(
1297 @NonNull Application.ActivityLifecycleCallbacks callback) {
1298 synchronized (mActivityLifecycleCallbacks) {
1299 mActivityLifecycleCallbacks.remove(callback);
1300 }
1301 }
1302
1303 private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
1304 getApplication().dispatchActivityPreCreated(this, savedInstanceState);
1305 Object[] callbacks = collectActivityLifecycleCallbacks();
1306 if (callbacks != null) {
1307 for (int i = 0; i < callbacks.length; i++) {
1308 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
1309 savedInstanceState);
1310 }
1311 }
1312 }
1313
1314 private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
1315 getApplication().dispatchActivityCreated(this, savedInstanceState);
1316 Object[] callbacks = collectActivityLifecycleCallbacks();
1317 if (callbacks != null) {
1318 for (int i = 0; i < callbacks.length; i++) {
1319 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
1320 savedInstanceState);
1321 }
1322 }
1323 }
1324
1325 private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
1326 Object[] callbacks = collectActivityLifecycleCallbacks();
1327 if (callbacks != null) {
1328 for (int i = 0; i < callbacks.length; i++) {
1329 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
1330 savedInstanceState);
1331 }
1332 }
1333 getApplication().dispatchActivityPostCreated(this, savedInstanceState);
1334 }
1335
1336 private void dispatchActivityPreStarted() {
1337 getApplication().dispatchActivityPreStarted(this);
1338 Object[] callbacks = collectActivityLifecycleCallbacks();
1339 if (callbacks != null) {
1340 for (int i = 0; i < callbacks.length; i++) {
1341 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(this);
1342 }
1343 }
1344 }
1345
1346 private void dispatchActivityStarted() {
1347 getApplication().dispatchActivityStarted(this);
1348 Object[] callbacks = collectActivityLifecycleCallbacks();
1349 if (callbacks != null) {
1350 for (int i = 0; i < callbacks.length; i++) {
1351 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
1352 }
1353 }
1354 }
1355
1356 private void dispatchActivityPostStarted() {
1357 Object[] callbacks = collectActivityLifecycleCallbacks();
1358 if (callbacks != null) {
1359 for (int i = 0; i < callbacks.length; i++) {
1360 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1361 .onActivityPostStarted(this);
1362 }
1363 }
1364 getApplication().dispatchActivityPostStarted(this);
1365 }
1366
1367 private void dispatchActivityPreResumed() {
1368 getApplication().dispatchActivityPreResumed(this);
1369 Object[] callbacks = collectActivityLifecycleCallbacks();
1370 if (callbacks != null) {
1371 for (int i = 0; i < callbacks.length; i++) {
1372 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(this);
1373 }
1374 }
1375 }
1376
1377 private void dispatchActivityResumed() {
1378 getApplication().dispatchActivityResumed(this);
1379 Object[] callbacks = collectActivityLifecycleCallbacks();
1380 if (callbacks != null) {
1381 for (int i = 0; i < callbacks.length; i++) {
1382 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
1383 }
1384 }
1385 }
1386
1387 private void dispatchActivityPostResumed() {
1388 Object[] callbacks = collectActivityLifecycleCallbacks();
1389 if (callbacks != null) {
1390 for (int i = 0; i < callbacks.length; i++) {
1391 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(this);
1392 }
1393 }
1394 getApplication().dispatchActivityPostResumed(this);
1395 }
1396
1397 private void dispatchActivityPrePaused() {
1398 getApplication().dispatchActivityPrePaused(this);
1399 Object[] callbacks = collectActivityLifecycleCallbacks();
1400 if (callbacks != null) {
1401 for (int i = callbacks.length - 1; i >= 0; i--) {
1402 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(this);
1403 }
1404 }
1405 }
1406
1407 private void dispatchActivityPaused() {
1408 Object[] callbacks = collectActivityLifecycleCallbacks();
1409 if (callbacks != null) {
1410 for (int i = callbacks.length - 1; i >= 0; i--) {
1411 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(this);
1412 }
1413 }
1414 getApplication().dispatchActivityPaused(this);
1415 }
1416
1417 private void dispatchActivityPostPaused() {
1418 Object[] callbacks = collectActivityLifecycleCallbacks();
1419 if (callbacks != null) {
1420 for (int i = callbacks.length - 1; i >= 0; i--) {
1421 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(this);
1422 }
1423 }
1424 getApplication().dispatchActivityPostPaused(this);
1425 }
1426
1427 private void dispatchActivityPreStopped() {
1428 getApplication().dispatchActivityPreStopped(this);
1429 Object[] callbacks = collectActivityLifecycleCallbacks();
1430 if (callbacks != null) {
1431 for (int i = callbacks.length - 1; i >= 0; i--) {
1432 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(this);
1433 }
1434 }
1435 }
1436
1437 private void dispatchActivityStopped() {
1438 Object[] callbacks = collectActivityLifecycleCallbacks();
1439 if (callbacks != null) {
1440 for (int i = callbacks.length - 1; i >= 0; i--) {
1441 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(this);
1442 }
1443 }
1444 getApplication().dispatchActivityStopped(this);
1445 }
1446
1447 private void dispatchActivityPostStopped() {
1448 Object[] callbacks = collectActivityLifecycleCallbacks();
1449 if (callbacks != null) {
1450 for (int i = callbacks.length - 1; i >= 0; i--) {
1451 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1452 .onActivityPostStopped(this);
1453 }
1454 }
1455 getApplication().dispatchActivityPostStopped(this);
1456 }
1457
1458 private void dispatchActivityPreSaveInstanceState(@NonNull Bundle outState) {
1459 getApplication().dispatchActivityPreSaveInstanceState(this, outState);
1460 Object[] callbacks = collectActivityLifecycleCallbacks();
1461 if (callbacks != null) {
1462 for (int i = callbacks.length - 1; i >= 0; i--) {
1463 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1464 .onActivityPreSaveInstanceState(this, outState);
1465 }
1466 }
1467 }
1468
1469 private void dispatchActivitySaveInstanceState(@NonNull Bundle outState) {
1470 Object[] callbacks = collectActivityLifecycleCallbacks();
1471 if (callbacks != null) {
1472 for (int i = callbacks.length - 1; i >= 0; i--) {
1473 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1474 .onActivitySaveInstanceState(this, outState);
1475 }
1476 }
1477 getApplication().dispatchActivitySaveInstanceState(this, outState);
1478 }
1479
1480 private void dispatchActivityPostSaveInstanceState(@NonNull Bundle outState) {
1481 Object[] callbacks = collectActivityLifecycleCallbacks();
1482 if (callbacks != null) {
1483 for (int i = callbacks.length - 1; i >= 0; i--) {
1484 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1485 .onActivityPostSaveInstanceState(this, outState);
1486 }
1487 }
1488 getApplication().dispatchActivityPostSaveInstanceState(this, outState);
1489 }
1490
1491 private void dispatchActivityPreDestroyed() {
1492 getApplication().dispatchActivityPreDestroyed(this);
1493 Object[] callbacks = collectActivityLifecycleCallbacks();
1494 if (callbacks != null) {
1495 for (int i = callbacks.length - 1; i >= 0; i--) {
1496 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1497 .onActivityPreDestroyed(this);
1498 }
1499 }
1500 }
1501
1502 private void dispatchActivityDestroyed() {
1503 Object[] callbacks = collectActivityLifecycleCallbacks();
1504 if (callbacks != null) {
1505 for (int i = callbacks.length - 1; i >= 0; i--) {
1506 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
1507 }
1508 }
1509 getApplication().dispatchActivityDestroyed(this);
1510 }
1511
1512 private void dispatchActivityPostDestroyed() {
1513 Object[] callbacks = collectActivityLifecycleCallbacks();
1514 if (callbacks != null) {
1515 for (int i = callbacks.length - 1; i >= 0; i--) {
1516 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1517 .onActivityPostDestroyed(this);
1518 }
1519 }
1520 getApplication().dispatchActivityPostDestroyed(this);
1521 }
1522
1523 private Object[] collectActivityLifecycleCallbacks() {
1524 Object[] callbacks = null;
1525 synchronized (mActivityLifecycleCallbacks) {
1526 if (mActivityLifecycleCallbacks.size() > 0) {
1527 callbacks = mActivityLifecycleCallbacks.toArray();
1528 }
1529 }
1530 return callbacks;
1531 }
1532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Called when the activity is starting. This is where most initialization
1535 * should go: calling {@link #setContentView(int)} to inflate the
1536 * activity's UI, using {@link #findViewById} to programmatically interact
1537 * with widgets in the UI, calling
1538 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1539 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001542 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1543 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1544 * executing.
RoboErik55011652014-07-09 15:05:53 -07001545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 * <p><em>Derived classes must call through to the super class's
1547 * implementation of this method. If they do not, an exception will be
1548 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001549 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * @param savedInstanceState If the activity is being re-initialized after
1551 * previously being shut down then this Bundle contains the data it most
1552 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001553 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 * @see #onStart
1555 * @see #onSaveInstanceState
1556 * @see #onRestoreInstanceState
1557 * @see #onPostCreate
1558 */
Tor Norbye83c68962015-03-10 20:55:31 -07001559 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001560 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001561 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001562 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001563
Dianne Hackborn2707d602010-07-09 18:01:20 -07001564 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001565 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001566 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001567 if (mActivityInfo.parentActivityName != null) {
1568 if (mActionBar == null) {
1569 mEnableDefaultActionBarUp = true;
1570 } else {
1571 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1572 }
1573 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001574 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001575 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001576 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1577 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001578
1579 if (mAutoFillResetNeeded) {
1580 getAutofillManager().onCreate(savedInstanceState);
1581 }
1582
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001583 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1584 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1585 ? mLastNonConfigurationInstances.fragments : null);
1586 }
1587 mFragments.dispatchCreate();
Ian Lake8a88cd62018-09-28 13:25:20 -07001588 dispatchActivityCreated(savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001589 if (mVoiceInteractor != null) {
1590 mVoiceInteractor.attachActivity(this);
1591 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001592 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 mCalled = true;
Felipe Lemee348dc32018-11-05 12:35:29 -08001594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596
1597 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001598 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001599 * the attribute {@link android.R.attr#persistableMode} set to
1600 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001601 *
1602 * @param savedInstanceState if the activity is being re-initialized after
1603 * previously being shut down then this Bundle contains the data it most
1604 * recently supplied in {@link #onSaveInstanceState}.
1605 * <b><i>Note: Otherwise it is null.</i></b>
1606 * @param persistentState if the activity is being re-initialized after
1607 * previously being shut down or powered off then this Bundle contains the data it most
1608 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1609 * <b><i>Note: Otherwise it is null.</i></b>
1610 *
1611 * @see #onCreate(android.os.Bundle)
1612 * @see #onStart
1613 * @see #onSaveInstanceState
1614 * @see #onRestoreInstanceState
1615 * @see #onPostCreate
1616 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001617 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001618 @Nullable PersistableBundle persistentState) {
1619 onCreate(savedInstanceState);
1620 }
1621
1622 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 * The hook for {@link ActivityThread} to restore the state of this activity.
1624 *
1625 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1626 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1627 *
1628 * @param savedInstanceState contains the saved state
1629 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001630 final void performRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 onRestoreInstanceState(savedInstanceState);
1632 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
1634
1635 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001636 * The hook for {@link ActivityThread} to restore the state of this activity.
1637 *
1638 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1639 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1640 *
1641 * @param savedInstanceState contains the saved state
1642 * @param persistentState contains the persistable saved state
1643 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001644 final void performRestoreInstanceState(@Nullable Bundle savedInstanceState,
1645 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001646 onRestoreInstanceState(savedInstanceState, persistentState);
1647 if (savedInstanceState != null) {
1648 restoreManagedDialogs(savedInstanceState);
1649 }
1650 }
1651
1652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 * This method is called after {@link #onStart} when the activity is
1654 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001655 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 * to restore their state, but it is sometimes convenient to do it here
1657 * after all of the initialization has been done or to allow subclasses to
1658 * decide whether to use your default implementation. The default
1659 * implementation of this method performs a restore of any view state that
1660 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001661 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 * <p>This method is called between {@link #onStart} and
Kevin Hufnagle4fbcec12019-07-18 21:37:36 +00001663 * {@link #onPostCreate}. This method is called only when recreating
1664 * an activity; the method isn't invoked if {@link #onStart} is called for
1665 * any other reason.</p>
RoboErik55011652014-07-09 15:05:53 -07001666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001668 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 * @see #onCreate
1670 * @see #onPostCreate
1671 * @see #onResume
1672 * @see #onSaveInstanceState
1673 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001674 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (mWindow != null) {
1676 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1677 if (windowState != null) {
1678 mWindow.restoreHierarchyState(windowState);
1679 }
1680 }
1681 }
Craig Mautnera0026042014-04-23 11:45:37 -07001682
1683 /**
1684 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001685 * created with the attribute {@link android.R.attr#persistableMode} set to
1686 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1687 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001688 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1689 *
1690 * <p>This method is called between {@link #onStart} and
1691 * {@link #onPostCreate}.
1692 *
1693 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1694 *
Jake Wharton63f4d892018-07-10 12:27:59 -04001695 * <p>At least one of {@code savedInstanceState} or {@code persistentState} will not be null.
1696 *
1697 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}
1698 * or null.
1699 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}
1700 * or null.
Craig Mautnera0026042014-04-23 11:45:37 -07001701 *
1702 * @see #onRestoreInstanceState(Bundle)
1703 * @see #onCreate
1704 * @see #onPostCreate
1705 * @see #onResume
1706 * @see #onSaveInstanceState
1707 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001708 public void onRestoreInstanceState(@Nullable Bundle savedInstanceState,
1709 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001710 if (savedInstanceState != null) {
1711 onRestoreInstanceState(savedInstanceState);
1712 }
1713 }
1714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 /**
1716 * Restore the state of any saved managed dialogs.
1717 *
1718 * @param savedInstanceState The bundle to restore from.
1719 */
1720 private void restoreManagedDialogs(Bundle savedInstanceState) {
1721 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1722 if (b == null) {
1723 return;
1724 }
1725
1726 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1727 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001728 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 for (int i = 0; i < numDialogs; i++) {
1730 final Integer dialogId = ids[i];
1731 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1732 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001733 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1734 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001735 final ManagedDialog md = new ManagedDialog();
1736 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1737 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1738 if (md.mDialog != null) {
1739 mManagedDialogs.put(dialogId, md);
1740 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1741 md.mDialog.onRestoreInstanceState(dialogState);
1742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 }
1745 }
1746
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001747 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1748 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001749 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001750 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001751 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001752 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001753 return dialog;
1754 }
1755
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001756 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 return SAVED_DIALOG_KEY_PREFIX + key;
1758 }
1759
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001760 private static String savedDialogArgsKeyFor(int key) {
1761 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763
1764 /**
1765 * Called when activity start-up is complete (after {@link #onStart}
1766 * and {@link #onRestoreInstanceState} have been called). Applications will
1767 * generally not implement this method; it is intended for system
1768 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 * <p><em>Derived classes must call through to the super class's
1771 * implementation of this method. If they do not, an exception will be
1772 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001773 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 * @param savedInstanceState If the activity is being re-initialized after
1775 * previously being shut down then this Bundle contains the data it most
1776 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1777 * @see #onCreate
1778 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001779 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001780 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 if (!isChild()) {
1782 mTitleReady = true;
1783 onTitleChanged(getTitle(), getTitleColor());
1784 }
Winsonb6403152016-02-23 13:32:09 -08001785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 mCalled = true;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001787
1788 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_START);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790
1791 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001792 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001793 * created with the attribute {@link android.R.attr#persistableMode} set to
1794 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001795 *
1796 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1797 * @param persistentState The data caming from the PersistableBundle first
1798 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1799 *
1800 * @see #onCreate
1801 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001802 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001803 @Nullable PersistableBundle persistentState) {
1804 onPostCreate(savedInstanceState);
1805 }
1806
1807 /**
RoboErik55011652014-07-09 15:05:53 -07001808 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1809 * the activity had been stopped, but is now again being displayed to the
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001810 * user. It will usually be followed by {@link #onResume}. This is a good place to begin
1811 * drawing visual elements, running animations, etc.
1812 *
1813 * <p>You can call {@link #finish} from within this function, in
1814 * which case {@link #onStop} will be immediately called after {@link #onStart} without the
1815 * lifecycle transitions in-between ({@link #onResume}, {@link #onPause}, etc) executing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 *
1817 * <p><em>Derived classes must call through to the super class's
1818 * implementation of this method. If they do not, an exception will be
1819 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001820 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 * @see #onCreate
1822 * @see #onStop
1823 * @see #onResume
1824 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001825 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001827 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001829
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001830 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001831
Ian Lake8a88cd62018-09-28 13:25:20 -07001832 dispatchActivityStarted();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001833
1834 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001835 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838
1839 /**
1840 * Called after {@link #onStop} when the current activity is being
1841 * re-displayed to the user (the user has navigated back to it). It will
1842 * be followed by {@link #onStart} and then {@link #onResume}.
1843 *
1844 * <p>For activities that are using raw {@link Cursor} objects (instead of
1845 * creating them through
1846 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1847 * this is usually the place
1848 * where the cursor should be requeried (because you had deactivated it in
1849 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 * <p><em>Derived classes must call through to the super class's
1852 * implementation of this method. If they do not, an exception will be
1853 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 * @see #onStop
1856 * @see #onStart
1857 * @see #onResume
1858 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001859 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 protected void onRestart() {
1861 mCalled = true;
1862 }
1863
1864 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001865 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1866 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1867 * to give the activity a hint that its state is no longer saved -- it will generally
1868 * be called after {@link #onSaveInstanceState} and prior to the activity being
1869 * resumed/started again.
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001870 *
1871 * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
1872 * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
1873 * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001874 */
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001875 @Deprecated
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001876 public void onStateNotSaved() {
1877 }
1878
1879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001881 * {@link #onPause}, for your activity to start interacting with the user. This is an indicator
1882 * that the activity became active and ready to receive input. It is on top of an activity stack
1883 * and visible to user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001885 * <p>On platform versions prior to {@link android.os.Build.VERSION_CODES#Q} this is also a good
1886 * place to try to open exclusive-access devices or to get access to singleton resources.
1887 * Starting with {@link android.os.Build.VERSION_CODES#Q} there can be multiple resumed
1888 * activities in the system simultaneously, so {@link #onTopResumedActivityChanged(boolean)}
1889 * should be used for that purpose instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 *
1891 * <p><em>Derived classes must call through to the super class's
1892 * implementation of this method. If they do not, an exception will be
1893 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001894 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 * @see #onRestoreInstanceState
1896 * @see #onRestart
1897 * @see #onPostResume
1898 * @see #onPause
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001899 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001901 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001903 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07001904 dispatchActivityResumed();
Sunny Goyal64643f62019-01-31 15:15:33 -08001905 mActivityTransitionState.onResume(this);
Felipe Lemece404982018-09-17 09:46:13 -07001906 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001907 if (mAutoFillResetNeeded) {
1908 if (!mAutoFillIgnoreFirstResumePause) {
1909 View focus = getCurrentFocus();
Ben Line55cd3e2018-12-07 20:26:37 +00001910 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
Adam He5be0f152020-02-03 15:53:32 -08001911 // TODO(b/148815880): Bring up keyboard if resumed from inline authentication.
Dake Gu67decfa2017-12-27 11:48:08 -08001912 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1913 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1914 // window visibility after recreation is INVISIBLE in onResume() and next frame
1915 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1916 // So we cannot call View.notifyEnterOrExited() which will do nothing
1917 // when View.isVisibleToUser() is false.
1918 getAutofillManager().notifyViewEntered(focus);
1919 }
1920 }
1921 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001922
1923 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 mCalled = true;
1926 }
1927
1928 /**
1929 * Called when activity resume is complete (after {@link #onResume} has
1930 * been called). Applications will generally not implement this method;
1931 * it is intended for system classes to do final setup after application
1932 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 * <p><em>Derived classes must call through to the super class's
1935 * implementation of this method. If they do not, an exception will be
1936 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 * @see #onResume
1939 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001940 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 protected void onPostResume() {
1942 final Window win = getWindow();
1943 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001944 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 mCalled = true;
1946 }
1947
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001948 /**
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001949 * Called when activity gets or loses the top resumed position in the system.
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001950 *
1951 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} multiple activities can be resumed
1952 * at the same time in multi-window and multi-display modes. This callback should be used
1953 * instead of {@link #onResume()} as an indication that the activity can try to open
1954 * exclusive-access devices like camera.</p>
1955 *
1956 * <p>It will always be delivered after the activity was resumed and before it is paused. In
1957 * some cases it might be skipped and activity can go straight from {@link #onResume()} to
1958 * {@link #onPause()} without receiving the top resumed state.</p>
1959 *
1960 * @param isTopResumedActivity {@code true} if it's the topmost resumed activity in the system,
1961 * {@code false} otherwise. A call with this as {@code true} will
1962 * always be followed by another one with {@code false}.
1963 *
1964 * @see #onResume()
1965 * @see #onPause()
1966 * @see #onWindowFocusChanged(boolean)
1967 */
1968 public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
1969 }
1970
Andrii Kulian86024012019-04-18 19:41:23 -07001971 final void performTopResumedActivityChanged(boolean isTopResumedActivity, String reason) {
1972 onTopResumedActivityChanged(isTopResumedActivity);
1973
Jeff Changd136e772019-11-05 20:33:52 +08001974 if (isTopResumedActivity) {
1975 EventLogTags.writeWmOnTopResumedGainedCalled(mIdent, getComponentName().getClassName(),
1976 reason);
1977 } else {
1978 EventLogTags.writeWmOnTopResumedLostCalled(mIdent, getComponentName().getClassName(),
1979 reason);
1980 }
Andrii Kulian86024012019-04-18 19:41:23 -07001981 }
1982
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001983 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001984 if (mVoiceInteractor != null) {
Sunny Goyald40c3452019-03-20 12:46:55 -07001985 final Request[] requests = mVoiceInteractor.getActiveRequests();
1986 if (requests != null) {
1987 for (Request activeRequest : mVoiceInteractor.getActiveRequests()) {
1988 activeRequest.cancel();
1989 activeRequest.clear();
1990 }
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001991 }
1992 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001993 if (voiceInteractor == null) {
1994 mVoiceInteractor = null;
1995 } else {
1996 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1997 Looper.myLooper());
1998 }
1999 }
2000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 /**
Phil Weaver846cda932017-06-15 10:10:06 -07002002 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002003 *
Phil Weaver846cda932017-06-15 10:10:06 -07002004 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002005 * will be unique.
2006 *
2007 * @return A ID that is unique in the activity
2008 *
2009 * {@hide}
2010 */
Felipe Leme42b97932018-02-20 13:04:31 -08002011 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07002012 public int getNextAutofillId() {
2013 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
2014 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002015 }
2016
Phil Weaver846cda932017-06-15 10:10:06 -07002017 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002018
Phil Weaver846cda932017-06-15 10:10:06 -07002019 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002020 }
2021
2022 /**
Felipe Leme42b97932018-02-20 13:04:31 -08002023 * @hide
2024 */
2025 @Override
2026 public AutofillId autofillClientGetNextAutofillId() {
Felipe Leme305fd402018-09-11 18:20:42 +00002027 return new AutofillId(getNextAutofillId());
Felipe Leme42b97932018-02-20 13:04:31 -08002028 }
2029
2030 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07002031 * Check whether this activity is running as part of a voice interaction with the user.
2032 * If true, it should perform its interaction with the user through the
2033 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
2034 */
2035 public boolean isVoiceInteraction() {
2036 return mVoiceInteractor != null;
2037 }
2038
2039 /**
Charles Cheneba0a202019-09-17 21:02:07 +08002040 * Like {@link #isVoiceInteraction}, but only returns {@code true} if this is also the root
2041 * of a voice interaction. That is, returns {@code true} if this activity was directly
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002042 * started by the voice interaction service as the initiation of a voice interaction.
2043 * Otherwise, for example if it was started by another activity while under voice
Charles Cheneba0a202019-09-17 21:02:07 +08002044 * interaction, returns {@code false}.
2045 * If the activity {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode} is
2046 * {@code singleTask}, it forces the activity to launch in a new task, separate from the one
2047 * that started it. Therefore, there is no longer a relationship between them, and
2048 * {@link #isVoiceInteractionRoot()} return {@code false} in this case.
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002049 */
2050 public boolean isVoiceInteractionRoot() {
2051 try {
2052 return mVoiceInteractor != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002053 && ActivityTaskManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002054 } catch (RemoteException e) {
2055 }
2056 return false;
2057 }
2058
2059 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07002060 * Retrieve the active {@link VoiceInteractor} that the user is going through to
2061 * interact with this activity.
2062 */
2063 public VoiceInteractor getVoiceInteractor() {
2064 return mVoiceInteractor;
2065 }
2066
2067 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002068 * Queries whether the currently enabled voice interaction service supports returning
2069 * a voice interactor for use by the activity. This is valid only for the duration of the
2070 * activity.
2071 *
2072 * @return whether the current voice interaction service supports local voice interaction
2073 */
2074 public boolean isLocalVoiceInteractionSupported() {
2075 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002076 return ActivityTaskManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002077 } catch (RemoteException re) {
2078 }
2079 return false;
2080 }
2081
2082 /**
2083 * Starts a local voice interaction session. When ready,
2084 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
2085 * to the registered voice interaction service.
2086 * @param privateOptions a Bundle of private arguments to the current voice interaction service
2087 */
2088 public void startLocalVoiceInteraction(Bundle privateOptions) {
2089 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002090 ActivityTaskManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002091 } catch (RemoteException re) {
2092 }
2093 }
2094
2095 /**
2096 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
2097 * voice interaction session being started. You can now retrieve a voice interactor using
2098 * {@link #getVoiceInteractor()}.
2099 */
2100 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002101 }
2102
2103 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07002104 * Callback to indicate that the local voice interaction has stopped either
2105 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
2106 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
2107 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002108 */
2109 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002110 }
2111
2112 /**
2113 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07002114 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
2115 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002116 */
2117 public void stopLocalVoiceInteraction() {
2118 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002119 ActivityTaskManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002120 } catch (RemoteException re) {
2121 }
2122 }
2123
2124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 * This is called for activities that set launchMode to "singleTop" in
2126 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
2127 * flag when calling {@link #startActivity}. In either case, when the
2128 * activity is re-launched while at the top of the activity stack instead
2129 * of a new instance of the activity being started, onNewIntent() will be
2130 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07002131 * re-launch it.
2132 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002133 * <p>An activity can never receive a new intent in the resumed state. You can count on
2134 * {@link #onResume} being called after this method, though not necessarily immediately after
2135 * the completion this callback. If the activity was resumed, it will be paused and new intent
2136 * will be delivered, followed by {@link #onResume}. If the activity wasn't in the resumed
2137 * state, then new intent can be delivered immediately, with {@link #onResume()} called
2138 * sometime later when activity becomes active again.
RoboErik55011652014-07-09 15:05:53 -07002139 *
2140 * <p>Note that {@link #getIntent} still returns the original Intent. You
2141 * can use {@link #setIntent} to update it to this new Intent.
2142 *
2143 * @param intent The new intent that was started for the activity.
2144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07002146 * @see #setIntent
2147 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 */
2149 protected void onNewIntent(Intent intent) {
2150 }
2151
2152 /**
2153 * The hook for {@link ActivityThread} to save the state of this activity.
2154 *
2155 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2156 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2157 *
2158 * @param outState The bundle to save the state to.
2159 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002160 final void performSaveInstanceState(@NonNull Bundle outState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002161 dispatchActivityPreSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 onSaveInstanceState(outState);
2163 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07002164 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002165 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002166 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002167 dispatchActivityPostSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
2169
2170 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002171 * The hook for {@link ActivityThread} to save the state of this activity.
2172 *
2173 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2174 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2175 *
2176 * @param outState The bundle to save the state to.
2177 * @param outPersistentState The bundle to save persistent state to.
2178 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002179 final void performSaveInstanceState(@NonNull Bundle outState,
2180 @NonNull PersistableBundle outPersistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002181 dispatchActivityPreSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002182 onSaveInstanceState(outState, outPersistentState);
2183 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002184 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002185 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
2186 ", " + outPersistentState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002187 dispatchActivityPostSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002188 }
2189
2190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 * Called to retrieve per-instance state from an activity before being killed
2192 * so that the state can be restored in {@link #onCreate} or
2193 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
2194 * will be passed to both).
2195 *
2196 * <p>This method is called before an activity may be killed so that when it
2197 * comes back some time in the future it can restore its state. For example,
2198 * if activity B is launched in front of activity A, and at some point activity
2199 * A is killed to reclaim resources, activity A will have a chance to save the
2200 * current state of its user interface via this method so that when the user
2201 * returns to activity A, the state of the user interface can be restored
2202 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
2203 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002204 * <p>Do not confuse this method with activity lifecycle callbacks such as {@link #onPause},
2205 * which is always called when the user no longer actively interacts with an activity, or
2206 * {@link #onStop} which is called when activity becomes invisible. One example of when
2207 * {@link #onPause} and {@link #onStop} is called and not this method is when a user navigates
2208 * back from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
2209 * on B because that particular instance will never be restored,
2210 * so the system avoids calling it. An example when {@link #onPause} is called and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
2212 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
2213 * killed during the lifetime of B since the state of the user interface of
2214 * A will stay intact.
2215 *
2216 * <p>The default implementation takes care of most of the UI per-instance
2217 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
2218 * view in the hierarchy that has an id, and by saving the id of the currently
2219 * focused view (all of which is restored by the default implementation of
2220 * {@link #onRestoreInstanceState}). If you override this method to save additional
2221 * information not captured by each individual view, you will likely want to
2222 * call through to the default implementation, otherwise be prepared to save
2223 * all of the state of each view yourself.
2224 *
Andrii Kulian391161f2018-01-29 10:50:02 -08002225 * <p>If called, this method will occur after {@link #onStop} for applications
2226 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
2227 * For applications targeting earlier platform versions this method will occur
2228 * before {@link #onStop} and there are no guarantees about whether it will
2229 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07002230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07002232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 * @see #onCreate
2234 * @see #onRestoreInstanceState
2235 * @see #onPause
2236 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002237 protected void onSaveInstanceState(@NonNull Bundle outState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002239
Phil Weaver846cda932017-06-15 10:10:06 -07002240 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002241 Parcelable p = mFragments.saveAllState();
2242 if (p != null) {
2243 outState.putParcelable(FRAGMENTS_TAG, p);
2244 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002245 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002246 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
2247 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002248 }
Ian Lake8a88cd62018-09-28 13:25:20 -07002249 dispatchActivitySaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 }
2251
2252 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002253 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07002254 * created with the attribute {@link android.R.attr#persistableMode} set to
2255 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
2256 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
2257 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07002258 *
2259 * @param outState Bundle in which to place your saved state.
2260 * @param outPersistentState State which will be saved across reboots.
2261 *
2262 * @see #onSaveInstanceState(Bundle)
2263 * @see #onCreate
2264 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
2265 * @see #onPause
2266 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002267 public void onSaveInstanceState(@NonNull Bundle outState,
2268 @NonNull PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07002269 onSaveInstanceState(outState);
2270 }
2271
2272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 * Save the state of any managed dialogs.
2274 *
2275 * @param outState place to store the saved state.
2276 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002277 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 private void saveManagedDialogs(Bundle outState) {
2279 if (mManagedDialogs == null) {
2280 return;
2281 }
2282
2283 final int numDialogs = mManagedDialogs.size();
2284 if (numDialogs == 0) {
2285 return;
2286 }
2287
2288 Bundle dialogState = new Bundle();
2289
2290 int[] ids = new int[mManagedDialogs.size()];
2291
2292 // save each dialog's bundle, gather the ids
2293 for (int i = 0; i < numDialogs; i++) {
2294 final int key = mManagedDialogs.keyAt(i);
2295 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002296 final ManagedDialog md = mManagedDialogs.valueAt(i);
2297 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
2298 if (md.mArgs != null) {
2299 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
2300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 }
2302
2303 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
2304 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
2305 }
2306
2307
2308 /**
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002309 * Called as part of the activity lifecycle when the user no longer actively interacts with the
2310 * activity, but it is still visible on screen. The counterpart to {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 *
2312 * <p>When activity B is launched in front of activity A, this callback will
2313 * be invoked on A. B will not be created until A's {@link #onPause} returns,
2314 * so be sure to not do anything lengthy here.
2315 *
2316 * <p>This callback is mostly used for saving any persistent state the
2317 * activity is editing, to present a "edit in place" model to the user and
2318 * making sure nothing is lost if there are not enough resources to start
2319 * the new activity without first killing this one. This is also a good
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002320 * place to stop things that consume a noticeable amount of CPU in order to
2321 * make the switch to the next activity as fast as possible.
RoboErik55011652014-07-09 15:05:53 -07002322 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002323 * <p>On platform versions prior to {@link android.os.Build.VERSION_CODES#Q} this is also a good
2324 * place to try to close exclusive-access devices or to release access to singleton resources.
2325 * Starting with {@link android.os.Build.VERSION_CODES#Q} there can be multiple resumed
2326 * activities in the system at the same time, so {@link #onTopResumedActivityChanged(boolean)}
2327 * should be used for that purpose instead.
RoboErik55011652014-07-09 15:05:53 -07002328 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002329 * <p>If an activity is launched on top, after receiving this call you will usually receive a
2330 * following call to {@link #onStop} (after the next activity has been resumed and displayed
2331 * above). However in some cases there will be a direct call back to {@link #onResume} without
2332 * going through the stopped state. An activity can also rest in paused state in some cases when
2333 * in multi-window mode, still visible to user.
RoboErik55011652014-07-09 15:05:53 -07002334 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 * <p><em>Derived classes must call through to the super class's
2336 * implementation of this method. If they do not, an exception will be
2337 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 * @see #onResume
2340 * @see #onSaveInstanceState
2341 * @see #onStop
2342 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002343 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002345 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07002346 dispatchActivityPaused();
Dake Gu67decfa2017-12-27 11:48:08 -08002347 if (mAutoFillResetNeeded) {
2348 if (!mAutoFillIgnoreFirstResumePause) {
2349 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
2350 View focus = getCurrentFocus();
2351 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
2352 getAutofillManager().notifyViewExited(focus);
2353 }
2354 } else {
2355 // reset after first pause()
2356 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
2357 mAutoFillIgnoreFirstResumePause = false;
2358 }
2359 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002360
2361 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_PAUSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 mCalled = true;
2363 }
2364
2365 /**
2366 * Called as part of the activity lifecycle when an activity is about to go
2367 * into the background as the result of user choice. For example, when the
2368 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
2369 * when an incoming phone call causes the in-call Activity to be automatically
2370 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
2371 * the activity being interrupted. In cases when it is invoked, this method
2372 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07002373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 * <p>This callback and {@link #onUserInteraction} are intended to help
2375 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002376 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07002377 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 * @see #onUserInteraction()
Louis Changc2e193a2019-01-09 15:33:57 +08002379 * @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 */
2381 protected void onUserLeaveHint() {
2382 }
RoboErik55011652014-07-09 15:05:53 -07002383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002385 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002387 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002389 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
2391
2392 /**
2393 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002394 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07002396 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 * <p>The default implementation returns null, which will cause you to
2398 * inherit the description from the previous activity. If all activities
2399 * return null, generally the label of the top activity will be used as the
2400 * description.
RoboErik55011652014-07-09 15:05:53 -07002401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 * @return A description of what the user is doing. It should be short and
2403 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07002404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002406 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002408 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 public CharSequence onCreateDescription() {
2410 return null;
2411 }
2412
2413 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002414 * This is called when the user is requesting an assist, to build a full
2415 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
2416 * application. You can override this method to place into the bundle anything
2417 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002418 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002419 *
2420 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07002421 * been registered with {@link Application#registerOnProvideAssistDataListener
2422 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002423 */
2424 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002425 }
2426
2427 /**
2428 * This is called when the user is requesting an assist, to provide references
2429 * to content related to the current activity. Before being called, the
2430 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
2431 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
2432 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
2433 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
2434 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
2435 *
2436 * <p>Custom implementation may adjust the content intent to better reflect the top-level
2437 * context of the activity, and fill in its ClipData with additional content of
2438 * interest that the user is currently viewing. For example, an image gallery application
2439 * that has launched in to an activity allowing the user to swipe through pictures should
2440 * modify the intent to reference the current image they are looking it; such an
2441 * application when showing a list of pictures should add a ClipData that has
2442 * references to all of the pictures currently visible on screen.</p>
2443 *
2444 * @param outContent The assist content to return.
2445 */
2446 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002447 }
2448
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002449 /**
Sunny Goyald40c3452019-03-20 12:46:55 -07002450 * Returns the list of direct actions supported by the app.
2451 *
2452 * <p>You should return the list of actions that could be executed in the
2453 * current context, which is in the current state of the app. If the actions
2454 * that could be executed by the app changes you should report that via
2455 * calling {@link VoiceInteractor#notifyDirectActionsChanged()}.
2456 *
2457 * <p>To get the voice interactor you need to call {@link #getVoiceInteractor()}
Andrew Solovay7ad21702019-06-06 15:10:43 -07002458 * which would return non <code>null</code> only if there is an ongoing voice
Sunny Goyald40c3452019-03-20 12:46:55 -07002459 * interaction session. You an also detect when the voice interactor is no
2460 * longer valid because the voice interaction session that is backing is finished
2461 * by calling {@link VoiceInteractor#registerOnDestroyedCallback(Executor, Runnable)}.
2462 *
2463 * <p>This method will be called only after {@link #onStart()} is being called and
2464 * before {@link #onStop()} is being called.
2465 *
Svet Ganov3b6be082019-04-28 10:21:01 -07002466 * <p>You should pass to the callback the currently supported direct actions which
Andrew Solovay7ad21702019-06-06 15:10:43 -07002467 * cannot be <code>null</code> or contain <code>null</code> elements.
Svet Ganov3b6be082019-04-28 10:21:01 -07002468 *
2469 * <p>You should return the action list as soon as possible to ensure the consumer,
2470 * for example the assistant, is as responsive as possible which would improve user
2471 * experience of your app.
2472 *
2473 * @param cancellationSignal A signal to cancel the operation in progress.
2474 * @param callback The callback to send the action list. The actions list cannot
Svet Ganov07e5fb22019-04-30 11:07:09 -07002475 * contain <code>null</code> elements. You can call this on any thread.
Sunny Goyald40c3452019-03-20 12:46:55 -07002476 */
Svet Ganov3b6be082019-04-28 10:21:01 -07002477 public void onGetDirectActions(@NonNull CancellationSignal cancellationSignal,
2478 @NonNull Consumer<List<DirectAction>> callback) {
2479 callback.accept(Collections.emptyList());
Sunny Goyald40c3452019-03-20 12:46:55 -07002480 }
2481
2482 /**
2483 * This is called to perform an action previously defined by the app.
2484 * Apps also have access to {@link #getVoiceInteractor()} to follow up on the action.
2485 *
Svet Ganov07e5fb22019-04-30 11:07:09 -07002486 * @param actionId The ID for the action you previously reported via
2487 * {@link #onGetDirectActions(CancellationSignal, Consumer)}.
2488 * @param arguments Any additional arguments provided by the caller that are
2489 * specific to the given action.
Svet Ganov3b6be082019-04-28 10:21:01 -07002490 * @param cancellationSignal A signal to cancel the operation in progress.
Svet Ganov07e5fb22019-04-30 11:07:09 -07002491 * @param resultListener The callback to provide the result back to the caller.
2492 * You can call this on any thread. The result bundle is action specific.
Sunny Goyald40c3452019-03-20 12:46:55 -07002493 *
Svet Ganov3b6be082019-04-28 10:21:01 -07002494 * @see #onGetDirectActions(CancellationSignal, Consumer)
Sunny Goyald40c3452019-03-20 12:46:55 -07002495 */
2496 public void onPerformDirectAction(@NonNull String actionId,
Svet Ganov3b6be082019-04-28 10:21:01 -07002497 @NonNull Bundle arguments, @NonNull CancellationSignal cancellationSignal,
Sunny Goyald40c3452019-03-20 12:46:55 -07002498 @NonNull Consumer<Bundle> resultListener) { }
2499
2500 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01002501 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002502 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
2503 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01002504 public final void requestShowKeyboardShortcuts() {
Winson Chungf2b41772019-11-06 15:00:48 -08002505 final ComponentName sysuiComponent = ComponentName.unflattenFromString(
2506 getResources().getString(
2507 com.android.internal.R.string.config_systemUIServiceComponent));
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002508 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Winson Chungf2b41772019-11-06 15:00:48 -08002509 intent.setPackage(sysuiComponent.getPackageName());
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002510 sendBroadcastAsUser(intent, Process.myUserHandle());
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002511 }
2512
2513 /**
2514 * Dismiss the Keyboard Shortcuts screen.
2515 */
2516 public final void dismissKeyboardShortcutsHelper() {
Winson Chungf2b41772019-11-06 15:00:48 -08002517 final ComponentName sysuiComponent = ComponentName.unflattenFromString(
2518 getResources().getString(
2519 com.android.internal.R.string.config_systemUIServiceComponent));
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002520 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Winson Chungf2b41772019-11-06 15:00:48 -08002521 intent.setPackage(sysuiComponent.getPackageName());
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002522 sendBroadcastAsUser(intent, Process.myUserHandle());
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002523 }
2524
Clara Bayarri75e09792015-07-29 16:20:40 +01002525 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00002526 public void onProvideKeyboardShortcuts(
2527 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01002528 if (menu == null) {
2529 return;
2530 }
2531 KeyboardShortcutGroup group = null;
2532 int menuSize = menu.size();
2533 for (int i = 0; i < menuSize; ++i) {
2534 final MenuItem item = menu.getItem(i);
2535 final CharSequence title = item.getTitle();
2536 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01002537 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01002538 if (title != null && alphaShortcut != MIN_VALUE) {
2539 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00002540 final int resource = mApplication.getApplicationInfo().labelRes;
2541 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01002542 }
2543 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01002544 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01002545 }
2546 }
2547 if (group != null) {
2548 data.add(group);
2549 }
2550 }
2551
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002552 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07002553 * Ask to have the current assistant shown to the user. This only works if the calling
2554 * activity is the current foreground activity. It is the same as calling
2555 * {@link android.service.voice.VoiceInteractionService#showSession
2556 * VoiceInteractionService.showSession} and requesting all of the possible context.
2557 * The receiver will always see
2558 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
2559 * @return Returns true if the assistant was successfully invoked, else false. For example
2560 * false will be returned if the caller is not the current top activity.
2561 */
2562 public boolean showAssist(Bundle args) {
2563 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002564 return ActivityTaskManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07002565 } catch (RemoteException e) {
2566 }
2567 return false;
2568 }
2569
2570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 * Called when you are no longer visible to the user. You will next
2572 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002573 * depending on later user activity. This is a good place to stop
2574 * refreshing UI, running animations and other visual things.
RoboErik55011652014-07-09 15:05:53 -07002575 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 * <p><em>Derived classes must call through to the super class's
2577 * implementation of this method. If they do not, an exception will be
2578 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002579 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 * @see #onRestart
2581 * @see #onResume
2582 * @see #onSaveInstanceState
2583 * @see #onDestroy
2584 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002585 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002587 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08002588 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07002589 mActivityTransitionState.onStop();
Ian Lake8a88cd62018-09-28 13:25:20 -07002590 dispatchActivityStopped();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002591 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07002593
Dake Gu67decfa2017-12-27 11:48:08 -08002594 if (mAutoFillResetNeeded) {
TYM Tsai88398352020-01-14 19:49:05 +08002595 // If stopped without changing the configurations, the response should expire.
2596 getAutofillManager().onInvisibleForAutofill(!mChangingConfigurations);
TYM Tsai8af721d2019-07-24 17:11:59 +08002597 } else if (mIntent != null
2598 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)
2599 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY)) {
2600 restoreAutofillSaveUi();
Felipe Lemebab851c2017-02-03 18:45:08 -08002601 }
chaviwfeb2e1e2018-12-19 17:24:11 -08002602 mEnterAnimationComplete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 }
2604
2605 /**
2606 * Perform any final cleanup before an activity is destroyed. This can
2607 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07002608 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 * this instance of the activity to save space. You can distinguish
2610 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07002611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 * <p><em>Note: do not count on this method being called as a place for
2613 * saving data! For example, if an activity is editing data in a content
2614 * provider, those edits should be committed in either {@link #onPause} or
2615 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
2616 * free resources like threads that are associated with an activity, so
2617 * that a destroyed activity does not leave such things around while the
2618 * rest of its application is still running. There are situations where
2619 * the system will simply kill the activity's hosting process without
2620 * calling this method (or any others) in it, so it should not be used to
2621 * do things that are intended to remain around after the process goes
2622 * away.
RoboErik55011652014-07-09 15:05:53 -07002623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 * <p><em>Derived classes must call through to the super class's
2625 * implementation of this method. If they do not, an exception will be
2626 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002627 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 * @see #onPause
2629 * @see #onStop
2630 * @see #finish
2631 * @see #isFinishing
2632 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002633 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002635 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 mCalled = true;
2637
lpeterc054c5d2019-09-11 18:14:56 +08002638 if (isFinishing() && mAutoFillResetNeeded) {
2639 getAutofillManager().onActivityFinishing();
2640 }
2641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 // dismiss any dialogs we are managing.
2643 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 final int numDialogs = mManagedDialogs.size();
2645 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002646 final ManagedDialog md = mManagedDialogs.valueAt(i);
2647 if (md.mDialog.isShowing()) {
2648 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 }
2650 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002651 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653
2654 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002655 synchronized (mManagedCursors) {
2656 int numCursors = mManagedCursors.size();
2657 for (int i = 0; i < numCursors; i++) {
2658 ManagedCursor c = mManagedCursors.get(i);
2659 if (c != null) {
2660 c.mCursor.close();
2661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002663 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
Amith Yamasani49860442010-03-17 20:54:10 -07002665
2666 // Close any open search dialog
2667 if (mSearchManager != null) {
2668 mSearchManager.stopSearch();
2669 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002670
Chris Banes21b25772016-01-04 20:41:59 +00002671 if (mActionBar != null) {
2672 mActionBar.onDestroy();
2673 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002674
Ian Lake8a88cd62018-09-28 13:25:20 -07002675 dispatchActivityDestroyed();
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002676
2677 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_STOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 }
2679
2680 /**
Mathieu Chartier7fe953e2019-10-16 10:27:29 -07002681 * Report to the system that your app is now fully drawn, for diagnostic and
2682 * optimization purposes. The system may adjust optimizations to prioritize
2683 * work that happens before reportFullyDrawn is called, to improve app startup.
2684 * Misrepresenting the startup window by calling reportFullyDrawn too late or too
2685 * early may decrease application and startup performance.<p>
2686 * This is also used to help instrument application launch times, so that the
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002687 * app can report when it is fully in a usable state; without this, the only thing
2688 * the system itself can determine is the point at which the activity's window
2689 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002690 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002691 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002692 * entirely drawn your UI and populated with all of the significant data. You
2693 * can safely call this method any time after first launch as well, in which case
2694 * it will simply be ignored.
Riddle Hsuc1f8f9c2019-10-25 16:54:33 +08002695 * <p>If this method is called before the activity's window is <em>first</em> drawn
2696 * and displayed as measured by the system, the reported time here will be shifted
2697 * to the system measured time.
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002698 */
2699 public void reportFullyDrawn() {
2700 if (mDoReportFullyDrawn) {
2701 mDoReportFullyDrawn = false;
2702 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002703 ActivityTaskManager.getService().reportActivityFullyDrawn(
2704 mToken, mRestoredFromBundle);
Mathieu Chartier63e14c12019-03-07 09:05:44 -08002705 VMRuntime.getRuntime().notifyStartupCompleted();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002706 } catch (RemoteException e) {
2707 }
2708 }
2709 }
2710
2711 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002712 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002713 * visa-versa. This method provides the same configuration that will be sent in the following
2714 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2715 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002716 * @see android.R.attr#resizeableActivity
2717 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002718 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002719 * @param newConfig The new configuration of the activity with the state
2720 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002721 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002722 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2723 // Left deliberately empty. There should be no side effects if a direct
2724 // subclass of Activity does not call super.
2725 onMultiWindowModeChanged(isInMultiWindowMode);
2726 }
2727
2728 /**
2729 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2730 * visa-versa.
2731 *
2732 * @see android.R.attr#resizeableActivity
2733 *
2734 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2735 *
2736 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2737 */
2738 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002739 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002740 // Left deliberately empty. There should be no side effects if a direct
2741 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002742 }
2743
2744 /**
2745 * Returns true if the activity is currently in multi-window mode.
2746 * @see android.R.attr#resizeableActivity
2747 *
2748 * @return True if the activity is in multi-window mode.
2749 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002750 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002751 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002752 return ActivityTaskManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002753 } catch (RemoteException e) {
2754 }
2755 return false;
2756 }
2757
2758 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002759 * Called by the system when the activity changes to and from picture-in-picture mode. This
2760 * method provides the same configuration that will be sent in the following
2761 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2762 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002763 * @see android.R.attr#supportsPictureInPicture
2764 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002765 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002766 * @param newConfig The new configuration of the activity with the state
2767 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002768 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002769 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2770 Configuration newConfig) {
2771 // Left deliberately empty. There should be no side effects if a direct
2772 // subclass of Activity does not call super.
2773 onPictureInPictureModeChanged(isInPictureInPictureMode);
2774 }
2775
2776 /**
2777 * Called by the system when the activity changes to and from picture-in-picture mode.
2778 *
2779 * @see android.R.attr#supportsPictureInPicture
2780 *
2781 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2782 *
2783 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2784 */
2785 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002786 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002787 // Left deliberately empty. There should be no side effects if a direct
2788 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002789 }
2790
2791 /**
2792 * Returns true if the activity is currently in picture-in-picture mode.
2793 * @see android.R.attr#supportsPictureInPicture
2794 *
2795 * @return True if the activity is in picture-in-picture mode.
2796 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002797 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002798 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002799 return ActivityTaskManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002800 } catch (RemoteException e) {
2801 }
2802 return false;
2803 }
2804
2805 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002806 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002807 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2808 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002809 *
Winson Chung709904f2017-04-25 11:00:48 -07002810 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002811 * @see android.R.attr#supportsPictureInPicture
2812 */
Winson Chung709904f2017-04-25 11:00:48 -07002813 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002814 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002815 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002816 }
2817
Winson Chungc2baac02017-01-11 13:34:47 -08002818 /**
Winson Chung709904f2017-04-25 11:00:48 -07002819 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2820 * set parameters in {@param params} will be combined with the parameters from prior calls to
2821 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2822 *
2823 * The system may disallow entering picture-in-picture in various cases, including when the
2824 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2825 *
2826 * @see android.R.attr#supportsPictureInPicture
2827 * @see PictureInPictureParams
2828 *
2829 * @param params non-null parameters to be combined with previously set parameters when entering
2830 * picture-in-picture.
2831 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002832 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002833 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002834 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002835 */
2836 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2837 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002838 if (!deviceSupportsPictureInPictureMode()) {
2839 return false;
2840 }
Winson Chung709904f2017-04-25 11:00:48 -07002841 if (params == null) {
2842 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2843 }
Winson Chung298f95b2017-08-10 15:57:18 -07002844 if (!mCanEnterPictureInPicture) {
2845 throw new IllegalStateException("Activity must be resumed to enter"
2846 + " picture-in-picture");
2847 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002848 return ActivityTaskManager.getService().enterPictureInPictureMode(mToken, params);
Winson Chung709904f2017-04-25 11:00:48 -07002849 } catch (RemoteException e) {
2850 return false;
2851 }
2852 }
2853
Winson Chung709904f2017-04-25 11:00:48 -07002854 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002855 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2856 * {@link #enterPictureInPictureMode()} is called.
2857 *
Winson Chung709904f2017-04-25 11:00:48 -07002858 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002859 */
Winson Chung709904f2017-04-25 11:00:48 -07002860 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002861 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002862 if (!deviceSupportsPictureInPictureMode()) {
2863 return;
2864 }
Winson Chung709904f2017-04-25 11:00:48 -07002865 if (params == null) {
2866 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002867 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002868 ActivityTaskManager.getService().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002869 } catch (RemoteException e) {
2870 }
2871 }
2872
Winson Chung8802eac2017-04-17 14:21:44 -07002873 /**
Winson Chung709904f2017-04-25 11:00:48 -07002874 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2875 * user interacts with the activity currently in picture-in-picture mode. This number may change
2876 * if the global configuration changes (ie. if the device is plugged into an external display),
2877 * but will always be larger than three.
2878 */
2879 public int getMaxNumPictureInPictureActions() {
2880 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002881 return ActivityTaskManager.getService().getMaxNumPictureInPictureActions(mToken);
Winson Chung709904f2017-04-25 11:00:48 -07002882 } catch (RemoteException e) {
2883 return 0;
2884 }
2885 }
2886
Winson Chung80fa2d82018-01-25 13:58:20 -08002887 /**
2888 * @return Whether this device supports picture-in-picture.
2889 */
2890 private boolean deviceSupportsPictureInPictureMode() {
2891 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2892 }
2893
jorgegil@google.com06bc3232019-10-31 14:51:22 -07002894 /**
jorgegil@google.com7a926dc2020-01-29 10:28:33 -08002895 * This method is called by the system in various cases where picture in picture mode should be
2896 * entered if supported.
2897 *
2898 * <p>It is up to the app developer to choose whether to call
2899 * {@link #enterPictureInPictureMode(PictureInPictureParams)} at this time. For example, the
2900 * system will call this method when the activity is being put into the background, so the app
2901 * developer might want to switch an activity into PIP mode instead.</p>
jorgegil@google.comb8ee18f2020-02-03 13:21:27 -08002902 *
2903 * @return {@code true} if the activity received this callback regardless of if it acts on it
2904 * or not. If {@code false}, the framework will assume the app hasn't been updated to leverage
2905 * this callback and will in turn send a legacy callback of {@link #onUserLeaveHint()} for the
2906 * app to enter picture-in-picture mode.
jorgegil@google.com06bc3232019-10-31 14:51:22 -07002907 */
jorgegil@google.comb8ee18f2020-02-03 13:21:27 -08002908 public boolean onPictureInPictureRequested() {
2909 return false;
jorgegil@google.com06bc3232019-10-31 14:51:22 -07002910 }
2911
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002912 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002913 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002914 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002915 }
2916
2917 /**
2918 * Called by the system when the activity is moved from one display to another without
2919 * recreation. This means that this activity is declared to handle all changes to configuration
2920 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002921 * again.
2922 *
2923 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2924 * applied configuration actually changed. It is up to app developer to choose whether to handle
2925 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2926 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002927 *
2928 * <p>Use this callback to track changes to the displays if some activity functionality relies
2929 * on an association with some display properties.
2930 *
2931 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002932 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002933 *
2934 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002935 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002936 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002937 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00002938 @UnsupportedAppUsage
Tiger Huangfc218452018-09-27 00:38:50 +08002939 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002940 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002941 }
2942
Winson Chungb5c41b72016-12-07 15:00:47 -08002943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 * Called by the system when the device configuration changes while your
2945 * activity is running. Note that this will <em>only</em> be called if
2946 * you have selected configurations you would like to handle with the
2947 * {@link android.R.attr#configChanges} attribute in your manifest. If
2948 * any configuration change occurs that is not selected to be reported
2949 * by that attribute, then instead of reporting it the system will stop
2950 * and restart the activity (to have it launched with the new
2951 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 * <p>At the time that this function has been called, your Resources
2954 * object will have been updated to return resource values matching the
2955 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002956 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 * @param newConfig The new device configuration.
2958 */
Ian Lake666a9652018-09-12 15:13:19 -07002959 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002960 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002962
Dianne Hackborn9d071802010-12-08 14:49:15 -08002963 mFragments.dispatchConfigurationChanged(newConfig);
2964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 if (mWindow != null) {
2966 // Pass the configuration changed event to the window
2967 mWindow.onConfigurationChanged(newConfig);
2968 }
Adam Powell45c0b192011-07-28 15:11:57 -07002969
2970 if (mActionBar != null) {
2971 // Do this last; the action bar will need to access
2972 // view changes from above.
2973 mActionBar.onConfigurationChanged(newConfig);
2974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 }
RoboErik55011652014-07-09 15:05:53 -07002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 /**
2978 * If this activity is being destroyed because it can not handle a
2979 * configuration parameter being changed (and thus its
2980 * {@link #onConfigurationChanged(Configuration)} method is
2981 * <em>not</em> being called), then you can use this method to discover
2982 * the set of changes that have occurred while in the process of being
2983 * destroyed. Note that there is no guarantee that these will be
2984 * accurate (other changes could have happened at any time), so you should
2985 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002986 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 * @return Returns a bit field of the configuration parameters that are
2988 * changing, as defined by the {@link android.content.res.Configuration}
2989 * class.
2990 */
2991 public int getChangingConfigurations() {
2992 return mConfigChangeFlags;
2993 }
RoboErik55011652014-07-09 15:05:53 -07002994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 /**
2996 * Retrieve the non-configuration instance data that was previously
2997 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2998 * be available from the initial {@link #onCreate} and
2999 * {@link #onStart} calls to the new instance, allowing you to extract
3000 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07003001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 * <p>Note that the data you retrieve here should <em>only</em> be used
3003 * as an optimization for handling configuration changes. You should always
3004 * be able to handle getting a null pointer back, and an activity must
3005 * still be able to restore itself to its previous state (through the
3006 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
3007 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07003008 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08003009 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003010 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08003011 * available on older platforms through the Android support libraries.
3012 *
3013 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003015 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003017 return mLastNonConfigurationInstances != null
3018 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
RoboErik55011652014-07-09 15:05:53 -07003020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 /**
3022 * Called by the system, as part of destroying an
3023 * activity due to a configuration change, when it is known that a new
3024 * instance will immediately be created for the new configuration. You
3025 * can return any object you like here, including the activity instance
3026 * itself, which can later be retrieved by calling
3027 * {@link #getLastNonConfigurationInstance()} in the new activity
3028 * instance.
RoboErik55011652014-07-09 15:05:53 -07003029 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003030 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3031 * or later, consider instead using a {@link Fragment} with
3032 * {@link Fragment#setRetainInstance(boolean)
3033 * Fragment.setRetainInstance(boolean}.</em>
3034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 * <p>This function is called purely as an optimization, and you must
3036 * not rely on it being called. When it is called, a number of guarantees
3037 * will be made to help optimize configuration switching:
3038 * <ul>
3039 * <li> The function will be called between {@link #onStop} and
3040 * {@link #onDestroy}.
3041 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07003042 * created after this one's {@link #onDestroy()} is called. In particular,
3043 * <em>no</em> messages will be dispatched during this time (when the returned
3044 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 * <li> The object you return here will <em>always</em> be available from
3046 * the {@link #getLastNonConfigurationInstance()} method of the following
3047 * activity instance as described there.
3048 * </ul>
RoboErik55011652014-07-09 15:05:53 -07003049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 * <p>These guarantees are designed so that an activity can use this API
3051 * to propagate extensive state from the old to new activity instance, from
3052 * loaded bitmaps, to network connections, to evenly actively running
3053 * threads. Note that you should <em>not</em> propagate any data that
3054 * may change based on the configuration, including any data loaded from
3055 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07003056 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07003057 * <p>The guarantee of no message handling during the switch to the next
3058 * activity simplifies use with active objects. For example if your retained
3059 * state is an {@link android.os.AsyncTask} you are guaranteed that its
3060 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
3061 * not be called from the call here until you execute the next instance's
3062 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
3063 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
3064 * running in a separate thread.)
3065 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08003066 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003067 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08003068 * available on older platforms through the Android support libraries.
3069 *
3070 * @return any Object holding the desired state to propagate to the
3071 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 */
3073 public Object onRetainNonConfigurationInstance() {
3074 return null;
3075 }
RoboErik55011652014-07-09 15:05:53 -07003076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 /**
3078 * Retrieve the non-configuration instance data that was previously
3079 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
3080 * be available from the initial {@link #onCreate} and
3081 * {@link #onStart} calls to the new instance, allowing you to extract
3082 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07003083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 * <p>Note that the data you retrieve here should <em>only</em> be used
3085 * as an optimization for handling configuration changes. You should always
3086 * be able to handle getting a null pointer back, and an activity must
3087 * still be able to restore itself to its previous state (through the
3088 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
3089 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07003090 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 * @return Returns the object previously returned by
3092 * {@link #onRetainNonConfigurationChildInstances()}
3093 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003094 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003095 HashMap<String, Object> getLastNonConfigurationChildInstances() {
3096 return mLastNonConfigurationInstances != null
3097 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 }
RoboErik55011652014-07-09 15:05:53 -07003099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 /**
3101 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
3102 * it should return either a mapping from child activity id strings to arbitrary objects,
3103 * or null. This method is intended to be used by Activity framework subclasses that control a
3104 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
3105 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
3106 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003107 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
3109 return null;
3110 }
RoboErik55011652014-07-09 15:05:53 -07003111
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003112 NonConfigurationInstances retainNonConfigurationInstances() {
3113 Object activity = onRetainNonConfigurationInstance();
3114 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08003115 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07003116
3117 // We're already stopped but we've been asked to retain.
3118 // Our fragments are taken care of but we need to mark the loaders for retention.
3119 // In order to do this correctly we need to restart the loaders first before
3120 // handing them off to the next activity.
3121 mFragments.doLoaderStart();
3122 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003123 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07003124
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003125 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07003126 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003127 return null;
3128 }
RoboErik55011652014-07-09 15:05:53 -07003129
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003130 NonConfigurationInstances nci = new NonConfigurationInstances();
3131 nci.activity = activity;
3132 nci.children = children;
3133 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003134 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07003135 if (mVoiceInteractor != null) {
3136 mVoiceInteractor.retainInstance();
3137 nci.voiceInteractor = mVoiceInteractor;
3138 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003139 return nci;
3140 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003143 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08003145 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003147
3148 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003149 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003150 mCalled = true;
3151 mFragments.dispatchTrimMemory(level);
3152 }
3153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003155 * Return the FragmentManager for interacting with fragments associated
3156 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08003157 *
3158 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003159 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003160 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003161 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003162 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07003163 }
RoboErik55011652014-07-09 15:05:53 -07003164
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003165 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07003166 * Called when a Fragment is being attached to this activity, immediately
3167 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
3168 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08003169 *
3170 * @deprecated Use {@link
3171 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07003172 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003173 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07003174 public void onAttachFragment(Fragment fragment) {
3175 }
RoboErik55011652014-07-09 15:05:53 -07003176
Dianne Hackbornc8017682010-07-06 13:34:38 -07003177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 * Wrapper around
3179 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3180 * that gives the resulting {@link Cursor} to call
3181 * {@link #startManagingCursor} so that the activity will manage its
3182 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003183 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003184 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3185 * or later, consider instead using {@link LoaderManager} instead, available
3186 * via {@link #getLoaderManager()}.</em>
3187 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003188 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3189 * this method, because the activity will do that for you at the appropriate time. However, if
3190 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3191 * not</em> automatically close the cursor and, in that case, you must call
3192 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 * @param uri The URI of the content provider to query.
3195 * @param projection List of columns to return.
3196 * @param selection SQL WHERE clause.
3197 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3202 * @see #startManagingCursor
3203 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05003204 *
3205 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 */
Jason parks6ed50de2010-08-25 10:18:50 -05003207 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003208 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07003209 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3210 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
3212 if (c != null) {
3213 startManagingCursor(c);
3214 }
3215 return c;
3216 }
3217
3218 /**
3219 * Wrapper around
3220 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3221 * that gives the resulting {@link Cursor} to call
3222 * {@link #startManagingCursor} so that the activity will manage its
3223 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003224 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003225 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3226 * or later, consider instead using {@link LoaderManager} instead, available
3227 * via {@link #getLoaderManager()}.</em>
3228 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003229 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3230 * this method, because the activity will do that for you at the appropriate time. However, if
3231 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3232 * not</em> automatically close the cursor and, in that case, you must call
3233 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 * @param uri The URI of the content provider to query.
3236 * @param projection List of columns to return.
3237 * @param selection SQL WHERE clause.
3238 * @param selectionArgs The arguments to selection, if any ?s are pesent
3239 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3244 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05003245 *
3246 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 */
Jason parks6ed50de2010-08-25 10:18:50 -05003248 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07003249 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3250 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
3252 if (c != null) {
3253 startManagingCursor(c);
3254 }
3255 return c;
3256 }
3257
3258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 * This method allows the activity to take care of managing the given
3260 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
3261 * That is, when the activity is stopped it will automatically call
3262 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
3263 * it will call {@link Cursor#requery} for you. When the activity is
3264 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07003265 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003266 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3267 * or later, consider instead using {@link LoaderManager} instead, available
3268 * via {@link #getLoaderManager()}.</em>
3269 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003270 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
3271 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
3272 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
3273 * <em>will not</em> automatically close the cursor and, in that case, you must call
3274 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07003277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
3279 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003280 *
3281 * @deprecated Use the new {@link android.content.CursorLoader} class with
3282 * {@link LoaderManager} instead; this is also
3283 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 */
Jason parks6ed50de2010-08-25 10:18:50 -05003285 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 public void startManagingCursor(Cursor c) {
3287 synchronized (mManagedCursors) {
3288 mManagedCursors.add(new ManagedCursor(c));
3289 }
3290 }
3291
3292 /**
3293 * Given a Cursor that was previously given to
3294 * {@link #startManagingCursor}, stop the activity's management of that
3295 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003296 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003297 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003298 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003299 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003300 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003302 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003304 *
3305 * @deprecated Use the new {@link android.content.CursorLoader} class with
3306 * {@link LoaderManager} instead; this is also
3307 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 */
Jason parks6ed50de2010-08-25 10:18:50 -05003309 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 public void stopManagingCursor(Cursor c) {
3311 synchronized (mManagedCursors) {
3312 final int N = mManagedCursors.size();
3313 for (int i=0; i<N; i++) {
3314 ManagedCursor mc = mManagedCursors.get(i);
3315 if (mc.mCursor == c) {
3316 mManagedCursors.remove(i);
3317 break;
3318 }
3319 }
3320 }
3321 }
3322
3323 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003324 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3325 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003326 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003328 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003329 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 }
3332
3333 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003334 * Finds a view that was identified by the {@code android:id} XML attribute
3335 * that was processed in {@link #onCreate}.
3336 * <p>
3337 * <strong>Note:</strong> In most cases -- depending on compiler support --
3338 * the resulting view is automatically cast to the target class type. If
3339 * the target class type is unconstrained, an explicit cast may be
3340 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003342 * @param id the ID to search for
3343 * @return a view with given ID if found, or {@code null} otherwise
3344 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003345 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003347 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003348 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 return getWindow().findViewById(id);
3350 }
RoboErik55011652014-07-09 15:05:53 -07003351
Adam Powell33b97432010-04-20 10:01:14 -07003352 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003353 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3354 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3355 * no matching view in the hierarchy.
3356 * <p>
3357 * <strong>Note:</strong> In most cases -- depending on compiler support --
3358 * the resulting view is automatically cast to the target class type. If
3359 * the target class type is unconstrained, an explicit cast may be
3360 * necessary.
3361 *
3362 * @param id the ID to search for
3363 * @return a view with given ID
3364 * @see View#requireViewById(int)
3365 * @see Activity#findViewById(int)
3366 */
3367 @NonNull
3368 public final <T extends View> T requireViewById(@IdRes int id) {
3369 T view = findViewById(id);
3370 if (view == null) {
3371 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3372 }
3373 return view;
3374 }
3375
3376 /**
Adam Powell33b97432010-04-20 10:01:14 -07003377 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003378 *
Adam Powell33b97432010-04-20 10:01:14 -07003379 * @return The Activity's ActionBar, or null if it does not have one.
3380 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003381 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003382 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003383 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003384 return mActionBar;
3385 }
Adam Powelle43340c2014-03-17 19:10:43 -07003386
3387 /**
3388 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3389 * Activity window.
3390 *
3391 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3392 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3393 * a traditional window decor action bar. The toolbar's menu will be populated with the
3394 * Activity's options menu and the navigation button will be wired through the standard
3395 * {@link android.R.id#home home} menu select action.</p>
3396 *
3397 * <p>In order to use a Toolbar within the Activity's window content the application
3398 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3399 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003400 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003401 */
Adam Powell37780142014-06-01 13:31:00 -07003402 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003403 final ActionBar ab = getActionBar();
3404 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003405 throw new IllegalStateException("This Activity already has an action bar supplied " +
3406 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3407 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3408 }
Chris Banes21b25772016-01-04 20:41:59 +00003409
3410 // If we reach here then we're setting a new action bar
3411 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003412 mMenuInflater = null;
3413
Chris Banes21b25772016-01-04 20:41:59 +00003414 // If we have an action bar currently, destroy it
3415 if (ab != null) {
3416 ab.onDestroy();
3417 }
3418
Chris Banesc7d6c322016-01-27 14:09:16 +00003419 if (toolbar != null) {
3420 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3421 mActionBar = tbab;
3422 mWindow.setCallback(tbab.getWrappedWindowCallback());
3423 } else {
3424 mActionBar = null;
3425 // Re-set the original window callback since we may have already set a Toolbar wrapper
3426 mWindow.setCallback(this);
3427 }
3428
3429 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003430 }
RoboErik55011652014-07-09 15:05:53 -07003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 /**
Adam Powell33b97432010-04-20 10:01:14 -07003433 * Creates a new ActionBar, locates the inflated ActionBarView,
3434 * initializes the ActionBar with the view, and sets mActionBar.
3435 */
Adam Powelle43340c2014-03-17 19:10:43 -07003436 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003437 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003438
3439 // Initializing the window decor can change window feature flags.
3440 // Make sure that we have the correct set before performing the test below.
3441 window.getDecorView();
3442
Adam Powell9b4c8042010-08-10 15:36:44 -07003443 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003444 return;
3445 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003446
Adam Powelle43340c2014-03-17 19:10:43 -07003447 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003448 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003449
3450 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3451 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003452 }
RoboErik55011652014-07-09 15:05:53 -07003453
Adam Powell33b97432010-04-20 10:01:14 -07003454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 * Set the activity content from a layout resource. The resource will be
3456 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003459 *
Romain Guy482b34a62011-01-20 10:59:28 -08003460 * @see #setContentView(android.view.View)
3461 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003463 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003465 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 }
3467
3468 /**
3469 * Set the activity content to an explicit view. This view is placed
3470 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003471 * view hierarchy. When calling this method, the layout parameters of the
3472 * specified view are ignored. Both the width and the height of the view are
3473 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3474 * your own layout parameters, invoke
3475 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3476 * instead.
RoboErik55011652014-07-09 15:05:53 -07003477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003479 *
3480 * @see #setContentView(int)
3481 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 */
3483 public void setContentView(View view) {
3484 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003485 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 }
3487
3488 /**
3489 * Set the activity content to an explicit view. This view is placed
3490 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003491 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003492 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 * @param view The desired content to display.
3494 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003495 *
3496 * @see #setContentView(android.view.View)
3497 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 */
3499 public void setContentView(View view, ViewGroup.LayoutParams params) {
3500 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003501 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 }
3503
3504 /**
3505 * Add an additional content view to the activity. Added after any existing
3506 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003507 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 * @param view The desired content to display.
3509 * @param params Layout parameters for the view.
3510 */
3511 public void addContentView(View view, ViewGroup.LayoutParams params) {
3512 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003513 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 }
3515
3516 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003517 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3518 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3519 *
3520 * <p>This method will return non-null after content has been initialized (e.g. by using
3521 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3522 *
3523 * @return This window's content TransitionManager or null if none is set.
3524 */
3525 public TransitionManager getContentTransitionManager() {
3526 return getWindow().getTransitionManager();
3527 }
3528
3529 /**
3530 * Set the {@link TransitionManager} to use for default transitions in this window.
3531 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3532 *
3533 * @param tm The TransitionManager to use for scene changes.
3534 */
3535 public void setContentTransitionManager(TransitionManager tm) {
3536 getWindow().setTransitionManager(tm);
3537 }
3538
3539 /**
3540 * Retrieve the {@link Scene} representing this window's current content.
3541 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3542 *
3543 * <p>This method will return null if the current content is not represented by a Scene.</p>
3544 *
3545 * @return Current Scene being shown or null
3546 */
3547 public Scene getContentScene() {
3548 return getWindow().getContentScene();
3549 }
3550
3551 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003552 * Sets whether this activity is finished when touched outside its window's
3553 * bounds.
3554 */
3555 public void setFinishOnTouchOutside(boolean finish) {
3556 mWindow.setCloseOnTouchOutside(finish);
3557 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003558
3559 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003560 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003561 DEFAULT_KEYS_DISABLE,
3562 DEFAULT_KEYS_DIALER,
3563 DEFAULT_KEYS_SHORTCUT,
3564 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003565 DEFAULT_KEYS_SEARCH_GLOBAL
3566 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003567 @Retention(RetentionPolicy.SOURCE)
3568 @interface DefaultKeyMode {}
3569
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3572 * keys.
RoboErik55011652014-07-09 15:05:53 -07003573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 * @see #setDefaultKeyMode
3575 */
3576 static public final int DEFAULT_KEYS_DISABLE = 0;
3577 /**
3578 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3579 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 * @see #setDefaultKeyMode
3582 */
3583 static public final int DEFAULT_KEYS_DIALER = 1;
3584 /**
3585 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3586 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 * @see #setDefaultKeyMode
3591 */
3592 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3593 /**
3594 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3595 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003596 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003597 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 * @see #setDefaultKeyMode
3601 */
3602 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3603
3604 /**
3605 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3606 * will start a global search (typically web search, but some platforms may define alternate
3607 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 * @see #setDefaultKeyMode
3612 */
3613 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3614
3615 /**
3616 * Select the default key handling for this activity. This controls what
3617 * will happen to key events that are not otherwise handled. The default
3618 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3619 * floor. Other modes allow you to launch the dialer
3620 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3621 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003622 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 * <p>Note that the mode selected here does not impact the default
3626 * handling of system keys, such as the "back" and "menu" keys, and your
3627 * activity and its views always get a first chance to receive and handle
3628 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003629 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 * @see #onKeyDown
3633 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003634 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 // Some modes use a SpannableStringBuilder to track & dispatch input events
3638 // This list must remain in sync with the switch in onKeyDown()
3639 switch (mode) {
3640 case DEFAULT_KEYS_DISABLE:
3641 case DEFAULT_KEYS_SHORTCUT:
3642 mDefaultKeySsb = null; // not used in these modes
3643 break;
3644 case DEFAULT_KEYS_DIALER:
3645 case DEFAULT_KEYS_SEARCH_LOCAL:
3646 case DEFAULT_KEYS_SEARCH_GLOBAL:
3647 mDefaultKeySsb = new SpannableStringBuilder();
3648 Selection.setSelection(mDefaultKeySsb,0);
3649 break;
3650 default:
3651 throw new IllegalArgumentException();
3652 }
3653 }
3654
3655 /**
3656 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003657 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 * is inside a TextView will not trigger the event (unless it is a navigation
3659 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 * <p>If the focused view didn't want this event, this method is called.
3662 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003663 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3664 * by calling {@link #onBackPressed()}, though the behavior varies based
3665 * on the application compatibility mode: for
3666 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3667 * it will set up the dispatch to call {@link #onKeyUp} where the action
3668 * will be performed; for earlier applications, it will perform the
3669 * action immediately in on-down, as those versions of the platform
3670 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003671 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003672 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003673 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003676 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 * this event and it should continue to be propagated.
3678 * @see #onKeyUp
3679 * @see android.view.KeyEvent
3680 */
3681 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003682 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003683 if (getApplicationInfo().targetSdkVersion
3684 >= Build.VERSION_CODES.ECLAIR) {
3685 event.startTracking();
3686 } else {
3687 onBackPressed();
3688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 return true;
3690 }
RoboErik55011652014-07-09 15:05:53 -07003691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3693 return false;
3694 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003695 Window w = getWindow();
3696 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3697 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3698 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003699 return true;
3700 }
3701 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003702 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3703 // Don't consume TAB here since it's used for navigation. Arrow keys
3704 // aren't considered "typing keys" so they already won't get consumed.
3705 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 } else {
3707 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3708 boolean clearSpannable = false;
3709 boolean handled;
3710 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3711 clearSpannable = true;
3712 handled = false;
3713 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003714 handled = TextKeyListener.getInstance().onKeyDown(
3715 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 if (handled && mDefaultKeySsb.length() > 0) {
3717 // something useable has been typed - dispatch it now.
3718
3719 final String str = mDefaultKeySsb.toString();
3720 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 switch (mDefaultKeyMode) {
3723 case DEFAULT_KEYS_DIALER:
3724 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3725 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003726 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 break;
3728 case DEFAULT_KEYS_SEARCH_LOCAL:
3729 startSearch(str, false, null, false);
3730 break;
3731 case DEFAULT_KEYS_SEARCH_GLOBAL:
3732 startSearch(str, false, null, true);
3733 break;
3734 }
3735 }
3736 }
3737 if (clearSpannable) {
3738 mDefaultKeySsb.clear();
3739 mDefaultKeySsb.clearSpans();
3740 Selection.setSelection(mDefaultKeySsb,0);
3741 }
3742 return handled;
3743 }
3744 }
3745
3746 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003747 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3748 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3749 * the event).
Siarhei Vishniakouccf973f2018-10-30 14:04:57 -07003750 *
3751 * To receive this callback, you must return true from onKeyDown for the current
3752 * event stream.
3753 *
3754 * @see KeyEvent.Callback#onKeyLongPress()
3755 * @see KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003756 */
3757 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3758 return false;
3759 }
3760
3761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003763 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 * is inside a TextView will not trigger the event (unless it is a navigation
3765 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003766 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003767 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3768 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003771 * further, or <code>false</code> to indicate that you have not handled
3772 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 * @see #onKeyDown
3774 * @see KeyEvent
3775 */
3776 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003777 if (getApplicationInfo().targetSdkVersion
3778 >= Build.VERSION_CODES.ECLAIR) {
3779 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3780 && !event.isCanceled()) {
3781 onBackPressed();
3782 return true;
3783 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 return false;
3786 }
3787
3788 /**
3789 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3790 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3791 * the event).
3792 */
3793 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3794 return false;
3795 }
RoboErik55011652014-07-09 15:05:53 -07003796
Mark Renouf1c25fc52019-09-09 13:13:43 -04003797 private static final class RequestFinishCallback extends IRequestFinishCallback.Stub {
3798 private final WeakReference<Activity> mActivityRef;
3799
3800 RequestFinishCallback(WeakReference<Activity> activityRef) {
3801 mActivityRef = activityRef;
3802 }
3803
3804 @Override
3805 public void requestFinish() {
3806 Activity activity = mActivityRef.get();
3807 if (activity != null) {
3808 activity.mHandler.post(activity::finishAfterTransition);
3809 }
3810 }
3811 }
3812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003814 * Called when the activity has detected the user's press of the back
3815 * key. The default implementation simply finishes the current activity,
3816 * but you can override this to do whatever you want.
3817 */
3818 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003819 if (mActionBar != null && mActionBar.collapseActionView()) {
3820 return;
3821 }
3822
George Mount27b0dc02017-02-21 10:24:09 -08003823 FragmentManager fragmentManager = mFragments.getFragmentManager();
3824
Mark Renouf446251d2019-04-26 10:22:41 -04003825 if (!fragmentManager.isStateSaved() && fragmentManager.popBackStackImmediate()) {
3826 return;
3827 }
3828 if (!isTaskRoot()) {
3829 // If the activity is not the root of the task, allow finish to proceed normally.
3830 finishAfterTransition();
3831 return;
3832 }
3833 try {
3834 // Inform activity task manager that the activity received a back press
3835 // while at the root of the task. This call allows ActivityTaskManager
3836 // to intercept or defer finishing.
3837 ActivityTaskManager.getService().onBackPressedOnTaskRoot(mToken,
Mark Renouf1c25fc52019-09-09 13:13:43 -04003838 new RequestFinishCallback(new WeakReference<>(this)));
Mark Renouf446251d2019-04-26 10:22:41 -04003839 } catch (RemoteException e) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003840 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003841 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003842 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003843
3844 /**
3845 * Called when a key shortcut event is not handled by any of the views in the Activity.
3846 * Override this method to implement global key shortcuts for the Activity.
3847 * Key shortcuts can also be implemented by setting the
3848 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3849 *
3850 * @param keyCode The value in event.getKeyCode().
3851 * @param event Description of the key event.
3852 * @return True if the key shortcut was handled.
3853 */
3854 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003855 // Let the Action Bar have a chance at handling the shortcut.
3856 ActionBar actionBar = getActionBar();
3857 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003858 }
3859
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003860 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 * Called when a touch screen event was not handled by any of the views
3862 * under it. This is most useful to process touch events that happen
3863 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003866 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 * @return Return true if you have consumed the event, false if you haven't.
3868 * The default implementation always returns false.
3869 */
3870 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003871 if (mWindow.shouldCloseOnTouch(this, event)) {
3872 finish();
3873 return true;
3874 }
RoboErik55011652014-07-09 15:05:53 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 return false;
3877 }
RoboErik55011652014-07-09 15:05:53 -07003878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 /**
3880 * Called when the trackball was moved and not handled by any of the
3881 * views inside of the activity. So, for example, if the trackball moves
3882 * while focus is on a button, you will receive a call here because
3883 * buttons do not normally do anything with trackball events. The call
3884 * here happens <em>before</em> trackball movements are converted to
3885 * DPAD key events, which then get sent back to the view hierarchy, and
3886 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 * @return Return true if you have consumed the event, false if you haven't.
3891 * The default implementation always returns false.
3892 */
3893 public boolean onTrackballEvent(MotionEvent event) {
3894 return false;
3895 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003896
3897 /**
3898 * Called when a generic motion event was not handled by any of the
3899 * views inside of the activity.
3900 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003901 * Generic motion events describe joystick movements, mouse hovers, track pad
3902 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003903 * {@link MotionEvent#getSource() source} of the motion event specifies
3904 * the class of input that was received. Implementations of this method
3905 * must examine the bits in the source before processing the event.
3906 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003907 * </p><p>
3908 * Generic motion events with source class
3909 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3910 * are delivered to the view under the pointer. All other generic motion events are
3911 * delivered to the focused view.
3912 * </p><p>
3913 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3914 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003915 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003916 *
3917 * @param event The generic motion event being processed.
3918 *
3919 * @return Return true if you have consumed the event, false if you haven't.
3920 * The default implementation always returns false.
3921 */
3922 public boolean onGenericMotionEvent(MotionEvent event) {
3923 return false;
3924 }
3925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 /**
3927 * Called whenever a key, touch, or trackball event is dispatched to the
3928 * activity. Implement this method if you wish to know that the user has
3929 * interacted with the device in some way while your activity is running.
3930 * This callback and {@link #onUserLeaveHint} are intended to help
3931 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003932 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3935 * be accompanied by calls to {@link #onUserInteraction}. This
3936 * ensures that your activity will be told of relevant user activity such
3937 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003938 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 * <p>Note that this callback will be invoked for the touch down action
3940 * that begins a touch gesture, but may not be invoked for the touch-moved
3941 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003942 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 * @see #onUserLeaveHint()
3944 */
3945 public void onUserInteraction() {
3946 }
RoboErik55011652014-07-09 15:05:53 -07003947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3949 // Update window manager if: we have a view, that view is
3950 // attached to its parent (which will be a RootView), and
3951 // this activity is not embedded.
3952 if (mParent == null) {
3953 View decor = mDecor;
3954 if (decor != null && decor.getParent() != null) {
3955 getWindowManager().updateViewLayout(decor, params);
Adam He43c06992019-04-15 15:41:49 -07003956 if (mContentCaptureManager != null) {
3957 mContentCaptureManager.updateWindowAttributes(params);
3958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960 }
3961 }
3962
3963 public void onContentChanged() {
3964 }
3965
3966 /**
3967 * Called when the current {@link Window} of the activity gains or loses
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003968 * focus. This is the best indicator of whether this activity is the entity
3969 * with which the user actively interacts. The default implementation
3970 * clears the key tracking state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003971 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003972 * <p>Note that this provides information about global focus state, which
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003973 * is managed independently of activity lifecycle. As such, while focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 * changes will generally have some relation to lifecycle changes (an
3975 * activity that is stopped will not generally get window focus), you
3976 * should not rely on any particular order between the callbacks here and
3977 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003978 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003979 * <p>As a general rule, however, a foreground activity will have window
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 * focus... unless it has displayed other dialogs or popups that take
3981 * input focus, in which case the activity itself will not have focus
3982 * when the other windows have it. Likewise, the system may display
3983 * system-level windows (such as the status bar notification panel or
3984 * a system alert) which will temporarily take window input focus without
3985 * pausing the foreground activity.
3986 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003987 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} there can be
3988 * multiple resumed activities at the same time in multi-window mode, so
3989 * resumed state does not guarantee window focus even if there are no
3990 * overlays above.
3991 *
3992 * <p>If the intent is to know when an activity is the topmost active, the
3993 * one the user interacted with last among all activities but not including
3994 * non-activity windows like dialogs and popups, then
3995 * {@link #onTopResumedActivityChanged(boolean)} should be used. On platform
3996 * versions prior to {@link android.os.Build.VERSION_CODES#Q},
3997 * {@link #onResume} is the best indicator.
3998 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07004000 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 * @see #hasWindowFocus()
4002 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07004003 * @see View#onWindowFocusChanged(boolean)
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07004004 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 */
4006 public void onWindowFocusChanged(boolean hasFocus) {
4007 }
RoboErik55011652014-07-09 15:05:53 -07004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07004010 * Called when the main window associated with the activity has been
4011 * attached to the window manager.
4012 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
4013 * for more information.
4014 * @see View#onAttachedToWindow
4015 */
4016 public void onAttachedToWindow() {
4017 }
RoboErik55011652014-07-09 15:05:53 -07004018
Dianne Hackborn3be63c02009-08-20 19:31:38 -07004019 /**
4020 * Called when the main window associated with the activity has been
4021 * detached from the window manager.
4022 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
4023 * for more information.
4024 * @see View#onDetachedFromWindow
4025 */
4026 public void onDetachedFromWindow() {
4027 }
RoboErik55011652014-07-09 15:05:53 -07004028
Dianne Hackborn3be63c02009-08-20 19:31:38 -07004029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 * Returns true if this activity's <em>main</em> window currently has window focus.
4031 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07004032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07004034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
4036 */
4037 public boolean hasWindowFocus() {
4038 Window w = getWindow();
4039 if (w != null) {
4040 View d = w.getDecorView();
4041 if (d != null) {
4042 return d.hasWindowFocus();
4043 }
4044 }
4045 return false;
4046 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08004047
4048 /**
4049 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07004050 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08004051 */
Adam Powell117b6952014-05-05 18:14:56 -07004052 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05004053 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07004054 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05004055 if (suppressWindowTransition) {
4056 overridePendingTransition(0, 0);
4057 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08004058 }
RoboErik55011652014-07-09 15:05:53 -07004059
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004060
Skuhnece2faa52015-08-11 10:36:38 -07004061 /**
RoboErik55011652014-07-09 15:05:53 -07004062 * Called to process key events. You can override this to intercept all
4063 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004065 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07004067 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 * @return boolean Return true if this event was consumed.
4069 */
4070 public boolean dispatchKeyEvent(KeyEvent event) {
4071 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07004072
4073 // Let action bars open menus in response to the menu key prioritized over
4074 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08004075 final int keyCode = event.getKeyCode();
4076 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07004077 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
4078 return true;
4079 }
4080
Dianne Hackborn8d374262009-09-14 21:21:52 -07004081 Window win = getWindow();
4082 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 return true;
4084 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07004085 View decor = mDecor;
4086 if (decor == null) decor = win.getDecorView();
4087 return event.dispatch(this, decor != null
4088 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 }
4090
4091 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08004092 * Called to process a key shortcut event.
4093 * You can override this to intercept all key shortcut events before they are
4094 * dispatched to the window. Be sure to call this implementation for key shortcut
4095 * events that should be handled normally.
4096 *
4097 * @param event The key shortcut event.
4098 * @return True if this event was consumed.
4099 */
4100 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4101 onUserInteraction();
4102 if (getWindow().superDispatchKeyShortcutEvent(event)) {
4103 return true;
4104 }
4105 return onKeyShortcut(event.getKeyCode(), event);
4106 }
4107
4108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 * Called to process touch screen events. You can override this to
4110 * intercept all touch screen events before they are dispatched to the
4111 * window. Be sure to call this implementation for touch screen events
4112 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07004115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 * @return boolean Return true if this event was consumed.
4117 */
4118 public boolean dispatchTouchEvent(MotionEvent ev) {
4119 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
4120 onUserInteraction();
4121 }
4122 if (getWindow().superDispatchTouchEvent(ev)) {
4123 return true;
4124 }
4125 return onTouchEvent(ev);
4126 }
RoboErik55011652014-07-09 15:05:53 -07004127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 /**
4129 * Called to process trackball events. You can override this to
4130 * intercept all trackball events before they are dispatched to the
4131 * window. Be sure to call this implementation for trackball events
4132 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07004135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 * @return boolean Return true if this event was consumed.
4137 */
4138 public boolean dispatchTrackballEvent(MotionEvent ev) {
4139 onUserInteraction();
4140 if (getWindow().superDispatchTrackballEvent(ev)) {
4141 return true;
4142 }
4143 return onTrackballEvent(ev);
4144 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004145
Jeff Browncb1404e2011-01-15 18:14:15 -08004146 /**
4147 * Called to process generic motion events. You can override this to
4148 * intercept all generic motion events before they are dispatched to the
4149 * window. Be sure to call this implementation for generic motion events
4150 * that should be handled normally.
4151 *
4152 * @param ev The generic motion event.
4153 *
4154 * @return boolean Return true if this event was consumed.
4155 */
4156 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
4157 onUserInteraction();
4158 if (getWindow().superDispatchGenericMotionEvent(ev)) {
4159 return true;
4160 }
4161 return onGenericMotionEvent(ev);
4162 }
4163
svetoslavganov75986cf2009-05-14 22:28:01 -07004164 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
4165 event.setClassName(getClass().getName());
4166 event.setPackageName(getPackageName());
4167
4168 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08004169 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
4170 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07004171 event.setFullScreen(isFullScreen);
4172
4173 CharSequence title = getTitle();
4174 if (!TextUtils.isEmpty(title)) {
4175 event.getText().add(title);
4176 }
4177
4178 return true;
4179 }
4180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 /**
4182 * Default implementation of
4183 * {@link android.view.Window.Callback#onCreatePanelView}
4184 * for activities. This
4185 * simply returns null so that all panel sub-windows will have the default
4186 * menu behavior.
4187 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004188 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 public View onCreatePanelView(int featureId) {
4190 return null;
4191 }
4192
4193 /**
4194 * Default implementation of
4195 * {@link android.view.Window.Callback#onCreatePanelMenu}
4196 * for activities. This calls through to the new
4197 * {@link #onCreateOptionsMenu} method for the
4198 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4199 * so that subclasses of Activity don't need to deal with feature codes.
4200 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004201 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004203 boolean show = onCreateOptionsMenu(menu);
4204 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
4205 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 }
4207 return false;
4208 }
4209
4210 /**
4211 * Default implementation of
4212 * {@link android.view.Window.Callback#onPreparePanel}
4213 * for activities. This
4214 * calls through to the new {@link #onPrepareOptionsMenu} method for the
4215 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4216 * panel, so that subclasses of
4217 * Activity don't need to deal with feature codes.
4218 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004219 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
4220 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004222 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07004223 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 }
4225 return true;
4226 }
4227
4228 /**
4229 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07004230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 * @return The default implementation returns true.
4232 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004233 @Override
4234 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08004235 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07004236 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08004237 if (mActionBar != null) {
4238 mActionBar.dispatchMenuVisibilityChanged(true);
4239 } else {
4240 Log.e(TAG, "Tried to open action bar menu with no action bar");
4241 }
Adam Powell8515ee82010-11-30 14:09:55 -08004242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 return true;
4244 }
4245
4246 /**
4247 * Default implementation of
4248 * {@link android.view.Window.Callback#onMenuItemSelected}
4249 * for activities. This calls through to the new
4250 * {@link #onOptionsItemSelected} method for the
4251 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4252 * panel, so that subclasses of
4253 * Activity don't need to deal with feature codes.
4254 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004255 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09004256 CharSequence titleCondensed = item.getTitleCondensed();
4257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 switch (featureId) {
4259 case Window.FEATURE_OPTIONS_PANEL:
4260 // Put event logging here so it gets called even if subclass
4261 // doesn't call through to superclass's implmeentation of each
4262 // of these methods below
zobject686a8052012-12-14 21:11:08 +09004263 if(titleCondensed != null) {
4264 EventLog.writeEvent(50000, 0, titleCondensed.toString());
4265 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004266 if (onOptionsItemSelected(item)) {
4267 return true;
4268 }
Adam Powell04d58112012-04-09 10:22:12 -07004269 if (mFragments.dispatchOptionsItemSelected(item)) {
4270 return true;
4271 }
4272 if (item.getItemId() == android.R.id.home && mActionBar != null &&
4273 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
4274 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07004275 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07004276 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07004277 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07004278 }
Adam Powell04d58112012-04-09 10:22:12 -07004279 }
4280 return false;
RoboErik55011652014-07-09 15:05:53 -07004281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09004283 if(titleCondensed != null) {
4284 EventLog.writeEvent(50000, 1, titleCondensed.toString());
4285 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07004286 if (onContextItemSelected(item)) {
4287 return true;
4288 }
4289 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08004290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 default:
4292 return false;
4293 }
4294 }
RoboErik55011652014-07-09 15:05:53 -07004295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 /**
4297 * Default implementation of
4298 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
4299 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
4300 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4301 * so that subclasses of Activity don't need to deal with feature codes.
4302 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
4303 * {@link #onContextMenuClosed(Menu)} will be called.
4304 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004305 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 switch (featureId) {
4307 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004308 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 onOptionsMenuClosed(menu);
4310 break;
RoboErik55011652014-07-09 15:05:53 -07004311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 case Window.FEATURE_CONTEXT_MENU:
4313 onContextMenuClosed(menu);
4314 break;
Adam Powell8515ee82010-11-30 14:09:55 -08004315
4316 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07004317 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08004318 mActionBar.dispatchMenuVisibilityChanged(false);
4319 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 }
4321 }
4322
4323 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004324 * Declare that the options menu has changed, so should be recreated.
4325 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4326 * time it needs to be displayed.
4327 */
4328 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004329 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4330 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004331 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4332 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004333 }
RoboErik55011652014-07-09 15:05:53 -07004334
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 * Initialize the contents of the Activity's standard options menu. You
4337 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 * <p>This is only called once, the first time the options menu is
4340 * displayed. To update the menu every time it is displayed, see
4341 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004342 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004344 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4345 * they will be correctly ordered with application-defined menu items.
4346 * Deriving classes should always call through to the base implementation.
4347 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 * <p>You can safely hold on to <var>menu</var> (and any items created
4349 * from it), making modifications to it as desired, until the next
4350 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 * <p>When you add items to the menu, you can implement the Activity's
4353 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004356 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 * @return You must return true for the menu to be displayed;
4358 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004359 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 * @see #onPrepareOptionsMenu
4361 * @see #onOptionsItemSelected
4362 */
4363 public boolean onCreateOptionsMenu(Menu menu) {
4364 if (mParent != null) {
4365 return mParent.onCreateOptionsMenu(menu);
4366 }
4367 return true;
4368 }
4369
4370 /**
4371 * Prepare the Screen's standard options menu to be displayed. This is
4372 * called right before the menu is shown, every time it is shown. You can
4373 * use this method to efficiently enable/disable items or otherwise
4374 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 * <p>The default implementation updates the system menu items based on the
4377 * activity's state. Deriving classes should always call through to the
4378 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004379 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 * @param menu The options menu as last shown or first initialized by
4381 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 * @return You must return true for the menu to be displayed;
4384 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004385 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 * @see #onCreateOptionsMenu
4387 */
4388 public boolean onPrepareOptionsMenu(Menu menu) {
4389 if (mParent != null) {
4390 return mParent.onPrepareOptionsMenu(menu);
4391 }
4392 return true;
4393 }
4394
4395 /**
4396 * This hook is called whenever an item in your options menu is selected.
4397 * The default implementation simply returns false to have the normal
4398 * processing happen (calling the item's Runnable or sending a message to
4399 * its Handler as appropriate). You can use this method for any items
4400 * for which you would like to do processing without those other
4401 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004404 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004407 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 * @return boolean Return false to allow normal menu processing to
4409 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 * @see #onCreateOptionsMenu
4412 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004413 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 if (mParent != null) {
4415 return mParent.onOptionsItemSelected(item);
4416 }
4417 return false;
4418 }
4419
4420 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004421 * This method is called whenever the user chooses to navigate Up within your application's
4422 * activity hierarchy from the action bar.
4423 *
4424 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4425 * was specified in the manifest for this activity or an activity-alias to it,
4426 * default Up navigation will be handled automatically. If any activity
4427 * along the parent chain requires extra Intent arguments, the Activity subclass
4428 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4429 * to supply those arguments.</p>
4430 *
Mark Lufa434852016-08-11 17:40:33 -07004431 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004432 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4433 * from the design guide for more information about navigating within your app.</p>
4434 *
4435 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4436 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4437 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4438 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4439 *
4440 * @return true if Up navigation completed successfully and this Activity was finished,
4441 * false otherwise.
4442 */
4443 public boolean onNavigateUp() {
4444 // Automatically handle hierarchical Up navigation if the proper
4445 // metadata is available.
4446 Intent upIntent = getParentActivityIntent();
4447 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004448 if (mActivityInfo.taskAffinity == null) {
4449 // Activities with a null affinity are special; they really shouldn't
4450 // specify a parent activity intent in the first place. Just finish
4451 // the current activity and call it a day.
4452 finish();
4453 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004454 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004455 onCreateNavigateUpTaskStack(b);
4456 onPrepareNavigateUpTaskStack(b);
4457 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004458
4459 // We can't finishAffinity if we have a result.
4460 // Fall back and simply finish the current activity instead.
4461 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4462 // Tell the developer what's going on to avoid hair-pulling.
4463 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4464 finish();
4465 } else {
4466 finishAffinity();
4467 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004468 } else {
4469 navigateUpTo(upIntent);
4470 }
4471 return true;
4472 }
4473 return false;
4474 }
4475
4476 /**
4477 * This is called when a child activity of this one attempts to navigate up.
4478 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4479 *
4480 * @param child The activity making the call.
Charles Chenc380f022019-08-13 18:39:17 +08004481 * @deprecated Use {@link #onNavigateUp()} instead.
Adam Powelldd8fab22012-03-22 17:47:27 -07004482 */
Charles Chenc380f022019-08-13 18:39:17 +08004483 @Deprecated
Adam Powelldd8fab22012-03-22 17:47:27 -07004484 public boolean onNavigateUpFromChild(Activity child) {
4485 return onNavigateUp();
4486 }
4487
4488 /**
4489 * Define the synthetic task stack that will be generated during Up navigation from
4490 * a different task.
4491 *
4492 * <p>The default implementation of this method adds the parent chain of this activity
4493 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4494 * may choose to override this method to construct the desired task stack in a different
4495 * way.</p>
4496 *
Adam Powellf0195952012-05-02 21:38:54 -07004497 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4498 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4499 * returned by {@link #getParentActivityIntent()}.</p>
4500 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004501 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4502 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4503 *
4504 * @param builder An empty TaskStackBuilder - the application should add intents representing
4505 * the desired task stack
4506 */
4507 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4508 builder.addParentStack(this);
4509 }
4510
4511 /**
4512 * Prepare the synthetic task stack that will be generated during Up navigation
4513 * from a different task.
4514 *
4515 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4516 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4517 * If any extra data should be added to these intents before launching the new task,
4518 * the application should override this method and add that data here.</p>
4519 *
4520 * @param builder A TaskStackBuilder that has been populated with Intents by
4521 * onCreateNavigateUpTaskStack.
4522 */
4523 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4524 }
4525
4526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 * This hook is called whenever the options menu is being closed (either by the user canceling
4528 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004529 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 * @param menu The options menu as last shown or first initialized by
4531 * onCreateOptionsMenu().
4532 */
4533 public void onOptionsMenuClosed(Menu menu) {
4534 if (mParent != null) {
4535 mParent.onOptionsMenuClosed(menu);
4536 }
4537 }
RoboErik55011652014-07-09 15:05:53 -07004538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 /**
4540 * Programmatically opens the options menu. If the options menu is already
4541 * open, this method does nothing.
4542 */
4543 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004544 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4545 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004546 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 }
RoboErik55011652014-07-09 15:05:53 -07004549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 /**
4551 * Progammatically closes the options menu. If the options menu is already
4552 * closed, this method does nothing.
4553 */
4554 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004555 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4556 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004557 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 }
4560
4561 /**
4562 * Called when a context menu for the {@code view} is about to be shown.
4563 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4564 * time the context menu is about to be shown and should be populated for
4565 * the view (or item inside the view for {@link AdapterView} subclasses,
4566 * this can be found in the {@code menuInfo})).
4567 * <p>
4568 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4569 * item has been selected.
4570 * <p>
4571 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004572 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 */
4574 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4575 }
4576
4577 /**
4578 * Registers a context menu to be shown for the given view (multiple views
4579 * can show the context menu). This method will set the
4580 * {@link OnCreateContextMenuListener} on the view to this activity, so
4581 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4582 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004583 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 * @see #unregisterForContextMenu(View)
4585 * @param view The view that should show a context menu.
4586 */
4587 public void registerForContextMenu(View view) {
4588 view.setOnCreateContextMenuListener(this);
4589 }
RoboErik55011652014-07-09 15:05:53 -07004590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 /**
4592 * Prevents a context menu to be shown for the given view. This method will remove the
4593 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004594 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 * @see #registerForContextMenu(View)
4596 * @param view The view that should stop showing a context menu.
4597 */
4598 public void unregisterForContextMenu(View view) {
4599 view.setOnCreateContextMenuListener(null);
4600 }
RoboErik55011652014-07-09 15:05:53 -07004601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 /**
4603 * Programmatically opens the context menu for a particular {@code view}.
4604 * The {@code view} should have been added via
4605 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004606 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 * @param view The view to show the context menu for.
4608 */
4609 public void openContextMenu(View view) {
4610 view.showContextMenu();
4611 }
RoboErik55011652014-07-09 15:05:53 -07004612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 /**
4614 * Programmatically closes the most recently opened context menu, if showing.
4615 */
4616 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004617 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4618 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 }
RoboErik55011652014-07-09 15:05:53 -07004621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 /**
4623 * This hook is called whenever an item in a context menu is selected. The
4624 * default implementation simply returns false to have the normal processing
4625 * happen (calling the item's Runnable or sending a message to its Handler
4626 * as appropriate). You can use this method for any items for which you
4627 * would like to do processing without those other facilities.
4628 * <p>
4629 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4630 * View that added this menu item.
4631 * <p>
4632 * Derived classes should call through to the base class for it to perform
4633 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 * @param item The context menu item that was selected.
4636 * @return boolean Return false to allow normal context menu processing to
4637 * proceed, true to consume it here.
4638 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004639 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 if (mParent != null) {
4641 return mParent.onContextItemSelected(item);
4642 }
4643 return false;
4644 }
4645
4646 /**
4647 * This hook is called whenever the context menu is being closed (either by
4648 * the user canceling the menu with the back/menu button, or when an item is
4649 * selected).
RoboErik55011652014-07-09 15:05:53 -07004650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 * @param menu The context menu that is being closed.
4652 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004653 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 if (mParent != null) {
4655 mParent.onContextMenuClosed(menu);
4656 }
4657 }
4658
4659 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004660 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004662 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 protected Dialog onCreateDialog(int id) {
4664 return null;
4665 }
4666
4667 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004668 * Callback for creating dialogs that are managed (saved and restored) for you
4669 * by the activity. The default implementation calls through to
4670 * {@link #onCreateDialog(int)} for compatibility.
4671 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004672 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4673 * or later, consider instead using a {@link DialogFragment} instead.</em>
4674 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004675 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4676 * this method the first time, and hang onto it thereafter. Any dialog
4677 * that is created by this method will automatically be saved and restored
4678 * for you, including whether it is showing.
4679 *
4680 * <p>If you would like the activity to manage saving and restoring dialogs
4681 * for you, you should override this method and handle any ids that are
4682 * passed to {@link #showDialog}.
4683 *
4684 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4685 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4686 *
4687 * @param id The id of the dialog.
4688 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4689 * @return The dialog. If you return null, the dialog will not be created.
4690 *
4691 * @see #onPrepareDialog(int, Dialog, Bundle)
4692 * @see #showDialog(int, Bundle)
4693 * @see #dismissDialog(int)
4694 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004695 *
4696 * @deprecated Use the new {@link DialogFragment} class with
4697 * {@link FragmentManager} instead; this is also
4698 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004699 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004700 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004701 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004702 protected Dialog onCreateDialog(int id, Bundle args) {
4703 return onCreateDialog(id);
4704 }
4705
4706 /**
4707 * @deprecated Old no-arguments version of
4708 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4709 */
4710 @Deprecated
4711 protected void onPrepareDialog(int id, Dialog dialog) {
4712 dialog.setOwnerActivity(this);
4713 }
4714
4715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004717 * shown. The default implementation calls through to
4718 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 * <p>
4721 * Override this if you need to update a managed dialog based on the state
4722 * of the application each time it is shown. For example, a time picker
4723 * dialog might want to be updated with the current time. You should call
4724 * through to the superclass's implementation. The default implementation
4725 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004726 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 * @param id The id of the managed dialog.
4728 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004729 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4730 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 * @see #showDialog(int)
4732 * @see #dismissDialog(int)
4733 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004734 *
4735 * @deprecated Use the new {@link DialogFragment} class with
4736 * {@link FragmentManager} instead; this is also
4737 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004739 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004740 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4741 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 }
4743
4744 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004745 * Simple version of {@link #showDialog(int, Bundle)} that does not
4746 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4747 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004748 *
4749 * @deprecated Use the new {@link DialogFragment} class with
4750 * {@link FragmentManager} instead; this is also
4751 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004752 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004753 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004754 public final void showDialog(int id) {
4755 showDialog(id, null);
4756 }
4757
4758 /**
4759 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 * will be made with the same id the first time this is called for a given
4761 * id. From thereafter, the dialog will be automatically saved and restored.
4762 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004763 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4764 * or later, consider instead using a {@link DialogFragment} instead.</em>
4765 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004766 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767 * be made to provide an opportunity to do any timely preparation.
4768 *
4769 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004770 * @param args Arguments to pass through to the dialog. These will be saved
4771 * and restored for you. Note that if the dialog is already created,
4772 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4773 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004774 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004775 * @return Returns true if the Dialog was created; false is returned if
4776 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004777 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004778 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004779 * @see #onCreateDialog(int, Bundle)
4780 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 * @see #dismissDialog(int)
4782 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004783 *
4784 * @deprecated Use the new {@link DialogFragment} class with
4785 * {@link FragmentManager} instead; this is also
4786 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004788 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004789 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004791 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004793 ManagedDialog md = mManagedDialogs.get(id);
4794 if (md == null) {
4795 md = new ManagedDialog();
4796 md.mDialog = createDialog(id, null, args);
4797 if (md.mDialog == null) {
4798 return false;
4799 }
4800 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 }
RoboErik55011652014-07-09 15:05:53 -07004802
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004803 md.mArgs = args;
4804 onPrepareDialog(id, md.mDialog, args);
4805 md.mDialog.show();
4806 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 }
4808
4809 /**
4810 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4811 *
4812 * @param id The id of the managed dialog.
4813 *
4814 * @throws IllegalArgumentException if the id was not previously shown via
4815 * {@link #showDialog(int)}.
4816 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004817 * @see #onCreateDialog(int, Bundle)
4818 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 * @see #showDialog(int)
4820 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004821 *
4822 * @deprecated Use the new {@link DialogFragment} class with
4823 * {@link FragmentManager} instead; this is also
4824 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004826 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 public final void dismissDialog(int id) {
4828 if (mManagedDialogs == null) {
4829 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 }
RoboErik55011652014-07-09 15:05:53 -07004831
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004832 final ManagedDialog md = mManagedDialogs.get(id);
4833 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 throw missingDialog(id);
4835 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004836 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 }
4838
4839 /**
4840 * Creates an exception to throw if a user passed in a dialog id that is
4841 * unexpected.
4842 */
4843 private IllegalArgumentException missingDialog(int id) {
4844 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4845 + "shown via Activity#showDialog");
4846 }
4847
4848 /**
4849 * Removes any internal references to a dialog managed by this Activity.
4850 * If the dialog is showing, it will dismiss it as part of the clean up.
4851 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004852 * <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 -08004853 * want to avoid the overhead of saving and restoring it in the future.
4854 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004855 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4856 * will not throw an exception if you try to remove an ID that does not
4857 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 * @param id The id of the managed dialog.
4860 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004861 * @see #onCreateDialog(int, Bundle)
4862 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 * @see #showDialog(int)
4864 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004865 *
4866 * @deprecated Use the new {@link DialogFragment} class with
4867 * {@link FragmentManager} instead; this is also
4868 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004870 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004872 if (mManagedDialogs != null) {
4873 final ManagedDialog md = mManagedDialogs.get(id);
4874 if (md != null) {
4875 md.mDialog.dismiss();
4876 mManagedDialogs.remove(id);
4877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 }
4880
4881 /**
4882 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004883 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004884 * <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 -07004885 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004886 * calling this function is the same as calling
4887 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4888 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 * <p>You can override this function to force global search, e.g. in response to a dedicated
4891 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004892 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004893 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4894 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4895 * return false and you must supply your own custom implementation if you want to support
4896 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004897 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004898 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004899 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4900 * not respond to search. The default implementation always returns {@code true}, except
4901 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004902 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 * @see android.app.SearchManager
4904 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004905 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4906 mSearchEvent = searchEvent;
4907 boolean result = onSearchRequested();
4908 mSearchEvent = null;
4909 return result;
4910 }
4911
4912 /**
4913 * @see #onSearchRequested(SearchEvent)
4914 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004916 final int uiMode = getResources().getConfiguration().uiMode
4917 & Configuration.UI_MODE_TYPE_MASK;
4918 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4919 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004920 startSearch(null, false, null, false);
4921 return true;
4922 } else {
4923 return false;
4924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 }
RoboErik55011652014-07-09 15:05:53 -07004926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004928 * During the onSearchRequested() callbacks, this function will return the
4929 * {@link SearchEvent} that triggered the callback, if it exists.
4930 *
4931 * @return SearchEvent The SearchEvent that triggered the {@link
4932 * #onSearchRequested} callback.
4933 */
4934 public final SearchEvent getSearchEvent() {
4935 return mSearchEvent;
4936 }
4937
4938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004940 *
4941 * <p>It is typically called from onSearchRequested(), either directly from
4942 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004944 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 * is to inject specific data such as context data, it is preferred to <i>override</i>
4946 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004947 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004948 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4949 * not supported.
4950 *
RoboErik55011652014-07-09 15:05:53 -07004951 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004953 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 * any further typing will replace it. This is useful for cases where an entire pre-formed
4955 * query is being inserted. If false, the selection point will be placed at the end of the
4956 * inserted query. This is useful when the inserted query is text that the user entered,
4957 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4958 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004959 * @param appSearchData An application can insert application-specific
4960 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 * searches. This data will be returned with SEARCH intent(s). Null if
4962 * no extra data is required.
4963 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004964 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004965 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004967 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004968 * @see android.app.SearchManager
4969 * @see #onSearchRequested
4970 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004971 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4972 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004973 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004974 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004975 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
4977
4978 /**
krosaend2d60142009-08-17 08:56:48 -07004979 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4980 * the search dialog. Made available for testing purposes.
4981 *
4982 * @param query The query to trigger. If empty, the request will be ignored.
4983 * @param appSearchData An application can insert application-specific
4984 * context here, in order to improve quality or specificity of its own
4985 * searches. This data will be returned with SEARCH intent(s). Null if
4986 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004987 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004988 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004989 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004990 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004991 }
4992
4993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 * Request that key events come to this activity. Use this if your
4995 * activity has no views with focus, but the activity still wants
4996 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004997 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 * @see android.view.Window#takeKeyEvents
4999 */
5000 public void takeKeyEvents(boolean get) {
5001 getWindow().takeKeyEvents(get);
5002 }
5003
5004 /**
5005 * Enable extended window features. This is a convenience for calling
5006 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07005007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 * @param featureId The desired feature as defined in
5009 * {@link android.view.Window}.
5010 * @return Returns true if the requested feature is supported and now
5011 * enabled.
RoboErik55011652014-07-09 15:05:53 -07005012 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 * @see android.view.Window#requestFeature
5014 */
5015 public final boolean requestWindowFeature(int featureId) {
5016 return getWindow().requestFeature(featureId);
5017 }
5018
5019 /**
5020 * Convenience for calling
5021 * {@link android.view.Window#setFeatureDrawableResource}.
5022 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07005023 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005024 getWindow().setFeatureDrawableResource(featureId, resId);
5025 }
5026
5027 /**
5028 * Convenience for calling
5029 * {@link android.view.Window#setFeatureDrawableUri}.
5030 */
5031 public final void setFeatureDrawableUri(int featureId, Uri uri) {
5032 getWindow().setFeatureDrawableUri(featureId, uri);
5033 }
5034
5035 /**
5036 * Convenience for calling
5037 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
5038 */
5039 public final void setFeatureDrawable(int featureId, Drawable drawable) {
5040 getWindow().setFeatureDrawable(featureId, drawable);
5041 }
5042
5043 /**
5044 * Convenience for calling
5045 * {@link android.view.Window#setFeatureDrawableAlpha}.
5046 */
5047 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
5048 getWindow().setFeatureDrawableAlpha(featureId, alpha);
5049 }
5050
5051 /**
5052 * Convenience for calling
5053 * {@link android.view.Window#getLayoutInflater}.
5054 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005055 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 public LayoutInflater getLayoutInflater() {
5057 return getWindow().getLayoutInflater();
5058 }
5059
5060 /**
5061 * Returns a {@link MenuInflater} with this context.
5062 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005063 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07005065 // Make sure that action views can get an appropriate theme.
5066 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07005067 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07005068 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07005069 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07005070 } else {
5071 mMenuInflater = new MenuInflater(this);
5072 }
5073 }
5074 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 }
5076
5077 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07005078 public void setTheme(int resid) {
5079 super.setTheme(resid);
5080 mWindow.setTheme(resid);
5081 }
5082
5083 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07005084 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005085 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 if (mParent == null) {
5087 super.onApplyThemeResource(theme, resid, first);
5088 } else {
5089 try {
5090 theme.setTo(mParent.getTheme());
5091 } catch (Exception e) {
5092 // Empty
5093 }
5094 theme.applyStyle(resid, false);
5095 }
Winson073a5262016-02-29 16:04:40 -08005096
5097 // Get the primary color and update the TaskDescription for this activity
5098 TypedArray a = theme.obtainStyledAttributes(
5099 com.android.internal.R.styleable.ActivityTaskDescription);
5100 if (mTaskDescription.getPrimaryColor() == 0) {
5101 int colorPrimary = a.getColor(
5102 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
5103 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
5104 mTaskDescription.setPrimaryColor(colorPrimary);
5105 }
5106 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02005107
5108 int colorBackground = a.getColor(
5109 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
5110 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
5111 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08005112 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02005113
5114 final int statusBarColor = a.getColor(
5115 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
5116 if (statusBarColor != 0) {
5117 mTaskDescription.setStatusBarColor(statusBarColor);
5118 }
5119
5120 final int navigationBarColor = a.getColor(
5121 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
5122 if (navigationBarColor != 0) {
5123 mTaskDescription.setNavigationBarColor(navigationBarColor);
5124 }
5125
Adrian Roos4c864592019-04-10 14:47:57 +02005126 final int targetSdk = getApplicationInfo().targetSdkVersion;
5127 final boolean targetPreQ = targetSdk < Build.VERSION_CODES.Q;
5128 if (!targetPreQ) {
5129 mTaskDescription.setEnsureStatusBarContrastWhenTransparent(a.getBoolean(
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005130 R.styleable.ActivityTaskDescription_enforceStatusBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005131 false));
5132 mTaskDescription.setEnsureNavigationBarContrastWhenTransparent(a.getBoolean(
Adrian Roos6eb1ed32019-04-08 16:23:43 +02005133 R.styleable
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005134 .ActivityTaskDescription_enforceNavigationBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005135 true));
5136 }
5137
Winson073a5262016-02-29 16:04:40 -08005138 a.recycle();
5139 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00005140 }
5141
5142 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08005143 * Requests permissions to be granted to this application. These permissions
5144 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005145 * and they should have protection level {@link
5146 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
5147 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08005148 * <p>
5149 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
5150 * are granted at install time if requested in the manifest. Signature permissions
5151 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
5152 * install time if requested in the manifest and the signature of your app matches
5153 * the signature of the app declaring the permissions.
5154 * </p>
5155 * <p>
5156 * If your app does not have the requested permissions the user will be presented
5157 * with UI for accepting them. After the user has accepted or rejected the
5158 * requested permissions you will receive a callback on {@link
5159 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
5160 * permissions were granted or not.
5161 * </p>
5162 * <p>
5163 * Note that requesting a permission does not guarantee it will be granted and
5164 * your app should be able to run without having this permission.
5165 * </p>
5166 * <p>
5167 * This method may start an activity allowing the user to choose which permissions
5168 * to grant and which to reject. Hence, you should be prepared that your activity
5169 * may be paused and resumed. Further, granting some permissions may require
5170 * a restart of you application. In such a case, the system will recreate the
5171 * activity stack before delivering the result to {@link
5172 * #onRequestPermissionsResult(int, String[], int[])}.
5173 * </p>
5174 * <p>
5175 * When checking whether you have a permission you should use {@link
5176 * #checkSelfPermission(String)}.
5177 * </p>
5178 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07005179 * Calling this API for permissions already granted to your app would show UI
5180 * to the user to decide whether the app can still hold these permissions. This
5181 * can be useful if the way your app uses data guarded by the permissions
5182 * changes significantly.
5183 * </p>
5184 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07005185 * You cannot request a permission if your activity sets {@link
5186 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5187 * <code>true</code> because in this case the activity would not receive
5188 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
5189 * </p>
5190 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005191 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07005192 * RuntimePermissions</a> sample app demonstrates how to use this method to
5193 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08005194 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005195 *
Svet Ganovf66381c2016-02-18 20:02:36 -08005196 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08005197 * @param requestCode Application specific request code to match with a result
5198 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07005199 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08005200 *
Paul Miller77a0e132016-02-11 13:46:49 -08005201 * @throws IllegalArgumentException if requestCode is negative.
5202 *
Svetoslavc6d1c342015-02-26 14:44:43 -08005203 * @see #onRequestPermissionsResult(int, String[], int[])
5204 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07005205 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08005206 */
5207 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08005208 if (requestCode < 0) {
5209 throw new IllegalArgumentException("requestCode should be >= 0");
5210 }
Svetoslavffb32b12015-10-15 16:54:00 -07005211 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00005212 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07005213 // Dispatch the callback with empty arrays which means a cancellation.
5214 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
5215 return;
5216 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005217 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07005218 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07005219 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08005220 }
5221
5222 /**
5223 * Callback for the result from requesting permissions. This method
5224 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07005225 * <p>
5226 * <strong>Note:</strong> It is possible that the permissions request interaction
5227 * with the user is interrupted. In this case you will receive empty permissions
5228 * and results arrays which should be treated as a cancellation.
5229 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005230 *
5231 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
5232 * @param permissions The requested permissions. Never null.
5233 * @param grantResults The grant results for the corresponding permissions
5234 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
5235 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
5236 *
5237 * @see #requestPermissions(String[], int)
5238 */
5239 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
5240 @NonNull int[] grantResults) {
5241 /* callback - no nothing */
5242 }
5243
5244 /**
Svetoslav20770dd2015-05-29 15:43:04 -07005245 * Gets whether you should show UI with rationale for requesting a permission.
5246 * You should do this only if you do not have the permission and the context in
5247 * which the permission is requested does not clearly communicate to the user
5248 * what would be the benefit from granting this permission.
5249 * <p>
5250 * For example, if you write a camera app, requesting the camera permission
5251 * would be expected by the user and no rationale for why it is requested is
5252 * needed. If however, the app needs location for tagging photos then a non-tech
5253 * savvy user may wonder how location is related to taking photos. In this case
5254 * you may choose to show UI with rationale of requesting this permission.
5255 * </p>
5256 *
5257 * @param permission A permission your app wants to request.
5258 * @return Whether you can show permission rationale UI.
5259 *
5260 * @see #checkSelfPermission(String)
5261 * @see #requestPermissions(String[], int)
5262 * @see #onRequestPermissionsResult(int, String[], int[])
5263 */
Svetoslav9ce9a152015-06-02 17:59:31 -07005264 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07005265 return getPackageManager().shouldShowRequestPermissionRationale(permission);
5266 }
5267
5268 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005269 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
5270 * with no options.
5271 *
5272 * @param intent The intent to start.
5273 * @param requestCode If >= 0, this code will be returned in
5274 * onActivityResult() when the activity exits.
5275 *
5276 * @throws android.content.ActivityNotFoundException
5277 *
George Mount0a778ed2013-12-13 13:35:36 -08005278 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07005279 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005280 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005281 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005282 }
5283
5284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 * Launch an activity for which you would like a result when it finished.
5286 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08005287 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07005288 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07005290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 * <p>Note that this method should only be used with Intent protocols
5292 * that are defined to return a result. In other protocols (such as
5293 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
5294 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05005295 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
5296 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005297 *
RoboErik55011652014-07-09 15:05:53 -07005298 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08005300 * activity, then your window will not be displayed until a result is
5301 * returned back from the started activity. This is to avoid visible
5302 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005303 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 * <p>This method throws {@link android.content.ActivityNotFoundException}
5305 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 * @param intent The intent to start.
5308 * @param requestCode If >= 0, this code will be returned in
5309 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005310 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005311 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005312 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005313 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005315 *
George Mount0a778ed2013-12-13 13:35:36 -08005316 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005318 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
5319 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07005321 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 Instrumentation.ActivityResult ar =
5323 mInstrumentation.execStartActivity(
5324 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005325 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 if (ar != null) {
5327 mMainThread.sendActivityResult(
5328 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
5329 ar.getResultData());
5330 }
5331 if (requestCode >= 0) {
5332 // If this start is requesting a result, we can avoid making
5333 // the activity visible until the result is received. Setting
5334 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5335 // activity hidden during this time, to avoid flickering.
5336 // This can only be done when a result is requested because
5337 // that guarantees we will get information back when the
5338 // activity is finished, no matter what happens to it.
5339 mStartedActivity = true;
5340 }
Adam Powell14874662013-07-18 19:42:41 -07005341
George Mount41725de2015-04-09 08:23:05 -07005342 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005343 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005345 if (options != null) {
5346 mParent.startActivityFromChild(this, intent, requestCode, options);
5347 } else {
5348 // Note we want to go through this method for compatibility with
5349 // existing applications that may have overridden it.
5350 mParent.startActivityFromChild(this, intent, requestCode);
5351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 }
George Mount41725de2015-04-09 08:23:05 -07005353 }
5354
5355 /**
5356 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5357 *
5358 * @param options The ActivityOptions bundle used to start an Activity.
5359 */
5360 private void cancelInputsAndStartExitTransition(Bundle options) {
5361 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5362 if (decor != null) {
5363 decor.cancelPendingInputEvents();
5364 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005365 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005366 mActivityTransitionState.startExitOutTransition(this, options);
5367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 }
5369
Chet Haaseb64e777f2016-12-09 07:17:10 -08005370 /**
5371 * Returns whether there are any activity transitions currently running on this
5372 * activity. A return value of {@code true} can mean that either an enter or
5373 * exit transition is running, including whether the background of the activity
5374 * is animating as a part of that transition.
5375 *
5376 * @return true if a transition is currently running on this activity, false otherwise.
5377 */
5378 public boolean isActivityTransitionRunning() {
5379 return mActivityTransitionState.isTransitionRunning();
5380 }
5381
George Mount413739e2016-06-08 07:13:37 -07005382 private Bundle transferSpringboardActivityOptions(Bundle options) {
5383 if (options == null && (mWindow != null && !mWindow.isActive())) {
5384 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005385 if (activityOptions != null &&
5386 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005387 return activityOptions.toBundle();
5388 }
5389 }
5390 return options;
5391 }
5392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005394 * @hide Implement to provide correct calling token.
5395 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005396 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005397 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5398 startActivityForResultAsUser(intent, requestCode, null, user);
5399 }
5400
5401 /**
5402 * @hide Implement to provide correct calling token.
5403 */
5404 public void startActivityForResultAsUser(Intent intent, int requestCode,
5405 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005406 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5407 }
5408
5409 /**
5410 * @hide Implement to provide correct calling token.
5411 */
5412 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5413 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005414 if (mParent != null) {
5415 throw new RuntimeException("Can't be called from a child");
5416 }
George Mount413739e2016-06-08 07:13:37 -07005417 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005418 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005419 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005420 options, user);
5421 if (ar != null) {
5422 mMainThread.sendActivityResult(
5423 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5424 }
5425 if (requestCode >= 0) {
5426 // If this start is requesting a result, we can avoid making
5427 // the activity visible until the result is received. Setting
5428 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5429 // activity hidden during this time, to avoid flickering.
5430 // This can only be done when a result is requested because
5431 // that guarantees we will get information back when the
5432 // activity is finished, no matter what happens to it.
5433 mStartedActivity = true;
5434 }
5435
George Mount41725de2015-04-09 08:23:05 -07005436 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005437 }
5438
5439 /**
5440 * @hide Implement to provide correct calling token.
5441 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005442 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005443 public void startActivityAsUser(Intent intent, UserHandle user) {
5444 startActivityAsUser(intent, null, user);
5445 }
5446
5447 /**
5448 * @hide Implement to provide correct calling token.
5449 */
5450 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5451 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005452 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005453 }
George Mount413739e2016-06-08 07:13:37 -07005454 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005455 Instrumentation.ActivityResult ar =
5456 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005457 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005458 intent, -1, options, user);
5459 if (ar != null) {
5460 mMainThread.sendActivityResult(
5461 mToken, mEmbeddedID, -1, ar.getResultCode(),
5462 ar.getResultData());
5463 }
George Mount41725de2015-04-09 08:23:05 -07005464 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005465 }
5466
5467 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005468 * Start a new activity as if it was started by the activity that started our
5469 * current activity. This is for the resolver and chooser activities, which operate
5470 * as intermediaries that dispatch their intent to the target the user selects -- to
5471 * do this, they must perform all security checks including permission grants as if
5472 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005473 * @param intent The Intent to start.
5474 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005475 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005476 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5477 * caller it is doing the start is, is actually allowed to start the target activity.
5478 * If you set this to true, you must set an explicit component in the Intent and do any
5479 * appropriate security checks yourself.
5480 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005481 * @hide
5482 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005483 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005484 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005485 if (mParent != null) {
5486 throw new RuntimeException("Can't be called from a child");
5487 }
George Mount413739e2016-06-08 07:13:37 -07005488 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005489 Instrumentation.ActivityResult ar =
5490 mInstrumentation.execStartActivityAsCaller(
5491 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005492 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005493 if (ar != null) {
5494 mMainThread.sendActivityResult(
5495 mToken, mEmbeddedID, -1, ar.getResultCode(),
5496 ar.getResultData());
5497 }
George Mount41725de2015-04-09 08:23:05 -07005498 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005499 }
5500
5501 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005502 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5503 * Intent, int, int, int, Bundle)} with no options.
5504 *
5505 * @param intent The IntentSender to launch.
5506 * @param requestCode If >= 0, this code will be returned in
5507 * onActivityResult() when the activity exits.
5508 * @param fillInIntent If non-null, this will be provided as the
5509 * intent parameter to {@link IntentSender#sendIntent}.
5510 * @param flagsMask Intent flags in the original IntentSender that you
5511 * would like to change.
5512 * @param flagsValues Desired values for any bits set in
5513 * <var>flagsMask</var>
5514 * @param extraFlags Always set to 0.
5515 */
5516 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005517 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005518 throws IntentSender.SendIntentException {
5519 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5520 flagsValues, extraFlags, null);
5521 }
5522
5523 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005524 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005525 * to use a IntentSender to describe the activity to be started. If
5526 * the IntentSender is for an activity, that activity will be started
5527 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5528 * here; otherwise, its associated action will be executed (such as
5529 * sending a broadcast) as if you had called
5530 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005531 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005532 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005533 * @param requestCode If >= 0, this code will be returned in
5534 * onActivityResult() when the activity exits.
5535 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005536 * intent parameter to {@link IntentSender#sendIntent}.
5537 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005538 * would like to change.
5539 * @param flagsValues Desired values for any bits set in
5540 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005541 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005542 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005543 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005544 * Context.startActivity(Intent, Bundle)} for more details. If options
5545 * have also been supplied by the IntentSender, options given here will
5546 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005547 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005548 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005549 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005550 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005551 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005552 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5553 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005554 } else if (options != null) {
5555 mParent.startIntentSenderFromChild(this, intent, requestCode,
5556 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005557 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005558 // Note we want to go through this call for compatibility with
5559 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005560 mParent.startIntentSenderFromChild(this, intent, requestCode,
5561 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005562 }
5563 }
5564
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005565 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5566 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005567 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005568 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005569 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005570 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005571 String resolvedType = null;
5572 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005573 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005574 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005575 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5576 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005577 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005578 .startActivityIntentSender(mMainThread.getApplicationThread(),
5579 intent != null ? intent.getTarget() : null,
5580 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005581 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005582 requestCode, flagsMask, flagsValues, options);
5583 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005584 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005585 }
5586 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005587
5588 if (options != null) {
5589 // Only when the options are not null, as the intent can point to something other
5590 // than an Activity.
5591 cancelInputsAndStartExitTransition(options);
5592 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005593 } catch (RemoteException e) {
5594 }
5595 if (requestCode >= 0) {
5596 // If this start is requesting a result, we can avoid making
5597 // the activity visible until the result is received. Setting
5598 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5599 // activity hidden during this time, to avoid flickering.
5600 // This can only be done when a result is requested because
5601 // that guarantees we will get information back when the
5602 // activity is finished, no matter what happens to it.
5603 mStartedActivity = true;
5604 }
5605 }
5606
5607 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005608 * Same as {@link #startActivity(Intent, Bundle)} with no options
5609 * specified.
5610 *
5611 * @param intent The intent to start.
5612 *
5613 * @throws android.content.ActivityNotFoundException
5614 *
Mark Lufa434852016-08-11 17:40:33 -07005615 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005616 * @see #startActivityForResult
5617 */
5618 @Override
5619 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005620 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005621 }
5622
5623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 * Launch a new activity. You will not receive any information about when
5625 * the activity exits. This implementation overrides the base version,
5626 * providing information about
5627 * the activity performing the launch. Because of this additional
5628 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5629 * required; if not specified, the new activity will be added to the
5630 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 * <p>This method throws {@link android.content.ActivityNotFoundException}
5633 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005634 *
5635 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005636 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005637 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005638 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005641 *
Mark Lufa434852016-08-11 17:40:33 -07005642 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005643 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 */
5645 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005646 public void startActivity(Intent intent, @Nullable Bundle options) {
TYM Tsai8af721d2019-07-24 17:11:59 +08005647 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)
5648 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY)) {
5649 if (TextUtils.equals(getPackageName(),
5650 intent.resolveActivity(getPackageManager()).getPackageName())) {
5651 // Apply Autofill restore mechanism on the started activity by startActivity()
5652 final IBinder token =
5653 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN);
5654 // Remove restore ability from current activity
5655 mIntent.removeExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN);
5656 mIntent.removeExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY);
5657 // Put restore token
5658 intent.putExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN, token);
5659 intent.putExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY, true);
5660 }
5661 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005662 if (options != null) {
5663 startActivityForResult(intent, -1, options);
5664 } else {
5665 // Note we want to go through this call for compatibility with
5666 // applications that may have overridden the method.
5667 startActivityForResult(intent, -1);
5668 }
5669 }
5670
5671 /**
5672 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5673 * specified.
5674 *
5675 * @param intents The intents to start.
5676 *
5677 * @throws android.content.ActivityNotFoundException
5678 *
Mark Lufa434852016-08-11 17:40:33 -07005679 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005680 * @see #startActivityForResult
5681 */
5682 @Override
5683 public void startActivities(Intent[] intents) {
5684 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 }
5686
5687 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005688 * Launch a new activity. You will not receive any information about when
5689 * the activity exits. This implementation overrides the base version,
5690 * providing information about
5691 * the activity performing the launch. Because of this additional
5692 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5693 * required; if not specified, the new activity will be added to the
5694 * task of the caller.
5695 *
5696 * <p>This method throws {@link android.content.ActivityNotFoundException}
5697 * if there was no Activity found to run the given Intent.
5698 *
5699 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005700 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005701 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005702 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005703 *
5704 * @throws android.content.ActivityNotFoundException
5705 *
Mark Lufa434852016-08-11 17:40:33 -07005706 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005707 * @see #startActivityForResult
5708 */
5709 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005710 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005711 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005712 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005713 }
5714
5715 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005716 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5717 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005718 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005719 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005720 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005721 * intent parameter to {@link IntentSender#sendIntent}.
5722 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005723 * would like to change.
5724 * @param flagsValues Desired values for any bits set in
5725 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005726 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005727 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005728 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005729 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005730 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005731 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5732 extraFlags, null);
5733 }
5734
5735 /**
5736 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5737 * to start; see
5738 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5739 * for more information.
5740 *
5741 * @param intent The IntentSender to launch.
5742 * @param fillInIntent If non-null, this will be provided as the
5743 * intent parameter to {@link IntentSender#sendIntent}.
5744 * @param flagsMask Intent flags in the original IntentSender that you
5745 * would like to change.
5746 * @param flagsValues Desired values for any bits set in
5747 * <var>flagsMask</var>
5748 * @param extraFlags Always set to 0.
5749 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005750 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005751 * Context.startActivity(Intent, Bundle)} for more details. If options
5752 * have also been supplied by the IntentSender, options given here will
5753 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005754 */
5755 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005756 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005757 Bundle options) throws IntentSender.SendIntentException {
5758 if (options != null) {
5759 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5760 flagsValues, extraFlags, options);
5761 } else {
5762 // Note we want to go through this call for compatibility with
5763 // applications that may have overridden the method.
5764 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5765 flagsValues, extraFlags);
5766 }
5767 }
5768
5769 /**
5770 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5771 * with no options.
5772 *
5773 * @param intent The intent to start.
5774 * @param requestCode If >= 0, this code will be returned in
5775 * onActivityResult() when the activity exits, as described in
5776 * {@link #startActivityForResult}.
5777 *
5778 * @return If a new activity was launched then true is returned; otherwise
5779 * false is returned and you must handle the Intent yourself.
5780 *
5781 * @see #startActivity
5782 * @see #startActivityForResult
5783 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005784 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5785 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005786 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005787 }
5788
5789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 * A special variation to launch an activity only if a new activity
5791 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005792 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005794 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005796 * and the activity
5797 * that handles <var>intent</var> is the same as your currently running
5798 * activity, then a new instance is not needed. In this case, instead of
5799 * the normal behavior of calling {@link #onNewIntent} this function will
5800 * return and you can handle the Intent yourself.
5801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 * <p>This function can only be called from a top-level activity; if it is
5803 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005804 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 * @param intent The intent to start.
5806 * @param requestCode If >= 0, this code will be returned in
5807 * onActivityResult() when the activity exits, as described in
5808 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005809 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005810 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005811 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005812 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 * @return If a new activity was launched then true is returned; otherwise
5814 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005815 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 * @see #startActivity
5817 * @see #startActivityForResult
5818 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005819 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5820 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005822 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005824 Uri referrer = onProvideReferrer();
5825 if (referrer != null) {
5826 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5827 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005828 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005829 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005830 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005831 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005832 getAttributionTag(), intent,
Philip P. Moltmannee295092020-02-10 08:46:26 -08005833 intent.resolveTypeIfNeeded(getContentResolver()), mToken, mEmbeddedID,
5834 requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 } catch (RemoteException e) {
5836 // Empty
5837 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 if (requestCode >= 0) {
5842 // If this start is requesting a result, we can avoid making
5843 // the activity visible until the result is received. Setting
5844 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5845 // activity hidden during this time, to avoid flickering.
5846 // This can only be done when a result is requested because
5847 // that guarantees we will get information back when the
5848 // activity is finished, no matter what happens to it.
5849 mStartedActivity = true;
5850 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005851 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 }
5853
5854 throw new UnsupportedOperationException(
5855 "startActivityIfNeeded can only be called from a top-level activity");
5856 }
5857
5858 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005859 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5860 * no options.
5861 *
5862 * @param intent The intent to dispatch to the next activity. For
5863 * correct behavior, this must be the same as the Intent that started
5864 * your own activity; the only changes you can make are to the extras
5865 * inside of it.
5866 *
5867 * @return Returns a boolean indicating whether there was another Activity
5868 * to start: true if there was a next activity to start, false if there
5869 * wasn't. In general, if true is returned you will then want to call
5870 * finish() on yourself.
5871 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005872 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005873 return startNextMatchingActivity(intent, null);
5874 }
5875
5876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 * Special version of starting an activity, for use when you are replacing
5878 * other activity components. You can use this to hand the Intent off
5879 * to the next Activity that can handle it. You typically call this in
5880 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005881 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 * @param intent The intent to dispatch to the next activity. For
5883 * correct behavior, this must be the same as the Intent that started
5884 * your own activity; the only changes you can make are to the extras
5885 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005886 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005887 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005888 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 * @return Returns a boolean indicating whether there was another Activity
5891 * to start: true if there was a next activity to start, false if there
5892 * wasn't. In general, if true is returned you will then want to call
5893 * finish() on yourself.
5894 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005895 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5896 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 if (mParent == null) {
5898 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005899 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005900 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005901 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005902 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 } catch (RemoteException e) {
5904 // Empty
5905 }
5906 return false;
5907 }
5908
5909 throw new UnsupportedOperationException(
5910 "startNextMatchingActivity can only be called from a top-level activity");
5911 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005912
5913 /**
5914 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5915 * with no options.
5916 *
5917 * @param child The activity making the call.
5918 * @param intent The intent to start.
5919 * @param requestCode Reply request code. < 0 if reply is not requested.
5920 *
5921 * @throws android.content.ActivityNotFoundException
5922 *
5923 * @see #startActivity
5924 * @see #startActivityForResult
Charles Chenc380f022019-08-13 18:39:17 +08005925 * @deprecated Use {@code androidx.fragment.app.FragmentActivity#startActivityFromFragment(
5926 * androidx.fragment.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005927 */
Charles Chenc380f022019-08-13 18:39:17 +08005928 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005929 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005930 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005931 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005932 }
5933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 /**
RoboErik55011652014-07-09 15:05:53 -07005935 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 * <p>This method throws {@link android.content.ActivityNotFoundException}
5939 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 * @param child The activity making the call.
5942 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005943 * @param requestCode Reply request code. < 0 if reply is not requested.
5944 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005945 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005946 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005947 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005949 *
5950 * @see #startActivity
5951 * @see #startActivityForResult
Charles Chenc380f022019-08-13 18:39:17 +08005952 * @deprecated Use {@code androidx.fragment.app.FragmentActivity#startActivityFromFragment(
5953 * androidx.fragment.app.Fragment,Intent,int,Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 */
Charles Chenc380f022019-08-13 18:39:17 +08005955 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005956 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005957 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005958 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 Instrumentation.ActivityResult ar =
5960 mInstrumentation.execStartActivity(
5961 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005962 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 if (ar != null) {
5964 mMainThread.sendActivityResult(
5965 mToken, child.mEmbeddedID, requestCode,
5966 ar.getResultCode(), ar.getResultData());
5967 }
George Mount41725de2015-04-09 08:23:05 -07005968 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 }
5970
5971 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005972 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5973 * with no options.
5974 *
5975 * @param fragment The fragment making the call.
5976 * @param intent The intent to start.
5977 * @param requestCode Reply request code. < 0 if reply is not requested.
5978 *
5979 * @throws android.content.ActivityNotFoundException
5980 *
5981 * @see Fragment#startActivity
5982 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005983 *
Charles Chenc380f022019-08-13 18:39:17 +08005984 * @deprecated Use {@code androidx.fragment.app.FragmentActivity#startActivityFromFragment(
5985 * androidx.fragment.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005986 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005987 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005988 public void startActivityFromFragment(@NonNull Fragment fragment,
5989 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005990 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005991 }
5992
5993 /**
RoboErik55011652014-07-09 15:05:53 -07005994 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005995 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5996 * method.
RoboErik55011652014-07-09 15:05:53 -07005997 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005998 * <p>This method throws {@link android.content.ActivityNotFoundException}
5999 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07006000 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006001 * @param fragment The fragment making the call.
6002 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07006003 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07006004 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08006005 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07006006 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07006007 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006008 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07006009 *
6010 * @see Fragment#startActivity
6011 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08006012 *
Charles Chenc380f022019-08-13 18:39:17 +08006013 * @deprecated Use {@code androidx.fragment.app.FragmentActivity#startActivityFromFragment(
6014 * androidx.fragment.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006015 */
Ian Lake0a1feb82017-11-13 10:26:46 -08006016 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07006017 public void startActivityFromFragment(@NonNull Fragment fragment,
6018 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006019 startActivityForResult(fragment.mWho, intent, requestCode, options);
6020 }
6021
Charles Chenc380f022019-08-13 18:39:17 +08006022 private void startActivityAsUserFromFragment(@NonNull Fragment fragment,
Tony Mak96d26fe2017-04-11 20:05:39 +01006023 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
6024 UserHandle user) {
6025 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
6026 }
6027
6028 /**
6029 * @hide
6030 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006031 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006032 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006033 public void startActivityForResult(
6034 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07006035 Uri referrer = onProvideReferrer();
6036 if (referrer != null) {
6037 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
6038 }
George Mount413739e2016-06-08 07:13:37 -07006039 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006040 Instrumentation.ActivityResult ar =
6041 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006042 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07006043 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006044 if (ar != null) {
6045 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006046 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006047 ar.getResultCode(), ar.getResultData());
6048 }
George Mount41725de2015-04-09 08:23:05 -07006049 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006050 }
6051
6052 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006053 * @hide
6054 */
6055 @Override
6056 public boolean canStartActivityForResult() {
6057 return true;
6058 }
6059
6060 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07006061 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
6062 * int, Intent, int, int, int, Bundle)} with no options.
Charles Chenc380f022019-08-13 18:39:17 +08006063 * @deprecated Use {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
6064 * instead.
Dianne Hackborna4972e92012-03-14 10:38:05 -07006065 */
Charles Chenc380f022019-08-13 18:39:17 +08006066 @Deprecated
Dianne Hackborna4972e92012-03-14 10:38:05 -07006067 public void startIntentSenderFromChild(Activity child, IntentSender intent,
6068 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
6069 int extraFlags)
6070 throws IntentSender.SendIntentException {
6071 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
6072 flagsMask, flagsValues, extraFlags, null);
6073 }
6074
6075 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07006076 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07006077 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07006078 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07006079 * for more information.
Charles Chenc380f022019-08-13 18:39:17 +08006080 * @deprecated Use
6081 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
6082 * instead.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07006083 */
Charles Chenc380f022019-08-13 18:39:17 +08006084 @Deprecated
Dianne Hackbornfa82f222009-09-17 15:14:12 -07006085 public void startIntentSenderFromChild(Activity child, IntentSender intent,
6086 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07006087 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07006088 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01006089 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
6090 flagsMask, flagsValues, options);
6091 }
6092
6093 /**
Charles Chenc380f022019-08-13 18:39:17 +08006094 * Like {@link #startIntentSender}, but taking a Fragment; see
Clara Bayarria0c2dc32016-04-12 12:00:15 +01006095 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
6096 * for more information.
Clara Bayarria0c2dc32016-04-12 12:00:15 +01006097 */
Charles Chenc380f022019-08-13 18:39:17 +08006098 private void startIntentSenderFromFragment(Fragment fragment, IntentSender intent,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01006099 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Charles Chenc380f022019-08-13 18:39:17 +08006100 @Nullable Bundle options)
Clara Bayarria0c2dc32016-04-12 12:00:15 +01006101 throws IntentSender.SendIntentException {
Charles Chenc380f022019-08-13 18:39:17 +08006102 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01006103 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07006104 }
6105
6106 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006107 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
6108 * or {@link #finish} to specify an explicit transition animation to
6109 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07006110 *
6111 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
6112 * to using this with starting activities is to supply the desired animation
6113 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07006114 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07006115 * you to specify a custom animation even when starting an activity from
6116 * outside the context of the current top activity.
6117 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006118 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07006119 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006120 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07006121 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006122 */
6123 public void overridePendingTransition(int enterAnim, int exitAnim) {
6124 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006125 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006126 mToken, getPackageName(), enterAnim, exitAnim);
6127 } catch (RemoteException e) {
6128 }
6129 }
RoboErik55011652014-07-09 15:05:53 -07006130
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 * Call this to set the result that your activity will return to its
6133 * caller.
RoboErik55011652014-07-09 15:05:53 -07006134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 * @param resultCode The result code to propagate back to the originating
6136 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07006137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 * @see #RESULT_CANCELED
6139 * @see #RESULT_OK
6140 * @see #RESULT_FIRST_USER
6141 * @see #setResult(int, Intent)
6142 */
6143 public final void setResult(int resultCode) {
6144 synchronized (this) {
6145 mResultCode = resultCode;
6146 mResultData = null;
6147 }
6148 }
6149
6150 /**
6151 * Call this to set the result that your activity will return to its
6152 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08006153 *
6154 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
6155 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
6156 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
6157 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
6158 * Activity receiving the result access to the specific URIs in the Intent.
6159 * Access will remain until the Activity has finished (it will remain across the hosting
6160 * process being killed and other temporary destruction) and will be added
6161 * to any existing set of URI permissions it already holds.
6162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 * @param resultCode The result code to propagate back to the originating
6164 * activity, often RESULT_CANCELED or RESULT_OK
6165 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07006166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 * @see #RESULT_CANCELED
6168 * @see #RESULT_OK
6169 * @see #RESULT_FIRST_USER
6170 * @see #setResult(int)
6171 */
6172 public final void setResult(int resultCode, Intent data) {
6173 synchronized (this) {
6174 mResultCode = resultCode;
6175 mResultData = data;
6176 }
6177 }
6178
6179 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006180 * Return information about who launched this activity. If the launching Intent
6181 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
6182 * that will be returned as-is; otherwise, if known, an
6183 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
6184 * package name that started the Intent will be returned. This may return null if no
6185 * referrer can be identified -- it is neither explicitly specified, nor is it known which
6186 * application package was involved.
6187 *
6188 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
6189 * return the referrer that submitted that new intent to the activity. Otherwise, it
6190 * always returns the referrer of the original Intent.</p>
6191 *
6192 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
6193 * referrer information, applications can spoof it.</p>
6194 */
6195 @Nullable
6196 public Uri getReferrer() {
6197 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07006198 try {
6199 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
6200 if (referrer != null) {
6201 return referrer;
6202 }
6203 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
6204 if (referrerName != null) {
6205 return Uri.parse(referrerName);
6206 }
6207 } catch (BadParcelableException e) {
6208 Log.w(TAG, "Cannot read referrer from intent;"
6209 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006210 }
6211 if (mReferrer != null) {
6212 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
6213 }
6214 return null;
6215 }
6216
6217 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07006218 * Override to generate the desired referrer for the content currently being shown
6219 * by the app. The default implementation returns null, meaning the referrer will simply
6220 * be the android-app: of the package name of this activity. Return a non-null Uri to
6221 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
6222 */
6223 public Uri onProvideReferrer() {
6224 return null;
6225 }
6226
6227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 * Return the name of the package that invoked this activity. This is who
6229 * the data in {@link #setResult setResult()} will be sent to. You can
6230 * use this information to validate that the recipient is allowed to
6231 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006232 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006233 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006234 * did not use the {@link #startActivityForResult}
6235 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07006236 * null.</p>
6237 *
6238 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
6239 * the result from this method was unstable. If the process hosting the calling
6240 * package was no longer running, it would return null instead of the proper package
6241 * name. You can use {@link #getCallingActivity()} and retrieve the package name
6242 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07006243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 * @return The package of the activity that will receive your
6245 * reply, or null if none.
6246 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006247 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 public String getCallingPackage() {
6249 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006250 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 } catch (RemoteException e) {
6252 return null;
6253 }
6254 }
6255
6256 /**
6257 * Return the name of the activity that invoked this activity. This is
6258 * who the data in {@link #setResult setResult()} will be sent to. You
6259 * can use this information to validate that the recipient is allowed to
6260 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006261 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006262 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006263 * did not use the {@link #startActivityForResult}
6264 * form that includes a request code), then the calling package will be
6265 * null.
6266 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006267 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 * reply, or null if none.
6269 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006270 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 public ComponentName getCallingActivity() {
6272 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006273 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 } catch (RemoteException e) {
6275 return null;
6276 }
6277 }
6278
6279 /**
6280 * Control whether this activity's main window is visible. This is intended
6281 * only for the special case of an activity that is not going to show a
6282 * UI itself, but can't just finish prior to onResume() because it needs
6283 * to wait for a service binding or such. Setting this to false allows
6284 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07006285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 * <p>The default value for this is taken from the
6287 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
6288 */
6289 public void setVisible(boolean visible) {
6290 if (mVisibleFromClient != visible) {
6291 mVisibleFromClient = visible;
6292 if (mVisibleFromServer) {
6293 if (visible) makeVisible();
6294 else mDecor.setVisibility(View.INVISIBLE);
6295 }
6296 }
6297 }
RoboErik55011652014-07-09 15:05:53 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 void makeVisible() {
6300 if (!mWindowAdded) {
6301 ViewManager wm = getWindowManager();
6302 wm.addView(mDecor, getWindow().getAttributes());
6303 mWindowAdded = true;
6304 }
6305 mDecor.setVisibility(View.VISIBLE);
6306 }
RoboErik55011652014-07-09 15:05:53 -07006307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 /**
6309 * Check to see whether this activity is in the process of finishing,
6310 * either because you called {@link #finish} on it or someone else
6311 * has requested that it finished. This is often used in
6312 * {@link #onPause} to determine whether the activity is simply pausing or
6313 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07006314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07006316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006317 * @see #finish
6318 */
6319 public boolean isFinishing() {
6320 return mFinished;
6321 }
6322
6323 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006324 * Returns true if the final {@link #onDestroy()} call has been made
6325 * on the Activity, so this instance is now dead.
6326 */
6327 public boolean isDestroyed() {
6328 return mDestroyed;
6329 }
6330
6331 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006332 * Check to see whether this activity is in the process of being destroyed in order to be
6333 * recreated with a new configuration. This is often used in
6334 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
6335 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07006336 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006337 * @return If the activity is being torn down in order to be recreated with a new configuration,
6338 * returns true; else returns false.
6339 */
6340 public boolean isChangingConfigurations() {
6341 return mChangingConfigurations;
6342 }
6343
6344 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006345 * Cause this Activity to be recreated with a new instance. This results
6346 * in essentially the same flow as when the Activity is created due to
6347 * a configuration change -- the current instance will go through its
6348 * lifecycle to {@link #onDestroy} and a new instance then created after it.
6349 */
6350 public void recreate() {
6351 if (mParent != null) {
6352 throw new IllegalStateException("Can only be called on top-level activity");
6353 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07006354 if (Looper.myLooper() != mMainThread.getLooper()) {
6355 throw new IllegalStateException("Must be called from main thread");
6356 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006357 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006358 }
6359
6360 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006361 * Finishes the current activity and specifies whether to remove the task associated with this
6362 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006364 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006365 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 if (mParent == null) {
6367 int resultCode;
6368 Intent resultData;
6369 synchronized (this) {
6370 resultCode = mResultCode;
6371 resultData = mResultData;
6372 }
Joe Onorato43a17652011-04-06 19:22:23 -07006373 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006375 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006376 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006377 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006378 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006379 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 mFinished = true;
6381 }
6382 } catch (RemoteException e) {
6383 // Empty
6384 }
6385 } else {
6386 mParent.finishFromChild(this);
6387 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006388
6389 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6390 // be restored now.
6391 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
TYM Tsai8af721d2019-07-24 17:11:59 +08006392 restoreAutofillSaveUi();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 }
6395
6396 /**
TYM Tsai8af721d2019-07-24 17:11:59 +08006397 * Restores Autofill Save UI
6398 */
6399 private void restoreAutofillSaveUi() {
6400 final IBinder token =
6401 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN);
6402 // Make only restore Autofill once
6403 mIntent.removeExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN);
6404 mIntent.removeExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY);
6405 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6406 token);
6407 }
6408
6409 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006410 * Call this when your activity is done and should be closed. The
6411 * ActivityResult is propagated back to whoever launched you via
6412 * onActivityResult().
6413 */
6414 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006415 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006416 }
6417
6418 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006419 * Finish this activity as well as all activities immediately below it
6420 * in the current task that have the same affinity. This is typically
6421 * used when an application can be launched on to another task (such as
6422 * from an ACTION_VIEW of a content type it understands) and the user
6423 * has used the up navigation to switch out of the current task and in
6424 * to its own task. In this case, if the user has navigated down into
6425 * any other activities of the second application, all of those should
6426 * be removed from the original task as part of the task switch.
6427 *
6428 * <p>Note that this finish does <em>not</em> allow you to deliver results
6429 * to the previous activity, and an exception will be thrown if you are trying
6430 * to do so.</p>
6431 */
6432 public void finishAffinity() {
6433 if (mParent != null) {
6434 throw new IllegalStateException("Can not be called from an embedded activity");
6435 }
6436 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6437 throw new IllegalStateException("Can not be called to deliver a result");
6438 }
6439 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006440 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006441 mFinished = true;
6442 }
6443 } catch (RemoteException e) {
6444 // Empty
6445 }
6446 }
6447
6448 /**
RoboErik55011652014-07-09 15:05:53 -07006449 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450 * {@link #finish} method. The default implementation simply calls
6451 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 * @see #finish
Charles Chenc380f022019-08-13 18:39:17 +08006456 * @deprecated Use {@link #finish()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 */
Charles Chenc380f022019-08-13 18:39:17 +08006458 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 public void finishFromChild(Activity child) {
6460 finish();
6461 }
6462
6463 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006464 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6465 * to reverse its exit Transition. When the exit Transition completes,
6466 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6467 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006468 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006469 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006470 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006471 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006472 finish();
6473 }
George Mountcb4b7d92014-02-25 10:47:55 -08006474 }
6475
6476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 * Force finish another activity that you had previously started with
6478 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006480 * @param requestCode The request code of the activity that you had
6481 * given to startActivityForResult(). If there are multiple
6482 * activities started with this request code, they
6483 * will all be finished.
6484 */
6485 public void finishActivity(int requestCode) {
6486 if (mParent == null) {
6487 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006488 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6490 } catch (RemoteException e) {
6491 // Empty
6492 }
6493 } else {
6494 mParent.finishActivityFromChild(this, requestCode);
6495 }
6496 }
6497
6498 /**
6499 * This is called when a child activity of this one calls its
6500 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006501 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 * @param child The activity making the call.
6503 * @param requestCode Request code that had been used to start the
6504 * activity.
Charles Chenc380f022019-08-13 18:39:17 +08006505 * @deprecated Use {@link #finishActivity(int)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 */
Charles Chenc380f022019-08-13 18:39:17 +08006507 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07006508 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006510 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6512 } catch (RemoteException e) {
6513 // Empty
6514 }
6515 }
6516
6517 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006518 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006519 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006520 */
6521 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006522 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006523 }
6524
6525 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006526 * Ask that the local app instance of this activity be released to free up its memory.
6527 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6528 * a new instance of the activity will later be re-created if needed due to the user
6529 * navigating back to it.
6530 *
6531 * @return Returns true if the activity was in a state that it has started the process
6532 * of destroying its current instance; returns false if for any reason this could not
6533 * be done: it is currently visible to the user, it is already being destroyed, it is
6534 * being finished, it hasn't yet saved its state, etc.
6535 */
6536 public boolean releaseInstance() {
6537 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006538 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006539 } catch (RemoteException e) {
6540 // Empty
6541 }
6542 return false;
6543 }
6544
6545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 * Called when an activity you launched exits, giving you the requestCode
6547 * you started it with, the resultCode it returned, and any additional
6548 * data from it. The <var>resultCode</var> will be
6549 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6550 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006552 * <p>You will receive this call immediately before onResume() when your
6553 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006554 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006555 * <p>This method is never invoked if your activity sets
6556 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6557 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006558 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 * @param requestCode The integer request code originally supplied to
6560 * startActivityForResult(), allowing you to identify who this
6561 * result came from.
6562 * @param resultCode The integer result code returned by the child activity
6563 * through its setResult().
6564 * @param data An Intent, which can return result data to the caller
6565 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006567 * @see #startActivityForResult
6568 * @see #createPendingResult
6569 * @see #setResult(int)
6570 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006571 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006572 }
6573
6574 /**
George Mount62ab9b72014-05-02 13:51:17 -07006575 * Called when an activity you launched with an activity transition exposes this
6576 * Activity through a returning activity transition, giving you the resultCode
6577 * and any additional data from it. This method will only be called if the activity
6578 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006579 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006580 *
6581 * <p>The purpose of this function is to let the called Activity send a hint about
6582 * its state so that this underlying Activity can prepare to be exposed. A call to
6583 * this method does not guarantee that the called Activity has or will be exiting soon.
6584 * It only indicates that it will expose this Activity's Window and it has
6585 * some data to pass to prepare it.</p>
6586 *
6587 * @param resultCode The integer result code returned by the child activity
6588 * through its setResult().
6589 * @param data An Intent, which can return result data to the caller
6590 * (various data can be attached to Intent "extras").
6591 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006592 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006593 }
6594
6595 /**
RoboErik55011652014-07-09 15:05:53 -07006596 * Create a new PendingIntent object which you can hand to others
6597 * for them to use to send result data back to your
6598 * {@link #onActivityResult} callback. The created object will be either
6599 * one-shot (becoming invalid after a result is sent back) or multiple
6600 * (allowing any number of results to be sent through it).
6601 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 * @param requestCode Private request code for the sender that will be
6603 * associated with the result data when it is returned. The sender can not
6604 * modify this value, allowing you to identify incoming results.
6605 * @param data Default data to supply in the result, which may be modified
6606 * by the sender.
6607 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6608 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6609 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6610 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6611 * or any of the flags as supported by
6612 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6613 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 * @return Returns an existing or new PendingIntent matching the given
6616 * parameters. May return null only if
6617 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6618 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006619 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 * @see PendingIntent
6621 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006622 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6623 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 String packageName = getPackageName();
6625 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006626 data.prepareToLeaveProcess(this);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08006627 IIntentSender target = ActivityManager.getService().getIntentSenderWithFeature(
6628 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName, getAttributionTag(),
6629 mParent == null ? mToken : mParent.mToken, mEmbeddedID, requestCode,
6630 new Intent[]{data}, null, flags, null, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 return target != null ? new PendingIntent(target) : null;
6632 } catch (RemoteException e) {
6633 // Empty
6634 }
6635 return null;
6636 }
6637
6638 /**
6639 * Change the desired orientation of this activity. If the activity
6640 * is currently in the foreground or otherwise impacting the screen
6641 * orientation, the screen will immediately be changed (possibly causing
6642 * the activity to be restarted). Otherwise, this will be used the next
6643 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 * @param requestedOrientation An orientation constant as used in
6646 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6647 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006648 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 if (mParent == null) {
6650 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006651 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 mToken, requestedOrientation);
6653 } catch (RemoteException e) {
6654 // Empty
6655 }
6656 } else {
6657 mParent.setRequestedOrientation(requestedOrientation);
6658 }
6659 }
RoboErik55011652014-07-09 15:05:53 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 /**
6662 * Return the current requested orientation of the activity. This will
6663 * either be the orientation requested in its component's manifest, or
6664 * the last requested orientation given to
6665 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667 * @return Returns an orientation constant as used in
6668 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6669 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006670 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 public int getRequestedOrientation() {
6672 if (mParent == null) {
6673 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006674 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 .getRequestedOrientation(mToken);
6676 } catch (RemoteException e) {
6677 // Empty
6678 }
6679 } else {
6680 return mParent.getRequestedOrientation();
6681 }
6682 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6683 }
RoboErik55011652014-07-09 15:05:53 -07006684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 /**
6686 * Return the identifier of the task this activity is in. This identifier
6687 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006688 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 * @return Task identifier, an opaque integer.
6690 */
6691 public int getTaskId() {
6692 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006693 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 } catch (RemoteException e) {
6695 return -1;
6696 }
6697 }
6698
6699 /**
6700 * Return whether this activity is the root of a task. The root is the
6701 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 * @return True if this is the root activity, else false.
6704 */
6705 public boolean isTaskRoot() {
Mathew Inwoodd6973862019-12-05 10:15:19 +00006706 return mWindowControllerCallback.isTaskRoot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 }
6708
6709 /**
6710 * Move the task containing this activity to the back of the activity
6711 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 * @param nonRoot If false then this only works if the activity is the root
6714 * of a task; if true it will work for any activity in
6715 * a task.
RoboErik55011652014-07-09 15:05:53 -07006716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 * @return If the task was moved (or it was already at the
6718 * back) true is returned, else false.
6719 */
6720 public boolean moveTaskToBack(boolean nonRoot) {
6721 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006722 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006723 } catch (RemoteException e) {
6724 // Empty
6725 }
6726 return false;
6727 }
6728
6729 /**
6730 * Returns class name for this activity with the package prefix removed.
6731 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 * @return The local class name.
6734 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006735 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 public String getLocalClassName() {
6737 final String pkg = getPackageName();
6738 final String cls = mComponent.getClassName();
6739 int packageLen = pkg.length();
6740 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6741 || cls.charAt(packageLen) != '.') {
6742 return cls;
6743 }
6744 return cls.substring(packageLen+1);
6745 }
RoboErik55011652014-07-09 15:05:53 -07006746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006748 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006749 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 * @return Returns the complete component name for this activity
6751 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006752 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 return mComponent;
6754 }
6755
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006756 /** @hide */
6757 @Override
6758 public final ComponentName autofillClientGetComponentName() {
6759 return getComponentName();
6760 }
6761
Felipe Lemecbf7f262019-04-17 13:57:59 -07006762 /** @hide */
6763 @Override
6764 public final ComponentName contentCaptureClientGetComponentName() {
6765 return getComponentName();
6766 }
6767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 /**
6769 * Retrieve a {@link SharedPreferences} object for accessing preferences
6770 * that are private to this activity. This simply calls the underlying
6771 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6772 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006773 *
6774 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006775 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 *
6777 * @return Returns the single SharedPreferences instance that can be used
6778 * to retrieve and modify the preference values.
6779 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006780 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 return getSharedPreferences(getLocalClassName(), mode);
6782 }
RoboErik55011652014-07-09 15:05:53 -07006783
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006784 private void ensureSearchManager() {
6785 if (mSearchManager != null) {
6786 return;
6787 }
RoboErik55011652014-07-09 15:05:53 -07006788
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006789 try {
6790 mSearchManager = new SearchManager(this, null);
6791 } catch (ServiceNotFoundException e) {
6792 throw new IllegalStateException(e);
6793 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006794 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006797 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 if (getBaseContext() == null) {
6799 throw new IllegalStateException(
6800 "System services not available to Activities before onCreate()");
6801 }
6802
6803 if (WINDOW_SERVICE.equals(name)) {
6804 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006805 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006806 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006807 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 }
6809 return super.getSystemService(name);
6810 }
6811
6812 /**
6813 * Change the title associated with this activity. If this is a
6814 * top-level activity, the title for its window will change. If it
6815 * is an embedded activity, the parent can do whatever it wants
6816 * with it.
6817 */
6818 public void setTitle(CharSequence title) {
6819 mTitle = title;
6820 onTitleChanged(title, mTitleColor);
6821
6822 if (mParent != null) {
6823 mParent.onChildTitleChanged(this, title);
6824 }
6825 }
6826
6827 /**
6828 * Change the title associated with this activity. If this is a
6829 * top-level activity, the title for its window will change. If it
6830 * is an embedded activity, the parent can do whatever it wants
6831 * with it.
6832 */
6833 public void setTitle(int titleId) {
6834 setTitle(getText(titleId));
6835 }
6836
Alan Viverette2525d9c2013-11-15 14:42:19 -08006837 /**
6838 * Change the color of the title associated with this activity.
6839 * <p>
6840 * This method is deprecated starting in API Level 11 and replaced by action
6841 * bar styles. For information on styling the Action Bar, read the <a
6842 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6843 * guide.
6844 *
6845 * @deprecated Use action bar styles instead.
6846 */
6847 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 public void setTitleColor(int textColor) {
6849 mTitleColor = textColor;
6850 onTitleChanged(mTitle, textColor);
6851 }
6852
6853 public final CharSequence getTitle() {
6854 return mTitle;
6855 }
6856
6857 public final int getTitleColor() {
6858 return mTitleColor;
6859 }
6860
6861 protected void onTitleChanged(CharSequence title, int color) {
6862 if (mTitleReady) {
6863 final Window win = getWindow();
6864 if (win != null) {
6865 win.setTitle(title);
6866 if (color != 0) {
6867 win.setTitleColor(color);
6868 }
6869 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006870 if (mActionBar != null) {
6871 mActionBar.setWindowTitle(title);
6872 }
Adam Powella557fdc2014-08-21 18:05:53 -07006873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 }
6875
6876 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6877 }
6878
6879 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006880 * Sets information describing the task with this activity for presentation inside the Recents
6881 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6882 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6883 * for each property until a suitable value is found. For each task the taskDescription will be
6884 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006885 *
6886 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006887 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006888 *
Winson Chunga449dc02014-05-16 11:15:04 -07006889 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006890 */
Winson Chunga449dc02014-05-16 11:15:04 -07006891 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006892 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006893 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006894 // Scale the icon down to something reasonable if it is provided
6895 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6896 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6897 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6898 true);
Winson Chungd6722032020-02-18 15:16:08 -08006899 mTaskDescription.setIcon(Icon.createWithBitmap(icon));
Winsonb6403152016-02-23 13:32:09 -08006900 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006901 }
6902 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006903 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006904 } catch (RemoteException e) {
6905 }
6906 }
6907
6908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 * Sets the visibility of the progress bar in the title.
6910 * <p>
6911 * In order for the progress bar to be shown, the feature must be requested
6912 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006913 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006915 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006917 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 public final void setProgressBarVisibility(boolean visible) {
6919 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6920 Window.PROGRESS_VISIBILITY_OFF);
6921 }
6922
6923 /**
6924 * Sets the visibility of the indeterminate progress bar in the title.
6925 * <p>
6926 * In order for the progress bar to be shown, the feature must be requested
6927 * via {@link #requestWindowFeature(int)}.
6928 *
6929 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006930 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006932 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6934 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6935 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6936 }
RoboErik55011652014-07-09 15:05:53 -07006937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 /**
6939 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6940 * is always indeterminate).
6941 * <p>
6942 * In order for the progress bar to be shown, the feature must be requested
6943 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006946 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006948 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 public final void setProgressBarIndeterminate(boolean indeterminate) {
6950 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006951 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6952 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 }
RoboErik55011652014-07-09 15:05:53 -07006954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 /**
6956 * Sets the progress for the progress bars in the title.
6957 * <p>
6958 * In order for the progress bar to be shown, the feature must be requested
6959 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006960 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 * @param progress The progress for the progress bar. Valid ranges are from
6962 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6963 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006964 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006966 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 public final void setProgress(int progress) {
6968 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6969 }
RoboErik55011652014-07-09 15:05:53 -07006970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006971 /**
6972 * Sets the secondary progress for the progress bar in the title. This
6973 * progress is drawn between the primary progress (set via
6974 * {@link #setProgress(int)} and the background. It can be ideal for media
6975 * scenarios such as showing the buffering progress while the default
6976 * progress shows the play progress.
6977 * <p>
6978 * In order for the progress bar to be shown, the feature must be requested
6979 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006980 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6982 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006983 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006985 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 public final void setSecondaryProgress(int secondaryProgress) {
6987 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6988 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6989 }
6990
6991 /**
6992 * Suggests an audio stream whose volume should be changed by the hardware
6993 * volume controls.
6994 * <p>
6995 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006996 * Volume requests which are received while the Activity is in the
6997 * foreground will affect this stream.
6998 * <p>
6999 * It is not guaranteed that the hardware volume controls will always change
7000 * this stream's volume (for example, if a call is in progress, its stream's
7001 * volume may be changed instead). To reset back to the default, use
7002 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
7003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07007005 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 */
7007 public final void setVolumeControlStream(int streamType) {
7008 getWindow().setVolumeControlStream(streamType);
7009 }
7010
7011 /**
7012 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07007013 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07007014 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 * @return The suggested audio stream type whose volume should be changed by
7016 * the hardware volume controls.
7017 * @see #setVolumeControlStream(int)
7018 */
7019 public final int getVolumeControlStream() {
7020 return getWindow().getVolumeControlStream();
7021 }
RoboErik55011652014-07-09 15:05:53 -07007022
7023 /**
7024 * Sets a {@link MediaController} to send media keys and volume changes to.
7025 * <p>
7026 * The controller will be tied to the window of this Activity. Media key and
7027 * volume events which are received while the Activity is in the foreground
7028 * will be forwarded to the controller and used to invoke transport controls
7029 * or adjust the volume. This may be used instead of or in addition to
7030 * {@link #setVolumeControlStream} to affect a specific session instead of a
7031 * specific stream.
7032 * <p>
7033 * It is not guaranteed that the hardware volume controls will always change
7034 * this session's volume (for example, if a call is in progress, its
7035 * stream's volume may be changed instead). To reset back to the default use
7036 * null as the controller.
7037 *
7038 * @param controller The controller for the session which should receive
7039 * media keys and volume changes.
7040 */
7041 public final void setMediaController(MediaController controller) {
7042 getWindow().setMediaController(controller);
7043 }
7044
7045 /**
7046 * Gets the controller which should be receiving media key and volume events
7047 * while this activity is in the foreground.
7048 *
7049 * @return The controller which should receive events.
7050 * @see #setMediaController(android.media.session.MediaController)
7051 */
7052 public final MediaController getMediaController() {
7053 return getWindow().getMediaController();
7054 }
7055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 /**
7057 * Runs the specified action on the UI thread. If the current thread is the UI
7058 * thread, then the action is executed immediately. If the current thread is
7059 * not the UI thread, the action is posted to the event queue of the UI thread.
7060 *
7061 * @param action the action to run on the UI thread
7062 */
7063 public final void runOnUiThread(Runnable action) {
7064 if (Thread.currentThread() != mUiThread) {
7065 mHandler.post(action);
7066 } else {
7067 action.run();
7068 }
7069 }
7070
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007071 /** @hide */
7072 @Override
7073 public final void autofillClientRunOnUiThread(Runnable action) {
7074 runOnUiThread(action);
7075 }
7076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07007078 * Standard implementation of
7079 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
7080 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07007081 * This implementation does nothing and is for
7082 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
7083 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
7084 *
7085 * @see android.view.LayoutInflater#createView
7086 * @see android.view.Window#getLayoutInflater
7087 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007088 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07007089 public View onCreateView(@NonNull String name, @NonNull Context context,
7090 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07007091 return null;
7092 }
7093
7094 /**
7095 * Standard implementation of
7096 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
7097 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07007098 * This implementation handles <fragment> tags to embed fragments inside
7099 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 *
7101 * @see android.view.LayoutInflater#createView
7102 * @see android.view.Window#getLayoutInflater
7103 */
Ian Lake0c807f42018-09-12 14:03:27 -07007104 @Nullable
7105 public View onCreateView(@Nullable View parent, @NonNull String name,
7106 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07007107 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07007108 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07007109 }
RoboErik55011652014-07-09 15:05:53 -07007110
Adam Powell371a8092014-06-20 12:51:12 -07007111 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 }
7113
Daniel Sandler69a48172010-06-23 16:29:36 -04007114 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07007115 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07007116 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07007117 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08007118 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07007119 * @param fd The raw file descriptor that the dump is being sent to.
7120 * @param writer The PrintWriter to which you should dump your state. This will be
7121 * closed for you after you return.
7122 * @param args additional arguments to the dump request.
7123 */
Ian Lake140fe122018-09-13 14:59:38 -07007124 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
7125 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07007126 dumpInner(prefix, fd, writer, args);
7127 }
7128
Ian Lake140fe122018-09-13 14:59:38 -07007129 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
7130 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08007131 if (args != null && args.length > 0) {
7132 // Handle special cases
7133 switch (args[0]) {
7134 case "--autofill":
7135 dumpAutofillManager(prefix, writer);
7136 return;
Felipe Lemeb718e032018-12-10 09:44:23 -08007137 case "--contentcapture":
7138 dumpContentCaptureManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08007139 return;
7140 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07007141 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08007142 writer.print(prefix); writer.print("Local Activity ");
7143 writer.print(Integer.toHexString(System.identityHashCode(this)));
7144 writer.println(" State:");
7145 String innerPrefix = prefix + " ";
7146 writer.print(innerPrefix); writer.print("mResumed=");
7147 writer.print(mResumed); writer.print(" mStopped=");
7148 writer.print(mStopped); writer.print(" mFinished=");
7149 writer.println(mFinished);
Hongwei Wang2cebfd52019-07-25 15:14:47 -07007150 writer.print(innerPrefix); writer.print("mLastDispatchedIsInMultiWindowMode=");
7151 writer.print(mLastDispatchedIsInMultiWindowMode);
7152 writer.print(" mLastDispatchedIsInPictureInPictureMode=");
7153 writer.println(mLastDispatchedIsInPictureInPictureMode);
Dianne Hackborn30d71892010-12-11 10:37:55 -08007154 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
7155 writer.println(mChangingConfigurations);
7156 writer.print(innerPrefix); writer.print("mCurrentConfig=");
7157 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07007158
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007159 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
7160 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07007161 if (mVoiceInteractor != null) {
7162 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
7163 }
Jeff Brown5182c782013-10-15 20:31:52 -07007164
Michael Wright5f48dc72013-11-01 12:42:49 -07007165 if (getWindow() != null &&
7166 getWindow().peekDecorView() != null &&
7167 getWindow().peekDecorView().getViewRootImpl() != null) {
7168 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
7169 }
Jeff Brown5182c782013-10-15 20:31:52 -07007170
7171 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07007172
Felipe Lemeb546ca72018-08-15 08:44:12 -07007173 dumpAutofillManager(prefix, writer);
Felipe Lemeb718e032018-12-10 09:44:23 -08007174 dumpContentCaptureManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07007175
7176 ResourcesManager.getInstance().dump(prefix, writer);
7177 }
7178
7179 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08007180 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07007181 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07007182 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08007183 writer.print(prefix); writer.print("Autofill Compat Mode: ");
7184 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07007185 } else {
7186 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07007187 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07007188 }
7189
Felipe Lemeb718e032018-12-10 09:44:23 -08007190 void dumpContentCaptureManager(String prefix, PrintWriter writer) {
7191 final ContentCaptureManager cm = getContentCaptureManager();
7192 if (cm != null) {
7193 cm.dump(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08007194 } else {
Felipe Lemeb718e032018-12-10 09:44:23 -08007195 writer.print(prefix); writer.println("No ContentCaptureManager");
Felipe Lemee348dc32018-11-05 12:35:29 -08007196 }
7197 }
7198
Dianne Hackborn625ac272010-09-17 18:29:22 -07007199 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007200 * Bit indicating that this activity is "immersive" and should not be
7201 * interrupted by notifications if possible.
7202 *
7203 * This value is initially set by the manifest property
7204 * <code>android:immersive</code> but may be changed at runtime by
7205 * {@link #setImmersive}.
7206 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007207 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04007208 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7209 */
7210 public boolean isImmersive() {
7211 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007212 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04007213 } catch (RemoteException e) {
7214 return false;
7215 }
7216 }
7217
7218 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07007219 * Indication of whether this is the highest level activity in this task. Can be used to
7220 * determine whether an activity launched by this activity was placed in the same task or
7221 * another task.
7222 *
7223 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07007224 */
Sunny Goyald85bed52018-09-25 12:01:01 -07007225 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07007226 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07007227 return false;
7228 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07007229 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007230 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07007231 } catch (RemoteException e) {
7232 return false;
7233 }
7234 }
7235
7236 /**
Louis Chang65b486e2019-06-05 16:00:03 +08007237 * Convert an activity, which particularly with {@link android.R.attr#windowIsTranslucent} or
7238 * {@link android.R.attr#windowIsFloating} attribute, to a fullscreen opaque activity, or
7239 * convert it from opaque back to translucent.
7240 *
7241 * @param translucent {@code true} convert from opaque to translucent.
7242 * {@code false} convert from translucent to opaque.
7243 * @return The result of setting translucency. Return {@code true} if set successfully,
7244 * {@code false} otherwise.
7245 */
7246 public boolean setTranslucent(boolean translucent) {
7247 if (translucent) {
7248 return convertToTranslucent(null /* callback */, null /* options */);
7249 } else {
7250 return convertFromTranslucentInternal();
7251 }
7252 }
7253
7254 /**
7255 * Convert an activity to a fullscreen opaque activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007256 * <p>
Louis Chang65b486e2019-06-05 16:00:03 +08007257 * Call this whenever the background of a translucent activity has changed to become opaque.
7258 * Doing so will allow the {@link android.view.Surface} of the activity behind to be released.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007259 *
George Mount62ab9b72014-05-02 13:51:17 -07007260 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7261 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007262 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007263 *
7264 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07007265 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007266 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007267 public void convertFromTranslucent() {
Louis Chang65b486e2019-06-05 16:00:03 +08007268 convertFromTranslucentInternal();
7269 }
7270
7271 private boolean convertFromTranslucentInternal() {
Craig Mautner4addfc52013-06-25 08:05:45 -07007272 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07007273 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007274 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007275 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
Louis Chang65b486e2019-06-05 16:00:03 +08007276 return true;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007277 }
Craig Mautner4addfc52013-06-25 08:05:45 -07007278 } catch (RemoteException e) {
7279 // pass
7280 }
Louis Chang65b486e2019-06-05 16:00:03 +08007281 return false;
Craig Mautner4addfc52013-06-25 08:05:45 -07007282 }
7283
7284 /**
Louis Chang65b486e2019-06-05 16:00:03 +08007285 * Convert an activity to a translucent activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007286 * <p>
Louis Chang65b486e2019-06-05 16:00:03 +08007287 * Calling this allows the activity behind this one to be seen again. Once all such activities
Craig Mautner5eda9b32013-07-02 11:58:16 -07007288 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
7289 * be called indicating that it is safe to make this activity translucent again. Until
7290 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
Louis Chang65b486e2019-06-05 16:00:03 +08007291 * behind the frontmost activity will be indeterminate.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007292 *
Louis Chang65b486e2019-06-05 16:00:03 +08007293 * @param callback the method to call when all visible activities behind this one have been
7294 * drawn and it is safe to make this activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07007295 * @param options activity options delivered to the activity below this one. The options
7296 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07007297 * @return <code>true</code> if Window was opaque and will become translucent or
7298 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007299 *
7300 * @see #convertFromTranslucent()
7301 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007302 *
7303 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007304 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007305 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07007306 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07007307 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007308 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007309 try {
7310 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007311 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007312 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07007313 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07007314 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007315 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007316 // Make callback return as though it timed out.
7317 mChangeCanvasToTranslucent = false;
7318 drawComplete = false;
7319 }
7320 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
7321 // Window is already translucent.
7322 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07007323 }
George Mount3cc716c2014-06-12 16:35:35 -07007324 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007325 }
7326
7327 /** @hide */
7328 void onTranslucentConversionComplete(boolean drawComplete) {
7329 if (mTranslucentCallback != null) {
7330 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
7331 mTranslucentCallback = null;
7332 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007333 if (mChangeCanvasToTranslucent) {
7334 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
7335 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007336 }
7337
Craig Mautnereb8abf72014-07-02 15:04:09 -07007338 /** @hide */
7339 public void onNewActivityOptions(ActivityOptions options) {
7340 mActivityTransitionState.setEnterActivityOptions(this, options);
7341 if (!mStopped) {
7342 mActivityTransitionState.enterReady(this);
7343 }
7344 }
7345
Craig Mautner5eda9b32013-07-02 11:58:16 -07007346 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07007347 * Retrieve the ActivityOptions passed in from the launching activity or passed back
7348 * from an activity launched by this activity in its call to {@link
7349 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
7350 *
7351 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
7352 * @hide
7353 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007354 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07007355 ActivityOptions getActivityOptions() {
7356 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007357 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007358 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07007359 } catch (RemoteException e) {
7360 }
7361 return null;
7362 }
7363
7364 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007365 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07007366 * this method anytime between the start of {@link #onResume()} and the return from
7367 * {@link #onPause()}. If this call is successful then the activity will remain visible after
7368 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
7369 *
7370 * <p>The actions of this call are reset each time that this activity is brought to the
7371 * front. That is, every time {@link #onResume()} is called the activity will be assumed
7372 * to not have requested visible behind. Therefore, if you want this activity to continue to
7373 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007374 *
7375 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
7376 * for dialog and translucent activities.
7377 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007378 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
7379 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
7380 *
7381 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07007382 * the next call to onResume.
7383 *
Bryce Leed59629e2017-04-18 14:35:29 -07007384 * @deprecated This method's functionality is no longer supported as of
7385 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7386 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007387 * @param visible true to notify the system that the activity wishes to be visible behind other
7388 * translucent activities, false to indicate otherwise. Resources must be
7389 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07007390 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007391 * @return the resulting visibiity state. If true the activity will remain visible beyond
7392 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7393 * then the activity may not count on being visible behind other translucent activities,
7394 * and must stop any media playback and release resources.
7395 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7396 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007397 *
Jose Limafcf70832014-08-27 23:09:05 -07007398 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007399 */
Bryce Leed59629e2017-04-18 14:35:29 -07007400 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007401 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007402 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007403 }
7404
7405 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007406 * Called when a translucent activity over this activity is becoming opaque or another
7407 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007408 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007409 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007410 * <p>When this method is called the activity has 500 msec to release any resources it may be
7411 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007412 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007413 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007414 * process. Otherwise {@link #onStop()} will be called following return.
7415 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007416 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007417 *
7418 * @deprecated This method's functionality is no longer supported as of
7419 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007420 */
Bryce Leed59629e2017-04-18 14:35:29 -07007421 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007422 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007423 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007424 mCalled = true;
7425 }
7426
7427 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007428 * Translucent activities may call this to determine if there is an activity below them that
7429 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007430 *
Bryce Leed59629e2017-04-18 14:35:29 -07007431 * @deprecated This method's functionality is no longer supported as of
7432 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7433 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007434 * @return true if an activity below is set to visible according to the most recent call to
7435 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007436 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007437 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007438 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007439 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007440 * @hide
7441 */
Bryce Leed59629e2017-04-18 14:35:29 -07007442 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007443 @SystemApi
7444 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007445 return false;
7446 }
7447
7448 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007449 * The topmost foreground activity will receive this call when the background visibility state
7450 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007451 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007452 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007453 * due to a background activity finishing itself.
7454 *
Bryce Leed59629e2017-04-18 14:35:29 -07007455 * @deprecated This method's functionality is no longer supported as of
7456 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7457 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007458 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007459 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007460 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007461 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007462 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007463 */
Bryce Leed59629e2017-04-18 14:35:29 -07007464 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007465 @SystemApi
7466 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007467 }
7468
7469 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007470 * Activities cannot draw during the period that their windows are animating in. In order
7471 * to know when it is safe to begin drawing they can override this method which will be
7472 * called when the entering animation has completed.
7473 */
7474 public void onEnterAnimationComplete() {
7475 }
7476
7477 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007478 * @hide
7479 */
7480 public void dispatchEnterAnimationComplete() {
chaviwfeb2e1e2018-12-19 17:24:11 -08007481 mEnterAnimationComplete = true;
7482 mInstrumentation.onEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007483 onEnterAnimationComplete();
7484 if (getWindow() != null && getWindow().getDecorView() != null) {
Felipe Leme01297692019-01-29 18:16:23 -08007485 View decorView = getWindow().getDecorView();
7486 decorView.getViewTreeObserver().dispatchOnEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007487 }
7488 }
7489
7490 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007491 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007492 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007493 * Note that changing this value will have no effect on the activity's
7494 * {@link android.content.pm.ActivityInfo} structure; that is, if
7495 * <code>android:immersive</code> is set to <code>true</code>
7496 * in the application's manifest entry for this activity, the {@link
7497 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7498 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7499 * FLAG_IMMERSIVE} bit set.
7500 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007501 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007502 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7503 */
7504 public void setImmersive(boolean i) {
7505 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007506 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007507 } catch (RemoteException e) {
7508 // pass
7509 }
7510 }
7511
Adam Powell6e346362010-07-23 10:18:23 -07007512 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007513 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007514 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007515 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7516 * while this Activity has user focus.</p>
7517 *
7518 * <p>It is recommended that applications additionally declare
7519 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7520 * transitions when switching between VR activities.</p>
7521 *
7522 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7523 * VR mode will not be started. Developers can handle this case as follows:</p>
7524 *
7525 * <pre>
7526 * String servicePackage = "com.whatever.app";
7527 * String serviceClass = "com.whatever.app.MyVrListenerService";
7528 *
7529 * // Name of the component of the VrListenerService to start.
7530 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7531 *
7532 * try {
7533 * setVrModeEnabled(true, myComponentName);
7534 * } catch (PackageManager.NameNotFoundException e) {
7535 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7536 * boolean isInstalled = false;
7537 * for (ApplicationInfo app : installed) {
7538 * if (app.packageName.equals(servicePackage)) {
7539 * isInstalled = true;
7540 * break;
7541 * }
7542 * }
7543 * if (isInstalled) {
7544 * // Package is installed, but not enabled in Settings. Let user enable it.
7545 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7546 * } else {
7547 * // Package is not installed. Send an intent to download this.
7548 * sentIntentToLaunchAppStore(servicePackage);
7549 * }
7550 * }
7551 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007552 *
7553 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007554 * @param requestedComponent the name of the component to use as a
7555 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7556 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007557 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7558 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7559 * not been enabled in user settings.
7560 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007561 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7562 * @see android.service.vr.VrListenerService
7563 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7564 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007565 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007566 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7567 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007568 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007569 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007570 != 0) {
7571 throw new PackageManager.NameNotFoundException(
7572 requestedComponent.flattenToString());
7573 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007574 } catch (RemoteException e) {
7575 // pass
7576 }
7577 }
7578
7579 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007580 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007581 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007582 * @param callback Callback that will manage lifecycle events for this action mode
7583 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007584 *
7585 * @see ActionMode
7586 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007587 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007588 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007589 return mWindow.getDecorView().startActionMode(callback);
7590 }
7591
Adam Powelldebf3be2010-11-15 18:58:48 -08007592 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007593 * Start an action mode of the given type.
7594 *
7595 * @param callback Callback that will manage lifecycle events for this action mode
7596 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7597 * @return The ActionMode that was started, or null if it was canceled
7598 *
7599 * @see ActionMode
7600 */
7601 @Nullable
7602 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7603 return mWindow.getDecorView().startActionMode(callback, type);
7604 }
7605
7606 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007607 * Give the Activity a chance to control the UI for an action mode requested
7608 * by the system.
7609 *
7610 * <p>Note: If you are looking for a notification callback that an action mode
7611 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7612 *
7613 * @param callback The callback that should control the new action mode
7614 * @return The new action mode, or <code>null</code> if the activity does not want to
7615 * provide special handling for this action mode. (It will be handled by the system.)
7616 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007617 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007618 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007619 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007620 // Only Primary ActionModes are represented in the ActionBar.
7621 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7622 initWindowDecorActionBar();
7623 if (mActionBar != null) {
7624 return mActionBar.startActionMode(callback);
7625 }
Adam Powell6e346362010-07-23 10:18:23 -07007626 }
7627 return null;
7628 }
7629
Adam Powelldebf3be2010-11-15 18:58:48 -08007630 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007631 * {@inheritDoc}
7632 */
7633 @Nullable
7634 @Override
7635 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7636 try {
7637 mActionModeTypeStarting = type;
7638 return onWindowStartingActionMode(callback);
7639 } finally {
7640 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7641 }
7642 }
7643
7644 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007645 * Notifies the Activity that an action mode has been started.
7646 * Activity subclasses overriding this method should call the superclass implementation.
7647 *
7648 * @param mode The new action mode.
7649 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007650 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007651 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007652 public void onActionModeStarted(ActionMode mode) {
7653 }
7654
7655 /**
7656 * Notifies the activity that an action mode has finished.
7657 * Activity subclasses overriding this method should call the superclass implementation.
7658 *
7659 * @param mode The action mode that just finished.
7660 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007661 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007662 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007663 public void onActionModeFinished(ActionMode mode) {
7664 }
7665
Adam Powelldd8fab22012-03-22 17:47:27 -07007666 /**
7667 * Returns true if the app should recreate the task when navigating 'up' from this activity
7668 * by using targetIntent.
7669 *
7670 * <p>If this method returns false the app can trivially call
7671 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7672 * up navigation. If this method returns false, the app should synthesize a new task stack
7673 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7674 *
7675 * @param targetIntent An intent representing the target destination for up navigation
7676 * @return true if navigating up should recreate a new task stack, false if the same task
7677 * should be used for the destination
7678 */
7679 public boolean shouldUpRecreateTask(Intent targetIntent) {
7680 try {
7681 PackageManager pm = getPackageManager();
7682 ComponentName cn = targetIntent.getComponent();
7683 if (cn == null) {
7684 cn = targetIntent.resolveActivity(pm);
7685 }
7686 ActivityInfo info = pm.getActivityInfo(cn, 0);
7687 if (info.taskAffinity == null) {
7688 return false;
7689 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007690 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007691 } catch (RemoteException e) {
7692 return false;
7693 } catch (NameNotFoundException e) {
7694 return false;
7695 }
7696 }
7697
7698 /**
7699 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7700 * in the process. If the activity indicated by upIntent already exists in the task's history,
7701 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007702 * finished.
7703 *
7704 * <p>If the indicated activity does not appear in the history stack, this will finish
7705 * each activity in this task until the root activity of the task is reached, resulting in
7706 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7707 * when an activity may be reached by a path not passing through a canonical parent
7708 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007709 *
7710 * <p>This method should be used when performing up navigation from within the same task
7711 * as the destination. If up navigation should cross tasks in some cases, see
7712 * {@link #shouldUpRecreateTask(Intent)}.</p>
7713 *
7714 * @param upIntent An intent representing the target destination for up navigation
7715 *
7716 * @return true if up navigation successfully reached the activity indicated by upIntent and
7717 * upIntent was delivered to it. false if an instance of the indicated activity could
7718 * not be found and this activity was simply finished normally.
7719 */
7720 public boolean navigateUpTo(Intent upIntent) {
7721 if (mParent == null) {
7722 ComponentName destInfo = upIntent.getComponent();
7723 if (destInfo == null) {
7724 destInfo = upIntent.resolveActivity(getPackageManager());
7725 if (destInfo == null) {
7726 return false;
7727 }
7728 upIntent = new Intent(upIntent);
7729 upIntent.setComponent(destInfo);
7730 }
7731 int resultCode;
7732 Intent resultData;
7733 synchronized (this) {
7734 resultCode = mResultCode;
7735 resultData = mResultData;
7736 }
7737 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007738 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007739 }
7740 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007741 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007742 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007743 resultCode, resultData);
7744 } catch (RemoteException e) {
7745 return false;
7746 }
7747 } else {
7748 return mParent.navigateUpToFromChild(this, upIntent);
7749 }
7750 }
7751
7752 /**
7753 * This is called when a child activity of this one calls its
7754 * {@link #navigateUpTo} method. The default implementation simply calls
7755 * navigateUpTo(upIntent) on this activity (the parent).
7756 *
7757 * @param child The activity making the call.
7758 * @param upIntent An intent representing the target destination for up navigation
7759 *
7760 * @return true if up navigation successfully reached the activity indicated by upIntent and
7761 * upIntent was delivered to it. false if an instance of the indicated activity could
7762 * not be found and this activity was simply finished normally.
Charles Chenc380f022019-08-13 18:39:17 +08007763 * @deprecated Use {@link #navigateUpTo(Intent)} instead.
Adam Powelldd8fab22012-03-22 17:47:27 -07007764 */
Charles Chenc380f022019-08-13 18:39:17 +08007765 @Deprecated
Adam Powelldd8fab22012-03-22 17:47:27 -07007766 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7767 return navigateUpTo(upIntent);
7768 }
7769
7770 /**
7771 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7772 * this activity's logical parent. The logical parent is named in the application's manifest
7773 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007774 * Activity subclasses may override this method to modify the Intent returned by
7775 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7776 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007777 *
Adam Powell04d58112012-04-09 10:22:12 -07007778 * @return a new Intent targeting the defined parent of this activity or null if
7779 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007780 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007781 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007782 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007783 final String parentName = mActivityInfo.parentActivityName;
7784 if (TextUtils.isEmpty(parentName)) {
7785 return null;
7786 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007787
7788 // If the parent itself has no parent, generate a main activity intent.
7789 final ComponentName target = new ComponentName(this, parentName);
7790 try {
7791 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7792 final String parentActivity = parentInfo.parentActivityName;
7793 final Intent parentIntent = parentActivity == null
7794 ? Intent.makeMainActivity(target)
7795 : new Intent().setComponent(target);
7796 return parentIntent;
7797 } catch (NameNotFoundException e) {
7798 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7799 "' in manifest");
7800 return null;
7801 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007802 }
7803
George Mount31a21722014-03-24 17:44:36 -07007804 /**
George Mount62ab9b72014-05-02 13:51:17 -07007805 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007806 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007807 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007808 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007809 *
George Mount65580562014-08-29 08:15:48 -07007810 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007811 */
George Mount65580562014-08-29 08:15:48 -07007812 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7813 if (callback == null) {
7814 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007815 }
George Mount65580562014-08-29 08:15:48 -07007816 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007817 }
7818
7819 /**
7820 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007821 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007822 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7823 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007824 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007825 *
George Mount65580562014-08-29 08:15:48 -07007826 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007827 */
George Mount65580562014-08-29 08:15:48 -07007828 public void setExitSharedElementCallback(SharedElementCallback callback) {
7829 if (callback == null) {
7830 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007831 }
George Mount65580562014-08-29 08:15:48 -07007832 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007833 }
7834
George Mount8c2614c2014-06-10 11:12:01 -07007835 /**
7836 * Postpone the entering activity transition when Activity was started with
7837 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7838 * android.util.Pair[])}.
7839 * <p>This method gives the Activity the ability to delay starting the entering and
7840 * shared element transitions until all data is loaded. Until then, the Activity won't
7841 * draw into its window, leaving the window transparent. This may also cause the
7842 * returning animation to be delayed until data is ready. This method should be
7843 * called in {@link #onCreate(android.os.Bundle)} or in
7844 * {@link #onActivityReenter(int, android.content.Intent)}.
7845 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7846 * start the transitions. If the Activity did not use
7847 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7848 * android.util.Pair[])}, then this method does nothing.</p>
7849 */
7850 public void postponeEnterTransition() {
7851 mActivityTransitionState.postponeEnterTransition();
7852 }
7853
7854 /**
7855 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7856 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7857 * to have your Activity start drawing.
7858 */
7859 public void startPostponedEnterTransition() {
7860 mActivityTransitionState.startPostponedEnterTransition();
7861 }
7862
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007863 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007864 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7865 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007866 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007867 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7868 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007869 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007870 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7871 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7872 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7873 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007874 }
7875 return null;
7876 }
7877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007879
Mathew Inwood8c854f82018-09-14 12:35:36 +01007880 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 final void setParent(Activity parent) {
7882 mParent = parent;
7883 }
7884
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007885 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007886 final void attach(Context context, ActivityThread aThread,
7887 Instrumentation instr, IBinder token, int ident,
7888 Application application, Intent intent, ActivityInfo info,
7889 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007890 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007891 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Sunny Goyald40c3452019-03-20 12:46:55 -07007892 Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 attachBaseContext(context);
7894
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007895 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007896
Andrii Kulian51c1b672017-04-07 18:39:32 -07007897 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Mathew Inwoodd6973862019-12-05 10:15:19 +00007898 mWindow.setWindowControllerCallback(mWindowControllerCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007900 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007901 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7903 mWindow.setSoftInputMode(info.softInputMode);
7904 }
Adam Powell269248d2011-08-02 10:26:54 -07007905 if (info.uiOptions != 0) {
7906 mWindow.setUiOptions(info.uiOptions);
7907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 mMainThread = aThread;
7911 mInstrumentation = instr;
7912 mToken = token;
Sunny Goyald40c3452019-03-20 12:46:55 -07007913 mAssistToken = assistToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007914 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 mApplication = application;
7916 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007917 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 mComponent = intent.getComponent();
7919 mActivityInfo = info;
7920 mTitle = title;
7921 mParent = parent;
7922 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007923 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007924 if (voiceInteractor != null) {
7925 if (lastNonConfigurationInstances != null) {
7926 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7927 } else {
7928 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7929 Looper.myLooper());
7930 }
7931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932
Jeff Brown98365d72012-08-19 20:30:52 -07007933 mWindow.setWindowManager(
7934 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7935 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007936 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 if (mParent != null) {
7938 mWindow.setContainer(mParent.getWindow());
7939 }
7940 mWindowManager = mWindow.getWindowManager();
7941 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007942
7943 mWindow.setColorMode(info.colorMode);
Galia Peychevabdd66e32020-02-27 19:16:28 +01007944 mWindow.setPreferMinimalPostProcessing(
7945 (info.flags & ActivityInfo.FLAG_PREFER_MINIMAL_POST_PROCESSING) != 0);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007946
Felipe Lemea4f39cd2019-02-19 15:08:59 -08007947 setAutofillOptions(application.getAutofillOptions());
Felipe Leme326f15a2019-02-19 09:42:24 -08007948 setContentCaptureOptions(application.getContentCaptureOptions());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007949 }
7950
7951 private void enableAutofillCompatibilityIfNeeded() {
7952 if (isAutofillCompatibilityEnabled()) {
7953 final AutofillManager afm = getSystemService(AutofillManager.class);
7954 if (afm != null) {
7955 afm.enableCompatibilityMode();
7956 }
7957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 }
7959
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007960 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007961 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007962 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 return mParent != null ? mParent.getActivityToken() : mToken;
7964 }
7965
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007966 /** @hide */
Sunny Goyald40c3452019-03-20 12:46:55 -07007967 public final IBinder getAssistToken() {
7968 return mParent != null ? mParent.getAssistToken() : mAssistToken;
7969 }
7970
7971 /** @hide */
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007972 @VisibleForTesting
7973 public final ActivityThread getActivityThread() {
7974 return mMainThread;
7975 }
7976
Winson Chung298f95b2017-08-10 15:57:18 -07007977 final void performCreate(Bundle icicle) {
7978 performCreate(icicle, null);
7979 }
7980
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007981 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007982 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007983 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007984 mCanEnterPictureInPicture = true;
7985 restoreHasCurrentPermissionRequest(icicle);
7986 if (persistentState != null) {
7987 onCreate(icicle, persistentState);
7988 } else {
7989 onCreate(icicle);
7990 }
Jeff Changd136e772019-11-05 20:33:52 +08007991 EventLogTags.writeWmOnCreateCalled(mIdent, getComponentName().getClassName(),
7992 "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007993 mActivityTransitionState.readState(icicle);
7994
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007995 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7996 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007997 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007998 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007999 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008000 }
George Mount0a778ed2013-12-13 13:35:36 -08008001
Ian Lake0b71b8e2018-09-13 13:56:26 -07008002 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07008003 mCanEnterPictureInPicture = true;
8004 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07008005 }
8006
Andrii Kuliand25680c2018-02-21 15:16:58 -08008007 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07008008 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07008009 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07008010 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07008012 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 mInstrumentation.callActivityOnStart(this);
Jeff Changd136e772019-11-05 20:33:52 +08008014 EventLogTags.writeWmOnStartCalled(mIdent, getComponentName().getClassName(), reason);
Andrii Kuliand25680c2018-02-21 15:16:58 -08008015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 if (!mCalled) {
8017 throw new SuperNotCalledException(
8018 "Activity " + mComponent.toShortString() +
8019 " did not call through to super.onStart()");
8020 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008021 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008022 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08008023
Elliott Hughese14fa972019-10-02 11:35:53 -07008024 // Warn app developers if the dynamic linker logged anything during startup.
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08008025 boolean isAppDebuggable =
8026 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Elliott Hughese14fa972019-10-02 11:35:53 -07008027 if (isAppDebuggable) {
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08008028 String dlwarning = getDlWarning();
8029 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08008030 String appName = getApplicationInfo().loadLabel(getPackageManager())
8031 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08008032 String warning = "Detected problems with app native libraries\n" +
8033 "(please consult log for detail):\n" + dlwarning;
8034 if (isAppDebuggable) {
8035 new AlertDialog.Builder(this).
8036 setTitle(appName).
8037 setMessage(warning).
8038 setPositiveButton(android.R.string.ok, null).
8039 setCancelable(false).
8040 show();
8041 } else {
8042 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
8043 }
8044 }
8045 }
8046
Tim Van Pattenddc43912018-12-18 17:47:52 -07008047 GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
8048
George Mount62ab9b72014-05-02 13:51:17 -07008049 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07008050 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 }
RoboErik55011652014-07-09 15:05:53 -07008052
Andrii Kulian88e05cb2017-12-05 17:21:10 -08008053 /**
8054 * Restart the activity.
8055 * @param start Indicates whether the activity should also be started after restart.
8056 * The option to not start immediately is needed in case a transaction with
8057 * multiple lifecycle transitions is in progress.
8058 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08008059 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07008060 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07008061 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07008062
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07008063 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08008064 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07008065 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
8066 }
8067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 if (mStopped) {
8069 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07008070
8071 synchronized (mManagedCursors) {
8072 final int N = mManagedCursors.size();
8073 for (int i=0; i<N; i++) {
8074 ManagedCursor mc = mManagedCursors.get(i);
8075 if (mc.mReleased || mc.mUpdated) {
8076 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07008077 if (getApplicationInfo().targetSdkVersion
8078 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
8079 throw new IllegalStateException(
8080 "trying to requery an already closed cursor "
8081 + mc.mCursor);
8082 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07008083 }
8084 mc.mReleased = false;
8085 mc.mUpdated = false;
8086 }
8087 }
8088 }
8089
8090 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 mInstrumentation.callActivityOnRestart(this);
Jeff Changd136e772019-11-05 20:33:52 +08008092 EventLogTags.writeWmOnRestartCalled(mIdent, getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008093 if (!mCalled) {
8094 throw new SuperNotCalledException(
8095 "Activity " + mComponent.toShortString() +
8096 " did not call through to super.onRestart()");
8097 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08008098 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08008099 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08008100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101 }
8102 }
RoboErik55011652014-07-09 15:05:53 -07008103
Andrii Kuliand25680c2018-02-21 15:16:58 -08008104 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07008105 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08008106 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07008107
Dianne Hackborn445646c2010-06-25 15:52:59 -07008108 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07008109
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07008110 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07008111
Dake Gu67decfa2017-12-27 11:48:08 -08008112 if (mAutoFillResetNeeded) {
8113 // When Activity is destroyed in paused state, and relaunch activity, there will be
8114 // extra onResume and onPause event, ignore the first onResume and onPause.
8115 // see ActivityThread.handleRelaunchActivity()
8116 mAutoFillIgnoreFirstResumePause = followedByPause;
8117 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
8118 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
8119 }
8120 }
8121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06008123 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 mInstrumentation.callActivityOnResume(this);
Jeff Changd136e772019-11-05 20:33:52 +08008125 EventLogTags.writeWmOnResumeCalled(mIdent, getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008126 if (!mCalled) {
8127 throw new SuperNotCalledException(
8128 "Activity " + mComponent.toShortString() +
8129 " did not call through to super.onResume()");
8130 }
8131
Todd Kennedyee8c9c62014-12-10 14:22:59 -08008132 // invisible activities must be finished before onResume() completes
8133 if (!mVisibleFromClient && !mFinished) {
8134 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
8135 if (getApplicationInfo().targetSdkVersion
8136 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
8137 throw new IllegalStateException(
8138 "Activity " + mComponent.toShortString() +
8139 " did not call finish() prior to onResume() completing");
8140 }
8141 }
8142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07008145
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008146 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07008147 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00008148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 onPostResume();
8150 if (!mCalled) {
8151 throw new SuperNotCalledException(
8152 "Activity " + mComponent.toShortString() +
8153 " did not call through to super.onPostResume()");
8154 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008155 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 }
8157
8158 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07008159 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07008160 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008161 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07008162 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 onPause();
Jeff Changd136e772019-11-05 20:33:52 +08008164 EventLogTags.writeWmOnPausedCalled(mIdent, getComponentName().getClassName(),
8165 "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08008166 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07008167 if (!mCalled && getApplicationInfo().targetSdkVersion
8168 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
8169 throw new SuperNotCalledException(
8170 "Activity " + mComponent.toShortString() +
8171 " did not call through to super.onPause()");
8172 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008173 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 }
RoboErik55011652014-07-09 15:05:53 -07008175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 final void performUserLeaving() {
8177 onUserInteraction();
8178 onUserLeaveHint();
8179 }
RoboErik55011652014-07-09 15:05:53 -07008180
Andrii Kuliand25680c2018-02-21 15:16:58 -08008181 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07008182 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008183 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00008184
Winson Chung298f95b2017-08-10 15:57:18 -07008185 // Disallow entering picture-in-picture after the activity has been stopped
8186 mCanEnterPictureInPicture = false;
8187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008188 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07008189 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 if (mWindow != null) {
8191 mWindow.closeAllPanels();
8192 }
8193
Chong Zhang7687f252016-02-26 12:03:33 -08008194 // If we're preserving the window, don't setStoppedState to true, since we
8195 // need the window started immediately again. Stopping the window will
8196 // destroys hardware resources and causes flicker.
8197 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07008198 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08008199 }
RoboErik55011652014-07-09 15:05:53 -07008200
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008201 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07008202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008203 mCalled = false;
8204 mInstrumentation.callActivityOnStop(this);
Jeff Changd136e772019-11-05 20:33:52 +08008205 EventLogTags.writeWmOnStopCalled(mIdent, getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 if (!mCalled) {
8207 throw new SuperNotCalledException(
8208 "Activity " + mComponent.toShortString() +
8209 " did not call through to super.onStop()");
8210 }
RoboErik55011652014-07-09 15:05:53 -07008211
Makoto Onuki2f6a0182010-02-22 13:26:59 -08008212 synchronized (mManagedCursors) {
8213 final int N = mManagedCursors.size();
8214 for (int i=0; i<N; i++) {
8215 ManagedCursor mc = mManagedCursors.get(i);
8216 if (!mc.mReleased) {
8217 mc.mCursor.deactivate();
8218 mc.mReleased = true;
8219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 }
8221 }
George Mount0a778ed2013-12-13 13:35:36 -08008222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07008224 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 }
8226 mResumed = false;
8227 }
8228
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008229 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07008230 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07008231 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07008232 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008233 mFragments.dispatchDestroy();
8234 onDestroy();
Jeff Changd136e772019-11-05 20:33:52 +08008235 EventLogTags.writeWmOnDestroyCalled(mIdent, getComponentName().getClassName(),
8236 "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008237 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07008238 if (mVoiceInteractor != null) {
8239 mVoiceInteractor.detachActivity();
8240 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008241 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008242 }
George Mount0a778ed2013-12-13 13:35:36 -08008243
Winson Chung5af42fc2017-03-24 17:11:33 -07008244 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
8245 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008246 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008247 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
8248 + " " + newConfig);
8249 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008250 if (mWindow != null) {
8251 mWindow.onMultiWindowModeChanged();
8252 }
Hongwei Wang2cebfd52019-07-25 15:14:47 -07008253 mLastDispatchedIsInMultiWindowMode = isInMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -07008254 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008255 }
8256
Winson Chung5af42fc2017-03-24 17:11:33 -07008257 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
8258 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008259 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008260 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
8261 + " " + newConfig);
8262 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07008263 if (mWindow != null) {
8264 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
8265 }
Hongwei Wang2cebfd52019-07-25 15:14:47 -07008266 mLastDispatchedIsInPictureInPictureMode = isInPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -07008267 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008268 }
8269
George Mount0a778ed2013-12-13 13:35:36 -08008270 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06008271 * @hide
8272 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008273 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06008274 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 return mResumed;
8276 }
8277
Svetoslavffb32b12015-10-15 16:54:00 -07008278 private void storeHasCurrentPermissionRequest(Bundle bundle) {
8279 if (bundle != null && mHasCurrentPermissionsRequest) {
8280 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
8281 }
8282 }
8283
8284 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
8285 if (bundle != null) {
8286 mHasCurrentPermissionsRequest = bundle.getBoolean(
8287 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
8288 }
8289 }
8290
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008291 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07008292 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
8293 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07008294 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
8296 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07008297 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008299 onActivityResult(requestCode, resultCode, data);
8300 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
8301 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
8302 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08008303 dispatchRequestPermissionsResult(requestCode, data);
8304 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00008305 Fragment frag = mFragments.findFragmentByWho(who);
8306 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008307 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08008308 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07008309 }
Svetoslav970b59c2015-06-09 16:05:21 -07008310 } else if (who.startsWith("@android:view:")) {
8311 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
8312 getActivityToken());
8313 for (ViewRootImpl viewRoot : views) {
8314 if (viewRoot.getView() != null
8315 && viewRoot.getView().dispatchActivityResult(
8316 who, requestCode, resultCode, data)) {
8317 return;
8318 }
8319 }
Svet Ganov782043c2017-02-11 00:52:02 +00008320 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08008321 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08008322 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07008323 } else {
8324 Fragment frag = mFragments.findFragmentByWho(who);
8325 if (frag != null) {
8326 frag.onActivityResult(requestCode, resultCode, data);
8327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 }
Jeff Changd136e772019-11-05 20:33:52 +08008329
8330 EventLogTags.writeWmOnActivityResultCalled(mIdent, getComponentName().getClassName(),
8331 reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07008333
Jason Monka57e5e02014-05-07 10:06:48 -04008334 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008335 * Request to put this activity in a mode where the user is locked to a restricted set of
8336 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04008337 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008338 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
8339 * for this component, the current task will be launched directly into LockTask mode. Only apps
8340 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
8341 * be launched while LockTask mode is active. The user will not be able to leave this mode
8342 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
8343 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04008344 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008345 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
8346 * system will prompt the user with a dialog requesting permission to use this mode.
8347 * The user can exit at any time through instructions shown on the request dialog. Calling
8348 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04008349 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008350 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
8351 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008352 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008353 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008354 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04008355 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008356 public void startLockTask() {
8357 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008358 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008359 } catch (RemoteException e) {
8360 }
8361 }
8362
Jason Monka57e5e02014-05-07 10:06:48 -04008363 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008364 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04008365 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008366 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
8367 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04008368 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008369 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
8370 * by this activity, then calling this method will not terminate the LockTask mode, but only
8371 * finish its own task. The device will remain in LockTask mode, until the activity which
8372 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
8373 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008374 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008375 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008376 * @see android.R.attr#lockTaskMode
8377 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04008378 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008379 public void stopLockTask() {
8380 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008381 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008382 } catch (RemoteException e) {
8383 }
8384 }
8385
Craig Mautner5eda9b32013-07-02 11:58:16 -07008386 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008387 * Shows the user the system defined message for telling the user how to exit
8388 * lock task mode. The task containing this activity must be in lock task mode at the time
8389 * of this call for the message to be displayed.
8390 */
8391 public void showLockTaskEscapeMessage() {
8392 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008393 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008394 } catch (RemoteException e) {
8395 }
8396 }
8397
8398 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07008399 * Check whether the caption on freeform windows is displayed directly on the content.
8400 *
8401 * @return True if caption is displayed on content, false if it pushes the content down.
8402 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08008403 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07008404 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07008405 */
8406 public boolean isOverlayWithDecorCaptionEnabled() {
8407 return mWindow.isOverlayWithDecorCaptionEnabled();
8408 }
8409
8410 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008411 * Set whether the caption should displayed directly on the content rather than push it down.
8412 *
8413 * This affects only freeform windows since they display the caption and only the main
8414 * window of the activity. The caption is used to drag the window around and also shows
8415 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008416 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008417 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008418 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8419 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008420 }
8421
8422 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008423 * Interface for informing a translucent {@link Activity} once all visible activities below it
8424 * have completed drawing. This is necessary only after an {@link Activity} has been made
8425 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8426 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008427 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8428 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008429 *
8430 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008431 */
Jose Lima14914852014-08-14 09:14:12 -07008432 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008433 public interface TranslucentConversionListener {
8434 /**
8435 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8436 * below the top one have been redrawn. Following this callback it is safe to make the top
8437 * Activity translucent because the underlying Activity has been drawn.
8438 *
8439 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8440 * occurred waiting for the Activity to complete drawing.
8441 *
8442 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008443 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008444 */
8445 public void onTranslucentConversionComplete(boolean drawComplete);
8446 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008447
8448 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008449 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008450 // If the package installer crashed we may have not data - best effort.
8451 String[] permissions = (data != null) ? data.getStringArrayExtra(
8452 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8453 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8454 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008455 onRequestPermissionsResult(requestCode, permissions, grantResults);
8456 }
8457
8458 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008459 Fragment fragment) {
8460 // If the package installer crashed we may have not data - best effort.
8461 String[] permissions = (data != null) ? data.getStringArrayExtra(
8462 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8463 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8464 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8465 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008466 }
8467
Svet Ganov782043c2017-02-11 00:52:02 +00008468 /** @hide */
8469 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008470 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Adam He5be0f152020-02-03 15:53:32 -08008471 Intent fillInIntent, boolean authenticateInline) {
Svet Ganov782043c2017-02-11 00:52:02 +00008472 try {
8473 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008474 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008475 } catch (IntentSender.SendIntentException e) {
8476 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8477 }
8478 }
8479
Svet Ganov17db9dc2017-02-21 19:54:31 -08008480 /** @hide */
8481 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008482 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008483 mAutoFillResetNeeded = true;
8484 }
8485
Felipe Leme4753bb02017-03-22 20:24:00 -07008486 /** @hide */
8487 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008488 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008489 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008490 final boolean wasShowing;
8491
8492 if (mAutofillPopupWindow == null) {
8493 wasShowing = false;
8494 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8495 } else {
8496 wasShowing = mAutofillPopupWindow.isShowing();
8497 }
Svet Ganov374cae12017-05-10 13:42:33 -07008498 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008499
8500 return !wasShowing && mAutofillPopupWindow.isShowing();
8501 }
8502
8503 /** @hide */
8504 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008505 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8506 @NonNull KeyEvent keyEvent) {
8507 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8508 if (rootImpl != null) {
8509 // dont care if anchorView is current focus, for example a custom view may only receive
8510 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8511 // might be inside parent of the custom view.
8512 rootImpl.dispatchKeyFromAutofill(keyEvent);
8513 }
8514 }
8515
8516 /** @hide */
8517 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008518 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008519 if (mAutofillPopupWindow == null) {
8520 return false;
8521 }
8522 mAutofillPopupWindow.dismiss();
8523 mAutofillPopupWindow = null;
8524 return true;
8525 }
8526
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008527 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008528 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008529 public final boolean autofillClientIsFillUiShowing() {
8530 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8531 }
8532
8533 /** @hide */
8534 @Override
8535 @NonNull
8536 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8537 @NonNull AutofillId[] autofillId) {
8538 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008539 final ArrayList<ViewRootImpl> roots =
8540 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8541
8542 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8543 final View rootView = roots.get(rootNum).getView();
8544
8545 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008546 final int viewCount = autofillId.length;
8547 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008548 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008549 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008550 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008551 }
8552 }
8553 }
8554 }
8555
8556 return views;
8557 }
8558
8559 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008560 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008561 @Nullable
8562 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008563 final ArrayList<ViewRootImpl> roots =
8564 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8565 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8566 final View rootView = roots.get(rootNum).getView();
8567
8568 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008569 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008570 if (view != null) {
8571 return view;
8572 }
8573 }
8574 }
8575
8576 return null;
8577 }
8578
8579 /** @hide */
8580 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008581 public final @NonNull boolean[] autofillClientGetViewVisibility(
8582 @NonNull AutofillId[] autofillIds) {
8583 final int autofillIdCount = autofillIds.length;
8584 final boolean[] visible = new boolean[autofillIdCount];
8585 for (int i = 0; i < autofillIdCount; i++) {
8586 final AutofillId autofillId = autofillIds[i];
8587 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8588 if (view != null) {
Felipe Lemea75333c2019-01-22 15:45:32 -08008589 if (!autofillId.isVirtualInt()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008590 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008591 } else {
Felipe Lemea75333c2019-01-22 15:45:32 -08008592 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildIntId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008593 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008594 }
8595 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008596 if (android.view.autofill.Helper.sVerbose) {
8597 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8598 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008599 return visible;
8600 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008601
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008602 /** @hide */
8603 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8604 int windowId) {
8605 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8606 .getRootViews(getActivityToken());
8607 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8608 final View rootView = roots.get(rootNum).getView();
8609 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8610 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8611 if (view != null) {
8612 return view;
8613 }
8614 }
8615 }
8616 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008617 }
8618
8619 /** @hide */
8620 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008621 public final @Nullable IBinder autofillClientGetActivityToken() {
8622 return getActivityToken();
8623 }
8624
8625 /** @hide */
8626 @Override
8627 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008628 return !mStopped;
8629 }
8630
Dake Gu67decfa2017-12-27 11:48:08 -08008631 /** @hide */
8632 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008633 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008634 return isAutofillCompatibilityEnabled();
8635 }
8636
8637 /** @hide */
8638 @Override
8639 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008640 return mAutoFillIgnoreFirstResumePause || !mResumed;
8641 }
8642
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008643 /**
8644 * If set to true, this indicates to the system that it should never take a
8645 * screenshot of the activity to be used as a representation while it is not in a started state.
8646 * <p>
8647 * Note that the system may use the window background of the theme instead to represent
8648 * the window when it is not running.
8649 * <p>
8650 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8651 * this only affects the behavior when the activity's screenshot would be used as a
8652 * representation when the activity is not in a started state, i.e. in Overview. The system may
8653 * still take screenshots of the activity in other contexts; for example, when the user takes a
8654 * screenshot of the entire screen, or when the active
8655 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8656 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8657 *
8658 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8659 * @hide
8660 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008661 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008662 public void setDisablePreviewScreenshots(boolean disable) {
8663 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008664 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008665 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008666 throw e.rethrowFromSystemServer();
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008667 }
8668 }
8669
chaviw59b98852017-06-13 12:05:44 -07008670 /**
koprivaa1a78482018-10-09 10:09:23 -07008671 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008672 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8673 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8674 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8675 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8676 *
8677 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8678 * {@code false} otherwise.
8679 * @see #setTurnScreenOn(boolean)
8680 * @see android.R.attr#turnScreenOn
8681 * @see android.R.attr#showWhenLocked
8682 */
8683 public void setShowWhenLocked(boolean showWhenLocked) {
8684 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008685 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008686 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008687 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008688 }
8689 }
8690
8691 /**
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008692 * Specifies whether this {@link Activity} should be shown on top of the lock screen whenever
8693 * the lockscreen is up and this activity has another activity behind it with the showWhenLock
8694 * attribute set. That is, this activity is only visible on the lock screen if there is another
8695 * activity with the showWhenLock attribute visible at the same time on the lock screen. A use
8696 * case for this is permission dialogs, that should only be visible on the lock screen if their
8697 * requesting activity is also visible. This value can be set as a manifest attribute using
8698 * android.R.attr#inheritShowWhenLocked.
8699 *
8700 * @param inheritShowWhenLocked {@code true} to show the {@link Activity} on top of the lock
8701 * screen when this activity has another activity behind it with
8702 * the showWhenLock attribute set; {@code false} otherwise.
8703 * @see #setShowWhenLocked(boolean)
Issei Suzuki7c09f0b2019-02-28 14:45:50 +01008704 * @see android.R.attr#inheritShowWhenLocked
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008705 */
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008706 public void setInheritShowWhenLocked(boolean inheritShowWhenLocked) {
8707 try {
8708 ActivityTaskManager.getService().setInheritShowWhenLocked(
8709 mToken, inheritShowWhenLocked);
8710 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008711 throw e.rethrowFromSystemServer();
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008712 }
8713 }
8714
8715 /**
chaviw59b98852017-06-13 12:05:44 -07008716 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8717 * Normally an activity will be transitioned to the stopped state if it is started while the
8718 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8719 * activity will be visible and resumed due to the screen coming on. The screen will not be
8720 * turned on if the activity won't be visible after the screen is turned on. This flag is
8721 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8722 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8723 * if this flag is set and the activity calls {@link
8724 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8725 * the screen will turn on.
8726 *
8727 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8728 *
8729 * @see #setShowWhenLocked(boolean)
8730 * @see android.R.attr#turnScreenOn
8731 * @see android.R.attr#showWhenLocked
8732 */
8733 public void setTurnScreenOn(boolean turnScreenOn) {
8734 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008735 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008736 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008737 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008738 }
8739 }
8740
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008741 /**
8742 * Registers remote animations per transition type for this activity.
8743 *
8744 * @param definition The remote animation definition that defines which transition whould run
8745 * which remote animation.
8746 * @hide
8747 */
8748 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
8749 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8750 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008751 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008752 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008753 throw e.rethrowFromSystemServer();
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008754 }
8755 }
8756
Winson Chung10fc25d2019-12-10 14:13:56 -08008757 /**
8758 * Unregisters all remote animations for this activity.
8759 *
8760 * @hide
8761 */
8762 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
8763 public void unregisterRemoteAnimations() {
8764 try {
8765 ActivityTaskManager.getService().unregisterRemoteAnimations(mToken);
8766 } catch (RemoteException e) {
8767 throw e.rethrowFromSystemServer();
8768 }
8769 }
8770
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008771 class HostCallbacks extends FragmentHostCallback<Activity> {
8772 public HostCallbacks() {
8773 super(Activity.this /*activity*/);
8774 }
8775
8776 @Override
8777 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8778 Activity.this.dump(prefix, fd, writer, args);
8779 }
8780
8781 @Override
8782 public boolean onShouldSaveFragmentState(Fragment fragment) {
8783 return !isFinishing();
8784 }
8785
8786 @Override
8787 public LayoutInflater onGetLayoutInflater() {
8788 final LayoutInflater result = Activity.this.getLayoutInflater();
8789 if (onUseFragmentManagerInflaterFactory()) {
8790 return result.cloneInContext(Activity.this);
8791 }
8792 return result;
8793 }
8794
8795 @Override
8796 public boolean onUseFragmentManagerInflaterFactory() {
8797 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8798 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8799 }
8800
8801 @Override
8802 public Activity onGetHost() {
8803 return Activity.this;
8804 }
8805
8806 @Override
8807 public void onInvalidateOptionsMenu() {
8808 Activity.this.invalidateOptionsMenu();
8809 }
8810
8811 @Override
8812 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8813 Bundle options) {
8814 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8815 }
8816
8817 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008818 public void onStartActivityAsUserFromFragment(
8819 Fragment fragment, Intent intent, int requestCode, Bundle options,
8820 UserHandle user) {
8821 Activity.this.startActivityAsUserFromFragment(
8822 fragment, intent, requestCode, options, user);
8823 }
8824
8825 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008826 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8827 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8828 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8829 if (mParent == null) {
8830 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8831 flagsMask, flagsValues, options);
8832 } else if (options != null) {
Charles Chenc380f022019-08-13 18:39:17 +08008833 mParent.startIntentSenderFromFragment(fragment, intent, requestCode,
8834 fillInIntent, flagsMask, flagsValues, options);
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008835 }
8836 }
8837
8838 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008839 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8840 int requestCode) {
8841 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8842 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8843 startActivityForResult(who, intent, requestCode, null);
8844 }
8845
8846 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008847 public boolean onHasWindowAnimations() {
8848 return getWindow() != null;
8849 }
8850
8851 @Override
8852 public int onGetWindowAnimations() {
8853 final Window w = getWindow();
8854 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8855 }
8856
Todd Kennedy434bd652015-05-04 12:29:50 -07008857 @Override
8858 public void onAttachFragment(Fragment fragment) {
8859 Activity.this.onAttachFragment(fragment);
8860 }
8861
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008862 @Nullable
8863 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008864 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008865 return Activity.this.findViewById(id);
8866 }
8867
8868 @Override
8869 public boolean onHasView() {
8870 final Window w = getWindow();
8871 return (w != null && w.peekDecorView() != null);
8872 }
8873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874}