blob: 4828c742a7ff0b48ff922d469d3608570a6b96d1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Jorim Jaggif84e2f62018-01-16 14:17:59 +010019import static android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS;
Felipe Leme0c6b23d2019-01-15 17:02:49 -080020import static android.os.Process.myUid;
Felipe Leme5b32ebe2018-02-15 12:52:19 -080021
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070022import static java.lang.Character.MIN_VALUE;
Winson Chung1af8eda2016-02-05 17:55:56 +000023
Tor Norbyec615c6f2015-03-02 10:11:44 -080024import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070025import android.annotation.DrawableRes;
26import android.annotation.IdRes;
27import android.annotation.IntDef;
28import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070029import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070030import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070031import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070032import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070033import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070034import android.annotation.SystemApi;
Tiger Huangfc218452018-09-27 00:38:50 +080035import android.annotation.TestApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010036import android.annotation.UnsupportedAppUsage;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070037import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040038import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070039import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070040import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050044import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070045import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070046import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070047import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.SharedPreferences;
49import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080050import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070051import android.content.pm.PackageManager;
52import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.res.Configuration;
54import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070055import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.database.Cursor;
57import android.graphics.Bitmap;
58import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080059import android.graphics.Color;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070060import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.graphics.drawable.Drawable;
62import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070063import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070065import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070066import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Bundle;
Sunny Goyald40c3452019-03-20 12:46:55 -070068import android.os.CancellationSignal;
Tim Van Pattenddc43912018-12-18 17:47:52 -070069import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080072import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070073import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000074import android.os.PersistableBundle;
Nicholas Sauer28348ed742019-02-15 11:58:32 -080075import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060077import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080078import android.os.StrictMode;
Felipe Lemeb337e1c2019-01-18 13:06:17 -080079import android.os.Trace;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070080import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.text.Selection;
82import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070083import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000085import android.transition.Scene;
86import android.transition.TransitionManager;
87import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.util.EventLog;
90import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070091import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070092import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000094import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070095import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070097import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070099import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +0000100import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +0100102import android.view.KeyboardShortcutGroup;
103import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.LayoutInflater;
105import android.view.Menu;
106import android.view.MenuInflater;
107import android.view.MenuItem;
108import android.view.MotionEvent;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100109import android.view.RemoteAnimationDefinition;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700110import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700112import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700114import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000116import android.view.ViewRootImpl;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700117import android.view.ViewRootImpl.ActivityConfigCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000119import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700121import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700122import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov24c90452017-12-27 15:17:14 -0800123import android.view.autofill.AutofillId;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700124import android.view.autofill.AutofillManager;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700125import android.view.autofill.AutofillManager.AutofillClient;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700126import android.view.autofill.AutofillPopupWindow;
127import android.view.autofill.IAutofillWindowPresenter;
Felipe Leme749b8892018-12-03 16:30:30 -0800128import android.view.contentcapture.ContentCaptureManager;
Felipe Lemecbf7f262019-04-17 13:57:59 -0700129import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800131import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000132import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100133
Adrian Roos4c864592019-04-10 14:47:57 +0200134import com.android.internal.R;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700135import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800136import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700137import com.android.internal.app.IVoiceInteractor;
138import com.android.internal.app.ToolbarActionBar;
139import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700140import com.android.internal.policy.PhoneWindow;
141
Mathieu Chartier63e14c12019-03-07 09:05:44 -0800142import dalvik.system.VMRuntime;
143
Dianne Hackborn625ac272010-09-17 18:29:22 -0700144import java.io.FileDescriptor;
145import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700146import java.lang.annotation.Retention;
147import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700148import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800149import java.util.Arrays;
Sunny Goyald40c3452019-03-20 12:46:55 -0700150import java.util.Collections;
Adam Powell6e346362010-07-23 10:18:23 -0700151import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700152import java.util.List;
Sunny Goyald40c3452019-03-20 12:46:55 -0700153import java.util.concurrent.Executor;
154import java.util.function.Consumer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
Bryce Leee83f34cd2017-10-31 19:50:54 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157/**
158 * An activity is a single, focused thing that the user can do. Almost all
159 * activities interact with the user, so the Activity class takes care of
160 * creating a window for you in which you can place your UI with
161 * {@link #setContentView}. While activities are often presented to the user
162 * as full-screen windows, they can also be used in other ways: as floating
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700163 * windows (via a theme with {@link android.R.attr#windowIsFloating} set),
164 * <a href="https://developer.android.com/guide/topics/ui/multi-window">
165 * Multi-Window mode</a> or embedded into other windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 *
167 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * <ul>
170 * <li> {@link #onCreate} is where you initialize your activity. Most
171 * importantly, here you will usually call {@link #setContentView(int)}
172 * with a layout resource defining your UI, and using {@link #findViewById}
173 * to retrieve the widgets in that UI that you need to interact with
174 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700175 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700176 * <li> {@link #onPause} is where you deal with the user pausing active
177 * interaction with the activity. Any changes made by the user should at
178 * this point be committed (usually to the
179 * {@link android.content.ContentProvider} holding the data). In this
180 * state the activity is still visible on screen.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * </ul>
182 *
183 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
184 * activity classes must have a corresponding
185 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
186 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <p>Topics covered here:
189 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700190 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
192 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
193 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
194 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
195 * <li><a href="#Permissions">Permissions</a>
196 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
197 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700198 *
199 * <div class="special reference">
200 * <h3>Developer Guides</h3>
201 * <p>The Activity class is an important part of an application's overall lifecycle,
202 * and the way activities are launched and put together is a fundamental
203 * part of the platform's application model. For a detailed perspective on the structure of an
204 * Android application and how activities behave, please read the
205 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700206 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700207 * developer guides.</p>
208 *
209 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700210 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700211 * developer guide.</p>
212 * </div>
213 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700214 * <a name="Fragments"></a>
215 * <h3>Fragments</h3>
216 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800217 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
218 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700219 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800220 * screens, and help scale their application between small and large screens.</p>
221 *
222 * <p>For more information about using fragments, read the
223 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <a name="ActivityLifecycle"></a>
226 * <h3>Activity Lifecycle</h3>
227 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700228 * <p>Activities in the system are managed as
229 * <a href="https://developer.android.com/guide/components/activities/tasks-and-back-stack">
230 * activity stacks</a>. When a new activity is started, it is usually placed on the top of the
231 * current stack and becomes the running activity -- the previous activity always remains
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * below it in the stack, and will not come to the foreground again until
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700233 * the new activity exits. There can be one or multiple activity stacks visible
234 * on screen.</p>
RoboErik55011652014-07-09 15:05:53 -0700235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <p>An activity has essentially four states:</p>
237 * <ul>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700238 * <li>If an activity is in the foreground of the screen (at the highest position of the topmost
239 * stack), it is <em>active</em> or <em>running</em>. This is usually the activity that the
240 * user is currently interacting with.</li>
241 * <li>If an activity has lost focus but is still presented to the user, it is <em>visible</em>.
242 * It is possible if a new non-full-sized or transparent activity has focus on top of your
243 * activity, another activity has higher position in multi-window mode, or the activity
244 * itself is not focusable in current windowing mode. Such activity is completely alive (it
245 * maintains all state and member information and remains attached to the window manager).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <li>If an activity is completely obscured by another activity,
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700247 * it is <em>stopped</em> or <em>hidden</em>. It still retains all state and member
248 * information, however, it is no longer visible to the user so its window is hidden
249 * and it will often be killed by the system when memory is needed elsewhere.</li>
250 * <li>The system can drop the activity from memory by either asking it to finish,
251 * or simply killing its process, making it <em>destroyed</em>. When it is displayed again
252 * 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 -0800253 * </ul>
254 *
255 * <p>The following diagram shows the important state paths of an Activity.
256 * The square rectangles represent callback methods you can implement to
257 * perform operations when the Activity moves between states. The colored
258 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * <p><img src="../../../images/activity_lifecycle.png"
261 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * <p>There are three key loops you may be interested in monitoring within your
264 * activity:
RoboErik55011652014-07-09 15:05:53 -0700265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * <ul>
267 * <li>The <b>entire lifetime</b> of an activity happens between the first call
268 * to {@link android.app.Activity#onCreate} through to a single final call
269 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
270 * of "global" state in onCreate(), and release all remaining resources in
271 * onDestroy(). For example, if it has a thread running in the background
272 * to download data from the network, it may create that thread in onCreate()
273 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * <li>The <b>visible lifetime</b> of an activity happens between a call to
276 * {@link android.app.Activity#onStart} until a corresponding call to
277 * {@link android.app.Activity#onStop}. During this time the user can see the
278 * activity on-screen, though it may not be in the foreground and interacting
279 * with the user. Between these two methods you can maintain resources that
280 * are needed to show the activity to the user. For example, you can register
281 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900282 * that impact your UI, and unregister it in onStop() when the user no
283 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * can be called multiple times, as the activity becomes visible and hidden
285 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700286 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
288 * {@link android.app.Activity#onResume} until a corresponding call to
289 * {@link android.app.Activity#onPause}. During this time the activity is
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700290 * in visible, active and interacting with the user. An activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 * can frequently go between the resumed and paused states -- for example when
292 * the device goes to sleep, when an activity result is delivered, when a new
293 * intent is delivered -- so the code in these methods should be fairly
294 * lightweight.
295 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 * <p>The entire lifecycle of an activity is defined by the following
298 * Activity methods. All of these are hooks that you can override
299 * to do appropriate work when the activity changes state. All
300 * activities will implement {@link android.app.Activity#onCreate}
301 * to do their initial setup; many will also implement
302 * {@link android.app.Activity#onPause} to commit changes to data and
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700303 * prepare to pause interacting with the user, and {@link android.app.Activity#onStop}
304 * to handle no longer being visible on screen. You should always
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * call up to your superclass when implementing these methods.</p>
306 *
307 * </p>
308 * <pre class="prettyprint">
309 * public class Activity extends ApplicationContext {
310 * protected void onCreate(Bundle savedInstanceState);
311 *
312 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700313 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * protected void onRestart();
315 *
316 * protected void onResume();
317 *
318 * protected void onPause();
319 *
320 * protected void onStop();
321 *
322 * protected void onDestroy();
323 * }
324 * </pre>
325 *
326 * <p>In general the movement through an activity's lifecycle looks like
327 * this:</p>
328 *
329 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
330 * <colgroup align="left" span="3" />
331 * <colgroup align="left" />
332 * <colgroup align="center" />
333 * <colgroup align="center" />
334 *
335 * <thead>
336 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
337 * </thead>
338 *
339 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700340 * <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 -0800341 * <td>Called when the activity is first created.
342 * This is where you should do all of your normal static set up:
343 * create views, bind data to lists, etc. This method also
344 * provides you with a Bundle containing the activity's previously
345 * frozen state, if there was one.
346 * <p>Always followed by <code>onStart()</code>.</td>
347 * <td align="center">No</td>
348 * <td align="center"><code>onStart()</code></td>
349 * </tr>
350 *
351 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700352 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 * <td>Called after your activity has been stopped, prior to it being
354 * started again.
355 * <p>Always followed by <code>onStart()</code></td>
356 * <td align="center">No</td>
357 * <td align="center"><code>onStart()</code></td>
358 * </tr>
359 *
smain@google.com10e29b82016-06-23 12:09:30 -0700360 * <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 -0800361 * <td>Called when the activity is becoming visible to the user.
362 * <p>Followed by <code>onResume()</code> if the activity comes
363 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
364 * <td align="center">No</td>
365 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
366 * </tr>
367 *
368 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700369 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * <td>Called when the activity will start
371 * interacting with the user. At this point your activity is at
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700372 * the top of its activity stack, with user input going to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * <p>Always followed by <code>onPause()</code>.</td>
374 * <td align="center">No</td>
375 * <td align="center"><code>onPause()</code></td>
376 * </tr>
377 *
smain@google.com10e29b82016-06-23 12:09:30 -0700378 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700379 * <td>Called when the activity loses foreground state, is no longer focusable or before
380 * transition to stopped/hidden or destroyed state. The activity is still visible to
381 * user, so it's recommended to keep it visually active and continue updating the UI.
382 * Implementations of this method must be very quick because
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 * the next activity will not be resumed until this method returns.
384 * <p>Followed by either <code>onResume()</code> if the activity
385 * returns back to the front, or <code>onStop()</code> if it becomes
386 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800387 * <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 -0800388 * <td align="center"><code>onResume()</code> or<br>
389 * <code>onStop()</code></td>
390 * </tr>
391 *
smain@google.com10e29b82016-06-23 12:09:30 -0700392 * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</td>
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700393 * <td>Called when the activity is no longer visible to the user. This may happen either
394 * because a new activity is being started on top, an existing one is being brought in
395 * front of this one, or this one is being destroyed. This is typically used to stop
396 * animations and refreshing the UI, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 * <p>Followed by either <code>onRestart()</code> if
398 * this activity is coming back to interact with the user, or
399 * <code>onDestroy()</code> if this activity is going away.</td>
400 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
401 * <td align="center"><code>onRestart()</code> or<br>
402 * <code>onDestroy()</code></td>
403 * </tr>
404 *
smain@google.com10e29b82016-06-23 12:09:30 -0700405 * <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 -0800406 * <td>The final call you receive before your
407 * activity is destroyed. This can happen either because the
408 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700409 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 * instance of the activity to save space. You can distinguish
411 * between these two scenarios with the {@link
412 * Activity#isFinishing} method.</td>
413 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
414 * <td align="center"><em>nothing</em></td>
415 * </tr>
416 * </tbody>
417 * </table>
418 *
419 * <p>Note the "Killable" column in the above table -- for those methods that
420 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800421 * 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 -0800422 * of its code being executed. Because of this, you should use the
423 * {@link #onPause} method to write any persistent data (such as user edits)
424 * to storage. In addition, the method
425 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
426 * in such a background state, allowing you to save away any dynamic instance
427 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700428 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
430 * section for more information on how the lifecycle of a process is tied
431 * to the activities it is hosting. Note that it is important to save
432 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800433 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * be called in every situation as described in its documentation.</p>
435 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800436 * <p class="note">Be aware that these semantics will change slightly between
437 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
438 * vs. those targeting prior platforms. Starting with Honeycomb, an application
439 * is not in the killable state until its {@link #onStop} has returned. This
440 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700441 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800442 * wait until {@link #onStop()} to save persistent state.</p>
443 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800444 * <p class="note">For applications targeting platforms starting with
445 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
446 * will always be called after {@link #onStop}, so an application may safely
447 * perform fragment transactions in {@link #onStop} and will be able to save
448 * persistent state later.</p>
449 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 * <p>For those methods that are not marked as being killable, the activity's
451 * process will not be killed by the system starting from the time the method
452 * is called and continuing after it returns. Thus an activity is in the killable
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700453 * state, for example, between after <code>onStop()</code> to the start of
454 * <code>onResume()</code>. Keep in mind that under extreme memory pressure the
455 * system can kill the application process at any time.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 *
457 * <a name="ConfigurationChanges"></a>
458 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700459 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * <p>If the configuration of the device (as defined by the
461 * {@link Configuration Resources.Configuration} class) changes,
462 * then anything displaying a user interface will need to update to match that
463 * configuration. Because Activity is the primary mechanism for interacting
464 * with the user, it includes special support for handling configuration
465 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 * <p>Unless you specify otherwise, a configuration change (such as a change
468 * in screen orientation, language, input devices, etc) will cause your
469 * current activity to be <em>destroyed</em>, going through the normal activity
470 * lifecycle process of {@link #onPause},
471 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
472 * had been in the foreground or visible to the user, once {@link #onDestroy} is
473 * called in that instance then a new instance of the activity will be
474 * created, with whatever savedInstanceState the previous instance had generated
475 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 * <p>This is done because any application resource,
478 * including layout files, can change based on any configuration value. Thus
479 * the only safe way to handle a configuration change is to re-retrieve all
480 * resources, including layouts, drawables, and strings. Because activities
481 * must already know how to save their state and re-create themselves from
482 * that state, this is a convenient way to have an activity restart itself
483 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700484 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 * <p>In some special cases, you may want to bypass restarting of your
486 * activity based on one or more types of configuration changes. This is
487 * done with the {@link android.R.attr#configChanges android:configChanges}
488 * attribute in its manifest. For any types of configuration changes you say
489 * that you handle there, you will receive a call to your current activity's
490 * {@link #onConfigurationChanged} method instead of being restarted. If
491 * a configuration change involves any that you do not handle, however, the
492 * activity will still be restarted and {@link #onConfigurationChanged}
493 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 * <a name="StartingActivities"></a>
496 * <h3>Starting Activities and Getting Results</h3>
497 *
498 * <p>The {@link android.app.Activity#startActivity}
499 * method is used to start a
500 * new activity, which will be placed at the top of the activity stack. It
501 * takes a single argument, an {@link android.content.Intent Intent},
502 * which describes the activity
503 * to be executed.</p>
504 *
505 * <p>Sometimes you want to get a result back from an activity when it
506 * ends. For example, you may start an activity that lets the user pick
507 * a person in a list of contacts; when it ends, it returns the person
508 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700509 * {@link android.app.Activity#startActivityForResult(Intent, int)}
510 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700512 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 *
514 * <p>When an activity exits, it can call
515 * {@link android.app.Activity#setResult(int)}
516 * to return data back to its parent. It must always supply a result code,
517 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
518 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
519 * return back an Intent containing any additional data it wants. All of this
520 * information appears back on the
521 * parent's <code>Activity.onActivityResult()</code>, along with the integer
522 * identifier it originally supplied.</p>
523 *
524 * <p>If a child activity fails for any reason (such as crashing), the parent
525 * activity will receive a result with the code RESULT_CANCELED.</p>
526 *
527 * <pre class="prettyprint">
528 * public class MyActivity extends Activity {
529 * ...
530 *
531 * static final int PICK_CONTACT_REQUEST = 0;
532 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700533 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
535 * // When the user center presses, let them pick a contact.
536 * startActivityForResult(
537 * new Intent(Intent.ACTION_PICK,
538 * new Uri("content://contacts")),
539 * PICK_CONTACT_REQUEST);
540 * return true;
541 * }
542 * return false;
543 * }
544 *
545 * protected void onActivityResult(int requestCode, int resultCode,
546 * Intent data) {
547 * if (requestCode == PICK_CONTACT_REQUEST) {
548 * if (resultCode == RESULT_OK) {
549 * // A contact was picked. Here we will just display it
550 * // to the user.
551 * startActivity(new Intent(Intent.ACTION_VIEW, data));
552 * }
553 * }
554 * }
555 * }
556 * </pre>
557 *
558 * <a name="SavingPersistentState"></a>
559 * <h3>Saving Persistent State</h3>
560 *
koprivafc3949e2018-09-17 11:35:13 -0700561 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * will deal with: shared document-like data (typically stored in a SQLite
563 * database using a {@linkplain android.content.ContentProvider content provider})
564 * and internal state such as user preferences.</p>
565 *
koprivafc3949e2018-09-17 11:35:13 -0700566 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 * "edit in place" user model. That is, any edits a user makes are effectively
568 * made immediately without requiring an additional confirmation step.
569 * Supporting this model is generally a simple matter of following two rules:</p>
570 *
571 * <ul>
572 * <li> <p>When creating a new document, the backing database entry or file for
573 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700574 * 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 -0800575 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700576 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
578 * commit to the backing content provider or file any changes the user
579 * has made. This ensures that those changes will be seen by any other
580 * activity that is about to run. You will probably want to commit
581 * your data even more aggressively at key times during your
582 * activity's lifecycle: for example before starting a new
583 * activity, before finishing your own activity, when the user
584 * switches between input fields, etc.</p>
585 * </ul>
586 *
587 * <p>This model is designed to prevent data loss when a user is navigating
588 * between activities, and allows the system to safely kill an activity (because
589 * system resources are needed somewhere else) at any time after it has been
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700590 * stopped (or paused on platform versions before {@link android.os.Build.VERSION_CODES#HONEYCOMB}).
591 * 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 -0800592 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800593 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
595 *
596 * <p>See the {@linkplain android.content.ContentProvider content package} for
597 * more information about content providers. These are a key aspect of how
598 * different activities invoke and propagate data between themselves.</p>
599 *
600 * <p>The Activity class also provides an API for managing internal persistent state
601 * associated with an activity. This can be used, for example, to remember
602 * the user's preferred initial display in a calendar (day view or week view)
603 * or the user's default home page in a web browser.</p>
604 *
605 * <p>Activity persistent state is managed
606 * with the method {@link #getPreferences},
607 * allowing you to retrieve and
608 * modify a set of name/value pairs associated with the activity. To use
609 * preferences that are shared across multiple application components
610 * (activities, receivers, services, providers), you can use the underlying
611 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
612 * to retrieve a preferences
613 * object stored under a specific name.
614 * (Note that it is not possible to share settings data across application
615 * packages -- for that you will need a content provider.)</p>
616 *
617 * <p>Here is an excerpt from a calendar activity that stores the user's
618 * preferred view mode in its persistent settings:</p>
619 *
620 * <pre class="prettyprint">
621 * public class CalendarActivity extends Activity {
622 * ...
623 *
624 * static final int DAY_VIEW_MODE = 0;
625 * static final int WEEK_VIEW_MODE = 1;
626 *
627 * private SharedPreferences mPrefs;
628 * private int mCurViewMode;
629 *
630 * protected void onCreate(Bundle savedInstanceState) {
631 * super.onCreate(savedInstanceState);
632 *
633 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700634 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * }
636 *
637 * protected void onPause() {
638 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * SharedPreferences.Editor ed = mPrefs.edit();
641 * ed.putInt("view_mode", mCurViewMode);
642 * ed.commit();
643 * }
644 * }
645 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * <a name="Permissions"></a>
648 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 * <p>The ability to start a particular Activity can be enforced when it is
651 * declared in its
652 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
653 * tag. By doing so, other applications will need to declare a corresponding
654 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
655 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800656 *
657 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
658 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
659 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
660 * Activity access to the specific URIs in the Intent. Access will remain
661 * until the Activity has finished (it will remain across the hosting
662 * process being killed and other temporary destruction). As of
663 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
664 * was already created and a new Intent is being delivered to
665 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
666 * to the existing ones it holds.
667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
669 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700670 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * <a name="ProcessLifecycle"></a>
672 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700673 *
kopriva4dddc632018-03-19 14:13:08 -0700674 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700676 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700678 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700680 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * processes (the last ones) before it resorts to killing more important
682 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 * <ol>
685 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
686 * that the user is currently interacting with) is considered the most important.
687 * Its process will only be killed as a last resort, if it uses more memory
688 * than is available on the device. Generally at this point the device has
689 * reached a memory paging state, so this is required in order to keep the user
690 * interface responsive.
691 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700692 * but not in the foreground, such as one sitting behind a foreground dialog
693 * or next to other activities in multi-window mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 * is considered extremely important and will not be killed unless that is
695 * required to keep the foreground activity running.
696 * <li> <p>A <b>background activity</b> (an activity that is not visible to
Andrii Kulian8e4df1e2019-03-19 15:17:56 -0700697 * the user and has been stopped) is no longer critical, so the system may
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 * safely kill its process to reclaim memory for other foreground or
699 * visible processes. If its process needs to be killed, when the user navigates
700 * back to the activity (making it visible on the screen again), its
701 * {@link #onCreate} method will be called with the savedInstanceState it had previously
702 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
703 * state as the user last left it.
704 * <li> <p>An <b>empty process</b> is one hosting no activities or other
705 * application components (such as {@link Service} or
706 * {@link android.content.BroadcastReceiver} classes). These are killed very
707 * quickly by the system as memory becomes low. For this reason, any
708 * background operation you do outside of an activity must be executed in the
709 * context of an activity BroadcastReceiver or Service to ensure that the system
710 * knows it needs to keep your process around.
711 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 * <p>Sometimes an Activity may need to do a long-running operation that exists
714 * independently of the activity lifecycle itself. An example may be a camera
715 * application that allows you to upload a picture to a web site. The upload
716 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700717 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 * should start a {@link Service} in which the upload takes place. This allows
719 * the system to properly prioritize your process (considering it to be more
720 * important than other non-visible applications) for the duration of the
721 * upload, independent of whether the original activity is paused, stopped,
722 * or finished.
723 */
724public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700725 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700727 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000728 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Lemecbf7f262019-04-17 13:57:59 -0700729 AutofillManager.AutofillClient, ContentCaptureManager.ContentCaptureClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700731 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732
733 /** Standard activity result: operation canceled. */
734 public static final int RESULT_CANCELED = 0;
735 /** Standard activity result: operation succeeded. */
736 public static final int RESULT_OK = -1;
737 /** Start of user-defined activity results. */
738 public static final int RESULT_FIRST_USER = 1;
739
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700740 /** @hide Task isn't finished when activity is finished */
741 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700742 /**
743 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
744 * past behavior the task is also removed from recents.
745 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700746 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700747 /**
748 * @hide Task is finished along with the finishing activity, but it is not removed from
749 * recents.
750 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700751 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
752
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100753 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700754 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700755 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700756
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700757 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
759 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
760 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
761 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800762 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700763 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
764 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
Svetoslav970b59c2015-06-09 16:05:21 -0700766 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000767 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700768
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100769 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100770
Andrii Kuliand25680c2018-02-21 15:16:58 -0800771 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
772 private static final int LOG_AM_ON_START_CALLED = 30059;
773 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
774 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
775 private static final int LOG_AM_ON_STOP_CALLED = 30049;
776 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
777 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700778 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kulian86024012019-04-18 19:41:23 -0700779 private static final int LOG_AM_ON_TOP_RESUMED_GAINED_CALLED = 30064;
780 private static final int LOG_AM_ON_TOP_RESUMED_LOST_CALLED = 30065;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800781
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800782 private static class ManagedDialog {
783 Dialog mDialog;
784 Bundle mArgs;
785 }
786 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
788 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100789 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 private Instrumentation mInstrumentation;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100791 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 private IBinder mToken;
Sunny Goyald40c3452019-03-20 12:46:55 -0700793 private IBinder mAssistToken;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100794 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700795 private int mIdent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100796 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 /*package*/ String mEmbeddedID;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100798 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 private Application mApplication;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100800 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700801 /*package*/ Intent mIntent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100802 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800803 /*package*/ String mReferrer;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100804 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 private ComponentName mComponent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100806 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100808 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 /*package*/ ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100810 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 Activity mParent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100812 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 boolean mCalled;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100814 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600815 /*package*/ boolean mResumed;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100816 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700817 /*package*/ boolean mStopped;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100818 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 boolean mFinished;
820 boolean mStartedActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100821 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700822 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700823 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200824 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700825
826 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
827 * This only affects the client-side exception, the actual state check still happens in AMS. */
828 private boolean mCanEnterPictureInPicture = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500829 /** true if the activity is being destroyed in order to recreate it with a new configuration */
830 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100831 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 /*package*/ int mConfigChangeFlags;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100833 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100835 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700836 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700838 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
839 @Nullable private AutofillManager mAutofillManager;
840
Adam He43c06992019-04-15 15:41:49 -0700841 /** The content capture manager. Access via {@link #getContentCaptureManager()}. */
Felipe Lemeecb08be2018-11-27 15:48:47 -0800842 @Nullable private ContentCaptureManager mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800843
Ian Lake8a88cd62018-09-28 13:25:20 -0700844 private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
845 new ArrayList<Application.ActivityLifecycleCallbacks>();
Felipe Lemee348dc32018-11-05 12:35:29 -0800846
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700847 static final class NonConfigurationInstances {
848 Object activity;
849 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800850 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700851 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700852 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700853 }
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100854 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700855 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700856
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100857 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 private Window mWindow;
859
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100860 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 private WindowManager mWindowManager;
862 /*package*/ View mDecor = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100863 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 /*package*/ boolean mWindowAdded = false;
865 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood31755f92018-12-20 13:53:36 +0000866 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700868 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700869 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870
Mathew Inwood8c854f82018-09-14 12:35:36 +0100871 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Sunny Goyald40c3452019-03-20 12:46:55 -0700872 VoiceInteractor mVoiceInteractor;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700873
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100874 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 private CharSequence mTitle;
876 private int mTitleColor = 0;
877
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700878 // we must have a handler before the FragmentController is constructed
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100879 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700880 final Handler mHandler = new Handler();
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100881 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700882 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 private static final class ManagedCursor {
885 ManagedCursor(Cursor cursor) {
886 mCursor = cursor;
887 mReleased = false;
888 mUpdated = false;
889 }
890
891 private final Cursor mCursor;
892 private boolean mReleased;
893 private boolean mUpdated;
894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700896 @GuardedBy("mManagedCursors")
897 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
898
899 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100900 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700902 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100903 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700905
Craig Mautner5eda9b32013-07-02 11:58:16 -0700906 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700907 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700909 private SearchEvent mSearchEvent;
910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000912 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913
914 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
915 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700916
Winsonb6403152016-02-23 13:32:09 -0800917 private ActivityManager.TaskDescription mTaskDescription =
918 new ActivityManager.TaskDescription();
919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
921
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700922 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700923 private final Object mInstanceTracker = StrictMode.trackActivity(this);
924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700926
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100927 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700928 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700929 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
930 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
Svetoslavffb32b12015-10-15 16:54:00 -0700932 private boolean mHasCurrentPermissionsRequest;
933
Svet Ganov17db9dc2017-02-21 19:54:31 -0800934 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800935 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800936
Phil Weaver846cda932017-06-15 10:10:06 -0700937 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
938 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700939
Felipe Leme4753bb02017-03-22 20:24:00 -0700940 private AutofillPopupWindow mAutofillPopupWindow;
941
chaviwfeb2e1e2018-12-19 17:24:11 -0800942 /** @hide */
943 boolean mEnterAnimationComplete;
944
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800945 private static native String getDlWarning();
946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 /** Return the intent that started this activity. */
948 public Intent getIntent() {
949 return mIntent;
950 }
951
RoboErik55011652014-07-09 15:05:53 -0700952 /**
953 * Change the intent returned by {@link #getIntent}. This holds a
954 * reference to the given intent; it does not copy it. Often used in
955 * conjunction with {@link #onNewIntent}.
956 *
957 * @param newIntent The new Intent object to return from getIntent
958 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 * @see #getIntent
960 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700961 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 public void setIntent(Intent newIntent) {
963 mIntent = newIntent;
964 }
965
966 /** Return the application that owns this activity. */
967 public final Application getApplication() {
968 return mApplication;
969 }
970
971 /** Is this activity embedded inside of another activity? */
972 public final boolean isChild() {
973 return mParent != null;
974 }
RoboErik55011652014-07-09 15:05:53 -0700975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 /** Return the parent activity if this view is an embedded child. */
977 public final Activity getParent() {
978 return mParent;
979 }
980
981 /** Retrieve the window manager for showing custom windows. */
982 public WindowManager getWindowManager() {
983 return mWindowManager;
984 }
985
986 /**
987 * Retrieve the current {@link android.view.Window} for the activity.
988 * This can be used to directly access parts of the Window API that
989 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 * @return Window The current window, or null if the activity is not
992 * visual.
993 */
994 public Window getWindow() {
995 return mWindow;
996 }
997
998 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800999 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -08001000 *
1001 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -07001002 */
Ian Lake0a1feb82017-11-13 10:26:46 -08001003 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07001004 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001005 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -07001006 }
RoboErik55011652014-07-09 15:05:53 -07001007
Dianne Hackbornc8017682010-07-06 13:34:38 -07001008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 * Calls {@link android.view.Window#getCurrentFocus} on the
1010 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -07001011 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -07001013 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 * @see #getWindow
1015 * @see android.view.Window#getCurrentFocus
1016 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001017 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 public View getCurrentFocus() {
1019 return mWindow != null ? mWindow.getCurrentFocus() : null;
1020 }
1021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 /**
Felipe Lemee348dc32018-11-05 12:35:29 -08001023 * (Creates, sets and) returns the autofill manager
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001024 *
1025 * @return The autofill manager
1026 */
1027 @NonNull private AutofillManager getAutofillManager() {
1028 if (mAutofillManager == null) {
1029 mAutofillManager = getSystemService(AutofillManager.class);
1030 }
1031
1032 return mAutofillManager;
1033 }
1034
Felipe Lemee348dc32018-11-05 12:35:29 -08001035 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -08001036 * (Creates, sets, and ) returns the content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001037 *
Felipe Lemeecb08be2018-11-27 15:48:47 -08001038 * @return The content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001039 */
Adam Heb66babb2019-01-08 15:43:53 -08001040 @Nullable private ContentCaptureManager getContentCaptureManager() {
1041 // ContextCapture disabled for system apps
Felipe Leme0c6b23d2019-01-15 17:02:49 -08001042 if (!UserHandle.isApp(myUid())) return null;
Felipe Lemeecb08be2018-11-27 15:48:47 -08001043 if (mContentCaptureManager == null) {
1044 mContentCaptureManager = getSystemService(ContentCaptureManager.class);
Felipe Lemee348dc32018-11-05 12:35:29 -08001045 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001046 return mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -08001047 }
1048
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001049 /** @hide */ private static final int CONTENT_CAPTURE_START = 1;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001050 /** @hide */ private static final int CONTENT_CAPTURE_RESUME = 2;
1051 /** @hide */ private static final int CONTENT_CAPTURE_PAUSE = 3;
1052 /** @hide */ private static final int CONTENT_CAPTURE_STOP = 4;
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001053
1054 /** @hide */
1055 @IntDef(prefix = { "CONTENT_CAPTURE_" }, value = {
1056 CONTENT_CAPTURE_START,
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001057 CONTENT_CAPTURE_RESUME,
1058 CONTENT_CAPTURE_PAUSE,
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001059 CONTENT_CAPTURE_STOP
1060 })
1061 @Retention(RetentionPolicy.SOURCE)
1062 @interface ContentCaptureNotificationType{}
1063
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001064 private String getContentCaptureTypeAsString(@ContentCaptureNotificationType int type) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001065 switch (type) {
1066 case CONTENT_CAPTURE_START:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001067 return "START";
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001068 case CONTENT_CAPTURE_RESUME:
1069 return "RESUME";
1070 case CONTENT_CAPTURE_PAUSE:
1071 return "PAUSE";
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001072 case CONTENT_CAPTURE_STOP:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001073 return "STOP";
Felipe Leme7a534082018-11-05 15:03:04 -08001074 default:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001075 return "UNKNOW-" + type;
1076 }
1077 }
1078
1079 private void notifyContentCaptureManagerIfNeeded(@ContentCaptureNotificationType int type) {
1080 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1081 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1082 "notifyContentCapture(" + getContentCaptureTypeAsString(type) + ") for "
1083 + mComponent.toShortString());
1084 }
1085 try {
1086 final ContentCaptureManager cm = getContentCaptureManager();
1087 if (cm == null) return;
1088
1089 switch (type) {
1090 case CONTENT_CAPTURE_START:
1091 //TODO(b/111276913): decide whether the InteractionSessionId should be
1092 // saved / restored in the activity bundle - probably not
Adam He43c06992019-04-15 15:41:49 -07001093 final Window window = getWindow();
1094 if (window != null) {
1095 cm.updateWindowAttributes(window.getAttributes());
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001096 }
Adam He43c06992019-04-15 15:41:49 -07001097 cm.onActivityCreated(mToken, getComponentName());
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001098 break;
1099 case CONTENT_CAPTURE_RESUME:
1100 cm.onActivityResumed();
1101 break;
1102 case CONTENT_CAPTURE_PAUSE:
1103 cm.onActivityPaused();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001104 break;
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001105 case CONTENT_CAPTURE_STOP:
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001106 cm.onActivityDestroyed();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001107 break;
1108 default:
1109 Log.wtf(TAG, "Invalid @ContentCaptureNotificationType: " + type);
1110 }
1111 } finally {
1112 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Felipe Leme7a534082018-11-05 15:03:04 -08001113 }
1114 }
1115
Felipe Lemebb567ae2017-10-04 09:56:21 -07001116 @Override
1117 protected void attachBaseContext(Context newBase) {
1118 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001119 if (newBase != null) {
1120 newBase.setAutofillClient(this);
Felipe Leme326f15a2019-02-19 09:42:24 -08001121 newBase.setContentCaptureOptions(getContentCaptureOptions());
Felipe Lemed247de82018-05-14 17:51:58 -07001122 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001123 }
1124
1125 /** @hide */
1126 @Override
1127 public final AutofillClient getAutofillClient() {
1128 return this;
1129 }
1130
Felipe Lemecbf7f262019-04-17 13:57:59 -07001131 /** @hide */
1132 @Override
1133 public final ContentCaptureClient getContentCaptureClient() {
1134 return this;
1135 }
1136
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001137 /**
Ian Lake8a88cd62018-09-28 13:25:20 -07001138 * Register an {@link Application.ActivityLifecycleCallbacks} instance that receives
1139 * lifecycle callbacks for only this Activity.
1140 * <p>
1141 * In relation to any
1142 * {@link Application#registerActivityLifecycleCallbacks Application registered callbacks},
1143 * the callbacks registered here will always occur nested within those callbacks. This means:
1144 * <ul>
1145 * <li>Pre events will first be sent to Application registered callbacks, then to callbacks
1146 * registered here.</li>
1147 * <li>{@link Application.ActivityLifecycleCallbacks#onActivityCreated(Activity, Bundle)},
1148 * {@link Application.ActivityLifecycleCallbacks#onActivityStarted(Activity)}, and
1149 * {@link Application.ActivityLifecycleCallbacks#onActivityResumed(Activity)} will
1150 * be sent first to Application registered callbacks, then to callbacks registered here.
1151 * For all other events, callbacks registered here will be sent first.</li>
1152 * <li>Post events will first be sent to callbacks registered here, then to
1153 * Application registered callbacks.</li>
1154 * </ul>
1155 * <p>
1156 * If multiple callbacks are registered here, they receive events in a first in (up through
1157 * {@link Application.ActivityLifecycleCallbacks#onActivityPostResumed}, last out
1158 * ordering.
1159 * <p>
1160 * It is strongly recommended to register this in the constructor of your Activity to ensure
1161 * you get all available callbacks. As this callback is associated with only this Activity,
1162 * it is not usually necessary to {@link #unregisterActivityLifecycleCallbacks unregister} it
1163 * unless you specifically do not want to receive further lifecycle callbacks.
1164 *
1165 * @param callback The callback instance to register
1166 */
1167 public void registerActivityLifecycleCallbacks(
1168 @NonNull Application.ActivityLifecycleCallbacks callback) {
1169 synchronized (mActivityLifecycleCallbacks) {
1170 mActivityLifecycleCallbacks.add(callback);
1171 }
1172 }
1173
1174 /**
1175 * Unregister an {@link Application.ActivityLifecycleCallbacks} previously registered
1176 * with {@link #registerActivityLifecycleCallbacks}. It will not receive any further
1177 * callbacks.
1178 *
1179 * @param callback The callback instance to unregister
1180 * @see #registerActivityLifecycleCallbacks
1181 */
1182 public void unregisterActivityLifecycleCallbacks(
1183 @NonNull Application.ActivityLifecycleCallbacks callback) {
1184 synchronized (mActivityLifecycleCallbacks) {
1185 mActivityLifecycleCallbacks.remove(callback);
1186 }
1187 }
1188
1189 private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
1190 getApplication().dispatchActivityPreCreated(this, savedInstanceState);
1191 Object[] callbacks = collectActivityLifecycleCallbacks();
1192 if (callbacks != null) {
1193 for (int i = 0; i < callbacks.length; i++) {
1194 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
1195 savedInstanceState);
1196 }
1197 }
1198 }
1199
1200 private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
1201 getApplication().dispatchActivityCreated(this, savedInstanceState);
1202 Object[] callbacks = collectActivityLifecycleCallbacks();
1203 if (callbacks != null) {
1204 for (int i = 0; i < callbacks.length; i++) {
1205 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
1206 savedInstanceState);
1207 }
1208 }
1209 }
1210
1211 private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
1212 Object[] callbacks = collectActivityLifecycleCallbacks();
1213 if (callbacks != null) {
1214 for (int i = 0; i < callbacks.length; i++) {
1215 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
1216 savedInstanceState);
1217 }
1218 }
1219 getApplication().dispatchActivityPostCreated(this, savedInstanceState);
1220 }
1221
1222 private void dispatchActivityPreStarted() {
1223 getApplication().dispatchActivityPreStarted(this);
1224 Object[] callbacks = collectActivityLifecycleCallbacks();
1225 if (callbacks != null) {
1226 for (int i = 0; i < callbacks.length; i++) {
1227 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(this);
1228 }
1229 }
1230 }
1231
1232 private void dispatchActivityStarted() {
1233 getApplication().dispatchActivityStarted(this);
1234 Object[] callbacks = collectActivityLifecycleCallbacks();
1235 if (callbacks != null) {
1236 for (int i = 0; i < callbacks.length; i++) {
1237 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
1238 }
1239 }
1240 }
1241
1242 private void dispatchActivityPostStarted() {
1243 Object[] callbacks = collectActivityLifecycleCallbacks();
1244 if (callbacks != null) {
1245 for (int i = 0; i < callbacks.length; i++) {
1246 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1247 .onActivityPostStarted(this);
1248 }
1249 }
1250 getApplication().dispatchActivityPostStarted(this);
1251 }
1252
1253 private void dispatchActivityPreResumed() {
1254 getApplication().dispatchActivityPreResumed(this);
1255 Object[] callbacks = collectActivityLifecycleCallbacks();
1256 if (callbacks != null) {
1257 for (int i = 0; i < callbacks.length; i++) {
1258 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(this);
1259 }
1260 }
1261 }
1262
1263 private void dispatchActivityResumed() {
1264 getApplication().dispatchActivityResumed(this);
1265 Object[] callbacks = collectActivityLifecycleCallbacks();
1266 if (callbacks != null) {
1267 for (int i = 0; i < callbacks.length; i++) {
1268 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
1269 }
1270 }
1271 }
1272
1273 private void dispatchActivityPostResumed() {
1274 Object[] callbacks = collectActivityLifecycleCallbacks();
1275 if (callbacks != null) {
1276 for (int i = 0; i < callbacks.length; i++) {
1277 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(this);
1278 }
1279 }
1280 getApplication().dispatchActivityPostResumed(this);
1281 }
1282
1283 private void dispatchActivityPrePaused() {
1284 getApplication().dispatchActivityPrePaused(this);
1285 Object[] callbacks = collectActivityLifecycleCallbacks();
1286 if (callbacks != null) {
1287 for (int i = callbacks.length - 1; i >= 0; i--) {
1288 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(this);
1289 }
1290 }
1291 }
1292
1293 private void dispatchActivityPaused() {
1294 Object[] callbacks = collectActivityLifecycleCallbacks();
1295 if (callbacks != null) {
1296 for (int i = callbacks.length - 1; i >= 0; i--) {
1297 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(this);
1298 }
1299 }
1300 getApplication().dispatchActivityPaused(this);
1301 }
1302
1303 private void dispatchActivityPostPaused() {
1304 Object[] callbacks = collectActivityLifecycleCallbacks();
1305 if (callbacks != null) {
1306 for (int i = callbacks.length - 1; i >= 0; i--) {
1307 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(this);
1308 }
1309 }
1310 getApplication().dispatchActivityPostPaused(this);
1311 }
1312
1313 private void dispatchActivityPreStopped() {
1314 getApplication().dispatchActivityPreStopped(this);
1315 Object[] callbacks = collectActivityLifecycleCallbacks();
1316 if (callbacks != null) {
1317 for (int i = callbacks.length - 1; i >= 0; i--) {
1318 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(this);
1319 }
1320 }
1321 }
1322
1323 private void dispatchActivityStopped() {
1324 Object[] callbacks = collectActivityLifecycleCallbacks();
1325 if (callbacks != null) {
1326 for (int i = callbacks.length - 1; i >= 0; i--) {
1327 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(this);
1328 }
1329 }
1330 getApplication().dispatchActivityStopped(this);
1331 }
1332
1333 private void dispatchActivityPostStopped() {
1334 Object[] callbacks = collectActivityLifecycleCallbacks();
1335 if (callbacks != null) {
1336 for (int i = callbacks.length - 1; i >= 0; i--) {
1337 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1338 .onActivityPostStopped(this);
1339 }
1340 }
1341 getApplication().dispatchActivityPostStopped(this);
1342 }
1343
1344 private void dispatchActivityPreSaveInstanceState(@NonNull Bundle outState) {
1345 getApplication().dispatchActivityPreSaveInstanceState(this, outState);
1346 Object[] callbacks = collectActivityLifecycleCallbacks();
1347 if (callbacks != null) {
1348 for (int i = callbacks.length - 1; i >= 0; i--) {
1349 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1350 .onActivityPreSaveInstanceState(this, outState);
1351 }
1352 }
1353 }
1354
1355 private void dispatchActivitySaveInstanceState(@NonNull Bundle outState) {
1356 Object[] callbacks = collectActivityLifecycleCallbacks();
1357 if (callbacks != null) {
1358 for (int i = callbacks.length - 1; i >= 0; i--) {
1359 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1360 .onActivitySaveInstanceState(this, outState);
1361 }
1362 }
1363 getApplication().dispatchActivitySaveInstanceState(this, outState);
1364 }
1365
1366 private void dispatchActivityPostSaveInstanceState(@NonNull Bundle outState) {
1367 Object[] callbacks = collectActivityLifecycleCallbacks();
1368 if (callbacks != null) {
1369 for (int i = callbacks.length - 1; i >= 0; i--) {
1370 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1371 .onActivityPostSaveInstanceState(this, outState);
1372 }
1373 }
1374 getApplication().dispatchActivityPostSaveInstanceState(this, outState);
1375 }
1376
1377 private void dispatchActivityPreDestroyed() {
1378 getApplication().dispatchActivityPreDestroyed(this);
1379 Object[] callbacks = collectActivityLifecycleCallbacks();
1380 if (callbacks != null) {
1381 for (int i = callbacks.length - 1; i >= 0; i--) {
1382 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1383 .onActivityPreDestroyed(this);
1384 }
1385 }
1386 }
1387
1388 private void dispatchActivityDestroyed() {
1389 Object[] callbacks = collectActivityLifecycleCallbacks();
1390 if (callbacks != null) {
1391 for (int i = callbacks.length - 1; i >= 0; i--) {
1392 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
1393 }
1394 }
1395 getApplication().dispatchActivityDestroyed(this);
1396 }
1397
1398 private void dispatchActivityPostDestroyed() {
1399 Object[] callbacks = collectActivityLifecycleCallbacks();
1400 if (callbacks != null) {
1401 for (int i = callbacks.length - 1; i >= 0; i--) {
1402 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1403 .onActivityPostDestroyed(this);
1404 }
1405 }
1406 getApplication().dispatchActivityPostDestroyed(this);
1407 }
1408
1409 private Object[] collectActivityLifecycleCallbacks() {
1410 Object[] callbacks = null;
1411 synchronized (mActivityLifecycleCallbacks) {
1412 if (mActivityLifecycleCallbacks.size() > 0) {
1413 callbacks = mActivityLifecycleCallbacks.toArray();
1414 }
1415 }
1416 return callbacks;
1417 }
1418
1419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 * Called when the activity is starting. This is where most initialization
1421 * should go: calling {@link #setContentView(int)} to inflate the
1422 * activity's UI, using {@link #findViewById} to programmatically interact
1423 * with widgets in the UI, calling
1424 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1425 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001428 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1429 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1430 * executing.
RoboErik55011652014-07-09 15:05:53 -07001431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * <p><em>Derived classes must call through to the super class's
1433 * implementation of this method. If they do not, an exception will be
1434 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001435 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 * @param savedInstanceState If the activity is being re-initialized after
1437 * previously being shut down then this Bundle contains the data it most
1438 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001439 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 * @see #onStart
1441 * @see #onSaveInstanceState
1442 * @see #onRestoreInstanceState
1443 * @see #onPostCreate
1444 */
Tor Norbye83c68962015-03-10 20:55:31 -07001445 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001446 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001447 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001448 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001449
Dianne Hackborn2707d602010-07-09 18:01:20 -07001450 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001451 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001452 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001453 if (mActivityInfo.parentActivityName != null) {
1454 if (mActionBar == null) {
1455 mEnableDefaultActionBarUp = true;
1456 } else {
1457 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1458 }
1459 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001460 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001461 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001462 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1463 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001464
1465 if (mAutoFillResetNeeded) {
1466 getAutofillManager().onCreate(savedInstanceState);
1467 }
1468
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001469 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1470 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1471 ? mLastNonConfigurationInstances.fragments : null);
1472 }
1473 mFragments.dispatchCreate();
Ian Lake8a88cd62018-09-28 13:25:20 -07001474 dispatchActivityCreated(savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001475 if (mVoiceInteractor != null) {
1476 mVoiceInteractor.attachActivity(this);
1477 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001478 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mCalled = true;
Felipe Lemee348dc32018-11-05 12:35:29 -08001480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
1482
1483 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001484 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001485 * the attribute {@link android.R.attr#persistableMode} set to
1486 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001487 *
1488 * @param savedInstanceState if the activity is being re-initialized after
1489 * previously being shut down then this Bundle contains the data it most
1490 * recently supplied in {@link #onSaveInstanceState}.
1491 * <b><i>Note: Otherwise it is null.</i></b>
1492 * @param persistentState if the activity is being re-initialized after
1493 * previously being shut down or powered off then this Bundle contains the data it most
1494 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1495 * <b><i>Note: Otherwise it is null.</i></b>
1496 *
1497 * @see #onCreate(android.os.Bundle)
1498 * @see #onStart
1499 * @see #onSaveInstanceState
1500 * @see #onRestoreInstanceState
1501 * @see #onPostCreate
1502 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001503 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001504 @Nullable PersistableBundle persistentState) {
1505 onCreate(savedInstanceState);
1506 }
1507
1508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * The hook for {@link ActivityThread} to restore the state of this activity.
1510 *
1511 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1512 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1513 *
1514 * @param savedInstanceState contains the saved state
1515 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001516 final void performRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 onRestoreInstanceState(savedInstanceState);
1518 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520
1521 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001522 * The hook for {@link ActivityThread} to restore the state of this activity.
1523 *
1524 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1525 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1526 *
1527 * @param savedInstanceState contains the saved state
1528 * @param persistentState contains the persistable saved state
1529 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001530 final void performRestoreInstanceState(@Nullable Bundle savedInstanceState,
1531 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001532 onRestoreInstanceState(savedInstanceState, persistentState);
1533 if (savedInstanceState != null) {
1534 restoreManagedDialogs(savedInstanceState);
1535 }
1536 }
1537
1538 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 * This method is called after {@link #onStart} when the activity is
1540 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001541 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * to restore their state, but it is sometimes convenient to do it here
1543 * after all of the initialization has been done or to allow subclasses to
1544 * decide whether to use your default implementation. The default
1545 * implementation of this method performs a restore of any view state that
1546 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 * <p>This method is called between {@link #onStart} and
Kevin Hufnagle4fbcec12019-07-18 21:37:36 +00001549 * {@link #onPostCreate}. This method is called only when recreating
1550 * an activity; the method isn't invoked if {@link #onStart} is called for
1551 * any other reason.</p>
RoboErik55011652014-07-09 15:05:53 -07001552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 * @see #onCreate
1556 * @see #onPostCreate
1557 * @see #onResume
1558 * @see #onSaveInstanceState
1559 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001560 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 if (mWindow != null) {
1562 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1563 if (windowState != null) {
1564 mWindow.restoreHierarchyState(windowState);
1565 }
1566 }
1567 }
Craig Mautnera0026042014-04-23 11:45:37 -07001568
1569 /**
1570 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001571 * created with the attribute {@link android.R.attr#persistableMode} set to
1572 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1573 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001574 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1575 *
1576 * <p>This method is called between {@link #onStart} and
1577 * {@link #onPostCreate}.
1578 *
1579 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1580 *
Jake Wharton63f4d892018-07-10 12:27:59 -04001581 * <p>At least one of {@code savedInstanceState} or {@code persistentState} will not be null.
1582 *
1583 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}
1584 * or null.
1585 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}
1586 * or null.
Craig Mautnera0026042014-04-23 11:45:37 -07001587 *
1588 * @see #onRestoreInstanceState(Bundle)
1589 * @see #onCreate
1590 * @see #onPostCreate
1591 * @see #onResume
1592 * @see #onSaveInstanceState
1593 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001594 public void onRestoreInstanceState(@Nullable Bundle savedInstanceState,
1595 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001596 if (savedInstanceState != null) {
1597 onRestoreInstanceState(savedInstanceState);
1598 }
1599 }
1600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /**
1602 * Restore the state of any saved managed dialogs.
1603 *
1604 * @param savedInstanceState The bundle to restore from.
1605 */
1606 private void restoreManagedDialogs(Bundle savedInstanceState) {
1607 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1608 if (b == null) {
1609 return;
1610 }
1611
1612 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1613 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001614 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 for (int i = 0; i < numDialogs; i++) {
1616 final Integer dialogId = ids[i];
1617 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1618 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001619 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1620 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001621 final ManagedDialog md = new ManagedDialog();
1622 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1623 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1624 if (md.mDialog != null) {
1625 mManagedDialogs.put(dialogId, md);
1626 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1627 md.mDialog.onRestoreInstanceState(dialogState);
1628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630 }
1631 }
1632
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001633 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1634 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001635 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001636 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001637 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001638 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001639 return dialog;
1640 }
1641
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001642 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 return SAVED_DIALOG_KEY_PREFIX + key;
1644 }
1645
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001646 private static String savedDialogArgsKeyFor(int key) {
1647 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649
1650 /**
1651 * Called when activity start-up is complete (after {@link #onStart}
1652 * and {@link #onRestoreInstanceState} have been called). Applications will
1653 * generally not implement this method; it is intended for system
1654 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 * <p><em>Derived classes must call through to the super class's
1657 * implementation of this method. If they do not, an exception will be
1658 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 * @param savedInstanceState If the activity is being re-initialized after
1661 * previously being shut down then this Bundle contains the data it most
1662 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1663 * @see #onCreate
1664 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001665 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001666 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 if (!isChild()) {
1668 mTitleReady = true;
1669 onTitleChanged(getTitle(), getTitleColor());
1670 }
Winsonb6403152016-02-23 13:32:09 -08001671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 mCalled = true;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001673
1674 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_START);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
1676
1677 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001678 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001679 * created with the attribute {@link android.R.attr#persistableMode} set to
1680 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001681 *
1682 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1683 * @param persistentState The data caming from the PersistableBundle first
1684 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1685 *
1686 * @see #onCreate
1687 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001688 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001689 @Nullable PersistableBundle persistentState) {
1690 onPostCreate(savedInstanceState);
1691 }
1692
1693 /**
RoboErik55011652014-07-09 15:05:53 -07001694 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1695 * the activity had been stopped, but is now again being displayed to the
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001696 * user. It will usually be followed by {@link #onResume}. This is a good place to begin
1697 * drawing visual elements, running animations, etc.
1698 *
1699 * <p>You can call {@link #finish} from within this function, in
1700 * which case {@link #onStop} will be immediately called after {@link #onStart} without the
1701 * lifecycle transitions in-between ({@link #onResume}, {@link #onPause}, etc) executing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 *
1703 * <p><em>Derived classes must call through to the super class's
1704 * implementation of this method. If they do not, an exception will be
1705 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001706 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 * @see #onCreate
1708 * @see #onStop
1709 * @see #onResume
1710 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001711 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001713 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001715
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001716 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001717
Ian Lake8a88cd62018-09-28 13:25:20 -07001718 dispatchActivityStarted();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001719
1720 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001721 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724
1725 /**
1726 * Called after {@link #onStop} when the current activity is being
1727 * re-displayed to the user (the user has navigated back to it). It will
1728 * be followed by {@link #onStart} and then {@link #onResume}.
1729 *
1730 * <p>For activities that are using raw {@link Cursor} objects (instead of
1731 * creating them through
1732 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1733 * this is usually the place
1734 * where the cursor should be requeried (because you had deactivated it in
1735 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 * <p><em>Derived classes must call through to the super class's
1738 * implementation of this method. If they do not, an exception will be
1739 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001740 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 * @see #onStop
1742 * @see #onStart
1743 * @see #onResume
1744 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001745 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 protected void onRestart() {
1747 mCalled = true;
1748 }
1749
1750 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001751 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1752 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1753 * to give the activity a hint that its state is no longer saved -- it will generally
1754 * be called after {@link #onSaveInstanceState} and prior to the activity being
1755 * resumed/started again.
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001756 *
1757 * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
1758 * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
1759 * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001760 */
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001761 @Deprecated
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001762 public void onStateNotSaved() {
1763 }
1764
1765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001767 * {@link #onPause}, for your activity to start interacting with the user. This is an indicator
1768 * that the activity became active and ready to receive input. It is on top of an activity stack
1769 * and visible to user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001771 * <p>On platform versions prior to {@link android.os.Build.VERSION_CODES#Q} this is also a good
1772 * place to try to open exclusive-access devices or to get access to singleton resources.
1773 * Starting with {@link android.os.Build.VERSION_CODES#Q} there can be multiple resumed
1774 * activities in the system simultaneously, so {@link #onTopResumedActivityChanged(boolean)}
1775 * should be used for that purpose instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 *
1777 * <p><em>Derived classes must call through to the super class's
1778 * implementation of this method. If they do not, an exception will be
1779 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001780 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 * @see #onRestoreInstanceState
1782 * @see #onRestart
1783 * @see #onPostResume
1784 * @see #onPause
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001785 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001787 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001789 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07001790 dispatchActivityResumed();
Sunny Goyal64643f62019-01-31 15:15:33 -08001791 mActivityTransitionState.onResume(this);
Felipe Lemece404982018-09-17 09:46:13 -07001792 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001793 if (mAutoFillResetNeeded) {
1794 if (!mAutoFillIgnoreFirstResumePause) {
1795 View focus = getCurrentFocus();
Ben Line55cd3e2018-12-07 20:26:37 +00001796 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
Dake Gu67decfa2017-12-27 11:48:08 -08001797 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1798 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1799 // window visibility after recreation is INVISIBLE in onResume() and next frame
1800 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1801 // So we cannot call View.notifyEnterOrExited() which will do nothing
1802 // when View.isVisibleToUser() is false.
1803 getAutofillManager().notifyViewEntered(focus);
1804 }
1805 }
1806 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001807
1808 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);
1809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 mCalled = true;
1811 }
1812
1813 /**
1814 * Called when activity resume is complete (after {@link #onResume} has
1815 * been called). Applications will generally not implement this method;
1816 * it is intended for system classes to do final setup after application
1817 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 * <p><em>Derived classes must call through to the super class's
1820 * implementation of this method. If they do not, an exception will be
1821 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 * @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 onPostResume() {
1827 final Window win = getWindow();
1828 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001829 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 mCalled = true;
1831 }
1832
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001833 /**
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07001834 * Called when activity gets or loses the top resumed position in the system.
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001835 *
1836 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} multiple activities can be resumed
1837 * at the same time in multi-window and multi-display modes. This callback should be used
1838 * instead of {@link #onResume()} as an indication that the activity can try to open
1839 * exclusive-access devices like camera.</p>
1840 *
1841 * <p>It will always be delivered after the activity was resumed and before it is paused. In
1842 * some cases it might be skipped and activity can go straight from {@link #onResume()} to
1843 * {@link #onPause()} without receiving the top resumed state.</p>
1844 *
1845 * @param isTopResumedActivity {@code true} if it's the topmost resumed activity in the system,
1846 * {@code false} otherwise. A call with this as {@code true} will
1847 * always be followed by another one with {@code false}.
1848 *
1849 * @see #onResume()
1850 * @see #onPause()
1851 * @see #onWindowFocusChanged(boolean)
1852 */
1853 public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
1854 }
1855
Andrii Kulian86024012019-04-18 19:41:23 -07001856 final void performTopResumedActivityChanged(boolean isTopResumedActivity, String reason) {
1857 onTopResumedActivityChanged(isTopResumedActivity);
1858
1859 writeEventLog(isTopResumedActivity
1860 ? LOG_AM_ON_TOP_RESUMED_GAINED_CALLED : LOG_AM_ON_TOP_RESUMED_LOST_CALLED, reason);
1861 }
1862
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001863 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001864 if (mVoiceInteractor != null) {
Sunny Goyald40c3452019-03-20 12:46:55 -07001865 final Request[] requests = mVoiceInteractor.getActiveRequests();
1866 if (requests != null) {
1867 for (Request activeRequest : mVoiceInteractor.getActiveRequests()) {
1868 activeRequest.cancel();
1869 activeRequest.clear();
1870 }
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001871 }
1872 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001873 if (voiceInteractor == null) {
1874 mVoiceInteractor = null;
1875 } else {
1876 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1877 Looper.myLooper());
1878 }
1879 }
1880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001882 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001883 *
Phil Weaver846cda932017-06-15 10:10:06 -07001884 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001885 * will be unique.
1886 *
1887 * @return A ID that is unique in the activity
1888 *
1889 * {@hide}
1890 */
Felipe Leme42b97932018-02-20 13:04:31 -08001891 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001892 public int getNextAutofillId() {
1893 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1894 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001895 }
1896
Phil Weaver846cda932017-06-15 10:10:06 -07001897 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001898
Phil Weaver846cda932017-06-15 10:10:06 -07001899 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001900 }
1901
1902 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001903 * @hide
1904 */
1905 @Override
1906 public AutofillId autofillClientGetNextAutofillId() {
Felipe Leme305fd402018-09-11 18:20:42 +00001907 return new AutofillId(getNextAutofillId());
Felipe Leme42b97932018-02-20 13:04:31 -08001908 }
1909
1910 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001911 * Check whether this activity is running as part of a voice interaction with the user.
1912 * If true, it should perform its interaction with the user through the
1913 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1914 */
1915 public boolean isVoiceInteraction() {
1916 return mVoiceInteractor != null;
1917 }
1918
1919 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001920 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1921 * of a voice interaction. That is, returns true if this activity was directly
1922 * started by the voice interaction service as the initiation of a voice interaction.
1923 * Otherwise, for example if it was started by another activity while under voice
1924 * interaction, returns false.
1925 */
1926 public boolean isVoiceInteractionRoot() {
1927 try {
1928 return mVoiceInteractor != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001929 && ActivityTaskManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001930 } catch (RemoteException e) {
1931 }
1932 return false;
1933 }
1934
1935 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001936 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1937 * interact with this activity.
1938 */
1939 public VoiceInteractor getVoiceInteractor() {
1940 return mVoiceInteractor;
1941 }
1942
1943 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001944 * Queries whether the currently enabled voice interaction service supports returning
1945 * a voice interactor for use by the activity. This is valid only for the duration of the
1946 * activity.
1947 *
1948 * @return whether the current voice interaction service supports local voice interaction
1949 */
1950 public boolean isLocalVoiceInteractionSupported() {
1951 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001952 return ActivityTaskManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001953 } catch (RemoteException re) {
1954 }
1955 return false;
1956 }
1957
1958 /**
1959 * Starts a local voice interaction session. When ready,
1960 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1961 * to the registered voice interaction service.
1962 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1963 */
1964 public void startLocalVoiceInteraction(Bundle privateOptions) {
1965 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001966 ActivityTaskManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001967 } catch (RemoteException re) {
1968 }
1969 }
1970
1971 /**
1972 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1973 * voice interaction session being started. You can now retrieve a voice interactor using
1974 * {@link #getVoiceInteractor()}.
1975 */
1976 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001977 }
1978
1979 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001980 * Callback to indicate that the local voice interaction has stopped either
1981 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1982 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1983 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001984 */
1985 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001986 }
1987
1988 /**
1989 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001990 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1991 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001992 */
1993 public void stopLocalVoiceInteraction() {
1994 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001995 ActivityTaskManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001996 } catch (RemoteException re) {
1997 }
1998 }
1999
2000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 * This is called for activities that set launchMode to "singleTop" in
2002 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
2003 * flag when calling {@link #startActivity}. In either case, when the
2004 * activity is re-launched while at the top of the activity stack instead
2005 * of a new instance of the activity being started, onNewIntent() will be
2006 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07002007 * re-launch it.
2008 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002009 * <p>An activity can never receive a new intent in the resumed state. You can count on
2010 * {@link #onResume} being called after this method, though not necessarily immediately after
2011 * the completion this callback. If the activity was resumed, it will be paused and new intent
2012 * will be delivered, followed by {@link #onResume}. If the activity wasn't in the resumed
2013 * state, then new intent can be delivered immediately, with {@link #onResume()} called
2014 * sometime later when activity becomes active again.
RoboErik55011652014-07-09 15:05:53 -07002015 *
2016 * <p>Note that {@link #getIntent} still returns the original Intent. You
2017 * can use {@link #setIntent} to update it to this new Intent.
2018 *
2019 * @param intent The new intent that was started for the activity.
2020 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07002022 * @see #setIntent
2023 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 */
2025 protected void onNewIntent(Intent intent) {
2026 }
2027
2028 /**
2029 * The hook for {@link ActivityThread} to save the state of this activity.
2030 *
2031 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2032 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2033 *
2034 * @param outState The bundle to save the state to.
2035 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002036 final void performSaveInstanceState(@NonNull Bundle outState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002037 dispatchActivityPreSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 onSaveInstanceState(outState);
2039 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07002040 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002041 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002042 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002043 dispatchActivityPostSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045
2046 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002047 * The hook for {@link ActivityThread} to save the state of this activity.
2048 *
2049 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2050 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2051 *
2052 * @param outState The bundle to save the state to.
2053 * @param outPersistentState The bundle to save persistent state to.
2054 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002055 final void performSaveInstanceState(@NonNull Bundle outState,
2056 @NonNull PersistableBundle outPersistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002057 dispatchActivityPreSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002058 onSaveInstanceState(outState, outPersistentState);
2059 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002060 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002061 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
2062 ", " + outPersistentState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002063 dispatchActivityPostSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002064 }
2065
2066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 * Called to retrieve per-instance state from an activity before being killed
2068 * so that the state can be restored in {@link #onCreate} or
2069 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
2070 * will be passed to both).
2071 *
2072 * <p>This method is called before an activity may be killed so that when it
2073 * comes back some time in the future it can restore its state. For example,
2074 * if activity B is launched in front of activity A, and at some point activity
2075 * A is killed to reclaim resources, activity A will have a chance to save the
2076 * current state of its user interface via this method so that when the user
2077 * returns to activity A, the state of the user interface can be restored
2078 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
2079 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002080 * <p>Do not confuse this method with activity lifecycle callbacks such as {@link #onPause},
2081 * which is always called when the user no longer actively interacts with an activity, or
2082 * {@link #onStop} which is called when activity becomes invisible. One example of when
2083 * {@link #onPause} and {@link #onStop} is called and not this method is when a user navigates
2084 * back from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
2085 * on B because that particular instance will never be restored,
2086 * so the system avoids calling it. An example when {@link #onPause} is called and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
2088 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
2089 * killed during the lifetime of B since the state of the user interface of
2090 * A will stay intact.
2091 *
2092 * <p>The default implementation takes care of most of the UI per-instance
2093 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
2094 * view in the hierarchy that has an id, and by saving the id of the currently
2095 * focused view (all of which is restored by the default implementation of
2096 * {@link #onRestoreInstanceState}). If you override this method to save additional
2097 * information not captured by each individual view, you will likely want to
2098 * call through to the default implementation, otherwise be prepared to save
2099 * all of the state of each view yourself.
2100 *
Andrii Kulian391161f2018-01-29 10:50:02 -08002101 * <p>If called, this method will occur after {@link #onStop} for applications
2102 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
2103 * For applications targeting earlier platform versions this method will occur
2104 * before {@link #onStop} and there are no guarantees about whether it will
2105 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07002106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07002108 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 * @see #onCreate
2110 * @see #onRestoreInstanceState
2111 * @see #onPause
2112 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002113 protected void onSaveInstanceState(@NonNull Bundle outState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002115
Phil Weaver846cda932017-06-15 10:10:06 -07002116 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002117 Parcelable p = mFragments.saveAllState();
2118 if (p != null) {
2119 outState.putParcelable(FRAGMENTS_TAG, p);
2120 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002121 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002122 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
2123 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002124 }
Ian Lake8a88cd62018-09-28 13:25:20 -07002125 dispatchActivitySaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127
2128 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002129 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07002130 * created with the attribute {@link android.R.attr#persistableMode} set to
2131 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
2132 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
2133 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07002134 *
2135 * @param outState Bundle in which to place your saved state.
2136 * @param outPersistentState State which will be saved across reboots.
2137 *
2138 * @see #onSaveInstanceState(Bundle)
2139 * @see #onCreate
2140 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
2141 * @see #onPause
2142 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002143 public void onSaveInstanceState(@NonNull Bundle outState,
2144 @NonNull PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07002145 onSaveInstanceState(outState);
2146 }
2147
2148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 * Save the state of any managed dialogs.
2150 *
2151 * @param outState place to store the saved state.
2152 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002153 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 private void saveManagedDialogs(Bundle outState) {
2155 if (mManagedDialogs == null) {
2156 return;
2157 }
2158
2159 final int numDialogs = mManagedDialogs.size();
2160 if (numDialogs == 0) {
2161 return;
2162 }
2163
2164 Bundle dialogState = new Bundle();
2165
2166 int[] ids = new int[mManagedDialogs.size()];
2167
2168 // save each dialog's bundle, gather the ids
2169 for (int i = 0; i < numDialogs; i++) {
2170 final int key = mManagedDialogs.keyAt(i);
2171 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002172 final ManagedDialog md = mManagedDialogs.valueAt(i);
2173 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
2174 if (md.mArgs != null) {
2175 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
2176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178
2179 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
2180 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
2181 }
2182
2183
2184 /**
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002185 * Called as part of the activity lifecycle when the user no longer actively interacts with the
2186 * activity, but it is still visible on screen. The counterpart to {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 *
2188 * <p>When activity B is launched in front of activity A, this callback will
2189 * be invoked on A. B will not be created until A's {@link #onPause} returns,
2190 * so be sure to not do anything lengthy here.
2191 *
2192 * <p>This callback is mostly used for saving any persistent state the
2193 * activity is editing, to present a "edit in place" model to the user and
2194 * making sure nothing is lost if there are not enough resources to start
2195 * the new activity without first killing this one. This is also a good
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002196 * place to stop things that consume a noticeable amount of CPU in order to
2197 * make the switch to the next activity as fast as possible.
RoboErik55011652014-07-09 15:05:53 -07002198 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002199 * <p>On platform versions prior to {@link android.os.Build.VERSION_CODES#Q} this is also a good
2200 * place to try to close exclusive-access devices or to release access to singleton resources.
2201 * Starting with {@link android.os.Build.VERSION_CODES#Q} there can be multiple resumed
2202 * activities in the system at the same time, so {@link #onTopResumedActivityChanged(boolean)}
2203 * should be used for that purpose instead.
RoboErik55011652014-07-09 15:05:53 -07002204 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002205 * <p>If an activity is launched on top, after receiving this call you will usually receive a
2206 * following call to {@link #onStop} (after the next activity has been resumed and displayed
2207 * above). However in some cases there will be a direct call back to {@link #onResume} without
2208 * going through the stopped state. An activity can also rest in paused state in some cases when
2209 * in multi-window mode, still visible to user.
RoboErik55011652014-07-09 15:05:53 -07002210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 * <p><em>Derived classes must call through to the super class's
2212 * implementation of this method. If they do not, an exception will be
2213 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002214 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 * @see #onResume
2216 * @see #onSaveInstanceState
2217 * @see #onStop
2218 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002219 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002221 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07002222 dispatchActivityPaused();
Dake Gu67decfa2017-12-27 11:48:08 -08002223 if (mAutoFillResetNeeded) {
2224 if (!mAutoFillIgnoreFirstResumePause) {
2225 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
2226 View focus = getCurrentFocus();
2227 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
2228 getAutofillManager().notifyViewExited(focus);
2229 }
2230 } else {
2231 // reset after first pause()
2232 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
2233 mAutoFillIgnoreFirstResumePause = false;
2234 }
2235 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002236
2237 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_PAUSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 mCalled = true;
2239 }
2240
2241 /**
2242 * Called as part of the activity lifecycle when an activity is about to go
2243 * into the background as the result of user choice. For example, when the
2244 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
2245 * when an incoming phone call causes the in-call Activity to be automatically
2246 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
2247 * the activity being interrupted. In cases when it is invoked, this method
2248 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07002249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 * <p>This callback and {@link #onUserInteraction} are intended to help
2251 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002252 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07002253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 * @see #onUserInteraction()
Louis Changc2e193a2019-01-09 15:33:57 +08002255 * @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 */
2257 protected void onUserLeaveHint() {
2258 }
RoboErik55011652014-07-09 15:05:53 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002261 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002263 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002265 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 }
2267
2268 /**
2269 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002270 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07002272 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 * <p>The default implementation returns null, which will cause you to
2274 * inherit the description from the previous activity. If all activities
2275 * return null, generally the label of the top activity will be used as the
2276 * description.
RoboErik55011652014-07-09 15:05:53 -07002277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 * @return A description of what the user is doing. It should be short and
2279 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07002280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002282 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002284 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 public CharSequence onCreateDescription() {
2286 return null;
2287 }
2288
2289 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002290 * This is called when the user is requesting an assist, to build a full
2291 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
2292 * application. You can override this method to place into the bundle anything
2293 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002294 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002295 *
2296 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07002297 * been registered with {@link Application#registerOnProvideAssistDataListener
2298 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002299 */
2300 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002301 }
2302
2303 /**
2304 * This is called when the user is requesting an assist, to provide references
2305 * to content related to the current activity. Before being called, the
2306 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
2307 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
2308 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
2309 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
2310 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
2311 *
2312 * <p>Custom implementation may adjust the content intent to better reflect the top-level
2313 * context of the activity, and fill in its ClipData with additional content of
2314 * interest that the user is currently viewing. For example, an image gallery application
2315 * that has launched in to an activity allowing the user to swipe through pictures should
2316 * modify the intent to reference the current image they are looking it; such an
2317 * application when showing a list of pictures should add a ClipData that has
2318 * references to all of the pictures currently visible on screen.</p>
2319 *
2320 * @param outContent The assist content to return.
2321 */
2322 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002323 }
2324
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002325 /**
Sunny Goyald40c3452019-03-20 12:46:55 -07002326 * Returns the list of direct actions supported by the app.
2327 *
2328 * <p>You should return the list of actions that could be executed in the
2329 * current context, which is in the current state of the app. If the actions
2330 * that could be executed by the app changes you should report that via
2331 * calling {@link VoiceInteractor#notifyDirectActionsChanged()}.
2332 *
2333 * <p>To get the voice interactor you need to call {@link #getVoiceInteractor()}
Andrew Solovay7ad21702019-06-06 15:10:43 -07002334 * which would return non <code>null</code> only if there is an ongoing voice
Sunny Goyald40c3452019-03-20 12:46:55 -07002335 * interaction session. You an also detect when the voice interactor is no
2336 * longer valid because the voice interaction session that is backing is finished
2337 * by calling {@link VoiceInteractor#registerOnDestroyedCallback(Executor, Runnable)}.
2338 *
2339 * <p>This method will be called only after {@link #onStart()} is being called and
2340 * before {@link #onStop()} is being called.
2341 *
Svet Ganov3b6be082019-04-28 10:21:01 -07002342 * <p>You should pass to the callback the currently supported direct actions which
Andrew Solovay7ad21702019-06-06 15:10:43 -07002343 * cannot be <code>null</code> or contain <code>null</code> elements.
Svet Ganov3b6be082019-04-28 10:21:01 -07002344 *
2345 * <p>You should return the action list as soon as possible to ensure the consumer,
2346 * for example the assistant, is as responsive as possible which would improve user
2347 * experience of your app.
2348 *
2349 * @param cancellationSignal A signal to cancel the operation in progress.
2350 * @param callback The callback to send the action list. The actions list cannot
Svet Ganov07e5fb22019-04-30 11:07:09 -07002351 * contain <code>null</code> elements. You can call this on any thread.
Sunny Goyald40c3452019-03-20 12:46:55 -07002352 */
Svet Ganov3b6be082019-04-28 10:21:01 -07002353 public void onGetDirectActions(@NonNull CancellationSignal cancellationSignal,
2354 @NonNull Consumer<List<DirectAction>> callback) {
2355 callback.accept(Collections.emptyList());
Sunny Goyald40c3452019-03-20 12:46:55 -07002356 }
2357
2358 /**
2359 * This is called to perform an action previously defined by the app.
2360 * Apps also have access to {@link #getVoiceInteractor()} to follow up on the action.
2361 *
Svet Ganov07e5fb22019-04-30 11:07:09 -07002362 * @param actionId The ID for the action you previously reported via
2363 * {@link #onGetDirectActions(CancellationSignal, Consumer)}.
2364 * @param arguments Any additional arguments provided by the caller that are
2365 * specific to the given action.
Svet Ganov3b6be082019-04-28 10:21:01 -07002366 * @param cancellationSignal A signal to cancel the operation in progress.
Svet Ganov07e5fb22019-04-30 11:07:09 -07002367 * @param resultListener The callback to provide the result back to the caller.
2368 * You can call this on any thread. The result bundle is action specific.
Sunny Goyald40c3452019-03-20 12:46:55 -07002369 *
Svet Ganov3b6be082019-04-28 10:21:01 -07002370 * @see #onGetDirectActions(CancellationSignal, Consumer)
Sunny Goyald40c3452019-03-20 12:46:55 -07002371 */
2372 public void onPerformDirectAction(@NonNull String actionId,
Svet Ganov3b6be082019-04-28 10:21:01 -07002373 @NonNull Bundle arguments, @NonNull CancellationSignal cancellationSignal,
Sunny Goyald40c3452019-03-20 12:46:55 -07002374 @NonNull Consumer<Bundle> resultListener) { }
2375
2376 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01002377 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002378 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
2379 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01002380 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002381 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002382 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002383 sendBroadcastAsUser(intent, Process.myUserHandle());
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002384 }
2385
2386 /**
2387 * Dismiss the Keyboard Shortcuts screen.
2388 */
2389 public final void dismissKeyboardShortcutsHelper() {
2390 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002391 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002392 sendBroadcastAsUser(intent, Process.myUserHandle());
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002393 }
2394
Clara Bayarri75e09792015-07-29 16:20:40 +01002395 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00002396 public void onProvideKeyboardShortcuts(
2397 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01002398 if (menu == null) {
2399 return;
2400 }
2401 KeyboardShortcutGroup group = null;
2402 int menuSize = menu.size();
2403 for (int i = 0; i < menuSize; ++i) {
2404 final MenuItem item = menu.getItem(i);
2405 final CharSequence title = item.getTitle();
2406 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01002407 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01002408 if (title != null && alphaShortcut != MIN_VALUE) {
2409 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00002410 final int resource = mApplication.getApplicationInfo().labelRes;
2411 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01002412 }
2413 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01002414 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01002415 }
2416 }
2417 if (group != null) {
2418 data.add(group);
2419 }
2420 }
2421
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002422 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07002423 * Ask to have the current assistant shown to the user. This only works if the calling
2424 * activity is the current foreground activity. It is the same as calling
2425 * {@link android.service.voice.VoiceInteractionService#showSession
2426 * VoiceInteractionService.showSession} and requesting all of the possible context.
2427 * The receiver will always see
2428 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
2429 * @return Returns true if the assistant was successfully invoked, else false. For example
2430 * false will be returned if the caller is not the current top activity.
2431 */
2432 public boolean showAssist(Bundle args) {
2433 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002434 return ActivityTaskManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07002435 } catch (RemoteException e) {
2436 }
2437 return false;
2438 }
2439
2440 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 * Called when you are no longer visible to the user. You will next
2442 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07002443 * depending on later user activity. This is a good place to stop
2444 * refreshing UI, running animations and other visual things.
RoboErik55011652014-07-09 15:05:53 -07002445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 * <p><em>Derived classes must call through to the super class's
2447 * implementation of this method. If they do not, an exception will be
2448 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002449 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 * @see #onRestart
2451 * @see #onResume
2452 * @see #onSaveInstanceState
2453 * @see #onDestroy
2454 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002455 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002457 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08002458 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07002459 mActivityTransitionState.onStop();
Ian Lake8a88cd62018-09-28 13:25:20 -07002460 dispatchActivityStopped();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002461 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07002463
Dake Gu67decfa2017-12-27 11:48:08 -08002464 if (mAutoFillResetNeeded) {
2465 getAutofillManager().onInvisibleForAutofill();
2466 }
2467
Felipe Lemec24a56a2017-08-03 14:27:57 -07002468 if (isFinishing()) {
2469 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08002470 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07002471 } else if (mIntent != null
2472 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
2473 // Activity was launched when user tapped a link in the Autofill Save UI - since
2474 // user launched another activity, the Save UI should not be restored when this
2475 // activity is finished.
2476 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
2477 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
2478 }
Felipe Lemebab851c2017-02-03 18:45:08 -08002479 }
chaviwfeb2e1e2018-12-19 17:24:11 -08002480 mEnterAnimationComplete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 }
2482
2483 /**
2484 * Perform any final cleanup before an activity is destroyed. This can
2485 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07002486 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 * this instance of the activity to save space. You can distinguish
2488 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07002489 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 * <p><em>Note: do not count on this method being called as a place for
2491 * saving data! For example, if an activity is editing data in a content
2492 * provider, those edits should be committed in either {@link #onPause} or
2493 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
2494 * free resources like threads that are associated with an activity, so
2495 * that a destroyed activity does not leave such things around while the
2496 * rest of its application is still running. There are situations where
2497 * the system will simply kill the activity's hosting process without
2498 * calling this method (or any others) in it, so it should not be used to
2499 * do things that are intended to remain around after the process goes
2500 * away.
RoboErik55011652014-07-09 15:05:53 -07002501 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 * <p><em>Derived classes must call through to the super class's
2503 * implementation of this method. If they do not, an exception will be
2504 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002505 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 * @see #onPause
2507 * @see #onStop
2508 * @see #finish
2509 * @see #isFinishing
2510 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002511 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002513 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 mCalled = true;
2515
2516 // dismiss any dialogs we are managing.
2517 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 final int numDialogs = mManagedDialogs.size();
2519 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002520 final ManagedDialog md = mManagedDialogs.valueAt(i);
2521 if (md.mDialog.isShowing()) {
2522 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 }
2524 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002525 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
2528 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002529 synchronized (mManagedCursors) {
2530 int numCursors = mManagedCursors.size();
2531 for (int i = 0; i < numCursors; i++) {
2532 ManagedCursor c = mManagedCursors.get(i);
2533 if (c != null) {
2534 c.mCursor.close();
2535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002537 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
Amith Yamasani49860442010-03-17 20:54:10 -07002539
2540 // Close any open search dialog
2541 if (mSearchManager != null) {
2542 mSearchManager.stopSearch();
2543 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002544
Chris Banes21b25772016-01-04 20:41:59 +00002545 if (mActionBar != null) {
2546 mActionBar.onDestroy();
2547 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002548
Ian Lake8a88cd62018-09-28 13:25:20 -07002549 dispatchActivityDestroyed();
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002550
2551 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_STOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
2553
2554 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002555 * Report to the system that your app is now fully drawn, purely for diagnostic
2556 * purposes (calling it does not impact the visible behavior of the activity).
2557 * This is only used to help instrument application launch times, so that the
2558 * app can report when it is fully in a usable state; without this, the only thing
2559 * the system itself can determine is the point at which the activity's window
2560 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002561 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002562 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002563 * entirely drawn your UI and populated with all of the significant data. You
2564 * can safely call this method any time after first launch as well, in which case
2565 * it will simply be ignored.
2566 */
2567 public void reportFullyDrawn() {
2568 if (mDoReportFullyDrawn) {
2569 mDoReportFullyDrawn = false;
2570 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002571 ActivityTaskManager.getService().reportActivityFullyDrawn(
2572 mToken, mRestoredFromBundle);
Mathieu Chartier63e14c12019-03-07 09:05:44 -08002573 VMRuntime.getRuntime().notifyStartupCompleted();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002574 } catch (RemoteException e) {
2575 }
2576 }
2577 }
2578
2579 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002580 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002581 * visa-versa. This method provides the same configuration that will be sent in the following
2582 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2583 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002584 * @see android.R.attr#resizeableActivity
2585 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002586 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002587 * @param newConfig The new configuration of the activity with the state
2588 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002589 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002590 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2591 // Left deliberately empty. There should be no side effects if a direct
2592 // subclass of Activity does not call super.
2593 onMultiWindowModeChanged(isInMultiWindowMode);
2594 }
2595
2596 /**
2597 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2598 * visa-versa.
2599 *
2600 * @see android.R.attr#resizeableActivity
2601 *
2602 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2603 *
2604 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2605 */
2606 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002607 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002608 // Left deliberately empty. There should be no side effects if a direct
2609 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002610 }
2611
2612 /**
2613 * Returns true if the activity is currently in multi-window mode.
2614 * @see android.R.attr#resizeableActivity
2615 *
2616 * @return True if the activity is in multi-window mode.
2617 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002618 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002619 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002620 return ActivityTaskManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002621 } catch (RemoteException e) {
2622 }
2623 return false;
2624 }
2625
2626 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002627 * Called by the system when the activity changes to and from picture-in-picture mode. This
2628 * method provides the same configuration that will be sent in the following
2629 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2630 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002631 * @see android.R.attr#supportsPictureInPicture
2632 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002633 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002634 * @param newConfig The new configuration of the activity with the state
2635 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002636 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002637 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2638 Configuration newConfig) {
2639 // Left deliberately empty. There should be no side effects if a direct
2640 // subclass of Activity does not call super.
2641 onPictureInPictureModeChanged(isInPictureInPictureMode);
2642 }
2643
2644 /**
2645 * Called by the system when the activity changes to and from picture-in-picture mode.
2646 *
2647 * @see android.R.attr#supportsPictureInPicture
2648 *
2649 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2650 *
2651 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2652 */
2653 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002654 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002655 // Left deliberately empty. There should be no side effects if a direct
2656 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002657 }
2658
2659 /**
2660 * Returns true if the activity is currently in picture-in-picture mode.
2661 * @see android.R.attr#supportsPictureInPicture
2662 *
2663 * @return True if the activity is in picture-in-picture mode.
2664 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002665 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002666 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002667 return ActivityTaskManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002668 } catch (RemoteException e) {
2669 }
2670 return false;
2671 }
2672
2673 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002674 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002675 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2676 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002677 *
Winson Chung709904f2017-04-25 11:00:48 -07002678 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002679 * @see android.R.attr#supportsPictureInPicture
2680 */
Winson Chung709904f2017-04-25 11:00:48 -07002681 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002682 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002683 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002684 }
2685
Jeff Sharkey000ce802017-04-29 13:13:27 -06002686 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002687 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002688 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002689 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002690 }
2691
2692 /**
Winson Chung709904f2017-04-25 11:00:48 -07002693 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2694 * set parameters in {@param params} will be combined with the parameters from prior calls to
2695 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2696 *
2697 * The system may disallow entering picture-in-picture in various cases, including when the
2698 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2699 *
2700 * @see android.R.attr#supportsPictureInPicture
2701 * @see PictureInPictureParams
2702 *
2703 * @param params non-null parameters to be combined with previously set parameters when entering
2704 * picture-in-picture.
2705 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002706 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002707 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002708 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002709 */
2710 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2711 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002712 if (!deviceSupportsPictureInPictureMode()) {
2713 return false;
2714 }
Winson Chung709904f2017-04-25 11:00:48 -07002715 if (params == null) {
2716 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2717 }
Winson Chung298f95b2017-08-10 15:57:18 -07002718 if (!mCanEnterPictureInPicture) {
2719 throw new IllegalStateException("Activity must be resumed to enter"
2720 + " picture-in-picture");
2721 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002722 return ActivityTaskManager.getService().enterPictureInPictureMode(mToken, params);
Winson Chung709904f2017-04-25 11:00:48 -07002723 } catch (RemoteException e) {
2724 return false;
2725 }
2726 }
2727
Jeff Sharkey000ce802017-04-29 13:13:27 -06002728 /** @removed */
2729 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002730 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002731 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002732 }
2733
2734 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002735 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2736 * {@link #enterPictureInPictureMode()} is called.
2737 *
Winson Chung709904f2017-04-25 11:00:48 -07002738 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002739 */
Winson Chung709904f2017-04-25 11:00:48 -07002740 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002741 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002742 if (!deviceSupportsPictureInPictureMode()) {
2743 return;
2744 }
Winson Chung709904f2017-04-25 11:00:48 -07002745 if (params == null) {
2746 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002747 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002748 ActivityTaskManager.getService().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002749 } catch (RemoteException e) {
2750 }
2751 }
2752
Winson Chung8802eac2017-04-17 14:21:44 -07002753 /**
Winson Chung709904f2017-04-25 11:00:48 -07002754 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2755 * user interacts with the activity currently in picture-in-picture mode. This number may change
2756 * if the global configuration changes (ie. if the device is plugged into an external display),
2757 * but will always be larger than three.
2758 */
2759 public int getMaxNumPictureInPictureActions() {
2760 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002761 return ActivityTaskManager.getService().getMaxNumPictureInPictureActions(mToken);
Winson Chung709904f2017-04-25 11:00:48 -07002762 } catch (RemoteException e) {
2763 return 0;
2764 }
2765 }
2766
Winson Chung80fa2d82018-01-25 13:58:20 -08002767 /**
2768 * @return Whether this device supports picture-in-picture.
2769 */
2770 private boolean deviceSupportsPictureInPictureMode() {
2771 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2772 }
2773
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002774 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002775 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002776 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002777 }
2778
2779 /**
2780 * Called by the system when the activity is moved from one display to another without
2781 * recreation. This means that this activity is declared to handle all changes to configuration
2782 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002783 * again.
2784 *
2785 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2786 * applied configuration actually changed. It is up to app developer to choose whether to handle
2787 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2788 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002789 *
2790 * <p>Use this callback to track changes to the displays if some activity functionality relies
2791 * on an association with some display properties.
2792 *
2793 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002794 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002795 *
2796 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002797 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002798 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002799 */
Tiger Huangfc218452018-09-27 00:38:50 +08002800 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002801 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002802 }
2803
Winson Chungb5c41b72016-12-07 15:00:47 -08002804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 * Called by the system when the device configuration changes while your
2806 * activity is running. Note that this will <em>only</em> be called if
2807 * you have selected configurations you would like to handle with the
2808 * {@link android.R.attr#configChanges} attribute in your manifest. If
2809 * any configuration change occurs that is not selected to be reported
2810 * by that attribute, then instead of reporting it the system will stop
2811 * and restart the activity (to have it launched with the new
2812 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 * <p>At the time that this function has been called, your Resources
2815 * object will have been updated to return resource values matching the
2816 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 * @param newConfig The new device configuration.
2819 */
Ian Lake666a9652018-09-12 15:13:19 -07002820 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002821 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002823
Dianne Hackborn9d071802010-12-08 14:49:15 -08002824 mFragments.dispatchConfigurationChanged(newConfig);
2825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 if (mWindow != null) {
2827 // Pass the configuration changed event to the window
2828 mWindow.onConfigurationChanged(newConfig);
2829 }
Adam Powell45c0b192011-07-28 15:11:57 -07002830
2831 if (mActionBar != null) {
2832 // Do this last; the action bar will need to access
2833 // view changes from above.
2834 mActionBar.onConfigurationChanged(newConfig);
2835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 }
RoboErik55011652014-07-09 15:05:53 -07002837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 /**
2839 * If this activity is being destroyed because it can not handle a
2840 * configuration parameter being changed (and thus its
2841 * {@link #onConfigurationChanged(Configuration)} method is
2842 * <em>not</em> being called), then you can use this method to discover
2843 * the set of changes that have occurred while in the process of being
2844 * destroyed. Note that there is no guarantee that these will be
2845 * accurate (other changes could have happened at any time), so you should
2846 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 * @return Returns a bit field of the configuration parameters that are
2849 * changing, as defined by the {@link android.content.res.Configuration}
2850 * class.
2851 */
2852 public int getChangingConfigurations() {
2853 return mConfigChangeFlags;
2854 }
RoboErik55011652014-07-09 15:05:53 -07002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 /**
2857 * Retrieve the non-configuration instance data that was previously
2858 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2859 * be available from the initial {@link #onCreate} and
2860 * {@link #onStart} calls to the new instance, allowing you to extract
2861 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002862 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 * <p>Note that the data you retrieve here should <em>only</em> be used
2864 * as an optimization for handling configuration changes. You should always
2865 * be able to handle getting a null pointer back, and an activity must
2866 * still be able to restore itself to its previous state (through the
2867 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2868 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002869 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002870 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002871 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002872 * available on older platforms through the Android support libraries.
2873 *
2874 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002876 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002878 return mLastNonConfigurationInstances != null
2879 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 }
RoboErik55011652014-07-09 15:05:53 -07002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 /**
2883 * Called by the system, as part of destroying an
2884 * activity due to a configuration change, when it is known that a new
2885 * instance will immediately be created for the new configuration. You
2886 * can return any object you like here, including the activity instance
2887 * itself, which can later be retrieved by calling
2888 * {@link #getLastNonConfigurationInstance()} in the new activity
2889 * instance.
RoboErik55011652014-07-09 15:05:53 -07002890 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002891 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2892 * or later, consider instead using a {@link Fragment} with
2893 * {@link Fragment#setRetainInstance(boolean)
2894 * Fragment.setRetainInstance(boolean}.</em>
2895 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 * <p>This function is called purely as an optimization, and you must
2897 * not rely on it being called. When it is called, a number of guarantees
2898 * will be made to help optimize configuration switching:
2899 * <ul>
2900 * <li> The function will be called between {@link #onStop} and
2901 * {@link #onDestroy}.
2902 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002903 * created after this one's {@link #onDestroy()} is called. In particular,
2904 * <em>no</em> messages will be dispatched during this time (when the returned
2905 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 * <li> The object you return here will <em>always</em> be available from
2907 * the {@link #getLastNonConfigurationInstance()} method of the following
2908 * activity instance as described there.
2909 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002910 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 * <p>These guarantees are designed so that an activity can use this API
2912 * to propagate extensive state from the old to new activity instance, from
2913 * loaded bitmaps, to network connections, to evenly actively running
2914 * threads. Note that you should <em>not</em> propagate any data that
2915 * may change based on the configuration, including any data loaded from
2916 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002917 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002918 * <p>The guarantee of no message handling during the switch to the next
2919 * activity simplifies use with active objects. For example if your retained
2920 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2921 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2922 * not be called from the call here until you execute the next instance's
2923 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2924 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2925 * running in a separate thread.)
2926 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002927 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002928 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002929 * available on older platforms through the Android support libraries.
2930 *
2931 * @return any Object holding the desired state to propagate to the
2932 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 */
2934 public Object onRetainNonConfigurationInstance() {
2935 return null;
2936 }
RoboErik55011652014-07-09 15:05:53 -07002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 /**
2939 * Retrieve the non-configuration instance data that was previously
2940 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2941 * be available from the initial {@link #onCreate} and
2942 * {@link #onStart} calls to the new instance, allowing you to extract
2943 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 * <p>Note that the data you retrieve here should <em>only</em> be used
2946 * as an optimization for handling configuration changes. You should always
2947 * be able to handle getting a null pointer back, and an activity must
2948 * still be able to restore itself to its previous state (through the
2949 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2950 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002951 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 * @return Returns the object previously returned by
2953 * {@link #onRetainNonConfigurationChildInstances()}
2954 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002955 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002956 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2957 return mLastNonConfigurationInstances != null
2958 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 }
RoboErik55011652014-07-09 15:05:53 -07002960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 /**
2962 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2963 * it should return either a mapping from child activity id strings to arbitrary objects,
2964 * or null. This method is intended to be used by Activity framework subclasses that control a
2965 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2966 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2967 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002968 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2970 return null;
2971 }
RoboErik55011652014-07-09 15:05:53 -07002972
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002973 NonConfigurationInstances retainNonConfigurationInstances() {
2974 Object activity = onRetainNonConfigurationInstance();
2975 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002976 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002977
2978 // We're already stopped but we've been asked to retain.
2979 // Our fragments are taken care of but we need to mark the loaders for retention.
2980 // In order to do this correctly we need to restart the loaders first before
2981 // handing them off to the next activity.
2982 mFragments.doLoaderStart();
2983 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002984 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002985
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002986 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002987 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002988 return null;
2989 }
RoboErik55011652014-07-09 15:05:53 -07002990
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002991 NonConfigurationInstances nci = new NonConfigurationInstances();
2992 nci.activity = activity;
2993 nci.children = children;
2994 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002995 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002996 if (mVoiceInteractor != null) {
2997 mVoiceInteractor.retainInstance();
2998 nci.voiceInteractor = mVoiceInteractor;
2999 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003000 return nci;
3001 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003004 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08003006 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003008
3009 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003010 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003011 mCalled = true;
3012 mFragments.dispatchTrimMemory(level);
3013 }
3014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003016 * Return the FragmentManager for interacting with fragments associated
3017 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08003018 *
3019 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003020 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003021 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07003022 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07003023 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07003024 }
RoboErik55011652014-07-09 15:05:53 -07003025
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003026 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07003027 * Called when a Fragment is being attached to this activity, immediately
3028 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
3029 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08003030 *
3031 * @deprecated Use {@link
3032 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07003033 */
Ian Lake0a1feb82017-11-13 10:26:46 -08003034 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07003035 public void onAttachFragment(Fragment fragment) {
3036 }
RoboErik55011652014-07-09 15:05:53 -07003037
Dianne Hackbornc8017682010-07-06 13:34:38 -07003038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 * Wrapper around
3040 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3041 * that gives the resulting {@link Cursor} to call
3042 * {@link #startManagingCursor} so that the activity will manage its
3043 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003044 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003045 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3046 * or later, consider instead using {@link LoaderManager} instead, available
3047 * via {@link #getLoaderManager()}.</em>
3048 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003049 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3050 * this method, because the activity will do that for you at the appropriate time. However, if
3051 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3052 * not</em> automatically close the cursor and, in that case, you must call
3053 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003054 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 * @param uri The URI of the content provider to query.
3056 * @param projection List of columns to return.
3057 * @param selection SQL WHERE clause.
3058 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003059 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003061 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3063 * @see #startManagingCursor
3064 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05003065 *
3066 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 */
Jason parks6ed50de2010-08-25 10:18:50 -05003068 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003069 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07003070 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3071 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
3073 if (c != null) {
3074 startManagingCursor(c);
3075 }
3076 return c;
3077 }
3078
3079 /**
3080 * Wrapper around
3081 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3082 * that gives the resulting {@link Cursor} to call
3083 * {@link #startManagingCursor} so that the activity will manage its
3084 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003085 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003086 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3087 * or later, consider instead using {@link LoaderManager} instead, available
3088 * via {@link #getLoaderManager()}.</em>
3089 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003090 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3091 * this method, because the activity will do that for you at the appropriate time. However, if
3092 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3093 * not</em> automatically close the cursor and, in that case, you must call
3094 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003095 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 * @param uri The URI of the content provider to query.
3097 * @param projection List of columns to return.
3098 * @param selection SQL WHERE clause.
3099 * @param selectionArgs The arguments to selection, if any ?s are pesent
3100 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3105 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05003106 *
3107 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 */
Jason parks6ed50de2010-08-25 10:18:50 -05003109 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07003110 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3111 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
3113 if (c != null) {
3114 startManagingCursor(c);
3115 }
3116 return c;
3117 }
3118
3119 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 * This method allows the activity to take care of managing the given
3121 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
3122 * That is, when the activity is stopped it will automatically call
3123 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
3124 * it will call {@link Cursor#requery} for you. When the activity is
3125 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07003126 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003127 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3128 * or later, consider instead using {@link LoaderManager} instead, available
3129 * via {@link #getLoaderManager()}.</em>
3130 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003131 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
3132 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
3133 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
3134 * <em>will not</em> automatically close the cursor and, in that case, you must call
3135 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003136 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07003138 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
3140 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003141 *
3142 * @deprecated Use the new {@link android.content.CursorLoader} class with
3143 * {@link LoaderManager} instead; this is also
3144 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 */
Jason parks6ed50de2010-08-25 10:18:50 -05003146 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 public void startManagingCursor(Cursor c) {
3148 synchronized (mManagedCursors) {
3149 mManagedCursors.add(new ManagedCursor(c));
3150 }
3151 }
3152
3153 /**
3154 * Given a Cursor that was previously given to
3155 * {@link #startManagingCursor}, stop the activity's management of that
3156 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003157 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003158 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003159 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003160 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003165 *
3166 * @deprecated Use the new {@link android.content.CursorLoader} class with
3167 * {@link LoaderManager} instead; this is also
3168 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 */
Jason parks6ed50de2010-08-25 10:18:50 -05003170 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 public void stopManagingCursor(Cursor c) {
3172 synchronized (mManagedCursors) {
3173 final int N = mManagedCursors.size();
3174 for (int i=0; i<N; i++) {
3175 ManagedCursor mc = mManagedCursors.get(i);
3176 if (mc.mCursor == c) {
3177 mManagedCursors.remove(i);
3178 break;
3179 }
3180 }
3181 }
3182 }
3183
3184 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003185 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3186 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003187 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003189 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003190 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
3193
3194 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003195 * Finds a view that was identified by the {@code android:id} XML attribute
3196 * that was processed in {@link #onCreate}.
3197 * <p>
3198 * <strong>Note:</strong> In most cases -- depending on compiler support --
3199 * the resulting view is automatically cast to the target class type. If
3200 * the target class type is unconstrained, an explicit cast may be
3201 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003203 * @param id the ID to search for
3204 * @return a view with given ID if found, or {@code null} otherwise
3205 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003206 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003208 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003209 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 return getWindow().findViewById(id);
3211 }
RoboErik55011652014-07-09 15:05:53 -07003212
Adam Powell33b97432010-04-20 10:01:14 -07003213 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003214 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3215 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3216 * no matching view in the hierarchy.
3217 * <p>
3218 * <strong>Note:</strong> In most cases -- depending on compiler support --
3219 * the resulting view is automatically cast to the target class type. If
3220 * the target class type is unconstrained, an explicit cast may be
3221 * necessary.
3222 *
3223 * @param id the ID to search for
3224 * @return a view with given ID
3225 * @see View#requireViewById(int)
3226 * @see Activity#findViewById(int)
3227 */
3228 @NonNull
3229 public final <T extends View> T requireViewById(@IdRes int id) {
3230 T view = findViewById(id);
3231 if (view == null) {
3232 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3233 }
3234 return view;
3235 }
3236
3237 /**
Adam Powell33b97432010-04-20 10:01:14 -07003238 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003239 *
Adam Powell33b97432010-04-20 10:01:14 -07003240 * @return The Activity's ActionBar, or null if it does not have one.
3241 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003242 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003243 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003244 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003245 return mActionBar;
3246 }
Adam Powelle43340c2014-03-17 19:10:43 -07003247
3248 /**
3249 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3250 * Activity window.
3251 *
3252 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3253 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3254 * a traditional window decor action bar. The toolbar's menu will be populated with the
3255 * Activity's options menu and the navigation button will be wired through the standard
3256 * {@link android.R.id#home home} menu select action.</p>
3257 *
3258 * <p>In order to use a Toolbar within the Activity's window content the application
3259 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3260 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003261 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003262 */
Adam Powell37780142014-06-01 13:31:00 -07003263 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003264 final ActionBar ab = getActionBar();
3265 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003266 throw new IllegalStateException("This Activity already has an action bar supplied " +
3267 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3268 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3269 }
Chris Banes21b25772016-01-04 20:41:59 +00003270
3271 // If we reach here then we're setting a new action bar
3272 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003273 mMenuInflater = null;
3274
Chris Banes21b25772016-01-04 20:41:59 +00003275 // If we have an action bar currently, destroy it
3276 if (ab != null) {
3277 ab.onDestroy();
3278 }
3279
Chris Banesc7d6c322016-01-27 14:09:16 +00003280 if (toolbar != null) {
3281 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3282 mActionBar = tbab;
3283 mWindow.setCallback(tbab.getWrappedWindowCallback());
3284 } else {
3285 mActionBar = null;
3286 // Re-set the original window callback since we may have already set a Toolbar wrapper
3287 mWindow.setCallback(this);
3288 }
3289
3290 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003291 }
RoboErik55011652014-07-09 15:05:53 -07003292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 /**
Adam Powell33b97432010-04-20 10:01:14 -07003294 * Creates a new ActionBar, locates the inflated ActionBarView,
3295 * initializes the ActionBar with the view, and sets mActionBar.
3296 */
Adam Powelle43340c2014-03-17 19:10:43 -07003297 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003298 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003299
3300 // Initializing the window decor can change window feature flags.
3301 // Make sure that we have the correct set before performing the test below.
3302 window.getDecorView();
3303
Adam Powell9b4c8042010-08-10 15:36:44 -07003304 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003305 return;
3306 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003307
Adam Powelle43340c2014-03-17 19:10:43 -07003308 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003309 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003310
3311 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3312 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003313 }
RoboErik55011652014-07-09 15:05:53 -07003314
Adam Powell33b97432010-04-20 10:01:14 -07003315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 * Set the activity content from a layout resource. The resource will be
3317 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003318 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003320 *
Romain Guy482b34a62011-01-20 10:59:28 -08003321 * @see #setContentView(android.view.View)
3322 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003324 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003326 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 }
3328
3329 /**
3330 * Set the activity content to an explicit view. This view is placed
3331 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003332 * view hierarchy. When calling this method, the layout parameters of the
3333 * specified view are ignored. Both the width and the height of the view are
3334 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3335 * your own layout parameters, invoke
3336 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3337 * instead.
RoboErik55011652014-07-09 15:05:53 -07003338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003340 *
3341 * @see #setContentView(int)
3342 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 */
3344 public void setContentView(View view) {
3345 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003346 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 }
3348
3349 /**
3350 * Set the activity content to an explicit view. This view is placed
3351 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003352 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 * @param view The desired content to display.
3355 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003356 *
3357 * @see #setContentView(android.view.View)
3358 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 */
3360 public void setContentView(View view, ViewGroup.LayoutParams params) {
3361 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003362 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 }
3364
3365 /**
3366 * Add an additional content view to the activity. Added after any existing
3367 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 * @param view The desired content to display.
3370 * @param params Layout parameters for the view.
3371 */
3372 public void addContentView(View view, ViewGroup.LayoutParams params) {
3373 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003374 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 }
3376
3377 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003378 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3379 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3380 *
3381 * <p>This method will return non-null after content has been initialized (e.g. by using
3382 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3383 *
3384 * @return This window's content TransitionManager or null if none is set.
3385 */
3386 public TransitionManager getContentTransitionManager() {
3387 return getWindow().getTransitionManager();
3388 }
3389
3390 /**
3391 * Set the {@link TransitionManager} to use for default transitions in this window.
3392 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3393 *
3394 * @param tm The TransitionManager to use for scene changes.
3395 */
3396 public void setContentTransitionManager(TransitionManager tm) {
3397 getWindow().setTransitionManager(tm);
3398 }
3399
3400 /**
3401 * Retrieve the {@link Scene} representing this window's current content.
3402 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3403 *
3404 * <p>This method will return null if the current content is not represented by a Scene.</p>
3405 *
3406 * @return Current Scene being shown or null
3407 */
3408 public Scene getContentScene() {
3409 return getWindow().getContentScene();
3410 }
3411
3412 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003413 * Sets whether this activity is finished when touched outside its window's
3414 * bounds.
3415 */
3416 public void setFinishOnTouchOutside(boolean finish) {
3417 mWindow.setCloseOnTouchOutside(finish);
3418 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003419
3420 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003421 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003422 DEFAULT_KEYS_DISABLE,
3423 DEFAULT_KEYS_DIALER,
3424 DEFAULT_KEYS_SHORTCUT,
3425 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003426 DEFAULT_KEYS_SEARCH_GLOBAL
3427 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003428 @Retention(RetentionPolicy.SOURCE)
3429 @interface DefaultKeyMode {}
3430
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3433 * keys.
RoboErik55011652014-07-09 15:05:53 -07003434 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 * @see #setDefaultKeyMode
3436 */
3437 static public final int DEFAULT_KEYS_DISABLE = 0;
3438 /**
3439 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3440 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 * @see #setDefaultKeyMode
3443 */
3444 static public final int DEFAULT_KEYS_DIALER = 1;
3445 /**
3446 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3447 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 * @see #setDefaultKeyMode
3452 */
3453 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3454 /**
3455 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3456 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003457 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 * @see #setDefaultKeyMode
3462 */
3463 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3464
3465 /**
3466 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3467 * will start a global search (typically web search, but some platforms may define alternate
3468 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003471 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 * @see #setDefaultKeyMode
3473 */
3474 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3475
3476 /**
3477 * Select the default key handling for this activity. This controls what
3478 * will happen to key events that are not otherwise handled. The default
3479 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3480 * floor. Other modes allow you to launch the dialer
3481 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3482 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003483 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003485 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 * <p>Note that the mode selected here does not impact the default
3487 * handling of system keys, such as the "back" and "menu" keys, and your
3488 * activity and its views always get a first chance to receive and handle
3489 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003490 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003492 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 * @see #onKeyDown
3494 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003495 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 // Some modes use a SpannableStringBuilder to track & dispatch input events
3499 // This list must remain in sync with the switch in onKeyDown()
3500 switch (mode) {
3501 case DEFAULT_KEYS_DISABLE:
3502 case DEFAULT_KEYS_SHORTCUT:
3503 mDefaultKeySsb = null; // not used in these modes
3504 break;
3505 case DEFAULT_KEYS_DIALER:
3506 case DEFAULT_KEYS_SEARCH_LOCAL:
3507 case DEFAULT_KEYS_SEARCH_GLOBAL:
3508 mDefaultKeySsb = new SpannableStringBuilder();
3509 Selection.setSelection(mDefaultKeySsb,0);
3510 break;
3511 default:
3512 throw new IllegalArgumentException();
3513 }
3514 }
3515
3516 /**
3517 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003518 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 * is inside a TextView will not trigger the event (unless it is a navigation
3520 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 * <p>If the focused view didn't want this event, this method is called.
3523 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003524 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3525 * by calling {@link #onBackPressed()}, though the behavior varies based
3526 * on the application compatibility mode: for
3527 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3528 * it will set up the dispatch to call {@link #onKeyUp} where the action
3529 * will be performed; for earlier applications, it will perform the
3530 * action immediately in on-down, as those versions of the platform
3531 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003532 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003533 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003534 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003535 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003537 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 * this event and it should continue to be propagated.
3539 * @see #onKeyUp
3540 * @see android.view.KeyEvent
3541 */
3542 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003543 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003544 if (getApplicationInfo().targetSdkVersion
3545 >= Build.VERSION_CODES.ECLAIR) {
3546 event.startTracking();
3547 } else {
3548 onBackPressed();
3549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 return true;
3551 }
RoboErik55011652014-07-09 15:05:53 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3554 return false;
3555 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003556 Window w = getWindow();
3557 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3558 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3559 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003560 return true;
3561 }
3562 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003563 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3564 // Don't consume TAB here since it's used for navigation. Arrow keys
3565 // aren't considered "typing keys" so they already won't get consumed.
3566 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 } else {
3568 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3569 boolean clearSpannable = false;
3570 boolean handled;
3571 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3572 clearSpannable = true;
3573 handled = false;
3574 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003575 handled = TextKeyListener.getInstance().onKeyDown(
3576 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 if (handled && mDefaultKeySsb.length() > 0) {
3578 // something useable has been typed - dispatch it now.
3579
3580 final String str = mDefaultKeySsb.toString();
3581 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 switch (mDefaultKeyMode) {
3584 case DEFAULT_KEYS_DIALER:
3585 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3586 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003587 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 break;
3589 case DEFAULT_KEYS_SEARCH_LOCAL:
3590 startSearch(str, false, null, false);
3591 break;
3592 case DEFAULT_KEYS_SEARCH_GLOBAL:
3593 startSearch(str, false, null, true);
3594 break;
3595 }
3596 }
3597 }
3598 if (clearSpannable) {
3599 mDefaultKeySsb.clear();
3600 mDefaultKeySsb.clearSpans();
3601 Selection.setSelection(mDefaultKeySsb,0);
3602 }
3603 return handled;
3604 }
3605 }
3606
3607 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003608 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3609 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3610 * the event).
Siarhei Vishniakouccf973f2018-10-30 14:04:57 -07003611 *
3612 * To receive this callback, you must return true from onKeyDown for the current
3613 * event stream.
3614 *
3615 * @see KeyEvent.Callback#onKeyLongPress()
3616 * @see KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003617 */
3618 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3619 return false;
3620 }
3621
3622 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003624 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 * is inside a TextView will not trigger the event (unless it is a navigation
3626 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003627 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003628 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3629 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003630 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003632 * further, or <code>false</code> to indicate that you have not handled
3633 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 * @see #onKeyDown
3635 * @see KeyEvent
3636 */
3637 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003638 if (getApplicationInfo().targetSdkVersion
3639 >= Build.VERSION_CODES.ECLAIR) {
3640 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3641 && !event.isCanceled()) {
3642 onBackPressed();
3643 return true;
3644 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 return false;
3647 }
3648
3649 /**
3650 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3651 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3652 * the event).
3653 */
3654 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3655 return false;
3656 }
RoboErik55011652014-07-09 15:05:53 -07003657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003659 * Called when the activity has detected the user's press of the back
3660 * key. The default implementation simply finishes the current activity,
3661 * but you can override this to do whatever you want.
3662 */
3663 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003664 if (mActionBar != null && mActionBar.collapseActionView()) {
3665 return;
3666 }
3667
George Mount27b0dc02017-02-21 10:24:09 -08003668 FragmentManager fragmentManager = mFragments.getFragmentManager();
3669
Mark Renouf446251d2019-04-26 10:22:41 -04003670 if (!fragmentManager.isStateSaved() && fragmentManager.popBackStackImmediate()) {
3671 return;
3672 }
3673 if (!isTaskRoot()) {
3674 // If the activity is not the root of the task, allow finish to proceed normally.
3675 finishAfterTransition();
3676 return;
3677 }
3678 try {
3679 // Inform activity task manager that the activity received a back press
3680 // while at the root of the task. This call allows ActivityTaskManager
3681 // to intercept or defer finishing.
3682 ActivityTaskManager.getService().onBackPressedOnTaskRoot(mToken,
3683 new IRequestFinishCallback.Stub() {
3684 public void requestFinish() {
Mady Mellor9d662c02019-05-23 07:09:18 -07003685 mHandler.post(() -> finishAfterTransition());
Mark Renouf446251d2019-04-26 10:22:41 -04003686 }
3687 });
3688 } catch (RemoteException e) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003689 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003690 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003691 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003692
3693 /**
3694 * Called when a key shortcut event is not handled by any of the views in the Activity.
3695 * Override this method to implement global key shortcuts for the Activity.
3696 * Key shortcuts can also be implemented by setting the
3697 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3698 *
3699 * @param keyCode The value in event.getKeyCode().
3700 * @param event Description of the key event.
3701 * @return True if the key shortcut was handled.
3702 */
3703 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003704 // Let the Action Bar have a chance at handling the shortcut.
3705 ActionBar actionBar = getActionBar();
3706 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003707 }
3708
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 * Called when a touch screen event was not handled by any of the views
3711 * under it. This is most useful to process touch events that happen
3712 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003713 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003715 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 * @return Return true if you have consumed the event, false if you haven't.
3717 * The default implementation always returns false.
3718 */
3719 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003720 if (mWindow.shouldCloseOnTouch(this, event)) {
3721 finish();
3722 return true;
3723 }
RoboErik55011652014-07-09 15:05:53 -07003724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 return false;
3726 }
RoboErik55011652014-07-09 15:05:53 -07003727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 /**
3729 * Called when the trackball was moved and not handled by any of the
3730 * views inside of the activity. So, for example, if the trackball moves
3731 * while focus is on a button, you will receive a call here because
3732 * buttons do not normally do anything with trackball events. The call
3733 * here happens <em>before</em> trackball movements are converted to
3734 * DPAD key events, which then get sent back to the view hierarchy, and
3735 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003738 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 * @return Return true if you have consumed the event, false if you haven't.
3740 * The default implementation always returns false.
3741 */
3742 public boolean onTrackballEvent(MotionEvent event) {
3743 return false;
3744 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003745
3746 /**
3747 * Called when a generic motion event was not handled by any of the
3748 * views inside of the activity.
3749 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003750 * Generic motion events describe joystick movements, mouse hovers, track pad
3751 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003752 * {@link MotionEvent#getSource() source} of the motion event specifies
3753 * the class of input that was received. Implementations of this method
3754 * must examine the bits in the source before processing the event.
3755 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003756 * </p><p>
3757 * Generic motion events with source class
3758 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3759 * are delivered to the view under the pointer. All other generic motion events are
3760 * delivered to the focused view.
3761 * </p><p>
3762 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3763 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003764 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003765 *
3766 * @param event The generic motion event being processed.
3767 *
3768 * @return Return true if you have consumed the event, false if you haven't.
3769 * The default implementation always returns false.
3770 */
3771 public boolean onGenericMotionEvent(MotionEvent event) {
3772 return false;
3773 }
3774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 /**
3776 * Called whenever a key, touch, or trackball event is dispatched to the
3777 * activity. Implement this method if you wish to know that the user has
3778 * interacted with the device in some way while your activity is running.
3779 * This callback and {@link #onUserLeaveHint} are intended to help
3780 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003781 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003782 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3784 * be accompanied by calls to {@link #onUserInteraction}. This
3785 * ensures that your activity will be told of relevant user activity such
3786 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 * <p>Note that this callback will be invoked for the touch down action
3789 * that begins a touch gesture, but may not be invoked for the touch-moved
3790 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003791 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 * @see #onUserLeaveHint()
3793 */
3794 public void onUserInteraction() {
3795 }
RoboErik55011652014-07-09 15:05:53 -07003796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3798 // Update window manager if: we have a view, that view is
3799 // attached to its parent (which will be a RootView), and
3800 // this activity is not embedded.
3801 if (mParent == null) {
3802 View decor = mDecor;
3803 if (decor != null && decor.getParent() != null) {
3804 getWindowManager().updateViewLayout(decor, params);
Adam He43c06992019-04-15 15:41:49 -07003805 if (mContentCaptureManager != null) {
3806 mContentCaptureManager.updateWindowAttributes(params);
3807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 }
3809 }
3810 }
3811
3812 public void onContentChanged() {
3813 }
3814
3815 /**
3816 * Called when the current {@link Window} of the activity gains or loses
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003817 * focus. This is the best indicator of whether this activity is the entity
3818 * with which the user actively interacts. The default implementation
3819 * clears the key tracking state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003820 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003821 * <p>Note that this provides information about global focus state, which
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003822 * is managed independently of activity lifecycle. As such, while focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 * changes will generally have some relation to lifecycle changes (an
3824 * activity that is stopped will not generally get window focus), you
3825 * should not rely on any particular order between the callbacks here and
3826 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003827 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003828 * <p>As a general rule, however, a foreground activity will have window
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 * focus... unless it has displayed other dialogs or popups that take
3830 * input focus, in which case the activity itself will not have focus
3831 * when the other windows have it. Likewise, the system may display
3832 * system-level windows (such as the status bar notification panel or
3833 * a system alert) which will temporarily take window input focus without
3834 * pausing the foreground activity.
3835 *
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003836 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} there can be
3837 * multiple resumed activities at the same time in multi-window mode, so
3838 * resumed state does not guarantee window focus even if there are no
3839 * overlays above.
3840 *
3841 * <p>If the intent is to know when an activity is the topmost active, the
3842 * one the user interacted with last among all activities but not including
3843 * non-activity windows like dialogs and popups, then
3844 * {@link #onTopResumedActivityChanged(boolean)} should be used. On platform
3845 * versions prior to {@link android.os.Build.VERSION_CODES#Q},
3846 * {@link #onResume} is the best indicator.
3847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003849 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 * @see #hasWindowFocus()
3851 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003852 * @see View#onWindowFocusChanged(boolean)
Andrii Kulian8e4df1e2019-03-19 15:17:56 -07003853 * @see #onTopResumedActivityChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 */
3855 public void onWindowFocusChanged(boolean hasFocus) {
3856 }
RoboErik55011652014-07-09 15:05:53 -07003857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003859 * Called when the main window associated with the activity has been
3860 * attached to the window manager.
3861 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3862 * for more information.
3863 * @see View#onAttachedToWindow
3864 */
3865 public void onAttachedToWindow() {
3866 }
RoboErik55011652014-07-09 15:05:53 -07003867
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003868 /**
3869 * Called when the main window associated with the activity has been
3870 * detached from the window manager.
3871 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3872 * for more information.
3873 * @see View#onDetachedFromWindow
3874 */
3875 public void onDetachedFromWindow() {
3876 }
RoboErik55011652014-07-09 15:05:53 -07003877
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 * Returns true if this activity's <em>main</em> window currently has window focus.
3880 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003881 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003883 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3885 */
3886 public boolean hasWindowFocus() {
3887 Window w = getWindow();
3888 if (w != null) {
3889 View d = w.getDecorView();
3890 if (d != null) {
3891 return d.hasWindowFocus();
3892 }
3893 }
3894 return false;
3895 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003896
3897 /**
3898 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003899 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003900 */
Adam Powell117b6952014-05-05 18:14:56 -07003901 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003902 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003903 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003904 if (suppressWindowTransition) {
3905 overridePendingTransition(0, 0);
3906 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003907 }
RoboErik55011652014-07-09 15:05:53 -07003908
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003909
Skuhnece2faa52015-08-11 10:36:38 -07003910 /**
Yunfan Chend967af82019-01-17 18:30:18 +09003911 * Moves the activity between {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode
3912 * and {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003913 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003914 * @hide
3915 */
3916 @Override
Yunfan Chend967af82019-01-17 18:30:18 +09003917 public void toggleFreeformWindowingMode() throws RemoteException {
3918 ActivityTaskManager.getService().toggleFreeformWindowingMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003919 }
3920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003922 * Puts the activity in picture-in-picture mode if the activity supports.
3923 * @see android.R.attr#supportsPictureInPicture
3924 * @hide
3925 */
3926 @Override
3927 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003928 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003929 enterPictureInPictureMode();
3930 }
3931 }
3932
3933 /**
RoboErik55011652014-07-09 15:05:53 -07003934 * Called to process key events. You can override this to intercept all
3935 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 * @return boolean Return true if this event was consumed.
3941 */
3942 public boolean dispatchKeyEvent(KeyEvent event) {
3943 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003944
3945 // Let action bars open menus in response to the menu key prioritized over
3946 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003947 final int keyCode = event.getKeyCode();
3948 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003949 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3950 return true;
3951 }
3952
Dianne Hackborn8d374262009-09-14 21:21:52 -07003953 Window win = getWindow();
3954 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 return true;
3956 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003957 View decor = mDecor;
3958 if (decor == null) decor = win.getDecorView();
3959 return event.dispatch(this, decor != null
3960 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 }
3962
3963 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003964 * Called to process a key shortcut event.
3965 * You can override this to intercept all key shortcut events before they are
3966 * dispatched to the window. Be sure to call this implementation for key shortcut
3967 * events that should be handled normally.
3968 *
3969 * @param event The key shortcut event.
3970 * @return True if this event was consumed.
3971 */
3972 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3973 onUserInteraction();
3974 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3975 return true;
3976 }
3977 return onKeyShortcut(event.getKeyCode(), event);
3978 }
3979
3980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 * Called to process touch screen events. You can override this to
3982 * intercept all touch screen events before they are dispatched to the
3983 * window. Be sure to call this implementation for touch screen events
3984 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003985 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003987 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 * @return boolean Return true if this event was consumed.
3989 */
3990 public boolean dispatchTouchEvent(MotionEvent ev) {
3991 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3992 onUserInteraction();
3993 }
3994 if (getWindow().superDispatchTouchEvent(ev)) {
3995 return true;
3996 }
3997 return onTouchEvent(ev);
3998 }
RoboErik55011652014-07-09 15:05:53 -07003999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 /**
4001 * Called to process trackball events. You can override this to
4002 * intercept all trackball events before they are dispatched to the
4003 * window. Be sure to call this implementation for trackball events
4004 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07004005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07004007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 * @return boolean Return true if this event was consumed.
4009 */
4010 public boolean dispatchTrackballEvent(MotionEvent ev) {
4011 onUserInteraction();
4012 if (getWindow().superDispatchTrackballEvent(ev)) {
4013 return true;
4014 }
4015 return onTrackballEvent(ev);
4016 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004017
Jeff Browncb1404e2011-01-15 18:14:15 -08004018 /**
4019 * Called to process generic motion events. You can override this to
4020 * intercept all generic motion events before they are dispatched to the
4021 * window. Be sure to call this implementation for generic motion events
4022 * that should be handled normally.
4023 *
4024 * @param ev The generic motion event.
4025 *
4026 * @return boolean Return true if this event was consumed.
4027 */
4028 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
4029 onUserInteraction();
4030 if (getWindow().superDispatchGenericMotionEvent(ev)) {
4031 return true;
4032 }
4033 return onGenericMotionEvent(ev);
4034 }
4035
svetoslavganov75986cf2009-05-14 22:28:01 -07004036 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
4037 event.setClassName(getClass().getName());
4038 event.setPackageName(getPackageName());
4039
4040 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08004041 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
4042 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07004043 event.setFullScreen(isFullScreen);
4044
4045 CharSequence title = getTitle();
4046 if (!TextUtils.isEmpty(title)) {
4047 event.getText().add(title);
4048 }
4049
4050 return true;
4051 }
4052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 /**
4054 * Default implementation of
4055 * {@link android.view.Window.Callback#onCreatePanelView}
4056 * for activities. This
4057 * simply returns null so that all panel sub-windows will have the default
4058 * menu behavior.
4059 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004060 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 public View onCreatePanelView(int featureId) {
4062 return null;
4063 }
4064
4065 /**
4066 * Default implementation of
4067 * {@link android.view.Window.Callback#onCreatePanelMenu}
4068 * for activities. This calls through to the new
4069 * {@link #onCreateOptionsMenu} method for the
4070 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4071 * so that subclasses of Activity don't need to deal with feature codes.
4072 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004073 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004075 boolean show = onCreateOptionsMenu(menu);
4076 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
4077 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
4079 return false;
4080 }
4081
4082 /**
4083 * Default implementation of
4084 * {@link android.view.Window.Callback#onPreparePanel}
4085 * for activities. This
4086 * calls through to the new {@link #onPrepareOptionsMenu} method for the
4087 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4088 * panel, so that subclasses of
4089 * Activity don't need to deal with feature codes.
4090 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004091 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
4092 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004094 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07004095 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
4097 return true;
4098 }
4099
4100 /**
4101 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07004102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 * @return The default implementation returns true.
4104 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004105 @Override
4106 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08004107 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07004108 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08004109 if (mActionBar != null) {
4110 mActionBar.dispatchMenuVisibilityChanged(true);
4111 } else {
4112 Log.e(TAG, "Tried to open action bar menu with no action bar");
4113 }
Adam Powell8515ee82010-11-30 14:09:55 -08004114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 return true;
4116 }
4117
4118 /**
4119 * Default implementation of
4120 * {@link android.view.Window.Callback#onMenuItemSelected}
4121 * for activities. This calls through to the new
4122 * {@link #onOptionsItemSelected} method for the
4123 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4124 * panel, so that subclasses of
4125 * Activity don't need to deal with feature codes.
4126 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004127 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09004128 CharSequence titleCondensed = item.getTitleCondensed();
4129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 switch (featureId) {
4131 case Window.FEATURE_OPTIONS_PANEL:
4132 // Put event logging here so it gets called even if subclass
4133 // doesn't call through to superclass's implmeentation of each
4134 // of these methods below
zobject686a8052012-12-14 21:11:08 +09004135 if(titleCondensed != null) {
4136 EventLog.writeEvent(50000, 0, titleCondensed.toString());
4137 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004138 if (onOptionsItemSelected(item)) {
4139 return true;
4140 }
Adam Powell04d58112012-04-09 10:22:12 -07004141 if (mFragments.dispatchOptionsItemSelected(item)) {
4142 return true;
4143 }
4144 if (item.getItemId() == android.R.id.home && mActionBar != null &&
4145 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
4146 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07004147 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07004148 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07004149 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07004150 }
Adam Powell04d58112012-04-09 10:22:12 -07004151 }
4152 return false;
RoboErik55011652014-07-09 15:05:53 -07004153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09004155 if(titleCondensed != null) {
4156 EventLog.writeEvent(50000, 1, titleCondensed.toString());
4157 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07004158 if (onContextItemSelected(item)) {
4159 return true;
4160 }
4161 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08004162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 default:
4164 return false;
4165 }
4166 }
RoboErik55011652014-07-09 15:05:53 -07004167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 /**
4169 * Default implementation of
4170 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
4171 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
4172 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4173 * so that subclasses of Activity don't need to deal with feature codes.
4174 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
4175 * {@link #onContextMenuClosed(Menu)} will be called.
4176 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004177 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 switch (featureId) {
4179 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004180 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 onOptionsMenuClosed(menu);
4182 break;
RoboErik55011652014-07-09 15:05:53 -07004183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 case Window.FEATURE_CONTEXT_MENU:
4185 onContextMenuClosed(menu);
4186 break;
Adam Powell8515ee82010-11-30 14:09:55 -08004187
4188 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07004189 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08004190 mActionBar.dispatchMenuVisibilityChanged(false);
4191 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 }
4193 }
4194
4195 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004196 * Declare that the options menu has changed, so should be recreated.
4197 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4198 * time it needs to be displayed.
4199 */
4200 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004201 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4202 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004203 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4204 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004205 }
RoboErik55011652014-07-09 15:05:53 -07004206
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 * Initialize the contents of the Activity's standard options menu. You
4209 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 * <p>This is only called once, the first time the options menu is
4212 * displayed. To update the menu every time it is displayed, see
4213 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004214 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004216 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4217 * they will be correctly ordered with application-defined menu items.
4218 * Deriving classes should always call through to the base implementation.
4219 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 * <p>You can safely hold on to <var>menu</var> (and any items created
4221 * from it), making modifications to it as desired, until the next
4222 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 * <p>When you add items to the menu, you can implement the Activity's
4225 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004226 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 * @return You must return true for the menu to be displayed;
4230 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 * @see #onPrepareOptionsMenu
4233 * @see #onOptionsItemSelected
4234 */
4235 public boolean onCreateOptionsMenu(Menu menu) {
4236 if (mParent != null) {
4237 return mParent.onCreateOptionsMenu(menu);
4238 }
4239 return true;
4240 }
4241
4242 /**
4243 * Prepare the Screen's standard options menu to be displayed. This is
4244 * called right before the menu is shown, every time it is shown. You can
4245 * use this method to efficiently enable/disable items or otherwise
4246 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 * <p>The default implementation updates the system menu items based on the
4249 * activity's state. Deriving classes should always call through to the
4250 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 * @param menu The options menu as last shown or first initialized by
4253 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 * @return You must return true for the menu to be displayed;
4256 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 * @see #onCreateOptionsMenu
4259 */
4260 public boolean onPrepareOptionsMenu(Menu menu) {
4261 if (mParent != null) {
4262 return mParent.onPrepareOptionsMenu(menu);
4263 }
4264 return true;
4265 }
4266
4267 /**
4268 * This hook is called whenever an item in your options menu is selected.
4269 * The default implementation simply returns false to have the normal
4270 * processing happen (calling the item's Runnable or sending a message to
4271 * its Handler as appropriate). You can use this method for any items
4272 * for which you would like to do processing without those other
4273 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004276 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 * @return boolean Return false to allow normal menu processing to
4281 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 * @see #onCreateOptionsMenu
4284 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004285 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 if (mParent != null) {
4287 return mParent.onOptionsItemSelected(item);
4288 }
4289 return false;
4290 }
4291
4292 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004293 * This method is called whenever the user chooses to navigate Up within your application's
4294 * activity hierarchy from the action bar.
4295 *
4296 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4297 * was specified in the manifest for this activity or an activity-alias to it,
4298 * default Up navigation will be handled automatically. If any activity
4299 * along the parent chain requires extra Intent arguments, the Activity subclass
4300 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4301 * to supply those arguments.</p>
4302 *
Mark Lufa434852016-08-11 17:40:33 -07004303 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004304 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4305 * from the design guide for more information about navigating within your app.</p>
4306 *
4307 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4308 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4309 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4310 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4311 *
4312 * @return true if Up navigation completed successfully and this Activity was finished,
4313 * false otherwise.
4314 */
4315 public boolean onNavigateUp() {
4316 // Automatically handle hierarchical Up navigation if the proper
4317 // metadata is available.
4318 Intent upIntent = getParentActivityIntent();
4319 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004320 if (mActivityInfo.taskAffinity == null) {
4321 // Activities with a null affinity are special; they really shouldn't
4322 // specify a parent activity intent in the first place. Just finish
4323 // the current activity and call it a day.
4324 finish();
4325 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004326 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004327 onCreateNavigateUpTaskStack(b);
4328 onPrepareNavigateUpTaskStack(b);
4329 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004330
4331 // We can't finishAffinity if we have a result.
4332 // Fall back and simply finish the current activity instead.
4333 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4334 // Tell the developer what's going on to avoid hair-pulling.
4335 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4336 finish();
4337 } else {
4338 finishAffinity();
4339 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004340 } else {
4341 navigateUpTo(upIntent);
4342 }
4343 return true;
4344 }
4345 return false;
4346 }
4347
4348 /**
4349 * This is called when a child activity of this one attempts to navigate up.
4350 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4351 *
4352 * @param child The activity making the call.
4353 */
4354 public boolean onNavigateUpFromChild(Activity child) {
4355 return onNavigateUp();
4356 }
4357
4358 /**
4359 * Define the synthetic task stack that will be generated during Up navigation from
4360 * a different task.
4361 *
4362 * <p>The default implementation of this method adds the parent chain of this activity
4363 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4364 * may choose to override this method to construct the desired task stack in a different
4365 * way.</p>
4366 *
Adam Powellf0195952012-05-02 21:38:54 -07004367 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4368 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4369 * returned by {@link #getParentActivityIntent()}.</p>
4370 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004371 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4372 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4373 *
4374 * @param builder An empty TaskStackBuilder - the application should add intents representing
4375 * the desired task stack
4376 */
4377 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4378 builder.addParentStack(this);
4379 }
4380
4381 /**
4382 * Prepare the synthetic task stack that will be generated during Up navigation
4383 * from a different task.
4384 *
4385 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4386 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4387 * If any extra data should be added to these intents before launching the new task,
4388 * the application should override this method and add that data here.</p>
4389 *
4390 * @param builder A TaskStackBuilder that has been populated with Intents by
4391 * onCreateNavigateUpTaskStack.
4392 */
4393 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4394 }
4395
4396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 * This hook is called whenever the options menu is being closed (either by the user canceling
4398 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 * @param menu The options menu as last shown or first initialized by
4401 * onCreateOptionsMenu().
4402 */
4403 public void onOptionsMenuClosed(Menu menu) {
4404 if (mParent != null) {
4405 mParent.onOptionsMenuClosed(menu);
4406 }
4407 }
RoboErik55011652014-07-09 15:05:53 -07004408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 /**
4410 * Programmatically opens the options menu. If the options menu is already
4411 * open, this method does nothing.
4412 */
4413 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004414 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4415 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004416 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 }
RoboErik55011652014-07-09 15:05:53 -07004419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 /**
4421 * Progammatically closes the options menu. If the options menu is already
4422 * closed, this method does nothing.
4423 */
4424 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004425 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4426 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004427 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 }
4430
4431 /**
4432 * Called when a context menu for the {@code view} is about to be shown.
4433 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4434 * time the context menu is about to be shown and should be populated for
4435 * the view (or item inside the view for {@link AdapterView} subclasses,
4436 * this can be found in the {@code menuInfo})).
4437 * <p>
4438 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4439 * item has been selected.
4440 * <p>
4441 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 */
4444 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4445 }
4446
4447 /**
4448 * Registers a context menu to be shown for the given view (multiple views
4449 * can show the context menu). This method will set the
4450 * {@link OnCreateContextMenuListener} on the view to this activity, so
4451 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4452 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 * @see #unregisterForContextMenu(View)
4455 * @param view The view that should show a context menu.
4456 */
4457 public void registerForContextMenu(View view) {
4458 view.setOnCreateContextMenuListener(this);
4459 }
RoboErik55011652014-07-09 15:05:53 -07004460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 /**
4462 * Prevents a context menu to be shown for the given view. This method will remove the
4463 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 * @see #registerForContextMenu(View)
4466 * @param view The view that should stop showing a context menu.
4467 */
4468 public void unregisterForContextMenu(View view) {
4469 view.setOnCreateContextMenuListener(null);
4470 }
RoboErik55011652014-07-09 15:05:53 -07004471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 /**
4473 * Programmatically opens the context menu for a particular {@code view}.
4474 * The {@code view} should have been added via
4475 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 * @param view The view to show the context menu for.
4478 */
4479 public void openContextMenu(View view) {
4480 view.showContextMenu();
4481 }
RoboErik55011652014-07-09 15:05:53 -07004482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 /**
4484 * Programmatically closes the most recently opened context menu, if showing.
4485 */
4486 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004487 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4488 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 }
RoboErik55011652014-07-09 15:05:53 -07004491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 /**
4493 * This hook is called whenever an item in a context menu is selected. The
4494 * default implementation simply returns false to have the normal processing
4495 * happen (calling the item's Runnable or sending a message to its Handler
4496 * as appropriate). You can use this method for any items for which you
4497 * would like to do processing without those other facilities.
4498 * <p>
4499 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4500 * View that added this menu item.
4501 * <p>
4502 * Derived classes should call through to the base class for it to perform
4503 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 * @param item The context menu item that was selected.
4506 * @return boolean Return false to allow normal context menu processing to
4507 * proceed, true to consume it here.
4508 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004509 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 if (mParent != null) {
4511 return mParent.onContextItemSelected(item);
4512 }
4513 return false;
4514 }
4515
4516 /**
4517 * This hook is called whenever the context menu is being closed (either by
4518 * the user canceling the menu with the back/menu button, or when an item is
4519 * selected).
RoboErik55011652014-07-09 15:05:53 -07004520 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 * @param menu The context menu that is being closed.
4522 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004523 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 if (mParent != null) {
4525 mParent.onContextMenuClosed(menu);
4526 }
4527 }
4528
4529 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004530 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004532 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 protected Dialog onCreateDialog(int id) {
4534 return null;
4535 }
4536
4537 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004538 * Callback for creating dialogs that are managed (saved and restored) for you
4539 * by the activity. The default implementation calls through to
4540 * {@link #onCreateDialog(int)} for compatibility.
4541 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004542 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4543 * or later, consider instead using a {@link DialogFragment} instead.</em>
4544 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004545 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4546 * this method the first time, and hang onto it thereafter. Any dialog
4547 * that is created by this method will automatically be saved and restored
4548 * for you, including whether it is showing.
4549 *
4550 * <p>If you would like the activity to manage saving and restoring dialogs
4551 * for you, you should override this method and handle any ids that are
4552 * passed to {@link #showDialog}.
4553 *
4554 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4555 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4556 *
4557 * @param id The id of the dialog.
4558 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4559 * @return The dialog. If you return null, the dialog will not be created.
4560 *
4561 * @see #onPrepareDialog(int, Dialog, Bundle)
4562 * @see #showDialog(int, Bundle)
4563 * @see #dismissDialog(int)
4564 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004565 *
4566 * @deprecated Use the new {@link DialogFragment} class with
4567 * {@link FragmentManager} instead; this is also
4568 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004569 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004570 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004571 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004572 protected Dialog onCreateDialog(int id, Bundle args) {
4573 return onCreateDialog(id);
4574 }
4575
4576 /**
4577 * @deprecated Old no-arguments version of
4578 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4579 */
4580 @Deprecated
4581 protected void onPrepareDialog(int id, Dialog dialog) {
4582 dialog.setOwnerActivity(this);
4583 }
4584
4585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004587 * shown. The default implementation calls through to
4588 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 * <p>
4591 * Override this if you need to update a managed dialog based on the state
4592 * of the application each time it is shown. For example, a time picker
4593 * dialog might want to be updated with the current time. You should call
4594 * through to the superclass's implementation. The default implementation
4595 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004596 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 * @param id The id of the managed dialog.
4598 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004599 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4600 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 * @see #showDialog(int)
4602 * @see #dismissDialog(int)
4603 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004604 *
4605 * @deprecated Use the new {@link DialogFragment} class with
4606 * {@link FragmentManager} instead; this is also
4607 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004609 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004610 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4611 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 }
4613
4614 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004615 * Simple version of {@link #showDialog(int, Bundle)} that does not
4616 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4617 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004618 *
4619 * @deprecated Use the new {@link DialogFragment} class with
4620 * {@link FragmentManager} instead; this is also
4621 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004622 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004623 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004624 public final void showDialog(int id) {
4625 showDialog(id, null);
4626 }
4627
4628 /**
4629 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 * will be made with the same id the first time this is called for a given
4631 * id. From thereafter, the dialog will be automatically saved and restored.
4632 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004633 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4634 * or later, consider instead using a {@link DialogFragment} instead.</em>
4635 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004636 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 * be made to provide an opportunity to do any timely preparation.
4638 *
4639 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004640 * @param args Arguments to pass through to the dialog. These will be saved
4641 * and restored for you. Note that if the dialog is already created,
4642 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4643 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004644 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004645 * @return Returns true if the Dialog was created; false is returned if
4646 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004647 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004648 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004649 * @see #onCreateDialog(int, Bundle)
4650 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 * @see #dismissDialog(int)
4652 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004653 *
4654 * @deprecated Use the new {@link DialogFragment} class with
4655 * {@link FragmentManager} instead; this is also
4656 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004658 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004659 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004661 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004663 ManagedDialog md = mManagedDialogs.get(id);
4664 if (md == null) {
4665 md = new ManagedDialog();
4666 md.mDialog = createDialog(id, null, args);
4667 if (md.mDialog == null) {
4668 return false;
4669 }
4670 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 }
RoboErik55011652014-07-09 15:05:53 -07004672
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004673 md.mArgs = args;
4674 onPrepareDialog(id, md.mDialog, args);
4675 md.mDialog.show();
4676 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 }
4678
4679 /**
4680 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4681 *
4682 * @param id The id of the managed dialog.
4683 *
4684 * @throws IllegalArgumentException if the id was not previously shown via
4685 * {@link #showDialog(int)}.
4686 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004687 * @see #onCreateDialog(int, Bundle)
4688 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 * @see #showDialog(int)
4690 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004691 *
4692 * @deprecated Use the new {@link DialogFragment} class with
4693 * {@link FragmentManager} instead; this is also
4694 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004696 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 public final void dismissDialog(int id) {
4698 if (mManagedDialogs == null) {
4699 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 }
RoboErik55011652014-07-09 15:05:53 -07004701
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004702 final ManagedDialog md = mManagedDialogs.get(id);
4703 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 throw missingDialog(id);
4705 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004706 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 }
4708
4709 /**
4710 * Creates an exception to throw if a user passed in a dialog id that is
4711 * unexpected.
4712 */
4713 private IllegalArgumentException missingDialog(int id) {
4714 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4715 + "shown via Activity#showDialog");
4716 }
4717
4718 /**
4719 * Removes any internal references to a dialog managed by this Activity.
4720 * If the dialog is showing, it will dismiss it as part of the clean up.
4721 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004722 * <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 -08004723 * want to avoid the overhead of saving and restoring it in the future.
4724 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004725 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4726 * will not throw an exception if you try to remove an ID that does not
4727 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004728 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 * @param id The id of the managed dialog.
4730 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004731 * @see #onCreateDialog(int, Bundle)
4732 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004733 * @see #showDialog(int)
4734 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004735 *
4736 * @deprecated Use the new {@link DialogFragment} class with
4737 * {@link FragmentManager} instead; this is also
4738 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004740 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004742 if (mManagedDialogs != null) {
4743 final ManagedDialog md = mManagedDialogs.get(id);
4744 if (md != null) {
4745 md.mDialog.dismiss();
4746 mManagedDialogs.remove(id);
4747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 }
4750
4751 /**
4752 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004753 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004754 * <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 -07004755 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004756 * calling this function is the same as calling
4757 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4758 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004759 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 * <p>You can override this function to force global search, e.g. in response to a dedicated
4761 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004762 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004763 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4764 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4765 * return false and you must supply your own custom implementation if you want to support
4766 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004767 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004768 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004769 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4770 * not respond to search. The default implementation always returns {@code true}, except
4771 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004772 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 * @see android.app.SearchManager
4774 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004775 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4776 mSearchEvent = searchEvent;
4777 boolean result = onSearchRequested();
4778 mSearchEvent = null;
4779 return result;
4780 }
4781
4782 /**
4783 * @see #onSearchRequested(SearchEvent)
4784 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004786 final int uiMode = getResources().getConfiguration().uiMode
4787 & Configuration.UI_MODE_TYPE_MASK;
4788 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4789 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004790 startSearch(null, false, null, false);
4791 return true;
4792 } else {
4793 return false;
4794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 }
RoboErik55011652014-07-09 15:05:53 -07004796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004798 * During the onSearchRequested() callbacks, this function will return the
4799 * {@link SearchEvent} that triggered the callback, if it exists.
4800 *
4801 * @return SearchEvent The SearchEvent that triggered the {@link
4802 * #onSearchRequested} callback.
4803 */
4804 public final SearchEvent getSearchEvent() {
4805 return mSearchEvent;
4806 }
4807
4808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004810 *
4811 * <p>It is typically called from onSearchRequested(), either directly from
4812 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004814 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 * is to inject specific data such as context data, it is preferred to <i>override</i>
4816 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004817 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004818 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4819 * not supported.
4820 *
RoboErik55011652014-07-09 15:05:53 -07004821 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004823 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 * any further typing will replace it. This is useful for cases where an entire pre-formed
4825 * query is being inserted. If false, the selection point will be placed at the end of the
4826 * inserted query. This is useful when the inserted query is text that the user entered,
4827 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4828 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004829 * @param appSearchData An application can insert application-specific
4830 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 * searches. This data will be returned with SEARCH intent(s). Null if
4832 * no extra data is required.
4833 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004834 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004835 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004837 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 * @see android.app.SearchManager
4839 * @see #onSearchRequested
4840 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004841 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4842 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004843 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004844 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004845 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 }
4847
4848 /**
krosaend2d60142009-08-17 08:56:48 -07004849 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4850 * the search dialog. Made available for testing purposes.
4851 *
4852 * @param query The query to trigger. If empty, the request will be ignored.
4853 * @param appSearchData An application can insert application-specific
4854 * context here, in order to improve quality or specificity of its own
4855 * searches. This data will be returned with SEARCH intent(s). Null if
4856 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004857 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004858 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004859 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004860 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004861 }
4862
4863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 * Request that key events come to this activity. Use this if your
4865 * activity has no views with focus, but the activity still wants
4866 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 * @see android.view.Window#takeKeyEvents
4869 */
4870 public void takeKeyEvents(boolean get) {
4871 getWindow().takeKeyEvents(get);
4872 }
4873
4874 /**
4875 * Enable extended window features. This is a convenience for calling
4876 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 * @param featureId The desired feature as defined in
4879 * {@link android.view.Window}.
4880 * @return Returns true if the requested feature is supported and now
4881 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004882 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 * @see android.view.Window#requestFeature
4884 */
4885 public final boolean requestWindowFeature(int featureId) {
4886 return getWindow().requestFeature(featureId);
4887 }
4888
4889 /**
4890 * Convenience for calling
4891 * {@link android.view.Window#setFeatureDrawableResource}.
4892 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004893 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 getWindow().setFeatureDrawableResource(featureId, resId);
4895 }
4896
4897 /**
4898 * Convenience for calling
4899 * {@link android.view.Window#setFeatureDrawableUri}.
4900 */
4901 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4902 getWindow().setFeatureDrawableUri(featureId, uri);
4903 }
4904
4905 /**
4906 * Convenience for calling
4907 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4908 */
4909 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4910 getWindow().setFeatureDrawable(featureId, drawable);
4911 }
4912
4913 /**
4914 * Convenience for calling
4915 * {@link android.view.Window#setFeatureDrawableAlpha}.
4916 */
4917 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4918 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4919 }
4920
4921 /**
4922 * Convenience for calling
4923 * {@link android.view.Window#getLayoutInflater}.
4924 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004925 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 public LayoutInflater getLayoutInflater() {
4927 return getWindow().getLayoutInflater();
4928 }
4929
4930 /**
4931 * Returns a {@link MenuInflater} with this context.
4932 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004933 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004935 // Make sure that action views can get an appropriate theme.
4936 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004937 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004938 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004939 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004940 } else {
4941 mMenuInflater = new MenuInflater(this);
4942 }
4943 }
4944 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 }
4946
4947 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004948 public void setTheme(int resid) {
4949 super.setTheme(resid);
4950 mWindow.setTheme(resid);
4951 }
4952
4953 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004954 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004955 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 if (mParent == null) {
4957 super.onApplyThemeResource(theme, resid, first);
4958 } else {
4959 try {
4960 theme.setTo(mParent.getTheme());
4961 } catch (Exception e) {
4962 // Empty
4963 }
4964 theme.applyStyle(resid, false);
4965 }
Winson073a5262016-02-29 16:04:40 -08004966
4967 // Get the primary color and update the TaskDescription for this activity
4968 TypedArray a = theme.obtainStyledAttributes(
4969 com.android.internal.R.styleable.ActivityTaskDescription);
4970 if (mTaskDescription.getPrimaryColor() == 0) {
4971 int colorPrimary = a.getColor(
4972 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4973 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4974 mTaskDescription.setPrimaryColor(colorPrimary);
4975 }
4976 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004977
4978 int colorBackground = a.getColor(
4979 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4980 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4981 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004982 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004983
4984 final int statusBarColor = a.getColor(
4985 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4986 if (statusBarColor != 0) {
4987 mTaskDescription.setStatusBarColor(statusBarColor);
4988 }
4989
4990 final int navigationBarColor = a.getColor(
4991 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4992 if (navigationBarColor != 0) {
4993 mTaskDescription.setNavigationBarColor(navigationBarColor);
4994 }
4995
Adrian Roos4c864592019-04-10 14:47:57 +02004996 final int targetSdk = getApplicationInfo().targetSdkVersion;
4997 final boolean targetPreQ = targetSdk < Build.VERSION_CODES.Q;
4998 if (!targetPreQ) {
4999 mTaskDescription.setEnsureStatusBarContrastWhenTransparent(a.getBoolean(
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005000 R.styleable.ActivityTaskDescription_enforceStatusBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005001 false));
5002 mTaskDescription.setEnsureNavigationBarContrastWhenTransparent(a.getBoolean(
Adrian Roos6eb1ed32019-04-08 16:23:43 +02005003 R.styleable
Adrian Roos8c7ca4f2019-04-17 11:35:22 +02005004 .ActivityTaskDescription_enforceNavigationBarContrast,
Adrian Roos4c864592019-04-10 14:47:57 +02005005 true));
5006 }
5007
Winson073a5262016-02-29 16:04:40 -08005008 a.recycle();
5009 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00005010 }
5011
5012 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08005013 * Requests permissions to be granted to this application. These permissions
5014 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005015 * and they should have protection level {@link
5016 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
5017 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08005018 * <p>
5019 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
5020 * are granted at install time if requested in the manifest. Signature permissions
5021 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
5022 * install time if requested in the manifest and the signature of your app matches
5023 * the signature of the app declaring the permissions.
5024 * </p>
5025 * <p>
5026 * If your app does not have the requested permissions the user will be presented
5027 * with UI for accepting them. After the user has accepted or rejected the
5028 * requested permissions you will receive a callback on {@link
5029 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
5030 * permissions were granted or not.
5031 * </p>
5032 * <p>
5033 * Note that requesting a permission does not guarantee it will be granted and
5034 * your app should be able to run without having this permission.
5035 * </p>
5036 * <p>
5037 * This method may start an activity allowing the user to choose which permissions
5038 * to grant and which to reject. Hence, you should be prepared that your activity
5039 * may be paused and resumed. Further, granting some permissions may require
5040 * a restart of you application. In such a case, the system will recreate the
5041 * activity stack before delivering the result to {@link
5042 * #onRequestPermissionsResult(int, String[], int[])}.
5043 * </p>
5044 * <p>
5045 * When checking whether you have a permission you should use {@link
5046 * #checkSelfPermission(String)}.
5047 * </p>
5048 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07005049 * Calling this API for permissions already granted to your app would show UI
5050 * to the user to decide whether the app can still hold these permissions. This
5051 * can be useful if the way your app uses data guarded by the permissions
5052 * changes significantly.
5053 * </p>
5054 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07005055 * You cannot request a permission if your activity sets {@link
5056 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5057 * <code>true</code> because in this case the activity would not receive
5058 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
5059 * </p>
5060 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01005061 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07005062 * RuntimePermissions</a> sample app demonstrates how to use this method to
5063 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08005064 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005065 *
Svet Ganovf66381c2016-02-18 20:02:36 -08005066 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08005067 * @param requestCode Application specific request code to match with a result
5068 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07005069 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08005070 *
Paul Miller77a0e132016-02-11 13:46:49 -08005071 * @throws IllegalArgumentException if requestCode is negative.
5072 *
Svetoslavc6d1c342015-02-26 14:44:43 -08005073 * @see #onRequestPermissionsResult(int, String[], int[])
5074 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07005075 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08005076 */
5077 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08005078 if (requestCode < 0) {
5079 throw new IllegalArgumentException("requestCode should be >= 0");
5080 }
Svetoslavffb32b12015-10-15 16:54:00 -07005081 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00005082 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07005083 // Dispatch the callback with empty arrays which means a cancellation.
5084 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
5085 return;
5086 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005087 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07005088 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07005089 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08005090 }
5091
5092 /**
5093 * Callback for the result from requesting permissions. This method
5094 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07005095 * <p>
5096 * <strong>Note:</strong> It is possible that the permissions request interaction
5097 * with the user is interrupted. In this case you will receive empty permissions
5098 * and results arrays which should be treated as a cancellation.
5099 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08005100 *
5101 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
5102 * @param permissions The requested permissions. Never null.
5103 * @param grantResults The grant results for the corresponding permissions
5104 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
5105 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
5106 *
5107 * @see #requestPermissions(String[], int)
5108 */
5109 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
5110 @NonNull int[] grantResults) {
5111 /* callback - no nothing */
5112 }
5113
5114 /**
Svetoslav20770dd2015-05-29 15:43:04 -07005115 * Gets whether you should show UI with rationale for requesting a permission.
5116 * You should do this only if you do not have the permission and the context in
5117 * which the permission is requested does not clearly communicate to the user
5118 * what would be the benefit from granting this permission.
5119 * <p>
5120 * For example, if you write a camera app, requesting the camera permission
5121 * would be expected by the user and no rationale for why it is requested is
5122 * needed. If however, the app needs location for tagging photos then a non-tech
5123 * savvy user may wonder how location is related to taking photos. In this case
5124 * you may choose to show UI with rationale of requesting this permission.
5125 * </p>
5126 *
5127 * @param permission A permission your app wants to request.
5128 * @return Whether you can show permission rationale UI.
5129 *
5130 * @see #checkSelfPermission(String)
5131 * @see #requestPermissions(String[], int)
5132 * @see #onRequestPermissionsResult(int, String[], int[])
5133 */
Svetoslav9ce9a152015-06-02 17:59:31 -07005134 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07005135 return getPackageManager().shouldShowRequestPermissionRationale(permission);
5136 }
5137
5138 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005139 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
5140 * with no options.
5141 *
5142 * @param intent The intent to start.
5143 * @param requestCode If >= 0, this code will be returned in
5144 * onActivityResult() when the activity exits.
5145 *
5146 * @throws android.content.ActivityNotFoundException
5147 *
George Mount0a778ed2013-12-13 13:35:36 -08005148 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07005149 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005150 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005151 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005152 }
5153
5154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 * Launch an activity for which you would like a result when it finished.
5156 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08005157 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07005158 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07005160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005161 * <p>Note that this method should only be used with Intent protocols
5162 * that are defined to return a result. In other protocols (such as
5163 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
5164 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05005165 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
5166 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005167 *
RoboErik55011652014-07-09 15:05:53 -07005168 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08005170 * activity, then your window will not be displayed until a result is
5171 * returned back from the started activity. This is to avoid visible
5172 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 * <p>This method throws {@link android.content.ActivityNotFoundException}
5175 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 * @param intent The intent to start.
5178 * @param requestCode If >= 0, this code will be returned in
5179 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005180 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005181 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005182 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005185 *
George Mount0a778ed2013-12-13 13:35:36 -08005186 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005188 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
5189 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07005191 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 Instrumentation.ActivityResult ar =
5193 mInstrumentation.execStartActivity(
5194 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005195 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 if (ar != null) {
5197 mMainThread.sendActivityResult(
5198 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
5199 ar.getResultData());
5200 }
5201 if (requestCode >= 0) {
5202 // If this start is requesting a result, we can avoid making
5203 // the activity visible until the result is received. Setting
5204 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5205 // activity hidden during this time, to avoid flickering.
5206 // This can only be done when a result is requested because
5207 // that guarantees we will get information back when the
5208 // activity is finished, no matter what happens to it.
5209 mStartedActivity = true;
5210 }
Adam Powell14874662013-07-18 19:42:41 -07005211
George Mount41725de2015-04-09 08:23:05 -07005212 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005213 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005215 if (options != null) {
5216 mParent.startActivityFromChild(this, intent, requestCode, options);
5217 } else {
5218 // Note we want to go through this method for compatibility with
5219 // existing applications that may have overridden it.
5220 mParent.startActivityFromChild(this, intent, requestCode);
5221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 }
George Mount41725de2015-04-09 08:23:05 -07005223 }
5224
5225 /**
5226 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5227 *
5228 * @param options The ActivityOptions bundle used to start an Activity.
5229 */
5230 private void cancelInputsAndStartExitTransition(Bundle options) {
5231 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5232 if (decor != null) {
5233 decor.cancelPendingInputEvents();
5234 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005235 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005236 mActivityTransitionState.startExitOutTransition(this, options);
5237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 }
5239
Chet Haaseb64e777f2016-12-09 07:17:10 -08005240 /**
5241 * Returns whether there are any activity transitions currently running on this
5242 * activity. A return value of {@code true} can mean that either an enter or
5243 * exit transition is running, including whether the background of the activity
5244 * is animating as a part of that transition.
5245 *
5246 * @return true if a transition is currently running on this activity, false otherwise.
5247 */
5248 public boolean isActivityTransitionRunning() {
5249 return mActivityTransitionState.isTransitionRunning();
5250 }
5251
George Mount413739e2016-06-08 07:13:37 -07005252 private Bundle transferSpringboardActivityOptions(Bundle options) {
5253 if (options == null && (mWindow != null && !mWindow.isActive())) {
5254 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005255 if (activityOptions != null &&
5256 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005257 return activityOptions.toBundle();
5258 }
5259 }
5260 return options;
5261 }
5262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005264 * @hide Implement to provide correct calling token.
5265 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005266 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005267 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5268 startActivityForResultAsUser(intent, requestCode, null, user);
5269 }
5270
5271 /**
5272 * @hide Implement to provide correct calling token.
5273 */
5274 public void startActivityForResultAsUser(Intent intent, int requestCode,
5275 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005276 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5277 }
5278
5279 /**
5280 * @hide Implement to provide correct calling token.
5281 */
5282 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5283 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005284 if (mParent != null) {
5285 throw new RuntimeException("Can't be called from a child");
5286 }
George Mount413739e2016-06-08 07:13:37 -07005287 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005288 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005289 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005290 options, user);
5291 if (ar != null) {
5292 mMainThread.sendActivityResult(
5293 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5294 }
5295 if (requestCode >= 0) {
5296 // If this start is requesting a result, we can avoid making
5297 // the activity visible until the result is received. Setting
5298 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5299 // activity hidden during this time, to avoid flickering.
5300 // This can only be done when a result is requested because
5301 // that guarantees we will get information back when the
5302 // activity is finished, no matter what happens to it.
5303 mStartedActivity = true;
5304 }
5305
George Mount41725de2015-04-09 08:23:05 -07005306 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005307 }
5308
5309 /**
5310 * @hide Implement to provide correct calling token.
5311 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005312 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005313 public void startActivityAsUser(Intent intent, UserHandle user) {
5314 startActivityAsUser(intent, null, user);
5315 }
5316
5317 /**
5318 * @hide Implement to provide correct calling token.
5319 */
5320 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5321 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005322 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005323 }
George Mount413739e2016-06-08 07:13:37 -07005324 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005325 Instrumentation.ActivityResult ar =
5326 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005327 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005328 intent, -1, options, user);
5329 if (ar != null) {
5330 mMainThread.sendActivityResult(
5331 mToken, mEmbeddedID, -1, ar.getResultCode(),
5332 ar.getResultData());
5333 }
George Mount41725de2015-04-09 08:23:05 -07005334 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005335 }
5336
5337 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005338 * Start a new activity as if it was started by the activity that started our
5339 * current activity. This is for the resolver and chooser activities, which operate
5340 * as intermediaries that dispatch their intent to the target the user selects -- to
5341 * do this, they must perform all security checks including permission grants as if
5342 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005343 * @param intent The Intent to start.
5344 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005345 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005346 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5347 * caller it is doing the start is, is actually allowed to start the target activity.
5348 * If you set this to true, you must set an explicit component in the Intent and do any
5349 * appropriate security checks yourself.
5350 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005351 * @hide
5352 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005353 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005354 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005355 if (mParent != null) {
5356 throw new RuntimeException("Can't be called from a child");
5357 }
George Mount413739e2016-06-08 07:13:37 -07005358 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005359 Instrumentation.ActivityResult ar =
5360 mInstrumentation.execStartActivityAsCaller(
5361 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005362 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005363 if (ar != null) {
5364 mMainThread.sendActivityResult(
5365 mToken, mEmbeddedID, -1, ar.getResultCode(),
5366 ar.getResultData());
5367 }
George Mount41725de2015-04-09 08:23:05 -07005368 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005369 }
5370
5371 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005372 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5373 * Intent, int, int, int, Bundle)} with no options.
5374 *
5375 * @param intent The IntentSender to launch.
5376 * @param requestCode If >= 0, this code will be returned in
5377 * onActivityResult() when the activity exits.
5378 * @param fillInIntent If non-null, this will be provided as the
5379 * intent parameter to {@link IntentSender#sendIntent}.
5380 * @param flagsMask Intent flags in the original IntentSender that you
5381 * would like to change.
5382 * @param flagsValues Desired values for any bits set in
5383 * <var>flagsMask</var>
5384 * @param extraFlags Always set to 0.
5385 */
5386 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005387 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005388 throws IntentSender.SendIntentException {
5389 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5390 flagsValues, extraFlags, null);
5391 }
5392
5393 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005394 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005395 * to use a IntentSender to describe the activity to be started. If
5396 * the IntentSender is for an activity, that activity will be started
5397 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5398 * here; otherwise, its associated action will be executed (such as
5399 * sending a broadcast) as if you had called
5400 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005401 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005402 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005403 * @param requestCode If >= 0, this code will be returned in
5404 * onActivityResult() when the activity exits.
5405 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005406 * intent parameter to {@link IntentSender#sendIntent}.
5407 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005408 * would like to change.
5409 * @param flagsValues Desired values for any bits set in
5410 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005411 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005412 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005413 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005414 * Context.startActivity(Intent, Bundle)} for more details. If options
5415 * have also been supplied by the IntentSender, options given here will
5416 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005417 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005418 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005419 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005420 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005421 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005422 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5423 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005424 } else if (options != null) {
5425 mParent.startIntentSenderFromChild(this, intent, requestCode,
5426 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005427 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005428 // Note we want to go through this call for compatibility with
5429 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005430 mParent.startIntentSenderFromChild(this, intent, requestCode,
5431 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005432 }
5433 }
5434
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005435 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5436 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005437 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005438 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005439 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005440 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005441 String resolvedType = null;
5442 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005443 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005444 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005445 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5446 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005447 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005448 .startActivityIntentSender(mMainThread.getApplicationThread(),
5449 intent != null ? intent.getTarget() : null,
5450 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005451 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005452 requestCode, flagsMask, flagsValues, options);
5453 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005454 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005455 }
5456 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005457
5458 if (options != null) {
5459 // Only when the options are not null, as the intent can point to something other
5460 // than an Activity.
5461 cancelInputsAndStartExitTransition(options);
5462 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005463 } catch (RemoteException e) {
5464 }
5465 if (requestCode >= 0) {
5466 // If this start is requesting a result, we can avoid making
5467 // the activity visible until the result is received. Setting
5468 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5469 // activity hidden during this time, to avoid flickering.
5470 // This can only be done when a result is requested because
5471 // that guarantees we will get information back when the
5472 // activity is finished, no matter what happens to it.
5473 mStartedActivity = true;
5474 }
5475 }
5476
5477 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005478 * Same as {@link #startActivity(Intent, Bundle)} with no options
5479 * specified.
5480 *
5481 * @param intent The intent to start.
5482 *
5483 * @throws android.content.ActivityNotFoundException
5484 *
Mark Lufa434852016-08-11 17:40:33 -07005485 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005486 * @see #startActivityForResult
5487 */
5488 @Override
5489 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005490 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005491 }
5492
5493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 * Launch a new activity. You will not receive any information about when
5495 * the activity exits. This implementation overrides the base version,
5496 * providing information about
5497 * the activity performing the launch. Because of this additional
5498 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5499 * required; if not specified, the new activity will be added to the
5500 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005501 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 * <p>This method throws {@link android.content.ActivityNotFoundException}
5503 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005504 *
5505 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005506 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005507 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005508 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005509 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005511 *
Mark Lufa434852016-08-11 17:40:33 -07005512 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005513 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 */
5515 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005516 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005517 if (options != null) {
5518 startActivityForResult(intent, -1, options);
5519 } else {
5520 // Note we want to go through this call for compatibility with
5521 // applications that may have overridden the method.
5522 startActivityForResult(intent, -1);
5523 }
5524 }
5525
5526 /**
5527 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5528 * specified.
5529 *
5530 * @param intents The intents to start.
5531 *
5532 * @throws android.content.ActivityNotFoundException
5533 *
Mark Lufa434852016-08-11 17:40:33 -07005534 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005535 * @see #startActivityForResult
5536 */
5537 @Override
5538 public void startActivities(Intent[] intents) {
5539 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 }
5541
5542 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005543 * Launch a new activity. You will not receive any information about when
5544 * the activity exits. This implementation overrides the base version,
5545 * providing information about
5546 * the activity performing the launch. Because of this additional
5547 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5548 * required; if not specified, the new activity will be added to the
5549 * task of the caller.
5550 *
5551 * <p>This method throws {@link android.content.ActivityNotFoundException}
5552 * if there was no Activity found to run the given Intent.
5553 *
5554 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005555 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005556 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005557 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005558 *
5559 * @throws android.content.ActivityNotFoundException
5560 *
Mark Lufa434852016-08-11 17:40:33 -07005561 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005562 * @see #startActivityForResult
5563 */
5564 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005565 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005566 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005567 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005568 }
5569
5570 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005571 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5572 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005573 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005574 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005575 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005576 * intent parameter to {@link IntentSender#sendIntent}.
5577 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005578 * would like to change.
5579 * @param flagsValues Desired values for any bits set in
5580 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005581 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005582 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005583 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005584 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005585 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005586 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5587 extraFlags, null);
5588 }
5589
5590 /**
5591 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5592 * to start; see
5593 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5594 * for more information.
5595 *
5596 * @param intent The IntentSender to launch.
5597 * @param fillInIntent If non-null, this will be provided as the
5598 * intent parameter to {@link IntentSender#sendIntent}.
5599 * @param flagsMask Intent flags in the original IntentSender that you
5600 * would like to change.
5601 * @param flagsValues Desired values for any bits set in
5602 * <var>flagsMask</var>
5603 * @param extraFlags Always set to 0.
5604 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005605 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005606 * Context.startActivity(Intent, Bundle)} for more details. If options
5607 * have also been supplied by the IntentSender, options given here will
5608 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005609 */
5610 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005611 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005612 Bundle options) throws IntentSender.SendIntentException {
5613 if (options != null) {
5614 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5615 flagsValues, extraFlags, options);
5616 } else {
5617 // Note we want to go through this call for compatibility with
5618 // applications that may have overridden the method.
5619 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5620 flagsValues, extraFlags);
5621 }
5622 }
5623
5624 /**
5625 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5626 * with no options.
5627 *
5628 * @param intent The intent to start.
5629 * @param requestCode If >= 0, this code will be returned in
5630 * onActivityResult() when the activity exits, as described in
5631 * {@link #startActivityForResult}.
5632 *
5633 * @return If a new activity was launched then true is returned; otherwise
5634 * false is returned and you must handle the Intent yourself.
5635 *
5636 * @see #startActivity
5637 * @see #startActivityForResult
5638 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005639 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5640 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005641 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005642 }
5643
5644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 * A special variation to launch an activity only if a new activity
5646 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005647 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005649 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005651 * and the activity
5652 * that handles <var>intent</var> is the same as your currently running
5653 * activity, then a new instance is not needed. In this case, instead of
5654 * the normal behavior of calling {@link #onNewIntent} this function will
5655 * return and you can handle the Intent yourself.
5656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 * <p>This function can only be called from a top-level activity; if it is
5658 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 * @param intent The intent to start.
5661 * @param requestCode If >= 0, this code will be returned in
5662 * onActivityResult() when the activity exits, as described in
5663 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005664 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005665 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005666 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 * @return If a new activity was launched then true is returned; otherwise
5669 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005670 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 * @see #startActivity
5672 * @see #startActivityForResult
5673 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005674 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5675 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005677 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005679 Uri referrer = onProvideReferrer();
5680 if (referrer != null) {
5681 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5682 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005683 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005684 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005685 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005686 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005687 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5688 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5689 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 } catch (RemoteException e) {
5691 // Empty
5692 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 if (requestCode >= 0) {
5697 // If this start is requesting a result, we can avoid making
5698 // the activity visible until the result is received. Setting
5699 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5700 // activity hidden during this time, to avoid flickering.
5701 // This can only be done when a result is requested because
5702 // that guarantees we will get information back when the
5703 // activity is finished, no matter what happens to it.
5704 mStartedActivity = true;
5705 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005706 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 }
5708
5709 throw new UnsupportedOperationException(
5710 "startActivityIfNeeded can only be called from a top-level activity");
5711 }
5712
5713 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005714 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5715 * no options.
5716 *
5717 * @param intent The intent to dispatch to the next activity. For
5718 * correct behavior, this must be the same as the Intent that started
5719 * your own activity; the only changes you can make are to the extras
5720 * inside of it.
5721 *
5722 * @return Returns a boolean indicating whether there was another Activity
5723 * to start: true if there was a next activity to start, false if there
5724 * wasn't. In general, if true is returned you will then want to call
5725 * finish() on yourself.
5726 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005727 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005728 return startNextMatchingActivity(intent, null);
5729 }
5730
5731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 * Special version of starting an activity, for use when you are replacing
5733 * other activity components. You can use this to hand the Intent off
5734 * to the next Activity that can handle it. You typically call this in
5735 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 * @param intent The intent to dispatch to the next activity. For
5738 * correct behavior, this must be the same as the Intent that started
5739 * your own activity; the only changes you can make are to the extras
5740 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005741 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005742 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005743 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 * @return Returns a boolean indicating whether there was another Activity
5746 * to start: true if there was a next activity to start, false if there
5747 * wasn't. In general, if true is returned you will then want to call
5748 * finish() on yourself.
5749 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005750 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5751 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 if (mParent == null) {
5753 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005754 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005755 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005756 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005757 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 } catch (RemoteException e) {
5759 // Empty
5760 }
5761 return false;
5762 }
5763
5764 throw new UnsupportedOperationException(
5765 "startNextMatchingActivity can only be called from a top-level activity");
5766 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005767
5768 /**
5769 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5770 * with no options.
5771 *
5772 * @param child The activity making the call.
5773 * @param intent The intent to start.
5774 * @param requestCode Reply request code. < 0 if reply is not requested.
5775 *
5776 * @throws android.content.ActivityNotFoundException
5777 *
5778 * @see #startActivity
5779 * @see #startActivityForResult
5780 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005781 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005782 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005783 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005784 }
5785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 /**
RoboErik55011652014-07-09 15:05:53 -07005787 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 * <p>This method throws {@link android.content.ActivityNotFoundException}
5791 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005792 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 * @param child The activity making the call.
5794 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005795 * @param requestCode Reply request code. < 0 if reply is not requested.
5796 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005797 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005798 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005800 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005801 *
5802 * @see #startActivity
5803 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005805 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005806 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005807 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 Instrumentation.ActivityResult ar =
5809 mInstrumentation.execStartActivity(
5810 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005811 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 if (ar != null) {
5813 mMainThread.sendActivityResult(
5814 mToken, child.mEmbeddedID, requestCode,
5815 ar.getResultCode(), ar.getResultData());
5816 }
George Mount41725de2015-04-09 08:23:05 -07005817 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 }
5819
5820 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005821 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5822 * with no options.
5823 *
5824 * @param fragment The fragment making the call.
5825 * @param intent The intent to start.
5826 * @param requestCode Reply request code. < 0 if reply is not requested.
5827 *
5828 * @throws android.content.ActivityNotFoundException
5829 *
5830 * @see Fragment#startActivity
5831 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005832 *
5833 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5834 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005835 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005836 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005837 public void startActivityFromFragment(@NonNull Fragment fragment,
5838 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005839 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005840 }
5841
5842 /**
RoboErik55011652014-07-09 15:05:53 -07005843 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005844 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5845 * method.
RoboErik55011652014-07-09 15:05:53 -07005846 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005847 * <p>This method throws {@link android.content.ActivityNotFoundException}
5848 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005849 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005850 * @param fragment The fragment making the call.
5851 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005852 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005853 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005854 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005855 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005856 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005857 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005858 *
5859 * @see Fragment#startActivity
5860 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005861 *
5862 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5863 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005864 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005865 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005866 public void startActivityFromFragment(@NonNull Fragment fragment,
5867 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005868 startActivityForResult(fragment.mWho, intent, requestCode, options);
5869 }
5870
5871 /**
5872 * @hide
5873 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005874 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5875 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5876 UserHandle user) {
5877 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5878 }
5879
5880 /**
5881 * @hide
5882 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005883 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005884 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005885 public void startActivityForResult(
5886 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005887 Uri referrer = onProvideReferrer();
5888 if (referrer != null) {
5889 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5890 }
George Mount413739e2016-06-08 07:13:37 -07005891 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005892 Instrumentation.ActivityResult ar =
5893 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005894 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005895 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005896 if (ar != null) {
5897 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005898 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005899 ar.getResultCode(), ar.getResultData());
5900 }
George Mount41725de2015-04-09 08:23:05 -07005901 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005902 }
5903
5904 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005905 * @hide
5906 */
5907 @Override
5908 public boolean canStartActivityForResult() {
5909 return true;
5910 }
5911
5912 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005913 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5914 * int, Intent, int, int, int, Bundle)} with no options.
5915 */
5916 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5917 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5918 int extraFlags)
5919 throws IntentSender.SendIntentException {
5920 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5921 flagsMask, flagsValues, extraFlags, null);
5922 }
5923
5924 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005925 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005926 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005927 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005928 * for more information.
5929 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005930 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5931 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005932 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005933 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005934 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5935 flagsMask, flagsValues, options);
5936 }
5937
5938 /**
5939 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5940 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5941 * for more information.
5942 *
5943 * @hide
5944 */
5945 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5946 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5947 int extraFlags, @Nullable Bundle options)
5948 throws IntentSender.SendIntentException {
5949 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5950 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005951 }
5952
5953 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005954 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5955 * or {@link #finish} to specify an explicit transition animation to
5956 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005957 *
5958 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5959 * to using this with starting activities is to supply the desired animation
5960 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005961 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005962 * you to specify a custom animation even when starting an activity from
5963 * outside the context of the current top activity.
5964 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005965 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005966 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005967 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005968 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005969 */
5970 public void overridePendingTransition(int enterAnim, int exitAnim) {
5971 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005972 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005973 mToken, getPackageName(), enterAnim, exitAnim);
5974 } catch (RemoteException e) {
5975 }
5976 }
RoboErik55011652014-07-09 15:05:53 -07005977
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 * Call this to set the result that your activity will return to its
5980 * caller.
RoboErik55011652014-07-09 15:05:53 -07005981 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 * @param resultCode The result code to propagate back to the originating
5983 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005984 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 * @see #RESULT_CANCELED
5986 * @see #RESULT_OK
5987 * @see #RESULT_FIRST_USER
5988 * @see #setResult(int, Intent)
5989 */
5990 public final void setResult(int resultCode) {
5991 synchronized (this) {
5992 mResultCode = resultCode;
5993 mResultData = null;
5994 }
5995 }
5996
5997 /**
5998 * Call this to set the result that your activity will return to its
5999 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08006000 *
6001 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
6002 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
6003 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
6004 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
6005 * Activity receiving the result access to the specific URIs in the Intent.
6006 * Access will remain until the Activity has finished (it will remain across the hosting
6007 * process being killed and other temporary destruction) and will be added
6008 * to any existing set of URI permissions it already holds.
6009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006010 * @param resultCode The result code to propagate back to the originating
6011 * activity, often RESULT_CANCELED or RESULT_OK
6012 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07006013 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 * @see #RESULT_CANCELED
6015 * @see #RESULT_OK
6016 * @see #RESULT_FIRST_USER
6017 * @see #setResult(int)
6018 */
6019 public final void setResult(int resultCode, Intent data) {
6020 synchronized (this) {
6021 mResultCode = resultCode;
6022 mResultData = data;
6023 }
6024 }
6025
6026 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006027 * Return information about who launched this activity. If the launching Intent
6028 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
6029 * that will be returned as-is; otherwise, if known, an
6030 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
6031 * package name that started the Intent will be returned. This may return null if no
6032 * referrer can be identified -- it is neither explicitly specified, nor is it known which
6033 * application package was involved.
6034 *
6035 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
6036 * return the referrer that submitted that new intent to the activity. Otherwise, it
6037 * always returns the referrer of the original Intent.</p>
6038 *
6039 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
6040 * referrer information, applications can spoof it.</p>
6041 */
6042 @Nullable
6043 public Uri getReferrer() {
6044 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07006045 try {
6046 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
6047 if (referrer != null) {
6048 return referrer;
6049 }
6050 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
6051 if (referrerName != null) {
6052 return Uri.parse(referrerName);
6053 }
6054 } catch (BadParcelableException e) {
6055 Log.w(TAG, "Cannot read referrer from intent;"
6056 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006057 }
6058 if (mReferrer != null) {
6059 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
6060 }
6061 return null;
6062 }
6063
6064 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07006065 * Override to generate the desired referrer for the content currently being shown
6066 * by the app. The default implementation returns null, meaning the referrer will simply
6067 * be the android-app: of the package name of this activity. Return a non-null Uri to
6068 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
6069 */
6070 public Uri onProvideReferrer() {
6071 return null;
6072 }
6073
6074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 * Return the name of the package that invoked this activity. This is who
6076 * the data in {@link #setResult setResult()} will be sent to. You can
6077 * use this information to validate that the recipient is allowed to
6078 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006079 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006080 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006081 * did not use the {@link #startActivityForResult}
6082 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07006083 * null.</p>
6084 *
6085 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
6086 * the result from this method was unstable. If the process hosting the calling
6087 * package was no longer running, it would return null instead of the proper package
6088 * name. You can use {@link #getCallingActivity()} and retrieve the package name
6089 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07006090 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 * @return The package of the activity that will receive your
6092 * reply, or null if none.
6093 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006094 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 public String getCallingPackage() {
6096 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006097 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 } catch (RemoteException e) {
6099 return null;
6100 }
6101 }
6102
6103 /**
6104 * Return the name of the activity that invoked this activity. This is
6105 * who the data in {@link #setResult setResult()} will be sent to. You
6106 * can use this information to validate that the recipient is allowed to
6107 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07006108 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006109 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07006110 * did not use the {@link #startActivityForResult}
6111 * form that includes a request code), then the calling package will be
6112 * null.
6113 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07006114 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 * reply, or null if none.
6116 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006117 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 public ComponentName getCallingActivity() {
6119 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006120 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 } catch (RemoteException e) {
6122 return null;
6123 }
6124 }
6125
6126 /**
6127 * Control whether this activity's main window is visible. This is intended
6128 * only for the special case of an activity that is not going to show a
6129 * UI itself, but can't just finish prior to onResume() because it needs
6130 * to wait for a service binding or such. Setting this to false allows
6131 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07006132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 * <p>The default value for this is taken from the
6134 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
6135 */
6136 public void setVisible(boolean visible) {
6137 if (mVisibleFromClient != visible) {
6138 mVisibleFromClient = visible;
6139 if (mVisibleFromServer) {
6140 if (visible) makeVisible();
6141 else mDecor.setVisibility(View.INVISIBLE);
6142 }
6143 }
6144 }
RoboErik55011652014-07-09 15:05:53 -07006145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 void makeVisible() {
6147 if (!mWindowAdded) {
6148 ViewManager wm = getWindowManager();
6149 wm.addView(mDecor, getWindow().getAttributes());
6150 mWindowAdded = true;
6151 }
6152 mDecor.setVisibility(View.VISIBLE);
6153 }
RoboErik55011652014-07-09 15:05:53 -07006154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 /**
6156 * Check to see whether this activity is in the process of finishing,
6157 * either because you called {@link #finish} on it or someone else
6158 * has requested that it finished. This is often used in
6159 * {@link #onPause} to determine whether the activity is simply pausing or
6160 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07006161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07006163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 * @see #finish
6165 */
6166 public boolean isFinishing() {
6167 return mFinished;
6168 }
6169
6170 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006171 * Returns true if the final {@link #onDestroy()} call has been made
6172 * on the Activity, so this instance is now dead.
6173 */
6174 public boolean isDestroyed() {
6175 return mDestroyed;
6176 }
6177
6178 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006179 * Check to see whether this activity is in the process of being destroyed in order to be
6180 * recreated with a new configuration. This is often used in
6181 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
6182 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07006183 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006184 * @return If the activity is being torn down in order to be recreated with a new configuration,
6185 * returns true; else returns false.
6186 */
6187 public boolean isChangingConfigurations() {
6188 return mChangingConfigurations;
6189 }
6190
6191 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006192 * Cause this Activity to be recreated with a new instance. This results
6193 * in essentially the same flow as when the Activity is created due to
6194 * a configuration change -- the current instance will go through its
6195 * lifecycle to {@link #onDestroy} and a new instance then created after it.
6196 */
6197 public void recreate() {
6198 if (mParent != null) {
6199 throw new IllegalStateException("Can only be called on top-level activity");
6200 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07006201 if (Looper.myLooper() != mMainThread.getLooper()) {
6202 throw new IllegalStateException("Must be called from main thread");
6203 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006204 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006205 }
6206
6207 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006208 * Finishes the current activity and specifies whether to remove the task associated with this
6209 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006210 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006211 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006212 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 if (mParent == null) {
6214 int resultCode;
6215 Intent resultData;
6216 synchronized (this) {
6217 resultCode = mResultCode;
6218 resultData = mResultData;
6219 }
Joe Onorato43a17652011-04-06 19:22:23 -07006220 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006222 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006223 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006224 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006225 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006226 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 mFinished = true;
6228 }
6229 } catch (RemoteException e) {
6230 // Empty
6231 }
6232 } else {
6233 mParent.finishFromChild(this);
6234 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006235
6236 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6237 // be restored now.
6238 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
6239 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6240 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
6241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 }
6243
6244 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006245 * Call this when your activity is done and should be closed. The
6246 * ActivityResult is propagated back to whoever launched you via
6247 * onActivityResult().
6248 */
6249 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006250 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006251 }
6252
6253 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006254 * Finish this activity as well as all activities immediately below it
6255 * in the current task that have the same affinity. This is typically
6256 * used when an application can be launched on to another task (such as
6257 * from an ACTION_VIEW of a content type it understands) and the user
6258 * has used the up navigation to switch out of the current task and in
6259 * to its own task. In this case, if the user has navigated down into
6260 * any other activities of the second application, all of those should
6261 * be removed from the original task as part of the task switch.
6262 *
6263 * <p>Note that this finish does <em>not</em> allow you to deliver results
6264 * to the previous activity, and an exception will be thrown if you are trying
6265 * to do so.</p>
6266 */
6267 public void finishAffinity() {
6268 if (mParent != null) {
6269 throw new IllegalStateException("Can not be called from an embedded activity");
6270 }
6271 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6272 throw new IllegalStateException("Can not be called to deliver a result");
6273 }
6274 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006275 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006276 mFinished = true;
6277 }
6278 } catch (RemoteException e) {
6279 // Empty
6280 }
6281 }
6282
6283 /**
RoboErik55011652014-07-09 15:05:53 -07006284 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 * {@link #finish} method. The default implementation simply calls
6286 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006287 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 * @see #finish
6291 */
6292 public void finishFromChild(Activity child) {
6293 finish();
6294 }
6295
6296 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006297 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6298 * to reverse its exit Transition. When the exit Transition completes,
6299 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6300 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006301 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006302 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006303 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006304 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006305 finish();
6306 }
George Mountcb4b7d92014-02-25 10:47:55 -08006307 }
6308
6309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 * Force finish another activity that you had previously started with
6311 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 * @param requestCode The request code of the activity that you had
6314 * given to startActivityForResult(). If there are multiple
6315 * activities started with this request code, they
6316 * will all be finished.
6317 */
6318 public void finishActivity(int requestCode) {
6319 if (mParent == null) {
6320 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006321 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6323 } catch (RemoteException e) {
6324 // Empty
6325 }
6326 } else {
6327 mParent.finishActivityFromChild(this, requestCode);
6328 }
6329 }
6330
6331 /**
6332 * This is called when a child activity of this one calls its
6333 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006334 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 * @param child The activity making the call.
6336 * @param requestCode Request code that had been used to start the
6337 * activity.
6338 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006339 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006341 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6343 } catch (RemoteException e) {
6344 // Empty
6345 }
6346 }
6347
6348 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006349 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006350 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006351 */
6352 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006353 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006354 }
6355
6356 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006357 * Ask that the local app instance of this activity be released to free up its memory.
6358 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6359 * a new instance of the activity will later be re-created if needed due to the user
6360 * navigating back to it.
6361 *
6362 * @return Returns true if the activity was in a state that it has started the process
6363 * of destroying its current instance; returns false if for any reason this could not
6364 * be done: it is currently visible to the user, it is already being destroyed, it is
6365 * being finished, it hasn't yet saved its state, etc.
6366 */
6367 public boolean releaseInstance() {
6368 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006369 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006370 } catch (RemoteException e) {
6371 // Empty
6372 }
6373 return false;
6374 }
6375
6376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 * Called when an activity you launched exits, giving you the requestCode
6378 * you started it with, the resultCode it returned, and any additional
6379 * data from it. The <var>resultCode</var> will be
6380 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6381 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 * <p>You will receive this call immediately before onResume() when your
6384 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006385 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006386 * <p>This method is never invoked if your activity sets
6387 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6388 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006389 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 * @param requestCode The integer request code originally supplied to
6391 * startActivityForResult(), allowing you to identify who this
6392 * result came from.
6393 * @param resultCode The integer result code returned by the child activity
6394 * through its setResult().
6395 * @param data An Intent, which can return result data to the caller
6396 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006397 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 * @see #startActivityForResult
6399 * @see #createPendingResult
6400 * @see #setResult(int)
6401 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006402 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 }
6404
6405 /**
George Mount62ab9b72014-05-02 13:51:17 -07006406 * Called when an activity you launched with an activity transition exposes this
6407 * Activity through a returning activity transition, giving you the resultCode
6408 * and any additional data from it. This method will only be called if the activity
6409 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006410 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006411 *
6412 * <p>The purpose of this function is to let the called Activity send a hint about
6413 * its state so that this underlying Activity can prepare to be exposed. A call to
6414 * this method does not guarantee that the called Activity has or will be exiting soon.
6415 * It only indicates that it will expose this Activity's Window and it has
6416 * some data to pass to prepare it.</p>
6417 *
6418 * @param resultCode The integer result code returned by the child activity
6419 * through its setResult().
6420 * @param data An Intent, which can return result data to the caller
6421 * (various data can be attached to Intent "extras").
6422 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006423 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006424 }
6425
6426 /**
RoboErik55011652014-07-09 15:05:53 -07006427 * Create a new PendingIntent object which you can hand to others
6428 * for them to use to send result data back to your
6429 * {@link #onActivityResult} callback. The created object will be either
6430 * one-shot (becoming invalid after a result is sent back) or multiple
6431 * (allowing any number of results to be sent through it).
6432 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 * @param requestCode Private request code for the sender that will be
6434 * associated with the result data when it is returned. The sender can not
6435 * modify this value, allowing you to identify incoming results.
6436 * @param data Default data to supply in the result, which may be modified
6437 * by the sender.
6438 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6439 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6440 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6441 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6442 * or any of the flags as supported by
6443 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6444 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 * @return Returns an existing or new PendingIntent matching the given
6447 * parameters. May return null only if
6448 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6449 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 * @see PendingIntent
6452 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006453 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6454 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 String packageName = getPackageName();
6456 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006457 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006459 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07006460 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07006462 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006463 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 return target != null ? new PendingIntent(target) : null;
6465 } catch (RemoteException e) {
6466 // Empty
6467 }
6468 return null;
6469 }
6470
6471 /**
6472 * Change the desired orientation of this activity. If the activity
6473 * is currently in the foreground or otherwise impacting the screen
6474 * orientation, the screen will immediately be changed (possibly causing
6475 * the activity to be restarted). Otherwise, this will be used the next
6476 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 * @param requestedOrientation An orientation constant as used in
6479 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6480 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006481 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 if (mParent == null) {
6483 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006484 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 mToken, requestedOrientation);
6486 } catch (RemoteException e) {
6487 // Empty
6488 }
6489 } else {
6490 mParent.setRequestedOrientation(requestedOrientation);
6491 }
6492 }
RoboErik55011652014-07-09 15:05:53 -07006493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 /**
6495 * Return the current requested orientation of the activity. This will
6496 * either be the orientation requested in its component's manifest, or
6497 * the last requested orientation given to
6498 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006499 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 * @return Returns an orientation constant as used in
6501 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6502 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006503 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 public int getRequestedOrientation() {
6505 if (mParent == null) {
6506 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006507 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 .getRequestedOrientation(mToken);
6509 } catch (RemoteException e) {
6510 // Empty
6511 }
6512 } else {
6513 return mParent.getRequestedOrientation();
6514 }
6515 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6516 }
RoboErik55011652014-07-09 15:05:53 -07006517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 /**
6519 * Return the identifier of the task this activity is in. This identifier
6520 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 * @return Task identifier, an opaque integer.
6523 */
6524 public int getTaskId() {
6525 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006526 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 } catch (RemoteException e) {
6528 return -1;
6529 }
6530 }
6531
6532 /**
6533 * Return whether this activity is the root of a task. The root is the
6534 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006535 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 * @return True if this is the root activity, else false.
6537 */
Winson Chung4d8681f2017-05-23 16:22:08 -07006538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 public boolean isTaskRoot() {
6540 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006541 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 } catch (RemoteException e) {
6543 return false;
6544 }
6545 }
6546
6547 /**
6548 * Move the task containing this activity to the back of the activity
6549 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 * @param nonRoot If false then this only works if the activity is the root
6552 * of a task; if true it will work for any activity in
6553 * a task.
RoboErik55011652014-07-09 15:05:53 -07006554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 * @return If the task was moved (or it was already at the
6556 * back) true is returned, else false.
6557 */
6558 public boolean moveTaskToBack(boolean nonRoot) {
6559 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006560 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 } catch (RemoteException e) {
6562 // Empty
6563 }
6564 return false;
6565 }
6566
6567 /**
6568 * Returns class name for this activity with the package prefix removed.
6569 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006570 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006571 * @return The local class name.
6572 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006573 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 public String getLocalClassName() {
6575 final String pkg = getPackageName();
6576 final String cls = mComponent.getClassName();
6577 int packageLen = pkg.length();
6578 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6579 || cls.charAt(packageLen) != '.') {
6580 return cls;
6581 }
6582 return cls.substring(packageLen+1);
6583 }
RoboErik55011652014-07-09 15:05:53 -07006584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006586 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 * @return Returns the complete component name for this activity
6589 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006590 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 return mComponent;
6592 }
6593
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006594 /** @hide */
6595 @Override
6596 public final ComponentName autofillClientGetComponentName() {
6597 return getComponentName();
6598 }
6599
Felipe Lemecbf7f262019-04-17 13:57:59 -07006600 /** @hide */
6601 @Override
6602 public final ComponentName contentCaptureClientGetComponentName() {
6603 return getComponentName();
6604 }
6605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 /**
6607 * Retrieve a {@link SharedPreferences} object for accessing preferences
6608 * that are private to this activity. This simply calls the underlying
6609 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6610 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006611 *
6612 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006613 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 *
6615 * @return Returns the single SharedPreferences instance that can be used
6616 * to retrieve and modify the preference values.
6617 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006618 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 return getSharedPreferences(getLocalClassName(), mode);
6620 }
RoboErik55011652014-07-09 15:05:53 -07006621
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006622 private void ensureSearchManager() {
6623 if (mSearchManager != null) {
6624 return;
6625 }
RoboErik55011652014-07-09 15:05:53 -07006626
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006627 try {
6628 mSearchManager = new SearchManager(this, null);
6629 } catch (ServiceNotFoundException e) {
6630 throw new IllegalStateException(e);
6631 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006632 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006635 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 if (getBaseContext() == null) {
6637 throw new IllegalStateException(
6638 "System services not available to Activities before onCreate()");
6639 }
6640
6641 if (WINDOW_SERVICE.equals(name)) {
6642 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006643 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006644 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006645 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 }
6647 return super.getSystemService(name);
6648 }
6649
6650 /**
6651 * Change the title associated with this activity. If this is a
6652 * top-level activity, the title for its window will change. If it
6653 * is an embedded activity, the parent can do whatever it wants
6654 * with it.
6655 */
6656 public void setTitle(CharSequence title) {
6657 mTitle = title;
6658 onTitleChanged(title, mTitleColor);
6659
6660 if (mParent != null) {
6661 mParent.onChildTitleChanged(this, title);
6662 }
6663 }
6664
6665 /**
6666 * Change the title associated with this activity. If this is a
6667 * top-level activity, the title for its window will change. If it
6668 * is an embedded activity, the parent can do whatever it wants
6669 * with it.
6670 */
6671 public void setTitle(int titleId) {
6672 setTitle(getText(titleId));
6673 }
6674
Alan Viverette2525d9c2013-11-15 14:42:19 -08006675 /**
6676 * Change the color of the title associated with this activity.
6677 * <p>
6678 * This method is deprecated starting in API Level 11 and replaced by action
6679 * bar styles. For information on styling the Action Bar, read the <a
6680 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6681 * guide.
6682 *
6683 * @deprecated Use action bar styles instead.
6684 */
6685 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 public void setTitleColor(int textColor) {
6687 mTitleColor = textColor;
6688 onTitleChanged(mTitle, textColor);
6689 }
6690
6691 public final CharSequence getTitle() {
6692 return mTitle;
6693 }
6694
6695 public final int getTitleColor() {
6696 return mTitleColor;
6697 }
6698
6699 protected void onTitleChanged(CharSequence title, int color) {
6700 if (mTitleReady) {
6701 final Window win = getWindow();
6702 if (win != null) {
6703 win.setTitle(title);
6704 if (color != 0) {
6705 win.setTitleColor(color);
6706 }
6707 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006708 if (mActionBar != null) {
6709 mActionBar.setWindowTitle(title);
6710 }
Adam Powella557fdc2014-08-21 18:05:53 -07006711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 }
6713
6714 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6715 }
6716
6717 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006718 * Sets information describing the task with this activity for presentation inside the Recents
6719 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6720 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6721 * for each property until a suitable value is found. For each task the taskDescription will be
6722 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006723 *
6724 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006725 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006726 *
Winson Chunga449dc02014-05-16 11:15:04 -07006727 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006728 */
Winson Chunga449dc02014-05-16 11:15:04 -07006729 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006730 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006731 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006732 // Scale the icon down to something reasonable if it is provided
6733 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6734 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6735 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6736 true);
6737 mTaskDescription.setIcon(icon);
6738 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006739 }
6740 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006741 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006742 } catch (RemoteException e) {
6743 }
6744 }
6745
6746 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 * Sets the visibility of the progress bar in the title.
6748 * <p>
6749 * In order for the progress bar to be shown, the feature must be requested
6750 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006751 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006753 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006755 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 public final void setProgressBarVisibility(boolean visible) {
6757 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6758 Window.PROGRESS_VISIBILITY_OFF);
6759 }
6760
6761 /**
6762 * Sets the visibility of the indeterminate progress bar in the title.
6763 * <p>
6764 * In order for the progress bar to be shown, the feature must be requested
6765 * via {@link #requestWindowFeature(int)}.
6766 *
6767 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006768 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006770 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6772 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6773 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6774 }
RoboErik55011652014-07-09 15:05:53 -07006775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 /**
6777 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6778 * is always indeterminate).
6779 * <p>
6780 * In order for the progress bar to be shown, the feature must be requested
6781 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006782 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006784 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006786 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 public final void setProgressBarIndeterminate(boolean indeterminate) {
6788 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006789 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6790 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 }
RoboErik55011652014-07-09 15:05:53 -07006792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 /**
6794 * Sets the progress for the progress bars in the title.
6795 * <p>
6796 * In order for the progress bar to be shown, the feature must be requested
6797 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006798 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 * @param progress The progress for the progress bar. Valid ranges are from
6800 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6801 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006802 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006804 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006805 public final void setProgress(int progress) {
6806 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6807 }
RoboErik55011652014-07-09 15:05:53 -07006808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 /**
6810 * Sets the secondary progress for the progress bar in the title. This
6811 * progress is drawn between the primary progress (set via
6812 * {@link #setProgress(int)} and the background. It can be ideal for media
6813 * scenarios such as showing the buffering progress while the default
6814 * progress shows the play progress.
6815 * <p>
6816 * In order for the progress bar to be shown, the feature must be requested
6817 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006819 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6820 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006821 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006823 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 public final void setSecondaryProgress(int secondaryProgress) {
6825 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6826 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6827 }
6828
6829 /**
6830 * Suggests an audio stream whose volume should be changed by the hardware
6831 * volume controls.
6832 * <p>
6833 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006834 * Volume requests which are received while the Activity is in the
6835 * foreground will affect this stream.
6836 * <p>
6837 * It is not guaranteed that the hardware volume controls will always change
6838 * this stream's volume (for example, if a call is in progress, its stream's
6839 * volume may be changed instead). To reset back to the default, use
6840 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6841 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006843 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 */
6845 public final void setVolumeControlStream(int streamType) {
6846 getWindow().setVolumeControlStream(streamType);
6847 }
6848
6849 /**
6850 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006851 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006852 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 * @return The suggested audio stream type whose volume should be changed by
6854 * the hardware volume controls.
6855 * @see #setVolumeControlStream(int)
6856 */
6857 public final int getVolumeControlStream() {
6858 return getWindow().getVolumeControlStream();
6859 }
RoboErik55011652014-07-09 15:05:53 -07006860
6861 /**
6862 * Sets a {@link MediaController} to send media keys and volume changes to.
6863 * <p>
6864 * The controller will be tied to the window of this Activity. Media key and
6865 * volume events which are received while the Activity is in the foreground
6866 * will be forwarded to the controller and used to invoke transport controls
6867 * or adjust the volume. This may be used instead of or in addition to
6868 * {@link #setVolumeControlStream} to affect a specific session instead of a
6869 * specific stream.
6870 * <p>
6871 * It is not guaranteed that the hardware volume controls will always change
6872 * this session's volume (for example, if a call is in progress, its
6873 * stream's volume may be changed instead). To reset back to the default use
6874 * null as the controller.
6875 *
6876 * @param controller The controller for the session which should receive
6877 * media keys and volume changes.
6878 */
6879 public final void setMediaController(MediaController controller) {
6880 getWindow().setMediaController(controller);
6881 }
6882
6883 /**
6884 * Gets the controller which should be receiving media key and volume events
6885 * while this activity is in the foreground.
6886 *
6887 * @return The controller which should receive events.
6888 * @see #setMediaController(android.media.session.MediaController)
6889 */
6890 public final MediaController getMediaController() {
6891 return getWindow().getMediaController();
6892 }
6893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 /**
6895 * Runs the specified action on the UI thread. If the current thread is the UI
6896 * thread, then the action is executed immediately. If the current thread is
6897 * not the UI thread, the action is posted to the event queue of the UI thread.
6898 *
6899 * @param action the action to run on the UI thread
6900 */
6901 public final void runOnUiThread(Runnable action) {
6902 if (Thread.currentThread() != mUiThread) {
6903 mHandler.post(action);
6904 } else {
6905 action.run();
6906 }
6907 }
6908
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006909 /** @hide */
6910 @Override
6911 public final void autofillClientRunOnUiThread(Runnable action) {
6912 runOnUiThread(action);
6913 }
6914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006916 * Standard implementation of
6917 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6918 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006919 * This implementation does nothing and is for
6920 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6921 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6922 *
6923 * @see android.view.LayoutInflater#createView
6924 * @see android.view.Window#getLayoutInflater
6925 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006926 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07006927 public View onCreateView(@NonNull String name, @NonNull Context context,
6928 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006929 return null;
6930 }
6931
6932 /**
6933 * Standard implementation of
6934 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6935 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006936 * This implementation handles <fragment> tags to embed fragments inside
6937 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 *
6939 * @see android.view.LayoutInflater#createView
6940 * @see android.view.Window#getLayoutInflater
6941 */
Ian Lake0c807f42018-09-12 14:03:27 -07006942 @Nullable
6943 public View onCreateView(@Nullable View parent, @NonNull String name,
6944 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006945 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006946 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006947 }
RoboErik55011652014-07-09 15:05:53 -07006948
Adam Powell371a8092014-06-20 12:51:12 -07006949 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 }
6951
Daniel Sandler69a48172010-06-23 16:29:36 -04006952 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006953 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006954 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006955 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006956 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006957 * @param fd The raw file descriptor that the dump is being sent to.
6958 * @param writer The PrintWriter to which you should dump your state. This will be
6959 * closed for you after you return.
6960 * @param args additional arguments to the dump request.
6961 */
Ian Lake140fe122018-09-13 14:59:38 -07006962 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
6963 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006964 dumpInner(prefix, fd, writer, args);
6965 }
6966
Ian Lake140fe122018-09-13 14:59:38 -07006967 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
6968 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08006969 if (args != null && args.length > 0) {
6970 // Handle special cases
6971 switch (args[0]) {
6972 case "--autofill":
6973 dumpAutofillManager(prefix, writer);
6974 return;
Felipe Lemeb718e032018-12-10 09:44:23 -08006975 case "--contentcapture":
6976 dumpContentCaptureManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006977 return;
6978 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07006979 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006980 writer.print(prefix); writer.print("Local Activity ");
6981 writer.print(Integer.toHexString(System.identityHashCode(this)));
6982 writer.println(" State:");
6983 String innerPrefix = prefix + " ";
6984 writer.print(innerPrefix); writer.print("mResumed=");
6985 writer.print(mResumed); writer.print(" mStopped=");
6986 writer.print(mStopped); writer.print(" mFinished=");
6987 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006988 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6989 writer.println(mChangingConfigurations);
6990 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6991 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006992
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006993 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6994 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006995 if (mVoiceInteractor != null) {
6996 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6997 }
Jeff Brown5182c782013-10-15 20:31:52 -07006998
Michael Wright5f48dc72013-11-01 12:42:49 -07006999 if (getWindow() != null &&
7000 getWindow().peekDecorView() != null &&
7001 getWindow().peekDecorView().getViewRootImpl() != null) {
7002 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
7003 }
Jeff Brown5182c782013-10-15 20:31:52 -07007004
7005 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07007006
Felipe Lemeb546ca72018-08-15 08:44:12 -07007007 dumpAutofillManager(prefix, writer);
Felipe Lemeb718e032018-12-10 09:44:23 -08007008 dumpContentCaptureManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07007009
7010 ResourcesManager.getInstance().dump(prefix, writer);
7011 }
7012
7013 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08007014 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07007015 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07007016 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08007017 writer.print(prefix); writer.print("Autofill Compat Mode: ");
7018 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07007019 } else {
7020 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07007021 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07007022 }
7023
Felipe Lemeb718e032018-12-10 09:44:23 -08007024 void dumpContentCaptureManager(String prefix, PrintWriter writer) {
7025 final ContentCaptureManager cm = getContentCaptureManager();
7026 if (cm != null) {
7027 cm.dump(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08007028 } else {
Felipe Lemeb718e032018-12-10 09:44:23 -08007029 writer.print(prefix); writer.println("No ContentCaptureManager");
Felipe Lemee348dc32018-11-05 12:35:29 -08007030 }
7031 }
7032
Dianne Hackborn625ac272010-09-17 18:29:22 -07007033 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007034 * Bit indicating that this activity is "immersive" and should not be
7035 * interrupted by notifications if possible.
7036 *
7037 * This value is initially set by the manifest property
7038 * <code>android:immersive</code> but may be changed at runtime by
7039 * {@link #setImmersive}.
7040 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007041 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04007042 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7043 */
7044 public boolean isImmersive() {
7045 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007046 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04007047 } catch (RemoteException e) {
7048 return false;
7049 }
7050 }
7051
7052 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07007053 * Indication of whether this is the highest level activity in this task. Can be used to
7054 * determine whether an activity launched by this activity was placed in the same task or
7055 * another task.
7056 *
7057 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07007058 */
Sunny Goyald85bed52018-09-25 12:01:01 -07007059 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07007060 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07007061 return false;
7062 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07007063 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007064 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07007065 } catch (RemoteException e) {
7066 return false;
7067 }
7068 }
7069
7070 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07007071 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
7072 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007073 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07007074 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007075 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
7076 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07007077 * This call has no effect on non-translucent activities or on activities with the
7078 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007079 *
George Mount62ab9b72014-05-02 13:51:17 -07007080 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7081 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007082 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007083 *
7084 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07007085 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007086 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007087 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07007088 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07007089 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007090 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007091 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
7092 }
Craig Mautner4addfc52013-06-25 08:05:45 -07007093 } catch (RemoteException e) {
7094 // pass
7095 }
7096 }
7097
7098 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007099 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
7100 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
7101 * <p>
7102 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
7103 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
7104 * be called indicating that it is safe to make this activity translucent again. Until
7105 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
7106 * behind the frontmost Activity will be indeterminate.
7107 * <p>
7108 * This call has no effect on non-translucent activities or on activities with the
7109 * {@link android.R.attr#windowIsFloating} attribute.
7110 *
7111 * @param callback the method to call when all visible Activities behind this one have been
7112 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07007113 * @param options activity options delivered to the activity below this one. The options
7114 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07007115 * @return <code>true</code> if Window was opaque and will become translucent or
7116 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07007117 *
7118 * @see #convertFromTranslucent()
7119 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07007120 *
7121 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007122 */
Jose Lima4b6c6692014-08-12 17:41:12 -07007123 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07007124 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07007125 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007126 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007127 try {
7128 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007129 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007130 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07007131 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07007132 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007133 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007134 // Make callback return as though it timed out.
7135 mChangeCanvasToTranslucent = false;
7136 drawComplete = false;
7137 }
7138 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
7139 // Window is already translucent.
7140 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07007141 }
George Mount3cc716c2014-06-12 16:35:35 -07007142 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007143 }
7144
7145 /** @hide */
7146 void onTranslucentConversionComplete(boolean drawComplete) {
7147 if (mTranslucentCallback != null) {
7148 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
7149 mTranslucentCallback = null;
7150 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007151 if (mChangeCanvasToTranslucent) {
7152 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
7153 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007154 }
7155
Craig Mautnereb8abf72014-07-02 15:04:09 -07007156 /** @hide */
7157 public void onNewActivityOptions(ActivityOptions options) {
7158 mActivityTransitionState.setEnterActivityOptions(this, options);
7159 if (!mStopped) {
7160 mActivityTransitionState.enterReady(this);
7161 }
7162 }
7163
Craig Mautner5eda9b32013-07-02 11:58:16 -07007164 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07007165 * Retrieve the ActivityOptions passed in from the launching activity or passed back
7166 * from an activity launched by this activity in its call to {@link
7167 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
7168 *
7169 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
7170 * @hide
7171 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007172 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07007173 ActivityOptions getActivityOptions() {
7174 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007175 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007176 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07007177 } catch (RemoteException e) {
7178 }
7179 return null;
7180 }
7181
7182 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007183 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07007184 * this method anytime between the start of {@link #onResume()} and the return from
7185 * {@link #onPause()}. If this call is successful then the activity will remain visible after
7186 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
7187 *
7188 * <p>The actions of this call are reset each time that this activity is brought to the
7189 * front. That is, every time {@link #onResume()} is called the activity will be assumed
7190 * to not have requested visible behind. Therefore, if you want this activity to continue to
7191 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007192 *
7193 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
7194 * for dialog and translucent activities.
7195 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007196 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
7197 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
7198 *
7199 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07007200 * the next call to onResume.
7201 *
Bryce Leed59629e2017-04-18 14:35:29 -07007202 * @deprecated This method's functionality is no longer supported as of
7203 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7204 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007205 * @param visible true to notify the system that the activity wishes to be visible behind other
7206 * translucent activities, false to indicate otherwise. Resources must be
7207 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07007208 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007209 * @return the resulting visibiity state. If true the activity will remain visible beyond
7210 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7211 * then the activity may not count on being visible behind other translucent activities,
7212 * and must stop any media playback and release resources.
7213 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7214 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007215 *
Jose Limafcf70832014-08-27 23:09:05 -07007216 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007217 */
Bryce Leed59629e2017-04-18 14:35:29 -07007218 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007219 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007220 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007221 }
7222
7223 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007224 * Called when a translucent activity over this activity is becoming opaque or another
7225 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007226 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007227 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007228 * <p>When this method is called the activity has 500 msec to release any resources it may be
7229 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007230 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007231 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007232 * process. Otherwise {@link #onStop()} will be called following return.
7233 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007234 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007235 *
7236 * @deprecated This method's functionality is no longer supported as of
7237 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007238 */
Bryce Leed59629e2017-04-18 14:35:29 -07007239 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007240 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007241 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007242 mCalled = true;
7243 }
7244
7245 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007246 * Translucent activities may call this to determine if there is an activity below them that
7247 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007248 *
Bryce Leed59629e2017-04-18 14:35:29 -07007249 * @deprecated This method's functionality is no longer supported as of
7250 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7251 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007252 * @return true if an activity below is set to visible according to the most recent call to
7253 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007254 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007255 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007256 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007257 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007258 * @hide
7259 */
Bryce Leed59629e2017-04-18 14:35:29 -07007260 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007261 @SystemApi
7262 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007263 return false;
7264 }
7265
7266 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007267 * The topmost foreground activity will receive this call when the background visibility state
7268 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007269 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007270 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007271 * due to a background activity finishing itself.
7272 *
Bryce Leed59629e2017-04-18 14:35:29 -07007273 * @deprecated This method's functionality is no longer supported as of
7274 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7275 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007276 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007277 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007278 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007279 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007280 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007281 */
Bryce Leed59629e2017-04-18 14:35:29 -07007282 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007283 @SystemApi
7284 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007285 }
7286
7287 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007288 * Activities cannot draw during the period that their windows are animating in. In order
7289 * to know when it is safe to begin drawing they can override this method which will be
7290 * called when the entering animation has completed.
7291 */
7292 public void onEnterAnimationComplete() {
7293 }
7294
7295 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007296 * @hide
7297 */
7298 public void dispatchEnterAnimationComplete() {
chaviwfeb2e1e2018-12-19 17:24:11 -08007299 mEnterAnimationComplete = true;
7300 mInstrumentation.onEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007301 onEnterAnimationComplete();
7302 if (getWindow() != null && getWindow().getDecorView() != null) {
Felipe Leme01297692019-01-29 18:16:23 -08007303 View decorView = getWindow().getDecorView();
7304 decorView.getViewTreeObserver().dispatchOnEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007305 }
7306 }
7307
7308 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007309 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007310 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007311 * Note that changing this value will have no effect on the activity's
7312 * {@link android.content.pm.ActivityInfo} structure; that is, if
7313 * <code>android:immersive</code> is set to <code>true</code>
7314 * in the application's manifest entry for this activity, the {@link
7315 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7316 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7317 * FLAG_IMMERSIVE} bit set.
7318 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007319 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007320 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7321 */
7322 public void setImmersive(boolean i) {
7323 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007324 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007325 } catch (RemoteException e) {
7326 // pass
7327 }
7328 }
7329
Adam Powell6e346362010-07-23 10:18:23 -07007330 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007331 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007332 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007333 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7334 * while this Activity has user focus.</p>
7335 *
7336 * <p>It is recommended that applications additionally declare
7337 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7338 * transitions when switching between VR activities.</p>
7339 *
7340 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7341 * VR mode will not be started. Developers can handle this case as follows:</p>
7342 *
7343 * <pre>
7344 * String servicePackage = "com.whatever.app";
7345 * String serviceClass = "com.whatever.app.MyVrListenerService";
7346 *
7347 * // Name of the component of the VrListenerService to start.
7348 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7349 *
7350 * try {
7351 * setVrModeEnabled(true, myComponentName);
7352 * } catch (PackageManager.NameNotFoundException e) {
7353 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7354 * boolean isInstalled = false;
7355 * for (ApplicationInfo app : installed) {
7356 * if (app.packageName.equals(servicePackage)) {
7357 * isInstalled = true;
7358 * break;
7359 * }
7360 * }
7361 * if (isInstalled) {
7362 * // Package is installed, but not enabled in Settings. Let user enable it.
7363 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7364 * } else {
7365 * // Package is not installed. Send an intent to download this.
7366 * sentIntentToLaunchAppStore(servicePackage);
7367 * }
7368 * }
7369 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007370 *
7371 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007372 * @param requestedComponent the name of the component to use as a
7373 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7374 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007375 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7376 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7377 * not been enabled in user settings.
7378 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007379 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7380 * @see android.service.vr.VrListenerService
7381 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7382 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007383 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007384 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7385 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007386 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007387 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007388 != 0) {
7389 throw new PackageManager.NameNotFoundException(
7390 requestedComponent.flattenToString());
7391 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007392 } catch (RemoteException e) {
7393 // pass
7394 }
7395 }
7396
7397 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007398 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007399 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007400 * @param callback Callback that will manage lifecycle events for this action mode
7401 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007402 *
7403 * @see ActionMode
7404 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007405 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007406 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007407 return mWindow.getDecorView().startActionMode(callback);
7408 }
7409
Adam Powelldebf3be2010-11-15 18:58:48 -08007410 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007411 * Start an action mode of the given type.
7412 *
7413 * @param callback Callback that will manage lifecycle events for this action mode
7414 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7415 * @return The ActionMode that was started, or null if it was canceled
7416 *
7417 * @see ActionMode
7418 */
7419 @Nullable
7420 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7421 return mWindow.getDecorView().startActionMode(callback, type);
7422 }
7423
7424 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007425 * Give the Activity a chance to control the UI for an action mode requested
7426 * by the system.
7427 *
7428 * <p>Note: If you are looking for a notification callback that an action mode
7429 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7430 *
7431 * @param callback The callback that should control the new action mode
7432 * @return The new action mode, or <code>null</code> if the activity does not want to
7433 * provide special handling for this action mode. (It will be handled by the system.)
7434 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007435 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007436 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007437 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007438 // Only Primary ActionModes are represented in the ActionBar.
7439 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7440 initWindowDecorActionBar();
7441 if (mActionBar != null) {
7442 return mActionBar.startActionMode(callback);
7443 }
Adam Powell6e346362010-07-23 10:18:23 -07007444 }
7445 return null;
7446 }
7447
Adam Powelldebf3be2010-11-15 18:58:48 -08007448 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007449 * {@inheritDoc}
7450 */
7451 @Nullable
7452 @Override
7453 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7454 try {
7455 mActionModeTypeStarting = type;
7456 return onWindowStartingActionMode(callback);
7457 } finally {
7458 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7459 }
7460 }
7461
7462 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007463 * Notifies the Activity that an action mode has been started.
7464 * Activity subclasses overriding this method should call the superclass implementation.
7465 *
7466 * @param mode The new action mode.
7467 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007468 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007469 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007470 public void onActionModeStarted(ActionMode mode) {
7471 }
7472
7473 /**
7474 * Notifies the activity that an action mode has finished.
7475 * Activity subclasses overriding this method should call the superclass implementation.
7476 *
7477 * @param mode The action mode that just finished.
7478 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007479 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007480 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007481 public void onActionModeFinished(ActionMode mode) {
7482 }
7483
Adam Powelldd8fab22012-03-22 17:47:27 -07007484 /**
7485 * Returns true if the app should recreate the task when navigating 'up' from this activity
7486 * by using targetIntent.
7487 *
7488 * <p>If this method returns false the app can trivially call
7489 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7490 * up navigation. If this method returns false, the app should synthesize a new task stack
7491 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7492 *
7493 * @param targetIntent An intent representing the target destination for up navigation
7494 * @return true if navigating up should recreate a new task stack, false if the same task
7495 * should be used for the destination
7496 */
7497 public boolean shouldUpRecreateTask(Intent targetIntent) {
7498 try {
7499 PackageManager pm = getPackageManager();
7500 ComponentName cn = targetIntent.getComponent();
7501 if (cn == null) {
7502 cn = targetIntent.resolveActivity(pm);
7503 }
7504 ActivityInfo info = pm.getActivityInfo(cn, 0);
7505 if (info.taskAffinity == null) {
7506 return false;
7507 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007508 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007509 } catch (RemoteException e) {
7510 return false;
7511 } catch (NameNotFoundException e) {
7512 return false;
7513 }
7514 }
7515
7516 /**
7517 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7518 * in the process. If the activity indicated by upIntent already exists in the task's history,
7519 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007520 * finished.
7521 *
7522 * <p>If the indicated activity does not appear in the history stack, this will finish
7523 * each activity in this task until the root activity of the task is reached, resulting in
7524 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7525 * when an activity may be reached by a path not passing through a canonical parent
7526 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007527 *
7528 * <p>This method should be used when performing up navigation from within the same task
7529 * as the destination. If up navigation should cross tasks in some cases, see
7530 * {@link #shouldUpRecreateTask(Intent)}.</p>
7531 *
7532 * @param upIntent An intent representing the target destination for up navigation
7533 *
7534 * @return true if up navigation successfully reached the activity indicated by upIntent and
7535 * upIntent was delivered to it. false if an instance of the indicated activity could
7536 * not be found and this activity was simply finished normally.
7537 */
7538 public boolean navigateUpTo(Intent upIntent) {
7539 if (mParent == null) {
7540 ComponentName destInfo = upIntent.getComponent();
7541 if (destInfo == null) {
7542 destInfo = upIntent.resolveActivity(getPackageManager());
7543 if (destInfo == null) {
7544 return false;
7545 }
7546 upIntent = new Intent(upIntent);
7547 upIntent.setComponent(destInfo);
7548 }
7549 int resultCode;
7550 Intent resultData;
7551 synchronized (this) {
7552 resultCode = mResultCode;
7553 resultData = mResultData;
7554 }
7555 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007556 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007557 }
7558 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007559 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007560 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007561 resultCode, resultData);
7562 } catch (RemoteException e) {
7563 return false;
7564 }
7565 } else {
7566 return mParent.navigateUpToFromChild(this, upIntent);
7567 }
7568 }
7569
7570 /**
7571 * This is called when a child activity of this one calls its
7572 * {@link #navigateUpTo} method. The default implementation simply calls
7573 * navigateUpTo(upIntent) on this activity (the parent).
7574 *
7575 * @param child The activity making the call.
7576 * @param upIntent An intent representing the target destination for up navigation
7577 *
7578 * @return true if up navigation successfully reached the activity indicated by upIntent and
7579 * upIntent was delivered to it. false if an instance of the indicated activity could
7580 * not be found and this activity was simply finished normally.
7581 */
7582 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7583 return navigateUpTo(upIntent);
7584 }
7585
7586 /**
7587 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7588 * this activity's logical parent. The logical parent is named in the application's manifest
7589 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007590 * Activity subclasses may override this method to modify the Intent returned by
7591 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7592 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007593 *
Adam Powell04d58112012-04-09 10:22:12 -07007594 * @return a new Intent targeting the defined parent of this activity or null if
7595 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007596 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007597 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007598 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007599 final String parentName = mActivityInfo.parentActivityName;
7600 if (TextUtils.isEmpty(parentName)) {
7601 return null;
7602 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007603
7604 // If the parent itself has no parent, generate a main activity intent.
7605 final ComponentName target = new ComponentName(this, parentName);
7606 try {
7607 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7608 final String parentActivity = parentInfo.parentActivityName;
7609 final Intent parentIntent = parentActivity == null
7610 ? Intent.makeMainActivity(target)
7611 : new Intent().setComponent(target);
7612 return parentIntent;
7613 } catch (NameNotFoundException e) {
7614 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7615 "' in manifest");
7616 return null;
7617 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007618 }
7619
George Mount31a21722014-03-24 17:44:36 -07007620 /**
George Mount62ab9b72014-05-02 13:51:17 -07007621 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007622 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007623 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007624 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007625 *
George Mount65580562014-08-29 08:15:48 -07007626 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007627 */
George Mount65580562014-08-29 08:15:48 -07007628 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7629 if (callback == null) {
7630 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007631 }
George Mount65580562014-08-29 08:15:48 -07007632 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007633 }
7634
7635 /**
7636 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007637 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007638 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7639 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007640 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007641 *
George Mount65580562014-08-29 08:15:48 -07007642 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007643 */
George Mount65580562014-08-29 08:15:48 -07007644 public void setExitSharedElementCallback(SharedElementCallback callback) {
7645 if (callback == null) {
7646 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007647 }
George Mount65580562014-08-29 08:15:48 -07007648 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007649 }
7650
George Mount8c2614c2014-06-10 11:12:01 -07007651 /**
7652 * Postpone the entering activity transition when Activity was started with
7653 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7654 * android.util.Pair[])}.
7655 * <p>This method gives the Activity the ability to delay starting the entering and
7656 * shared element transitions until all data is loaded. Until then, the Activity won't
7657 * draw into its window, leaving the window transparent. This may also cause the
7658 * returning animation to be delayed until data is ready. This method should be
7659 * called in {@link #onCreate(android.os.Bundle)} or in
7660 * {@link #onActivityReenter(int, android.content.Intent)}.
7661 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7662 * start the transitions. If the Activity did not use
7663 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7664 * android.util.Pair[])}, then this method does nothing.</p>
7665 */
7666 public void postponeEnterTransition() {
7667 mActivityTransitionState.postponeEnterTransition();
7668 }
7669
7670 /**
7671 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7672 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7673 * to have your Activity start drawing.
7674 */
7675 public void startPostponedEnterTransition() {
7676 mActivityTransitionState.startPostponedEnterTransition();
7677 }
7678
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007679 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007680 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7681 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007682 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007683 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7684 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007685 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007686 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7687 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7688 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7689 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007690 }
7691 return null;
7692 }
7693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007695
Mathew Inwood8c854f82018-09-14 12:35:36 +01007696 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 final void setParent(Activity parent) {
7698 mParent = parent;
7699 }
7700
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007701 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007702 final void attach(Context context, ActivityThread aThread,
7703 Instrumentation instr, IBinder token, int ident,
7704 Application application, Intent intent, ActivityInfo info,
7705 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007706 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007707 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Sunny Goyald40c3452019-03-20 12:46:55 -07007708 Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 attachBaseContext(context);
7710
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007711 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007712
Andrii Kulian51c1b672017-04-07 18:39:32 -07007713 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007714 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007716 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007717 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7719 mWindow.setSoftInputMode(info.softInputMode);
7720 }
Adam Powell269248d2011-08-02 10:26:54 -07007721 if (info.uiOptions != 0) {
7722 mWindow.setUiOptions(info.uiOptions);
7723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 mMainThread = aThread;
7727 mInstrumentation = instr;
7728 mToken = token;
Sunny Goyald40c3452019-03-20 12:46:55 -07007729 mAssistToken = assistToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007730 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 mApplication = application;
7732 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007733 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 mComponent = intent.getComponent();
7735 mActivityInfo = info;
7736 mTitle = title;
7737 mParent = parent;
7738 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007739 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007740 if (voiceInteractor != null) {
7741 if (lastNonConfigurationInstances != null) {
7742 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7743 } else {
7744 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7745 Looper.myLooper());
7746 }
7747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748
Jeff Brown98365d72012-08-19 20:30:52 -07007749 mWindow.setWindowManager(
7750 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7751 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007752 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 if (mParent != null) {
7754 mWindow.setContainer(mParent.getWindow());
7755 }
7756 mWindowManager = mWindow.getWindowManager();
7757 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007758
7759 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007760
Felipe Lemea4f39cd2019-02-19 15:08:59 -08007761 setAutofillOptions(application.getAutofillOptions());
Felipe Leme326f15a2019-02-19 09:42:24 -08007762 setContentCaptureOptions(application.getContentCaptureOptions());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007763 }
7764
7765 private void enableAutofillCompatibilityIfNeeded() {
7766 if (isAutofillCompatibilityEnabled()) {
7767 final AutofillManager afm = getSystemService(AutofillManager.class);
7768 if (afm != null) {
7769 afm.enableCompatibilityMode();
7770 }
7771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 }
7773
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007774 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007775 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007776 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 return mParent != null ? mParent.getActivityToken() : mToken;
7778 }
7779
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007780 /** @hide */
Sunny Goyald40c3452019-03-20 12:46:55 -07007781 public final IBinder getAssistToken() {
7782 return mParent != null ? mParent.getAssistToken() : mAssistToken;
7783 }
7784
7785 /** @hide */
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007786 @VisibleForTesting
7787 public final ActivityThread getActivityThread() {
7788 return mMainThread;
7789 }
7790
Winson Chung298f95b2017-08-10 15:57:18 -07007791 final void performCreate(Bundle icicle) {
7792 performCreate(icicle, null);
7793 }
7794
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007795 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007796 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007797 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007798 mCanEnterPictureInPicture = true;
7799 restoreHasCurrentPermissionRequest(icicle);
7800 if (persistentState != null) {
7801 onCreate(icicle, persistentState);
7802 } else {
7803 onCreate(icicle);
7804 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007805 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007806 mActivityTransitionState.readState(icicle);
7807
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007808 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7809 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007810 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007811 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007812 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007813 }
George Mount0a778ed2013-12-13 13:35:36 -08007814
Ian Lake0b71b8e2018-09-13 13:56:26 -07007815 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07007816 mCanEnterPictureInPicture = true;
7817 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007818 }
7819
Andrii Kuliand25680c2018-02-21 15:16:58 -08007820 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007821 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07007822 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007823 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007825 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007827 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 if (!mCalled) {
7830 throw new SuperNotCalledException(
7831 "Activity " + mComponent.toShortString() +
7832 " did not call through to super.onStart()");
7833 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007834 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007835 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007836
Elliott Hughese14fa972019-10-02 11:35:53 -07007837 // Warn app developers if the dynamic linker logged anything during startup.
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007838 boolean isAppDebuggable =
7839 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Elliott Hughese14fa972019-10-02 11:35:53 -07007840 if (isAppDebuggable) {
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007841 String dlwarning = getDlWarning();
7842 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007843 String appName = getApplicationInfo().loadLabel(getPackageManager())
7844 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007845 String warning = "Detected problems with app native libraries\n" +
7846 "(please consult log for detail):\n" + dlwarning;
7847 if (isAppDebuggable) {
7848 new AlertDialog.Builder(this).
7849 setTitle(appName).
7850 setMessage(warning).
7851 setPositiveButton(android.R.string.ok, null).
7852 setCancelable(false).
7853 show();
7854 } else {
7855 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7856 }
7857 }
7858 }
7859
Tim Van Pattenddc43912018-12-18 17:47:52 -07007860 GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
7861
George Mount62ab9b72014-05-02 13:51:17 -07007862 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07007863 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 }
RoboErik55011652014-07-09 15:05:53 -07007865
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007866 /**
7867 * Restart the activity.
7868 * @param start Indicates whether the activity should also be started after restart.
7869 * The option to not start immediately is needed in case a transaction with
7870 * multiple lifecycle transitions is in progress.
7871 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007872 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007873 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007874 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007875
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007876 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007877 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007878 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7879 }
7880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 if (mStopped) {
7882 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007883
7884 synchronized (mManagedCursors) {
7885 final int N = mManagedCursors.size();
7886 for (int i=0; i<N; i++) {
7887 ManagedCursor mc = mManagedCursors.get(i);
7888 if (mc.mReleased || mc.mUpdated) {
7889 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007890 if (getApplicationInfo().targetSdkVersion
7891 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7892 throw new IllegalStateException(
7893 "trying to requery an already closed cursor "
7894 + mc.mCursor);
7895 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007896 }
7897 mc.mReleased = false;
7898 mc.mUpdated = false;
7899 }
7900 }
7901 }
7902
7903 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007905 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 if (!mCalled) {
7907 throw new SuperNotCalledException(
7908 "Activity " + mComponent.toShortString() +
7909 " did not call through to super.onRestart()");
7910 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007911 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007912 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 }
7915 }
RoboErik55011652014-07-09 15:05:53 -07007916
Andrii Kuliand25680c2018-02-21 15:16:58 -08007917 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007918 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007919 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007920
Dianne Hackborn445646c2010-06-25 15:52:59 -07007921 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007922
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007923 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007924
Dake Gu67decfa2017-12-27 11:48:08 -08007925 if (mAutoFillResetNeeded) {
7926 // When Activity is destroyed in paused state, and relaunch activity, there will be
7927 // extra onResume and onPause event, ignore the first onResume and onPause.
7928 // see ActivityThread.handleRelaunchActivity()
7929 mAutoFillIgnoreFirstResumePause = followedByPause;
7930 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7931 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7932 }
7933 }
7934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007936 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007938 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007939 if (!mCalled) {
7940 throw new SuperNotCalledException(
7941 "Activity " + mComponent.toShortString() +
7942 " did not call through to super.onResume()");
7943 }
7944
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007945 // invisible activities must be finished before onResume() completes
7946 if (!mVisibleFromClient && !mFinished) {
7947 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7948 if (getApplicationInfo().targetSdkVersion
7949 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7950 throw new IllegalStateException(
7951 "Activity " + mComponent.toShortString() +
7952 " did not call finish() prior to onResume() completing");
7953 }
7954 }
7955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007958
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007959 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007960 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 onPostResume();
7963 if (!mCalled) {
7964 throw new SuperNotCalledException(
7965 "Activity " + mComponent.toShortString() +
7966 " did not call through to super.onPostResume()");
7967 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007968 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 }
7970
7971 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007972 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007973 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007974 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007975 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007977 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007978 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007979 if (!mCalled && getApplicationInfo().targetSdkVersion
7980 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7981 throw new SuperNotCalledException(
7982 "Activity " + mComponent.toShortString() +
7983 " did not call through to super.onPause()");
7984 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007985 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 }
RoboErik55011652014-07-09 15:05:53 -07007987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 final void performUserLeaving() {
7989 onUserInteraction();
7990 onUserLeaveHint();
7991 }
RoboErik55011652014-07-09 15:05:53 -07007992
Andrii Kuliand25680c2018-02-21 15:16:58 -08007993 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007994 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007995 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007996
Winson Chung298f95b2017-08-10 15:57:18 -07007997 // Disallow entering picture-in-picture after the activity has been stopped
7998 mCanEnterPictureInPicture = false;
7999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07008001 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 if (mWindow != null) {
8003 mWindow.closeAllPanels();
8004 }
8005
Chong Zhang7687f252016-02-26 12:03:33 -08008006 // If we're preserving the window, don't setStoppedState to true, since we
8007 // need the window started immediately again. Stopping the window will
8008 // destroys hardware resources and causes flicker.
8009 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07008010 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08008011 }
RoboErik55011652014-07-09 15:05:53 -07008012
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008013 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07008014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 mCalled = false;
8016 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08008017 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018 if (!mCalled) {
8019 throw new SuperNotCalledException(
8020 "Activity " + mComponent.toShortString() +
8021 " did not call through to super.onStop()");
8022 }
RoboErik55011652014-07-09 15:05:53 -07008023
Makoto Onuki2f6a0182010-02-22 13:26:59 -08008024 synchronized (mManagedCursors) {
8025 final int N = mManagedCursors.size();
8026 for (int i=0; i<N; i++) {
8027 ManagedCursor mc = mManagedCursors.get(i);
8028 if (!mc.mReleased) {
8029 mc.mCursor.deactivate();
8030 mc.mReleased = true;
8031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 }
8033 }
George Mount0a778ed2013-12-13 13:35:36 -08008034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07008036 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 }
8038 mResumed = false;
8039 }
8040
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008041 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07008042 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07008043 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07008044 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008045 mFragments.dispatchDestroy();
8046 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08008047 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008048 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07008049 if (mVoiceInteractor != null) {
8050 mVoiceInteractor.detachActivity();
8051 }
Ian Lake8a88cd62018-09-28 13:25:20 -07008052 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07008053 }
George Mount0a778ed2013-12-13 13:35:36 -08008054
Winson Chung5af42fc2017-03-24 17:11:33 -07008055 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
8056 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008057 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008058 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
8059 + " " + newConfig);
8060 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008061 if (mWindow != null) {
8062 mWindow.onMultiWindowModeChanged();
8063 }
Winson Chung5af42fc2017-03-24 17:11:33 -07008064 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008065 }
8066
Winson Chung5af42fc2017-03-24 17:11:33 -07008067 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
8068 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07008069 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07008070 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
8071 + " " + newConfig);
8072 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07008073 if (mWindow != null) {
8074 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
8075 }
Winson Chung5af42fc2017-03-24 17:11:33 -07008076 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07008077 }
8078
George Mount0a778ed2013-12-13 13:35:36 -08008079 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06008080 * @hide
8081 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008082 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06008083 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 return mResumed;
8085 }
8086
Svetoslavffb32b12015-10-15 16:54:00 -07008087 private void storeHasCurrentPermissionRequest(Bundle bundle) {
8088 if (bundle != null && mHasCurrentPermissionsRequest) {
8089 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
8090 }
8091 }
8092
8093 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
8094 if (bundle != null) {
8095 mHasCurrentPermissionsRequest = bundle.getBoolean(
8096 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
8097 }
8098 }
8099
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008100 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07008101 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
8102 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07008103 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
8105 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07008106 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008108 onActivityResult(requestCode, resultCode, data);
8109 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
8110 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
8111 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08008112 dispatchRequestPermissionsResult(requestCode, data);
8113 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00008114 Fragment frag = mFragments.findFragmentByWho(who);
8115 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07008116 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08008117 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07008118 }
Svetoslav970b59c2015-06-09 16:05:21 -07008119 } else if (who.startsWith("@android:view:")) {
8120 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
8121 getActivityToken());
8122 for (ViewRootImpl viewRoot : views) {
8123 if (viewRoot.getView() != null
8124 && viewRoot.getView().dispatchActivityResult(
8125 who, requestCode, resultCode, data)) {
8126 return;
8127 }
8128 }
Svet Ganov782043c2017-02-11 00:52:02 +00008129 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08008130 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08008131 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07008132 } else {
8133 Fragment frag = mFragments.findFragmentByWho(who);
8134 if (frag != null) {
8135 frag.onActivityResult(requestCode, resultCode, data);
8136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07008138 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07008140
Jason Monka57e5e02014-05-07 10:06:48 -04008141 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008142 * Request to put this activity in a mode where the user is locked to a restricted set of
8143 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04008144 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008145 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
8146 * for this component, the current task will be launched directly into LockTask mode. Only apps
8147 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
8148 * be launched while LockTask mode is active. The user will not be able to leave this mode
8149 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
8150 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04008151 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008152 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
8153 * system will prompt the user with a dialog requesting permission to use this mode.
8154 * The user can exit at any time through instructions shown on the request dialog. Calling
8155 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04008156 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008157 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
8158 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008159 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008160 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008161 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04008162 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008163 public void startLockTask() {
8164 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008165 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008166 } catch (RemoteException e) {
8167 }
8168 }
8169
Jason Monka57e5e02014-05-07 10:06:48 -04008170 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008171 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04008172 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008173 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
8174 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04008175 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008176 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
8177 * by this activity, then calling this method will not terminate the LockTask mode, but only
8178 * finish its own task. The device will remain in LockTask mode, until the activity which
8179 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
8180 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008181 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008182 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008183 * @see android.R.attr#lockTaskMode
8184 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04008185 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008186 public void stopLockTask() {
8187 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008188 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008189 } catch (RemoteException e) {
8190 }
8191 }
8192
Craig Mautner5eda9b32013-07-02 11:58:16 -07008193 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008194 * Shows the user the system defined message for telling the user how to exit
8195 * lock task mode. The task containing this activity must be in lock task mode at the time
8196 * of this call for the message to be displayed.
8197 */
8198 public void showLockTaskEscapeMessage() {
8199 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008200 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008201 } catch (RemoteException e) {
8202 }
8203 }
8204
8205 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07008206 * Check whether the caption on freeform windows is displayed directly on the content.
8207 *
8208 * @return True if caption is displayed on content, false if it pushes the content down.
8209 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08008210 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07008211 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07008212 */
8213 public boolean isOverlayWithDecorCaptionEnabled() {
8214 return mWindow.isOverlayWithDecorCaptionEnabled();
8215 }
8216
8217 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008218 * Set whether the caption should displayed directly on the content rather than push it down.
8219 *
8220 * This affects only freeform windows since they display the caption and only the main
8221 * window of the activity. The caption is used to drag the window around and also shows
8222 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008223 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008224 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008225 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8226 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008227 }
8228
8229 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008230 * Interface for informing a translucent {@link Activity} once all visible activities below it
8231 * have completed drawing. This is necessary only after an {@link Activity} has been made
8232 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8233 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008234 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8235 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008236 *
8237 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008238 */
Jose Lima14914852014-08-14 09:14:12 -07008239 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008240 public interface TranslucentConversionListener {
8241 /**
8242 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8243 * below the top one have been redrawn. Following this callback it is safe to make the top
8244 * Activity translucent because the underlying Activity has been drawn.
8245 *
8246 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8247 * occurred waiting for the Activity to complete drawing.
8248 *
8249 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008250 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008251 */
8252 public void onTranslucentConversionComplete(boolean drawComplete);
8253 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008254
8255 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008256 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008257 // If the package installer crashed we may have not data - best effort.
8258 String[] permissions = (data != null) ? data.getStringArrayExtra(
8259 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8260 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8261 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008262 onRequestPermissionsResult(requestCode, permissions, grantResults);
8263 }
8264
8265 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008266 Fragment fragment) {
8267 // If the package installer crashed we may have not data - best effort.
8268 String[] permissions = (data != null) ? data.getStringArrayExtra(
8269 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8270 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8271 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8272 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008273 }
8274
Svet Ganov782043c2017-02-11 00:52:02 +00008275 /** @hide */
8276 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008277 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008278 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00008279 try {
8280 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008281 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008282 } catch (IntentSender.SendIntentException e) {
8283 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8284 }
8285 }
8286
Svet Ganov17db9dc2017-02-21 19:54:31 -08008287 /** @hide */
8288 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008289 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008290 mAutoFillResetNeeded = true;
8291 }
8292
Felipe Leme4753bb02017-03-22 20:24:00 -07008293 /** @hide */
8294 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008295 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008296 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008297 final boolean wasShowing;
8298
8299 if (mAutofillPopupWindow == null) {
8300 wasShowing = false;
8301 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8302 } else {
8303 wasShowing = mAutofillPopupWindow.isShowing();
8304 }
Svet Ganov374cae12017-05-10 13:42:33 -07008305 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008306
8307 return !wasShowing && mAutofillPopupWindow.isShowing();
8308 }
8309
8310 /** @hide */
8311 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008312 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8313 @NonNull KeyEvent keyEvent) {
8314 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8315 if (rootImpl != null) {
8316 // dont care if anchorView is current focus, for example a custom view may only receive
8317 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8318 // might be inside parent of the custom view.
8319 rootImpl.dispatchKeyFromAutofill(keyEvent);
8320 }
8321 }
8322
8323 /** @hide */
8324 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008325 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008326 if (mAutofillPopupWindow == null) {
8327 return false;
8328 }
8329 mAutofillPopupWindow.dismiss();
8330 mAutofillPopupWindow = null;
8331 return true;
8332 }
8333
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008334 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008335 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008336 public final boolean autofillClientIsFillUiShowing() {
8337 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8338 }
8339
8340 /** @hide */
8341 @Override
8342 @NonNull
8343 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8344 @NonNull AutofillId[] autofillId) {
8345 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008346 final ArrayList<ViewRootImpl> roots =
8347 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8348
8349 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8350 final View rootView = roots.get(rootNum).getView();
8351
8352 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008353 final int viewCount = autofillId.length;
8354 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008355 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008356 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008357 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008358 }
8359 }
8360 }
8361 }
8362
8363 return views;
8364 }
8365
8366 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008367 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008368 @Nullable
8369 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008370 final ArrayList<ViewRootImpl> roots =
8371 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8372 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8373 final View rootView = roots.get(rootNum).getView();
8374
8375 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008376 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008377 if (view != null) {
8378 return view;
8379 }
8380 }
8381 }
8382
8383 return null;
8384 }
8385
8386 /** @hide */
8387 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008388 public final @NonNull boolean[] autofillClientGetViewVisibility(
8389 @NonNull AutofillId[] autofillIds) {
8390 final int autofillIdCount = autofillIds.length;
8391 final boolean[] visible = new boolean[autofillIdCount];
8392 for (int i = 0; i < autofillIdCount; i++) {
8393 final AutofillId autofillId = autofillIds[i];
8394 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8395 if (view != null) {
Felipe Lemea75333c2019-01-22 15:45:32 -08008396 if (!autofillId.isVirtualInt()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008397 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008398 } else {
Felipe Lemea75333c2019-01-22 15:45:32 -08008399 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildIntId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008400 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008401 }
8402 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008403 if (android.view.autofill.Helper.sVerbose) {
8404 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8405 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008406 return visible;
8407 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008408
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008409 /** @hide */
8410 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8411 int windowId) {
8412 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8413 .getRootViews(getActivityToken());
8414 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8415 final View rootView = roots.get(rootNum).getView();
8416 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8417 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8418 if (view != null) {
8419 return view;
8420 }
8421 }
8422 }
8423 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008424 }
8425
8426 /** @hide */
8427 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008428 public final @Nullable IBinder autofillClientGetActivityToken() {
8429 return getActivityToken();
8430 }
8431
8432 /** @hide */
8433 @Override
8434 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008435 return !mStopped;
8436 }
8437
Dake Gu67decfa2017-12-27 11:48:08 -08008438 /** @hide */
8439 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008440 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008441 return isAutofillCompatibilityEnabled();
8442 }
8443
8444 /** @hide */
8445 @Override
8446 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008447 return mAutoFillIgnoreFirstResumePause || !mResumed;
8448 }
8449
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008450 /**
8451 * If set to true, this indicates to the system that it should never take a
8452 * screenshot of the activity to be used as a representation while it is not in a started state.
8453 * <p>
8454 * Note that the system may use the window background of the theme instead to represent
8455 * the window when it is not running.
8456 * <p>
8457 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8458 * this only affects the behavior when the activity's screenshot would be used as a
8459 * representation when the activity is not in a started state, i.e. in Overview. The system may
8460 * still take screenshots of the activity in other contexts; for example, when the user takes a
8461 * screenshot of the entire screen, or when the active
8462 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8463 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8464 *
8465 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8466 * @hide
8467 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008468 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008469 public void setDisablePreviewScreenshots(boolean disable) {
8470 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008471 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008472 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008473 throw e.rethrowFromSystemServer();
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008474 }
8475 }
8476
chaviw59b98852017-06-13 12:05:44 -07008477 /**
koprivaa1a78482018-10-09 10:09:23 -07008478 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008479 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8480 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8481 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8482 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8483 *
8484 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8485 * {@code false} otherwise.
8486 * @see #setTurnScreenOn(boolean)
8487 * @see android.R.attr#turnScreenOn
8488 * @see android.R.attr#showWhenLocked
8489 */
8490 public void setShowWhenLocked(boolean showWhenLocked) {
8491 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008492 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008493 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008494 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008495 }
8496 }
8497
8498 /**
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008499 * Specifies whether this {@link Activity} should be shown on top of the lock screen whenever
8500 * the lockscreen is up and this activity has another activity behind it with the showWhenLock
8501 * attribute set. That is, this activity is only visible on the lock screen if there is another
8502 * activity with the showWhenLock attribute visible at the same time on the lock screen. A use
8503 * case for this is permission dialogs, that should only be visible on the lock screen if their
8504 * requesting activity is also visible. This value can be set as a manifest attribute using
8505 * android.R.attr#inheritShowWhenLocked.
8506 *
8507 * @param inheritShowWhenLocked {@code true} to show the {@link Activity} on top of the lock
8508 * screen when this activity has another activity behind it with
8509 * the showWhenLock attribute set; {@code false} otherwise.
8510 * @see #setShowWhenLocked(boolean)
Issei Suzuki7c09f0b2019-02-28 14:45:50 +01008511 * @see android.R.attr#inheritShowWhenLocked
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008512 */
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008513 public void setInheritShowWhenLocked(boolean inheritShowWhenLocked) {
8514 try {
8515 ActivityTaskManager.getService().setInheritShowWhenLocked(
8516 mToken, inheritShowWhenLocked);
8517 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008518 throw e.rethrowFromSystemServer();
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008519 }
8520 }
8521
8522 /**
chaviw59b98852017-06-13 12:05:44 -07008523 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8524 * Normally an activity will be transitioned to the stopped state if it is started while the
8525 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8526 * activity will be visible and resumed due to the screen coming on. The screen will not be
8527 * turned on if the activity won't be visible after the screen is turned on. This flag is
8528 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8529 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8530 * if this flag is set and the activity calls {@link
8531 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8532 * the screen will turn on.
8533 *
8534 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8535 *
8536 * @see #setShowWhenLocked(boolean)
8537 * @see android.R.attr#turnScreenOn
8538 * @see android.R.attr#showWhenLocked
8539 */
8540 public void setTurnScreenOn(boolean turnScreenOn) {
8541 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008542 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008543 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008544 throw e.rethrowFromSystemServer();
chaviw59b98852017-06-13 12:05:44 -07008545 }
8546 }
8547
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008548 /**
8549 * Registers remote animations per transition type for this activity.
8550 *
8551 * @param definition The remote animation definition that defines which transition whould run
8552 * which remote animation.
8553 * @hide
8554 */
8555 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008556 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008557 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8558 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008559 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008560 } catch (RemoteException e) {
Issei Suzukiae7b4932019-03-12 10:03:21 -07008561 throw e.rethrowFromSystemServer();
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008562 }
8563 }
8564
Andrii Kuliand25680c2018-02-21 15:16:58 -08008565 /** Log a lifecycle event for current user id and component class. */
8566 private void writeEventLog(int event, String reason) {
8567 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
8568 reason);
8569 }
8570
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008571 class HostCallbacks extends FragmentHostCallback<Activity> {
8572 public HostCallbacks() {
8573 super(Activity.this /*activity*/);
8574 }
8575
8576 @Override
8577 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8578 Activity.this.dump(prefix, fd, writer, args);
8579 }
8580
8581 @Override
8582 public boolean onShouldSaveFragmentState(Fragment fragment) {
8583 return !isFinishing();
8584 }
8585
8586 @Override
8587 public LayoutInflater onGetLayoutInflater() {
8588 final LayoutInflater result = Activity.this.getLayoutInflater();
8589 if (onUseFragmentManagerInflaterFactory()) {
8590 return result.cloneInContext(Activity.this);
8591 }
8592 return result;
8593 }
8594
8595 @Override
8596 public boolean onUseFragmentManagerInflaterFactory() {
8597 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8598 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8599 }
8600
8601 @Override
8602 public Activity onGetHost() {
8603 return Activity.this;
8604 }
8605
8606 @Override
8607 public void onInvalidateOptionsMenu() {
8608 Activity.this.invalidateOptionsMenu();
8609 }
8610
8611 @Override
8612 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8613 Bundle options) {
8614 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8615 }
8616
8617 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008618 public void onStartActivityAsUserFromFragment(
8619 Fragment fragment, Intent intent, int requestCode, Bundle options,
8620 UserHandle user) {
8621 Activity.this.startActivityAsUserFromFragment(
8622 fragment, intent, requestCode, options, user);
8623 }
8624
8625 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008626 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8627 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8628 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8629 if (mParent == null) {
8630 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8631 flagsMask, flagsValues, options);
8632 } else if (options != null) {
8633 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
8634 fillInIntent, flagsMask, flagsValues, extraFlags, options);
8635 }
8636 }
8637
8638 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008639 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8640 int requestCode) {
8641 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8642 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8643 startActivityForResult(who, intent, requestCode, null);
8644 }
8645
8646 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008647 public boolean onHasWindowAnimations() {
8648 return getWindow() != null;
8649 }
8650
8651 @Override
8652 public int onGetWindowAnimations() {
8653 final Window w = getWindow();
8654 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8655 }
8656
Todd Kennedy434bd652015-05-04 12:29:50 -07008657 @Override
8658 public void onAttachFragment(Fragment fragment) {
8659 Activity.this.onAttachFragment(fragment);
8660 }
8661
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008662 @Nullable
8663 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008664 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008665 return Activity.this.findViewById(id);
8666 }
8667
8668 @Override
8669 public boolean onHasView() {
8670 final Window w = getWindow();
8671 return (w != null && w.peekDecorView() != null);
8672 }
8673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674}