blob: 68b2de425b7241243dcaa96c358465669cb33c2a [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;
Tim Van Pattenddc43912018-12-18 17:47:52 -070068import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Handler;
70import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080071import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070072import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000073import android.os.PersistableBundle;
Nicholas Sauer28348ed742019-02-15 11:58:32 -080074import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060076import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080077import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080078import android.os.SystemProperties;
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;
Adam He328c0e32019-01-03 15:19:22 -0800128import android.view.contentcapture.ContentCaptureContext;
Felipe Leme749b8892018-12-03 16:30:30 -0800129import android.view.contentcapture.ContentCaptureManager;
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
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700134import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800135import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700136import com.android.internal.app.IVoiceInteractor;
137import com.android.internal.app.ToolbarActionBar;
138import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700139import com.android.internal.policy.PhoneWindow;
140
Dianne Hackborn625ac272010-09-17 18:29:22 -0700141import java.io.FileDescriptor;
142import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700143import java.lang.annotation.Retention;
144import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700145import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800146import java.util.Arrays;
Adam Powell6e346362010-07-23 10:18:23 -0700147import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700148import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Bryce Leee83f34cd2017-10-31 19:50:54 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151/**
152 * An activity is a single, focused thing that the user can do. Almost all
153 * activities interact with the user, so the Activity class takes care of
154 * creating a window for you in which you can place your UI with
155 * {@link #setContentView}. While activities are often presented to the user
156 * as full-screen windows, they can also be used in other ways: as floating
157 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
158 * or embedded inside of another activity (using {@link ActivityGroup}).
159 *
160 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * <ul>
163 * <li> {@link #onCreate} is where you initialize your activity. Most
164 * importantly, here you will usually call {@link #setContentView(int)}
165 * with a layout resource defining your UI, and using {@link #findViewById}
166 * to retrieve the widgets in that UI that you need to interact with
167 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * <li> {@link #onPause} is where you deal with the user leaving your
170 * activity. Most importantly, any changes made by the user should at this
171 * point be committed (usually to the
172 * {@link android.content.ContentProvider} holding the data).
173 * </ul>
174 *
175 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
176 * activity classes must have a corresponding
177 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
178 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <p>Topics covered here:
181 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700182 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
184 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
185 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
186 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
187 * <li><a href="#Permissions">Permissions</a>
188 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
189 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700190 *
191 * <div class="special reference">
192 * <h3>Developer Guides</h3>
193 * <p>The Activity class is an important part of an application's overall lifecycle,
194 * and the way activities are launched and put together is a fundamental
195 * part of the platform's application model. For a detailed perspective on the structure of an
196 * Android application and how activities behave, please read the
197 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700198 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700199 * developer guides.</p>
200 *
201 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700202 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700203 * developer guide.</p>
204 * </div>
205 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700206 * <a name="Fragments"></a>
207 * <h3>Fragments</h3>
208 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800209 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
210 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700211 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800212 * screens, and help scale their application between small and large screens.</p>
213 *
214 * <p>For more information about using fragments, read the
215 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700216 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <a name="ActivityLifecycle"></a>
218 * <h3>Activity Lifecycle</h3>
219 *
220 * <p>Activities in the system are managed as an <em>activity stack</em>.
221 * When a new activity is started, it is placed on the top of the stack
222 * and becomes the running activity -- the previous activity always remains
223 * below it in the stack, and will not come to the foreground again until
224 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * <p>An activity has essentially four states:</p>
227 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700228 * <li> If an activity is in the foreground of the screen (at the top of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * the stack),
230 * it is <em>active</em> or <em>running</em>. </li>
231 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700232 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 * is <em>paused</em>. A paused activity is completely alive (it
234 * maintains all state and member information and remains attached to
235 * the window manager), but can be killed by the system in extreme
236 * low memory situations.
237 * <li>If an activity is completely obscured by another activity,
238 * it is <em>stopped</em>. It still retains all state and member information,
239 * however, it is no longer visible to the user so its window is hidden
240 * and it will often be killed by the system when memory is needed
241 * elsewhere.</li>
242 * <li>If an activity is paused or stopped, the system can drop the activity
243 * from memory by either asking it to finish, or simply killing its
244 * process. When it is displayed again to the user, it must be
245 * completely restarted and restored to its previous state.</li>
246 * </ul>
247 *
248 * <p>The following diagram shows the important state paths of an Activity.
249 * The square rectangles represent callback methods you can implement to
250 * perform operations when the Activity moves between states. The colored
251 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * <p><img src="../../../images/activity_lifecycle.png"
254 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 * <p>There are three key loops you may be interested in monitoring within your
257 * activity:
RoboErik55011652014-07-09 15:05:53 -0700258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * <ul>
260 * <li>The <b>entire lifetime</b> of an activity happens between the first call
261 * to {@link android.app.Activity#onCreate} through to a single final call
262 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
263 * of "global" state in onCreate(), and release all remaining resources in
264 * onDestroy(). For example, if it has a thread running in the background
265 * to download data from the network, it may create that thread in onCreate()
266 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * <li>The <b>visible lifetime</b> of an activity happens between a call to
269 * {@link android.app.Activity#onStart} until a corresponding call to
270 * {@link android.app.Activity#onStop}. During this time the user can see the
271 * activity on-screen, though it may not be in the foreground and interacting
272 * with the user. Between these two methods you can maintain resources that
273 * are needed to show the activity to the user. For example, you can register
274 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900275 * that impact your UI, and unregister it in onStop() when the user no
276 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 * can be called multiple times, as the activity becomes visible and hidden
278 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
281 * {@link android.app.Activity#onResume} until a corresponding call to
282 * {@link android.app.Activity#onPause}. During this time the activity is
283 * in front of all other activities and interacting with the user. An activity
284 * can frequently go between the resumed and paused states -- for example when
285 * the device goes to sleep, when an activity result is delivered, when a new
286 * intent is delivered -- so the code in these methods should be fairly
287 * lightweight.
288 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 * <p>The entire lifecycle of an activity is defined by the following
291 * Activity methods. All of these are hooks that you can override
292 * to do appropriate work when the activity changes state. All
293 * activities will implement {@link android.app.Activity#onCreate}
294 * to do their initial setup; many will also implement
295 * {@link android.app.Activity#onPause} to commit changes to data and
296 * otherwise prepare to stop interacting with the user. You should always
297 * call up to your superclass when implementing these methods.</p>
298 *
299 * </p>
300 * <pre class="prettyprint">
301 * public class Activity extends ApplicationContext {
302 * protected void onCreate(Bundle savedInstanceState);
303 *
304 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * protected void onRestart();
307 *
308 * protected void onResume();
309 *
310 * protected void onPause();
311 *
312 * protected void onStop();
313 *
314 * protected void onDestroy();
315 * }
316 * </pre>
317 *
318 * <p>In general the movement through an activity's lifecycle looks like
319 * this:</p>
320 *
321 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
322 * <colgroup align="left" span="3" />
323 * <colgroup align="left" />
324 * <colgroup align="center" />
325 * <colgroup align="center" />
326 *
327 * <thead>
328 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
329 * </thead>
330 *
331 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700332 * <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 -0800333 * <td>Called when the activity is first created.
334 * This is where you should do all of your normal static set up:
335 * create views, bind data to lists, etc. This method also
336 * provides you with a Bundle containing the activity's previously
337 * frozen state, if there was one.
338 * <p>Always followed by <code>onStart()</code>.</td>
339 * <td align="center">No</td>
340 * <td align="center"><code>onStart()</code></td>
341 * </tr>
342 *
343 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700344 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 * <td>Called after your activity has been stopped, prior to it being
346 * started again.
347 * <p>Always followed by <code>onStart()</code></td>
348 * <td align="center">No</td>
349 * <td align="center"><code>onStart()</code></td>
350 * </tr>
351 *
smain@google.com10e29b82016-06-23 12:09:30 -0700352 * <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 -0800353 * <td>Called when the activity is becoming visible to the user.
354 * <p>Followed by <code>onResume()</code> if the activity comes
355 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
356 * <td align="center">No</td>
357 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
358 * </tr>
359 *
360 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700361 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 * <td>Called when the activity will start
363 * interacting with the user. At this point your activity is at
364 * the top of the activity stack, with user input going to it.
365 * <p>Always followed by <code>onPause()</code>.</td>
366 * <td align="center">No</td>
367 * <td align="center"><code>onPause()</code></td>
368 * </tr>
369 *
smain@google.com10e29b82016-06-23 12:09:30 -0700370 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * <td>Called when the system is about to start resuming a previous
372 * activity. This is typically used to commit unsaved changes to
373 * persistent data, stop animations and other things that may be consuming
374 * CPU, etc. Implementations of this method must be very quick because
375 * the next activity will not be resumed until this method returns.
376 * <p>Followed by either <code>onResume()</code> if the activity
377 * returns back to the front, or <code>onStop()</code> if it becomes
378 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800379 * <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 -0800380 * <td align="center"><code>onResume()</code> or<br>
381 * <code>onStop()</code></td>
382 * </tr>
383 *
smain@google.com10e29b82016-06-23 12:09:30 -0700384 * <tr><td colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * <td>Called when the activity is no longer visible to the user, because
386 * another activity has been resumed and is covering this one. This
387 * may happen either because a new activity is being started, an existing
388 * one is being brought in front of this one, or this one is being
389 * destroyed.
390 * <p>Followed by either <code>onRestart()</code> if
391 * this activity is coming back to interact with the user, or
392 * <code>onDestroy()</code> if this activity is going away.</td>
393 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
394 * <td align="center"><code>onRestart()</code> or<br>
395 * <code>onDestroy()</code></td>
396 * </tr>
397 *
smain@google.com10e29b82016-06-23 12:09:30 -0700398 * <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 -0800399 * <td>The final call you receive before your
400 * activity is destroyed. This can happen either because the
401 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700402 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * instance of the activity to save space. You can distinguish
404 * between these two scenarios with the {@link
405 * Activity#isFinishing} method.</td>
406 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
407 * <td align="center"><em>nothing</em></td>
408 * </tr>
409 * </tbody>
410 * </table>
411 *
412 * <p>Note the "Killable" column in the above table -- for those methods that
413 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800414 * 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 -0800415 * of its code being executed. Because of this, you should use the
416 * {@link #onPause} method to write any persistent data (such as user edits)
417 * to storage. In addition, the method
418 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
419 * in such a background state, allowing you to save away any dynamic instance
420 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700421 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
423 * section for more information on how the lifecycle of a process is tied
424 * to the activities it is hosting. Note that it is important to save
425 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800426 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * be called in every situation as described in its documentation.</p>
428 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800429 * <p class="note">Be aware that these semantics will change slightly between
430 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
431 * vs. those targeting prior platforms. Starting with Honeycomb, an application
432 * is not in the killable state until its {@link #onStop} has returned. This
433 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700434 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800435 * wait until {@link #onStop()} to save persistent state.</p>
436 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800437 * <p class="note">For applications targeting platforms starting with
438 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
439 * will always be called after {@link #onStop}, so an application may safely
440 * perform fragment transactions in {@link #onStop} and will be able to save
441 * persistent state later.</p>
442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * <p>For those methods that are not marked as being killable, the activity's
444 * process will not be killed by the system starting from the time the method
445 * is called and continuing after it returns. Thus an activity is in the killable
446 * state, for example, between after <code>onPause()</code> to the start of
447 * <code>onResume()</code>.</p>
448 *
449 * <a name="ConfigurationChanges"></a>
450 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700451 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * <p>If the configuration of the device (as defined by the
453 * {@link Configuration Resources.Configuration} class) changes,
454 * then anything displaying a user interface will need to update to match that
455 * configuration. Because Activity is the primary mechanism for interacting
456 * with the user, it includes special support for handling configuration
457 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * <p>Unless you specify otherwise, a configuration change (such as a change
460 * in screen orientation, language, input devices, etc) will cause your
461 * current activity to be <em>destroyed</em>, going through the normal activity
462 * lifecycle process of {@link #onPause},
463 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
464 * had been in the foreground or visible to the user, once {@link #onDestroy} is
465 * called in that instance then a new instance of the activity will be
466 * created, with whatever savedInstanceState the previous instance had generated
467 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * <p>This is done because any application resource,
470 * including layout files, can change based on any configuration value. Thus
471 * the only safe way to handle a configuration change is to re-retrieve all
472 * resources, including layouts, drawables, and strings. Because activities
473 * must already know how to save their state and re-create themselves from
474 * that state, this is a convenient way to have an activity restart itself
475 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 * <p>In some special cases, you may want to bypass restarting of your
478 * activity based on one or more types of configuration changes. This is
479 * done with the {@link android.R.attr#configChanges android:configChanges}
480 * attribute in its manifest. For any types of configuration changes you say
481 * that you handle there, you will receive a call to your current activity's
482 * {@link #onConfigurationChanged} method instead of being restarted. If
483 * a configuration change involves any that you do not handle, however, the
484 * activity will still be restarted and {@link #onConfigurationChanged}
485 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700486 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 * <a name="StartingActivities"></a>
488 * <h3>Starting Activities and Getting Results</h3>
489 *
490 * <p>The {@link android.app.Activity#startActivity}
491 * method is used to start a
492 * new activity, which will be placed at the top of the activity stack. It
493 * takes a single argument, an {@link android.content.Intent Intent},
494 * which describes the activity
495 * to be executed.</p>
496 *
497 * <p>Sometimes you want to get a result back from an activity when it
498 * ends. For example, you may start an activity that lets the user pick
499 * a person in a list of contacts; when it ends, it returns the person
500 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700501 * {@link android.app.Activity#startActivityForResult(Intent, int)}
502 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700504 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 *
506 * <p>When an activity exits, it can call
507 * {@link android.app.Activity#setResult(int)}
508 * to return data back to its parent. It must always supply a result code,
509 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
510 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
511 * return back an Intent containing any additional data it wants. All of this
512 * information appears back on the
513 * parent's <code>Activity.onActivityResult()</code>, along with the integer
514 * identifier it originally supplied.</p>
515 *
516 * <p>If a child activity fails for any reason (such as crashing), the parent
517 * activity will receive a result with the code RESULT_CANCELED.</p>
518 *
519 * <pre class="prettyprint">
520 * public class MyActivity extends Activity {
521 * ...
522 *
523 * static final int PICK_CONTACT_REQUEST = 0;
524 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700525 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
527 * // When the user center presses, let them pick a contact.
528 * startActivityForResult(
529 * new Intent(Intent.ACTION_PICK,
530 * new Uri("content://contacts")),
531 * PICK_CONTACT_REQUEST);
532 * return true;
533 * }
534 * return false;
535 * }
536 *
537 * protected void onActivityResult(int requestCode, int resultCode,
538 * Intent data) {
539 * if (requestCode == PICK_CONTACT_REQUEST) {
540 * if (resultCode == RESULT_OK) {
541 * // A contact was picked. Here we will just display it
542 * // to the user.
543 * startActivity(new Intent(Intent.ACTION_VIEW, data));
544 * }
545 * }
546 * }
547 * }
548 * </pre>
549 *
550 * <a name="SavingPersistentState"></a>
551 * <h3>Saving Persistent State</h3>
552 *
koprivafc3949e2018-09-17 11:35:13 -0700553 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 * will deal with: shared document-like data (typically stored in a SQLite
555 * database using a {@linkplain android.content.ContentProvider content provider})
556 * and internal state such as user preferences.</p>
557 *
koprivafc3949e2018-09-17 11:35:13 -0700558 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 * "edit in place" user model. That is, any edits a user makes are effectively
560 * made immediately without requiring an additional confirmation step.
561 * Supporting this model is generally a simple matter of following two rules:</p>
562 *
563 * <ul>
564 * <li> <p>When creating a new document, the backing database entry or file for
565 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700566 * 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 -0800567 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700568 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
570 * commit to the backing content provider or file any changes the user
571 * has made. This ensures that those changes will be seen by any other
572 * activity that is about to run. You will probably want to commit
573 * your data even more aggressively at key times during your
574 * activity's lifecycle: for example before starting a new
575 * activity, before finishing your own activity, when the user
576 * switches between input fields, etc.</p>
577 * </ul>
578 *
579 * <p>This model is designed to prevent data loss when a user is navigating
580 * between activities, and allows the system to safely kill an activity (because
581 * system resources are needed somewhere else) at any time after it has been
582 * paused. Note this implies
583 * that the user pressing BACK from your activity does <em>not</em>
584 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800585 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
587 *
588 * <p>See the {@linkplain android.content.ContentProvider content package} for
589 * more information about content providers. These are a key aspect of how
590 * different activities invoke and propagate data between themselves.</p>
591 *
592 * <p>The Activity class also provides an API for managing internal persistent state
593 * associated with an activity. This can be used, for example, to remember
594 * the user's preferred initial display in a calendar (day view or week view)
595 * or the user's default home page in a web browser.</p>
596 *
597 * <p>Activity persistent state is managed
598 * with the method {@link #getPreferences},
599 * allowing you to retrieve and
600 * modify a set of name/value pairs associated with the activity. To use
601 * preferences that are shared across multiple application components
602 * (activities, receivers, services, providers), you can use the underlying
603 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
604 * to retrieve a preferences
605 * object stored under a specific name.
606 * (Note that it is not possible to share settings data across application
607 * packages -- for that you will need a content provider.)</p>
608 *
609 * <p>Here is an excerpt from a calendar activity that stores the user's
610 * preferred view mode in its persistent settings:</p>
611 *
612 * <pre class="prettyprint">
613 * public class CalendarActivity extends Activity {
614 * ...
615 *
616 * static final int DAY_VIEW_MODE = 0;
617 * static final int WEEK_VIEW_MODE = 1;
618 *
619 * private SharedPreferences mPrefs;
620 * private int mCurViewMode;
621 *
622 * protected void onCreate(Bundle savedInstanceState) {
623 * super.onCreate(savedInstanceState);
624 *
625 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700626 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * }
628 *
629 * protected void onPause() {
630 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * SharedPreferences.Editor ed = mPrefs.edit();
633 * ed.putInt("view_mode", mCurViewMode);
634 * ed.commit();
635 * }
636 * }
637 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * <a name="Permissions"></a>
640 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700641 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 * <p>The ability to start a particular Activity can be enforced when it is
643 * declared in its
644 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
645 * tag. By doing so, other applications will need to declare a corresponding
646 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
647 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800648 *
649 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
650 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
651 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
652 * Activity access to the specific URIs in the Intent. Access will remain
653 * until the Activity has finished (it will remain across the hosting
654 * process being killed and other temporary destruction). As of
655 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
656 * was already created and a new Intent is being delivered to
657 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
658 * to the existing ones it holds.
659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
661 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 * <a name="ProcessLifecycle"></a>
664 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700665 *
kopriva4dddc632018-03-19 14:13:08 -0700666 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700668 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700670 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700672 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 * processes (the last ones) before it resorts to killing more important
674 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * <ol>
677 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
678 * that the user is currently interacting with) is considered the most important.
679 * Its process will only be killed as a last resort, if it uses more memory
680 * than is available on the device. Generally at this point the device has
681 * reached a memory paging state, so this is required in order to keep the user
682 * interface responsive.
683 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
684 * but not in the foreground, such as one sitting behind a foreground dialog)
685 * is considered extremely important and will not be killed unless that is
686 * required to keep the foreground activity running.
687 * <li> <p>A <b>background activity</b> (an activity that is not visible to
688 * the user and has been paused) is no longer critical, so the system may
689 * safely kill its process to reclaim memory for other foreground or
690 * visible processes. If its process needs to be killed, when the user navigates
691 * back to the activity (making it visible on the screen again), its
692 * {@link #onCreate} method will be called with the savedInstanceState it had previously
693 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
694 * state as the user last left it.
695 * <li> <p>An <b>empty process</b> is one hosting no activities or other
696 * application components (such as {@link Service} or
697 * {@link android.content.BroadcastReceiver} classes). These are killed very
698 * quickly by the system as memory becomes low. For this reason, any
699 * background operation you do outside of an activity must be executed in the
700 * context of an activity BroadcastReceiver or Service to ensure that the system
701 * knows it needs to keep your process around.
702 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700703 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * <p>Sometimes an Activity may need to do a long-running operation that exists
705 * independently of the activity lifecycle itself. An example may be a camera
706 * application that allows you to upload a picture to a web site. The upload
707 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700708 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 * should start a {@link Service} in which the upload takes place. This allows
710 * the system to properly prioritize your process (considering it to be more
711 * important than other non-visible applications) for the duration of the
712 * upload, independent of whether the original activity is paused, stopped,
713 * or finished.
714 */
715public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700716 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700718 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000719 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Leme640f30a2017-03-06 15:44:06 -0800720 AutofillManager.AutofillClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700722 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723
724 /** Standard activity result: operation canceled. */
725 public static final int RESULT_CANCELED = 0;
726 /** Standard activity result: operation succeeded. */
727 public static final int RESULT_OK = -1;
728 /** Start of user-defined activity results. */
729 public static final int RESULT_FIRST_USER = 1;
730
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700731 /** @hide Task isn't finished when activity is finished */
732 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700733 /**
734 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
735 * past behavior the task is also removed from recents.
736 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700737 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700738 /**
739 * @hide Task is finished along with the finishing activity, but it is not removed from
740 * recents.
741 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700742 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
743
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100744 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700745 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700746 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700747
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700748 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
750 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
751 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
752 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800753 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700754 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
755 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756
Svetoslav970b59c2015-06-09 16:05:21 -0700757 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000758 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700759
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100760 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100761
Andrii Kuliand25680c2018-02-21 15:16:58 -0800762 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
763 private static final int LOG_AM_ON_START_CALLED = 30059;
764 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
765 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
766 private static final int LOG_AM_ON_STOP_CALLED = 30049;
767 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
768 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700769 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800770
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800771 private static class ManagedDialog {
772 Dialog mDialog;
773 Bundle mArgs;
774 }
775 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776
777 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100778 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 private Instrumentation mInstrumentation;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100780 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 private IBinder mToken;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100782 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700783 private int mIdent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100784 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 /*package*/ String mEmbeddedID;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100786 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 private Application mApplication;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100788 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700789 /*package*/ Intent mIntent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100790 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800791 /*package*/ String mReferrer;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100792 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 private ComponentName mComponent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100794 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100796 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 /*package*/ ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100798 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 Activity mParent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100800 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 boolean mCalled;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100802 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600803 /*package*/ boolean mResumed;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100804 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700805 /*package*/ boolean mStopped;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100806 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 boolean mFinished;
808 boolean mStartedActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100809 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700810 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700811 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200812 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700813
814 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
815 * This only affects the client-side exception, the actual state check still happens in AMS. */
816 private boolean mCanEnterPictureInPicture = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700817 /** true if the activity is going through a transient pause */
818 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500819 /** true if the activity is being destroyed in order to recreate it with a new configuration */
820 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100821 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 /*package*/ int mConfigChangeFlags;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100823 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100825 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700826 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700828 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
829 @Nullable private AutofillManager mAutofillManager;
830
Felipe Lemeecb08be2018-11-27 15:48:47 -0800831 /** The content capture manager. Always access via {@link #getContentCaptureManager()}. */
832 @Nullable private ContentCaptureManager mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800833
Ian Lake8a88cd62018-09-28 13:25:20 -0700834 private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
835 new ArrayList<Application.ActivityLifecycleCallbacks>();
Felipe Lemee348dc32018-11-05 12:35:29 -0800836
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700837 static final class NonConfigurationInstances {
838 Object activity;
839 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800840 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700841 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700842 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700843 }
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100844 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700845 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700846
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100847 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 private Window mWindow;
849
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100850 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 private WindowManager mWindowManager;
852 /*package*/ View mDecor = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100853 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 /*package*/ boolean mWindowAdded = false;
855 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood31755f92018-12-20 13:53:36 +0000856 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700858 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700859 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860
Mathew Inwood8c854f82018-09-14 12:35:36 +0100861 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn91097de2014-04-04 18:02:06 -0700862 private VoiceInteractor mVoiceInteractor;
863
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100864 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 private CharSequence mTitle;
866 private int mTitleColor = 0;
867
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700868 // we must have a handler before the FragmentController is constructed
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100869 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700870 final Handler mHandler = new Handler();
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100871 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700872 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 private static final class ManagedCursor {
875 ManagedCursor(Cursor cursor) {
876 mCursor = cursor;
877 mReleased = false;
878 mUpdated = false;
879 }
880
881 private final Cursor mCursor;
882 private boolean mReleased;
883 private boolean mUpdated;
884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700886 @GuardedBy("mManagedCursors")
887 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
888
889 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100890 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700892 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100893 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700895
Craig Mautner5eda9b32013-07-02 11:58:16 -0700896 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700897 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700899 private SearchEvent mSearchEvent;
900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000902 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903
904 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
905 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700906
Winsonb6403152016-02-23 13:32:09 -0800907 private ActivityManager.TaskDescription mTaskDescription =
908 new ActivityManager.TaskDescription();
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
911
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700912 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700913 private final Object mInstanceTracker = StrictMode.trackActivity(this);
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700916
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100917 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700918 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700919 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
920 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921
Svetoslavffb32b12015-10-15 16:54:00 -0700922 private boolean mHasCurrentPermissionsRequest;
923
Svet Ganov17db9dc2017-02-21 19:54:31 -0800924 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800925 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800926
Phil Weaver846cda932017-06-15 10:10:06 -0700927 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
928 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700929
Felipe Leme4753bb02017-03-22 20:24:00 -0700930 private AutofillPopupWindow mAutofillPopupWindow;
931
chaviwfeb2e1e2018-12-19 17:24:11 -0800932 /** @hide */
933 boolean mEnterAnimationComplete;
934
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800935 private static native String getDlWarning();
936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 /** Return the intent that started this activity. */
938 public Intent getIntent() {
939 return mIntent;
940 }
941
RoboErik55011652014-07-09 15:05:53 -0700942 /**
943 * Change the intent returned by {@link #getIntent}. This holds a
944 * reference to the given intent; it does not copy it. Often used in
945 * conjunction with {@link #onNewIntent}.
946 *
947 * @param newIntent The new Intent object to return from getIntent
948 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 * @see #getIntent
950 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700951 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public void setIntent(Intent newIntent) {
953 mIntent = newIntent;
954 }
955
956 /** Return the application that owns this activity. */
957 public final Application getApplication() {
958 return mApplication;
959 }
960
961 /** Is this activity embedded inside of another activity? */
962 public final boolean isChild() {
963 return mParent != null;
964 }
RoboErik55011652014-07-09 15:05:53 -0700965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 /** Return the parent activity if this view is an embedded child. */
967 public final Activity getParent() {
968 return mParent;
969 }
970
971 /** Retrieve the window manager for showing custom windows. */
972 public WindowManager getWindowManager() {
973 return mWindowManager;
974 }
975
976 /**
977 * Retrieve the current {@link android.view.Window} for the activity.
978 * This can be used to directly access parts of the Window API that
979 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700980 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 * @return Window The current window, or null if the activity is not
982 * visual.
983 */
984 public Window getWindow() {
985 return mWindow;
986 }
987
988 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800989 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -0800990 *
991 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -0700992 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800993 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -0700994 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700995 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700996 }
RoboErik55011652014-07-09 15:05:53 -0700997
Dianne Hackbornc8017682010-07-06 13:34:38 -0700998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 * Calls {@link android.view.Window#getCurrentFocus} on the
1000 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -07001001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -07001003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 * @see #getWindow
1005 * @see android.view.Window#getCurrentFocus
1006 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001007 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 public View getCurrentFocus() {
1009 return mWindow != null ? mWindow.getCurrentFocus() : null;
1010 }
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 /**
Felipe Lemee348dc32018-11-05 12:35:29 -08001013 * (Creates, sets and) returns the autofill manager
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001014 *
1015 * @return The autofill manager
1016 */
1017 @NonNull private AutofillManager getAutofillManager() {
1018 if (mAutofillManager == null) {
1019 mAutofillManager = getSystemService(AutofillManager.class);
1020 }
1021
1022 return mAutofillManager;
1023 }
1024
Felipe Lemee348dc32018-11-05 12:35:29 -08001025 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -08001026 * (Creates, sets, and ) returns the content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001027 *
Felipe Lemeecb08be2018-11-27 15:48:47 -08001028 * @return The content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001029 */
Adam Heb66babb2019-01-08 15:43:53 -08001030 @Nullable private ContentCaptureManager getContentCaptureManager() {
1031 // ContextCapture disabled for system apps
Felipe Leme0c6b23d2019-01-15 17:02:49 -08001032 if (!UserHandle.isApp(myUid())) return null;
Felipe Lemeecb08be2018-11-27 15:48:47 -08001033 if (mContentCaptureManager == null) {
1034 mContentCaptureManager = getSystemService(ContentCaptureManager.class);
Felipe Lemee348dc32018-11-05 12:35:29 -08001035 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001036 return mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -08001037 }
1038
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001039 /** @hide */ private static final int CONTENT_CAPTURE_START = 1;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001040 /** @hide */ private static final int CONTENT_CAPTURE_RESUME = 2;
1041 /** @hide */ private static final int CONTENT_CAPTURE_PAUSE = 3;
1042 /** @hide */ private static final int CONTENT_CAPTURE_STOP = 4;
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001043
1044 /** @hide */
1045 @IntDef(prefix = { "CONTENT_CAPTURE_" }, value = {
1046 CONTENT_CAPTURE_START,
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001047 CONTENT_CAPTURE_RESUME,
1048 CONTENT_CAPTURE_PAUSE,
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001049 CONTENT_CAPTURE_STOP
1050 })
1051 @Retention(RetentionPolicy.SOURCE)
1052 @interface ContentCaptureNotificationType{}
1053
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001054 private String getContentCaptureTypeAsString(@ContentCaptureNotificationType int type) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001055 switch (type) {
1056 case CONTENT_CAPTURE_START:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001057 return "START";
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001058 case CONTENT_CAPTURE_RESUME:
1059 return "RESUME";
1060 case CONTENT_CAPTURE_PAUSE:
1061 return "PAUSE";
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001062 case CONTENT_CAPTURE_STOP:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001063 return "STOP";
Felipe Leme7a534082018-11-05 15:03:04 -08001064 default:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001065 return "UNKNOW-" + type;
1066 }
1067 }
1068
1069 private void notifyContentCaptureManagerIfNeeded(@ContentCaptureNotificationType int type) {
1070 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1071 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1072 "notifyContentCapture(" + getContentCaptureTypeAsString(type) + ") for "
1073 + mComponent.toShortString());
1074 }
1075 try {
1076 final ContentCaptureManager cm = getContentCaptureManager();
1077 if (cm == null) return;
1078
1079 switch (type) {
1080 case CONTENT_CAPTURE_START:
1081 //TODO(b/111276913): decide whether the InteractionSessionId should be
1082 // saved / restored in the activity bundle - probably not
1083 int flags = 0;
1084 if ((getWindow().getAttributes().flags
1085 & WindowManager.LayoutParams.FLAG_SECURE) != 0) {
1086 flags |= ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE;
1087 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001088 cm.onActivityCreated(mToken, getComponentName(), flags);
1089 break;
1090 case CONTENT_CAPTURE_RESUME:
1091 cm.onActivityResumed();
1092 break;
1093 case CONTENT_CAPTURE_PAUSE:
1094 cm.onActivityPaused();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001095 break;
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001096 case CONTENT_CAPTURE_STOP:
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001097 cm.onActivityDestroyed();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001098 break;
1099 default:
1100 Log.wtf(TAG, "Invalid @ContentCaptureNotificationType: " + type);
1101 }
1102 } finally {
1103 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Felipe Leme7a534082018-11-05 15:03:04 -08001104 }
1105 }
1106
Felipe Lemebb567ae2017-10-04 09:56:21 -07001107 @Override
1108 protected void attachBaseContext(Context newBase) {
1109 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001110 if (newBase != null) {
1111 newBase.setAutofillClient(this);
Felipe Leme326f15a2019-02-19 09:42:24 -08001112 newBase.setContentCaptureOptions(getContentCaptureOptions());
Felipe Lemed247de82018-05-14 17:51:58 -07001113 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001114 }
1115
1116 /** @hide */
1117 @Override
1118 public final AutofillClient getAutofillClient() {
1119 return this;
1120 }
1121
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001122 /**
Ian Lake8a88cd62018-09-28 13:25:20 -07001123 * Register an {@link Application.ActivityLifecycleCallbacks} instance that receives
1124 * lifecycle callbacks for only this Activity.
1125 * <p>
1126 * In relation to any
1127 * {@link Application#registerActivityLifecycleCallbacks Application registered callbacks},
1128 * the callbacks registered here will always occur nested within those callbacks. This means:
1129 * <ul>
1130 * <li>Pre events will first be sent to Application registered callbacks, then to callbacks
1131 * registered here.</li>
1132 * <li>{@link Application.ActivityLifecycleCallbacks#onActivityCreated(Activity, Bundle)},
1133 * {@link Application.ActivityLifecycleCallbacks#onActivityStarted(Activity)}, and
1134 * {@link Application.ActivityLifecycleCallbacks#onActivityResumed(Activity)} will
1135 * be sent first to Application registered callbacks, then to callbacks registered here.
1136 * For all other events, callbacks registered here will be sent first.</li>
1137 * <li>Post events will first be sent to callbacks registered here, then to
1138 * Application registered callbacks.</li>
1139 * </ul>
1140 * <p>
1141 * If multiple callbacks are registered here, they receive events in a first in (up through
1142 * {@link Application.ActivityLifecycleCallbacks#onActivityPostResumed}, last out
1143 * ordering.
1144 * <p>
1145 * It is strongly recommended to register this in the constructor of your Activity to ensure
1146 * you get all available callbacks. As this callback is associated with only this Activity,
1147 * it is not usually necessary to {@link #unregisterActivityLifecycleCallbacks unregister} it
1148 * unless you specifically do not want to receive further lifecycle callbacks.
1149 *
1150 * @param callback The callback instance to register
1151 */
1152 public void registerActivityLifecycleCallbacks(
1153 @NonNull Application.ActivityLifecycleCallbacks callback) {
1154 synchronized (mActivityLifecycleCallbacks) {
1155 mActivityLifecycleCallbacks.add(callback);
1156 }
1157 }
1158
1159 /**
1160 * Unregister an {@link Application.ActivityLifecycleCallbacks} previously registered
1161 * with {@link #registerActivityLifecycleCallbacks}. It will not receive any further
1162 * callbacks.
1163 *
1164 * @param callback The callback instance to unregister
1165 * @see #registerActivityLifecycleCallbacks
1166 */
1167 public void unregisterActivityLifecycleCallbacks(
1168 @NonNull Application.ActivityLifecycleCallbacks callback) {
1169 synchronized (mActivityLifecycleCallbacks) {
1170 mActivityLifecycleCallbacks.remove(callback);
1171 }
1172 }
1173
1174 private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
1175 getApplication().dispatchActivityPreCreated(this, savedInstanceState);
1176 Object[] callbacks = collectActivityLifecycleCallbacks();
1177 if (callbacks != null) {
1178 for (int i = 0; i < callbacks.length; i++) {
1179 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
1180 savedInstanceState);
1181 }
1182 }
1183 }
1184
1185 private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
1186 getApplication().dispatchActivityCreated(this, savedInstanceState);
1187 Object[] callbacks = collectActivityLifecycleCallbacks();
1188 if (callbacks != null) {
1189 for (int i = 0; i < callbacks.length; i++) {
1190 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
1191 savedInstanceState);
1192 }
1193 }
1194 }
1195
1196 private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
1197 Object[] callbacks = collectActivityLifecycleCallbacks();
1198 if (callbacks != null) {
1199 for (int i = 0; i < callbacks.length; i++) {
1200 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
1201 savedInstanceState);
1202 }
1203 }
1204 getApplication().dispatchActivityPostCreated(this, savedInstanceState);
1205 }
1206
1207 private void dispatchActivityPreStarted() {
1208 getApplication().dispatchActivityPreStarted(this);
1209 Object[] callbacks = collectActivityLifecycleCallbacks();
1210 if (callbacks != null) {
1211 for (int i = 0; i < callbacks.length; i++) {
1212 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(this);
1213 }
1214 }
1215 }
1216
1217 private void dispatchActivityStarted() {
1218 getApplication().dispatchActivityStarted(this);
1219 Object[] callbacks = collectActivityLifecycleCallbacks();
1220 if (callbacks != null) {
1221 for (int i = 0; i < callbacks.length; i++) {
1222 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
1223 }
1224 }
1225 }
1226
1227 private void dispatchActivityPostStarted() {
1228 Object[] callbacks = collectActivityLifecycleCallbacks();
1229 if (callbacks != null) {
1230 for (int i = 0; i < callbacks.length; i++) {
1231 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1232 .onActivityPostStarted(this);
1233 }
1234 }
1235 getApplication().dispatchActivityPostStarted(this);
1236 }
1237
1238 private void dispatchActivityPreResumed() {
1239 getApplication().dispatchActivityPreResumed(this);
1240 Object[] callbacks = collectActivityLifecycleCallbacks();
1241 if (callbacks != null) {
1242 for (int i = 0; i < callbacks.length; i++) {
1243 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(this);
1244 }
1245 }
1246 }
1247
1248 private void dispatchActivityResumed() {
1249 getApplication().dispatchActivityResumed(this);
1250 Object[] callbacks = collectActivityLifecycleCallbacks();
1251 if (callbacks != null) {
1252 for (int i = 0; i < callbacks.length; i++) {
1253 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
1254 }
1255 }
1256 }
1257
1258 private void dispatchActivityPostResumed() {
1259 Object[] callbacks = collectActivityLifecycleCallbacks();
1260 if (callbacks != null) {
1261 for (int i = 0; i < callbacks.length; i++) {
1262 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(this);
1263 }
1264 }
1265 getApplication().dispatchActivityPostResumed(this);
1266 }
1267
1268 private void dispatchActivityPrePaused() {
1269 getApplication().dispatchActivityPrePaused(this);
1270 Object[] callbacks = collectActivityLifecycleCallbacks();
1271 if (callbacks != null) {
1272 for (int i = callbacks.length - 1; i >= 0; i--) {
1273 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(this);
1274 }
1275 }
1276 }
1277
1278 private void dispatchActivityPaused() {
1279 Object[] callbacks = collectActivityLifecycleCallbacks();
1280 if (callbacks != null) {
1281 for (int i = callbacks.length - 1; i >= 0; i--) {
1282 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(this);
1283 }
1284 }
1285 getApplication().dispatchActivityPaused(this);
1286 }
1287
1288 private void dispatchActivityPostPaused() {
1289 Object[] callbacks = collectActivityLifecycleCallbacks();
1290 if (callbacks != null) {
1291 for (int i = callbacks.length - 1; i >= 0; i--) {
1292 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(this);
1293 }
1294 }
1295 getApplication().dispatchActivityPostPaused(this);
1296 }
1297
1298 private void dispatchActivityPreStopped() {
1299 getApplication().dispatchActivityPreStopped(this);
1300 Object[] callbacks = collectActivityLifecycleCallbacks();
1301 if (callbacks != null) {
1302 for (int i = callbacks.length - 1; i >= 0; i--) {
1303 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(this);
1304 }
1305 }
1306 }
1307
1308 private void dispatchActivityStopped() {
1309 Object[] callbacks = collectActivityLifecycleCallbacks();
1310 if (callbacks != null) {
1311 for (int i = callbacks.length - 1; i >= 0; i--) {
1312 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(this);
1313 }
1314 }
1315 getApplication().dispatchActivityStopped(this);
1316 }
1317
1318 private void dispatchActivityPostStopped() {
1319 Object[] callbacks = collectActivityLifecycleCallbacks();
1320 if (callbacks != null) {
1321 for (int i = callbacks.length - 1; i >= 0; i--) {
1322 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1323 .onActivityPostStopped(this);
1324 }
1325 }
1326 getApplication().dispatchActivityPostStopped(this);
1327 }
1328
1329 private void dispatchActivityPreSaveInstanceState(@NonNull Bundle outState) {
1330 getApplication().dispatchActivityPreSaveInstanceState(this, outState);
1331 Object[] callbacks = collectActivityLifecycleCallbacks();
1332 if (callbacks != null) {
1333 for (int i = callbacks.length - 1; i >= 0; i--) {
1334 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1335 .onActivityPreSaveInstanceState(this, outState);
1336 }
1337 }
1338 }
1339
1340 private void dispatchActivitySaveInstanceState(@NonNull Bundle outState) {
1341 Object[] callbacks = collectActivityLifecycleCallbacks();
1342 if (callbacks != null) {
1343 for (int i = callbacks.length - 1; i >= 0; i--) {
1344 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1345 .onActivitySaveInstanceState(this, outState);
1346 }
1347 }
1348 getApplication().dispatchActivitySaveInstanceState(this, outState);
1349 }
1350
1351 private void dispatchActivityPostSaveInstanceState(@NonNull Bundle outState) {
1352 Object[] callbacks = collectActivityLifecycleCallbacks();
1353 if (callbacks != null) {
1354 for (int i = callbacks.length - 1; i >= 0; i--) {
1355 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1356 .onActivityPostSaveInstanceState(this, outState);
1357 }
1358 }
1359 getApplication().dispatchActivityPostSaveInstanceState(this, outState);
1360 }
1361
1362 private void dispatchActivityPreDestroyed() {
1363 getApplication().dispatchActivityPreDestroyed(this);
1364 Object[] callbacks = collectActivityLifecycleCallbacks();
1365 if (callbacks != null) {
1366 for (int i = callbacks.length - 1; i >= 0; i--) {
1367 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1368 .onActivityPreDestroyed(this);
1369 }
1370 }
1371 }
1372
1373 private void dispatchActivityDestroyed() {
1374 Object[] callbacks = collectActivityLifecycleCallbacks();
1375 if (callbacks != null) {
1376 for (int i = callbacks.length - 1; i >= 0; i--) {
1377 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
1378 }
1379 }
1380 getApplication().dispatchActivityDestroyed(this);
1381 }
1382
1383 private void dispatchActivityPostDestroyed() {
1384 Object[] callbacks = collectActivityLifecycleCallbacks();
1385 if (callbacks != null) {
1386 for (int i = callbacks.length - 1; i >= 0; i--) {
1387 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1388 .onActivityPostDestroyed(this);
1389 }
1390 }
1391 getApplication().dispatchActivityPostDestroyed(this);
1392 }
1393
1394 private Object[] collectActivityLifecycleCallbacks() {
1395 Object[] callbacks = null;
1396 synchronized (mActivityLifecycleCallbacks) {
1397 if (mActivityLifecycleCallbacks.size() > 0) {
1398 callbacks = mActivityLifecycleCallbacks.toArray();
1399 }
1400 }
1401 return callbacks;
1402 }
1403
1404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 * Called when the activity is starting. This is where most initialization
1406 * should go: calling {@link #setContentView(int)} to inflate the
1407 * activity's UI, using {@link #findViewById} to programmatically interact
1408 * with widgets in the UI, calling
1409 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1410 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001411 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001413 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1414 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1415 * executing.
RoboErik55011652014-07-09 15:05:53 -07001416 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 * <p><em>Derived classes must call through to the super class's
1418 * implementation of this method. If they do not, an exception will be
1419 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001420 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 * @param savedInstanceState If the activity is being re-initialized after
1422 * previously being shut down then this Bundle contains the data it most
1423 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 * @see #onStart
1426 * @see #onSaveInstanceState
1427 * @see #onRestoreInstanceState
1428 * @see #onPostCreate
1429 */
Tor Norbye83c68962015-03-10 20:55:31 -07001430 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001431 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001432 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001433 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001434
Dianne Hackborn2707d602010-07-09 18:01:20 -07001435 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001436 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001437 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001438 if (mActivityInfo.parentActivityName != null) {
1439 if (mActionBar == null) {
1440 mEnableDefaultActionBarUp = true;
1441 } else {
1442 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1443 }
1444 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001445 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001446 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001447 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1448 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001449
1450 if (mAutoFillResetNeeded) {
1451 getAutofillManager().onCreate(savedInstanceState);
1452 }
1453
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001454 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1455 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1456 ? mLastNonConfigurationInstances.fragments : null);
1457 }
1458 mFragments.dispatchCreate();
Ian Lake8a88cd62018-09-28 13:25:20 -07001459 dispatchActivityCreated(savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001460 if (mVoiceInteractor != null) {
1461 mVoiceInteractor.attachActivity(this);
1462 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001463 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 mCalled = true;
Felipe Lemee348dc32018-11-05 12:35:29 -08001465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467
1468 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001469 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001470 * the attribute {@link android.R.attr#persistableMode} set to
1471 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001472 *
1473 * @param savedInstanceState if the activity is being re-initialized after
1474 * previously being shut down then this Bundle contains the data it most
1475 * recently supplied in {@link #onSaveInstanceState}.
1476 * <b><i>Note: Otherwise it is null.</i></b>
1477 * @param persistentState if the activity is being re-initialized after
1478 * previously being shut down or powered off then this Bundle contains the data it most
1479 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1480 * <b><i>Note: Otherwise it is null.</i></b>
1481 *
1482 * @see #onCreate(android.os.Bundle)
1483 * @see #onStart
1484 * @see #onSaveInstanceState
1485 * @see #onRestoreInstanceState
1486 * @see #onPostCreate
1487 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001488 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001489 @Nullable PersistableBundle persistentState) {
1490 onCreate(savedInstanceState);
1491 }
1492
1493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * The hook for {@link ActivityThread} to restore the state of this activity.
1495 *
1496 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1497 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1498 *
1499 * @param savedInstanceState contains the saved state
1500 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001501 final void performRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 onRestoreInstanceState(savedInstanceState);
1503 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
1505
1506 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001507 * The hook for {@link ActivityThread} to restore the state of this activity.
1508 *
1509 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1510 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1511 *
1512 * @param savedInstanceState contains the saved state
1513 * @param persistentState contains the persistable saved state
1514 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001515 final void performRestoreInstanceState(@Nullable Bundle savedInstanceState,
1516 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001517 onRestoreInstanceState(savedInstanceState, persistentState);
1518 if (savedInstanceState != null) {
1519 restoreManagedDialogs(savedInstanceState);
1520 }
1521 }
1522
1523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 * This method is called after {@link #onStart} when the activity is
1525 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001526 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 * to restore their state, but it is sometimes convenient to do it here
1528 * after all of the initialization has been done or to allow subclasses to
1529 * decide whether to use your default implementation. The default
1530 * implementation of this method performs a restore of any view state that
1531 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 * <p>This method is called between {@link #onStart} and
1534 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001535 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001537 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * @see #onCreate
1539 * @see #onPostCreate
1540 * @see #onResume
1541 * @see #onSaveInstanceState
1542 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001543 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (mWindow != null) {
1545 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1546 if (windowState != null) {
1547 mWindow.restoreHierarchyState(windowState);
1548 }
1549 }
1550 }
Craig Mautnera0026042014-04-23 11:45:37 -07001551
1552 /**
1553 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001554 * created with the attribute {@link android.R.attr#persistableMode} set to
1555 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1556 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001557 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1558 *
1559 * <p>This method is called between {@link #onStart} and
1560 * {@link #onPostCreate}.
1561 *
1562 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1563 *
Jake Wharton63f4d892018-07-10 12:27:59 -04001564 * <p>At least one of {@code savedInstanceState} or {@code persistentState} will not be null.
1565 *
1566 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}
1567 * or null.
1568 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}
1569 * or null.
Craig Mautnera0026042014-04-23 11:45:37 -07001570 *
1571 * @see #onRestoreInstanceState(Bundle)
1572 * @see #onCreate
1573 * @see #onPostCreate
1574 * @see #onResume
1575 * @see #onSaveInstanceState
1576 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001577 public void onRestoreInstanceState(@Nullable Bundle savedInstanceState,
1578 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001579 if (savedInstanceState != null) {
1580 onRestoreInstanceState(savedInstanceState);
1581 }
1582 }
1583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 /**
1585 * Restore the state of any saved managed dialogs.
1586 *
1587 * @param savedInstanceState The bundle to restore from.
1588 */
1589 private void restoreManagedDialogs(Bundle savedInstanceState) {
1590 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1591 if (b == null) {
1592 return;
1593 }
1594
1595 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1596 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001597 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 for (int i = 0; i < numDialogs; i++) {
1599 final Integer dialogId = ids[i];
1600 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1601 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001602 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1603 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001604 final ManagedDialog md = new ManagedDialog();
1605 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1606 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1607 if (md.mDialog != null) {
1608 mManagedDialogs.put(dialogId, md);
1609 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1610 md.mDialog.onRestoreInstanceState(dialogState);
1611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 }
1614 }
1615
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001616 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1617 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001618 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001619 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001620 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001621 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001622 return dialog;
1623 }
1624
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001625 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 return SAVED_DIALOG_KEY_PREFIX + key;
1627 }
1628
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001629 private static String savedDialogArgsKeyFor(int key) {
1630 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632
1633 /**
1634 * Called when activity start-up is complete (after {@link #onStart}
1635 * and {@link #onRestoreInstanceState} have been called). Applications will
1636 * generally not implement this method; it is intended for system
1637 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 * <p><em>Derived classes must call through to the super class's
1640 * implementation of this method. If they do not, an exception will be
1641 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 * @param savedInstanceState If the activity is being re-initialized after
1644 * previously being shut down then this Bundle contains the data it most
1645 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1646 * @see #onCreate
1647 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001648 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001649 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 if (!isChild()) {
1651 mTitleReady = true;
1652 onTitleChanged(getTitle(), getTitleColor());
1653 }
Winsonb6403152016-02-23 13:32:09 -08001654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 mCalled = true;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001656
1657 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_START);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659
1660 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001661 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001662 * created with the attribute {@link android.R.attr#persistableMode} set to
1663 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001664 *
1665 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1666 * @param persistentState The data caming from the PersistableBundle first
1667 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1668 *
1669 * @see #onCreate
1670 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001671 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001672 @Nullable PersistableBundle persistentState) {
1673 onPostCreate(savedInstanceState);
1674 }
1675
1676 /**
RoboErik55011652014-07-09 15:05:53 -07001677 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1678 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001679 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 *
1681 * <p><em>Derived classes must call through to the super class's
1682 * implementation of this method. If they do not, an exception will be
1683 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 * @see #onCreate
1686 * @see #onStop
1687 * @see #onResume
1688 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001689 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001691 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001693
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001694 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001695
Ian Lake8a88cd62018-09-28 13:25:20 -07001696 dispatchActivityStarted();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001697
1698 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001699 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702
1703 /**
1704 * Called after {@link #onStop} when the current activity is being
1705 * re-displayed to the user (the user has navigated back to it). It will
1706 * be followed by {@link #onStart} and then {@link #onResume}.
1707 *
1708 * <p>For activities that are using raw {@link Cursor} objects (instead of
1709 * creating them through
1710 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1711 * this is usually the place
1712 * where the cursor should be requeried (because you had deactivated it in
1713 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001714 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 * <p><em>Derived classes must call through to the super class's
1716 * implementation of this method. If they do not, an exception will be
1717 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001718 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 * @see #onStop
1720 * @see #onStart
1721 * @see #onResume
1722 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001723 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 protected void onRestart() {
1725 mCalled = true;
1726 }
1727
1728 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001729 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1730 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1731 * to give the activity a hint that its state is no longer saved -- it will generally
1732 * be called after {@link #onSaveInstanceState} and prior to the activity being
1733 * resumed/started again.
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001734 *
1735 * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
1736 * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
1737 * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001738 */
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001739 @Deprecated
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001740 public void onStateNotSaved() {
1741 }
1742
1743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1745 * {@link #onPause}, for your activity to start interacting with the user.
1746 * This is a good place to begin animations, open exclusive-access devices
1747 * (such as the camera), etc.
1748 *
1749 * <p>Keep in mind that onResume is not the best indicator that your activity
1750 * is visible to the user; a system window such as the keyguard may be in
1751 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1752 * activity is visible to the user (for example, to resume a game).
1753 *
1754 * <p><em>Derived classes must call through to the super class's
1755 * implementation of this method. If they do not, an exception will be
1756 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 * @see #onRestoreInstanceState
1759 * @see #onRestart
1760 * @see #onPostResume
1761 * @see #onPause
1762 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001763 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001765 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07001766 dispatchActivityResumed();
Sunny Goyal64643f62019-01-31 15:15:33 -08001767 mActivityTransitionState.onResume(this);
Felipe Lemece404982018-09-17 09:46:13 -07001768 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001769 if (mAutoFillResetNeeded) {
1770 if (!mAutoFillIgnoreFirstResumePause) {
1771 View focus = getCurrentFocus();
Ben Line55cd3e2018-12-07 20:26:37 +00001772 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
Dake Gu67decfa2017-12-27 11:48:08 -08001773 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1774 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1775 // window visibility after recreation is INVISIBLE in onResume() and next frame
1776 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1777 // So we cannot call View.notifyEnterOrExited() which will do nothing
1778 // when View.isVisibleToUser() is false.
1779 getAutofillManager().notifyViewEntered(focus);
1780 }
1781 }
1782 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001783
1784 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);
1785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 mCalled = true;
1787 }
1788
1789 /**
1790 * Called when activity resume is complete (after {@link #onResume} has
1791 * been called). Applications will generally not implement this method;
1792 * it is intended for system classes to do final setup after application
1793 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001794 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 * <p><em>Derived classes must call through to the super class's
1796 * implementation of this method. If they do not, an exception will be
1797 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001798 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 * @see #onResume
1800 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001801 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 protected void onPostResume() {
1803 final Window win = getWindow();
1804 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001805 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 mCalled = true;
1807 }
1808
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001809 /**
1810 * Called when activity gets or looses the top resumed position in the system.
1811 *
1812 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} multiple activities can be resumed
1813 * at the same time in multi-window and multi-display modes. This callback should be used
1814 * instead of {@link #onResume()} as an indication that the activity can try to open
1815 * exclusive-access devices like camera.</p>
1816 *
1817 * <p>It will always be delivered after the activity was resumed and before it is paused. In
1818 * some cases it might be skipped and activity can go straight from {@link #onResume()} to
1819 * {@link #onPause()} without receiving the top resumed state.</p>
1820 *
1821 * @param isTopResumedActivity {@code true} if it's the topmost resumed activity in the system,
1822 * {@code false} otherwise. A call with this as {@code true} will
1823 * always be followed by another one with {@code false}.
1824 *
1825 * @see #onResume()
1826 * @see #onPause()
1827 * @see #onWindowFocusChanged(boolean)
1828 */
1829 public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
1830 }
1831
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001832 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001833 if (mVoiceInteractor != null) {
1834 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1835 activeRequest.cancel();
1836 activeRequest.clear();
1837 }
1838 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001839 if (voiceInteractor == null) {
1840 mVoiceInteractor = null;
1841 } else {
1842 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1843 Looper.myLooper());
1844 }
1845 }
1846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001848 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001849 *
Phil Weaver846cda932017-06-15 10:10:06 -07001850 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001851 * will be unique.
1852 *
1853 * @return A ID that is unique in the activity
1854 *
1855 * {@hide}
1856 */
Felipe Leme42b97932018-02-20 13:04:31 -08001857 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001858 public int getNextAutofillId() {
1859 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1860 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001861 }
1862
Phil Weaver846cda932017-06-15 10:10:06 -07001863 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001864
Phil Weaver846cda932017-06-15 10:10:06 -07001865 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001866 }
1867
1868 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001869 * @hide
1870 */
1871 @Override
1872 public AutofillId autofillClientGetNextAutofillId() {
Felipe Leme305fd402018-09-11 18:20:42 +00001873 return new AutofillId(getNextAutofillId());
Felipe Leme42b97932018-02-20 13:04:31 -08001874 }
1875
1876 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001877 * Check whether this activity is running as part of a voice interaction with the user.
1878 * If true, it should perform its interaction with the user through the
1879 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1880 */
1881 public boolean isVoiceInteraction() {
1882 return mVoiceInteractor != null;
1883 }
1884
1885 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001886 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1887 * of a voice interaction. That is, returns true if this activity was directly
1888 * started by the voice interaction service as the initiation of a voice interaction.
1889 * Otherwise, for example if it was started by another activity while under voice
1890 * interaction, returns false.
1891 */
1892 public boolean isVoiceInteractionRoot() {
1893 try {
1894 return mVoiceInteractor != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001895 && ActivityTaskManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001896 } catch (RemoteException e) {
1897 }
1898 return false;
1899 }
1900
1901 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001902 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1903 * interact with this activity.
1904 */
1905 public VoiceInteractor getVoiceInteractor() {
1906 return mVoiceInteractor;
1907 }
1908
1909 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001910 * Queries whether the currently enabled voice interaction service supports returning
1911 * a voice interactor for use by the activity. This is valid only for the duration of the
1912 * activity.
1913 *
1914 * @return whether the current voice interaction service supports local voice interaction
1915 */
1916 public boolean isLocalVoiceInteractionSupported() {
1917 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001918 return ActivityTaskManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001919 } catch (RemoteException re) {
1920 }
1921 return false;
1922 }
1923
1924 /**
1925 * Starts a local voice interaction session. When ready,
1926 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1927 * to the registered voice interaction service.
1928 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1929 */
1930 public void startLocalVoiceInteraction(Bundle privateOptions) {
1931 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001932 ActivityTaskManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001933 } catch (RemoteException re) {
1934 }
1935 }
1936
1937 /**
1938 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1939 * voice interaction session being started. You can now retrieve a voice interactor using
1940 * {@link #getVoiceInteractor()}.
1941 */
1942 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001943 }
1944
1945 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001946 * Callback to indicate that the local voice interaction has stopped either
1947 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1948 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1949 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001950 */
1951 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001952 }
1953
1954 /**
1955 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001956 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1957 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001958 */
1959 public void stopLocalVoiceInteraction() {
1960 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001961 ActivityTaskManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001962 } catch (RemoteException re) {
1963 }
1964 }
1965
1966 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 * This is called for activities that set launchMode to "singleTop" in
1968 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1969 * flag when calling {@link #startActivity}. In either case, when the
1970 * activity is re-launched while at the top of the activity stack instead
1971 * of a new instance of the activity being started, onNewIntent() will be
1972 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001973 * re-launch it.
1974 *
1975 * <p>An activity will always be paused before receiving a new intent, so
1976 * you can count on {@link #onResume} being called after this method.
1977 *
1978 * <p>Note that {@link #getIntent} still returns the original Intent. You
1979 * can use {@link #setIntent} to update it to this new Intent.
1980 *
1981 * @param intent The new intent that was started for the activity.
1982 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001984 * @see #setIntent
1985 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 */
1987 protected void onNewIntent(Intent intent) {
1988 }
1989
1990 /**
1991 * The hook for {@link ActivityThread} to save the state of this activity.
1992 *
1993 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1994 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1995 *
1996 * @param outState The bundle to save the state to.
1997 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001998 final void performSaveInstanceState(@NonNull Bundle outState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07001999 dispatchActivityPreSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 onSaveInstanceState(outState);
2001 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07002002 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002003 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002004 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002005 dispatchActivityPostSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
2007
2008 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002009 * The hook for {@link ActivityThread} to save the state of this activity.
2010 *
2011 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2012 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2013 *
2014 * @param outState The bundle to save the state to.
2015 * @param outPersistentState The bundle to save persistent state to.
2016 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002017 final void performSaveInstanceState(@NonNull Bundle outState,
2018 @NonNull PersistableBundle outPersistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002019 dispatchActivityPreSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002020 onSaveInstanceState(outState, outPersistentState);
2021 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002022 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002023 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
2024 ", " + outPersistentState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002025 dispatchActivityPostSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002026 }
2027
2028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 * Called to retrieve per-instance state from an activity before being killed
2030 * so that the state can be restored in {@link #onCreate} or
2031 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
2032 * will be passed to both).
2033 *
2034 * <p>This method is called before an activity may be killed so that when it
2035 * comes back some time in the future it can restore its state. For example,
2036 * if activity B is launched in front of activity A, and at some point activity
2037 * A is killed to reclaim resources, activity A will have a chance to save the
2038 * current state of its user interface via this method so that when the user
2039 * returns to activity A, the state of the user interface can be restored
2040 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
2041 *
2042 * <p>Do not confuse this method with activity lifecycle callbacks such as
2043 * {@link #onPause}, which is always called when an activity is being placed
2044 * in the background or on its way to destruction, or {@link #onStop} which
2045 * is called before destruction. One example of when {@link #onPause} and
2046 * {@link #onStop} is called and not this method is when a user navigates back
2047 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
2048 * on B because that particular instance will never be restored, so the
2049 * system avoids calling it. An example when {@link #onPause} is called and
2050 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
2051 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
2052 * killed during the lifetime of B since the state of the user interface of
2053 * A will stay intact.
2054 *
2055 * <p>The default implementation takes care of most of the UI per-instance
2056 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
2057 * view in the hierarchy that has an id, and by saving the id of the currently
2058 * focused view (all of which is restored by the default implementation of
2059 * {@link #onRestoreInstanceState}). If you override this method to save additional
2060 * information not captured by each individual view, you will likely want to
2061 * call through to the default implementation, otherwise be prepared to save
2062 * all of the state of each view yourself.
2063 *
Andrii Kulian391161f2018-01-29 10:50:02 -08002064 * <p>If called, this method will occur after {@link #onStop} for applications
2065 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
2066 * For applications targeting earlier platform versions this method will occur
2067 * before {@link #onStop} and there are no guarantees about whether it will
2068 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07002069 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07002071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 * @see #onCreate
2073 * @see #onRestoreInstanceState
2074 * @see #onPause
2075 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002076 protected void onSaveInstanceState(@NonNull Bundle outState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002078
Phil Weaver846cda932017-06-15 10:10:06 -07002079 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002080 Parcelable p = mFragments.saveAllState();
2081 if (p != null) {
2082 outState.putParcelable(FRAGMENTS_TAG, p);
2083 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002084 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002085 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
2086 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002087 }
Ian Lake8a88cd62018-09-28 13:25:20 -07002088 dispatchActivitySaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
2090
2091 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002092 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07002093 * created with the attribute {@link android.R.attr#persistableMode} set to
2094 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
2095 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
2096 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07002097 *
2098 * @param outState Bundle in which to place your saved state.
2099 * @param outPersistentState State which will be saved across reboots.
2100 *
2101 * @see #onSaveInstanceState(Bundle)
2102 * @see #onCreate
2103 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
2104 * @see #onPause
2105 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002106 public void onSaveInstanceState(@NonNull Bundle outState,
2107 @NonNull PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07002108 onSaveInstanceState(outState);
2109 }
2110
2111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 * Save the state of any managed dialogs.
2113 *
2114 * @param outState place to store the saved state.
2115 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002116 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 private void saveManagedDialogs(Bundle outState) {
2118 if (mManagedDialogs == null) {
2119 return;
2120 }
2121
2122 final int numDialogs = mManagedDialogs.size();
2123 if (numDialogs == 0) {
2124 return;
2125 }
2126
2127 Bundle dialogState = new Bundle();
2128
2129 int[] ids = new int[mManagedDialogs.size()];
2130
2131 // save each dialog's bundle, gather the ids
2132 for (int i = 0; i < numDialogs; i++) {
2133 final int key = mManagedDialogs.keyAt(i);
2134 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002135 final ManagedDialog md = mManagedDialogs.valueAt(i);
2136 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
2137 if (md.mArgs != null) {
2138 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
2139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141
2142 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
2143 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
2144 }
2145
2146
2147 /**
2148 * Called as part of the activity lifecycle when an activity is going into
2149 * the background, but has not (yet) been killed. The counterpart to
2150 * {@link #onResume}.
2151 *
2152 * <p>When activity B is launched in front of activity A, this callback will
2153 * be invoked on A. B will not be created until A's {@link #onPause} returns,
2154 * so be sure to not do anything lengthy here.
2155 *
2156 * <p>This callback is mostly used for saving any persistent state the
2157 * activity is editing, to present a "edit in place" model to the user and
2158 * making sure nothing is lost if there are not enough resources to start
2159 * the new activity without first killing this one. This is also a good
2160 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08002161 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 * as fast as possible, or to close resources that are exclusive access
2163 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07002164 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 * <p>In situations where the system needs more memory it may kill paused
2166 * processes to reclaim resources. Because of this, you should be sure
2167 * that all of your state is saved by the time you return from
2168 * this function. In general {@link #onSaveInstanceState} is used to save
2169 * per-instance state in the activity and this method is used to store
2170 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07002171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 * <p>After receiving this call you will usually receive a following call
2173 * to {@link #onStop} (after the next activity has been resumed and
2174 * displayed), however in some cases there will be a direct call back to
2175 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07002176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 * <p><em>Derived classes must call through to the super class's
2178 * implementation of this method. If they do not, an exception will be
2179 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 * @see #onResume
2182 * @see #onSaveInstanceState
2183 * @see #onStop
2184 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002185 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002187 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07002188 dispatchActivityPaused();
Dake Gu67decfa2017-12-27 11:48:08 -08002189 if (mAutoFillResetNeeded) {
2190 if (!mAutoFillIgnoreFirstResumePause) {
2191 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
2192 View focus = getCurrentFocus();
2193 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
2194 getAutofillManager().notifyViewExited(focus);
2195 }
2196 } else {
2197 // reset after first pause()
2198 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
2199 mAutoFillIgnoreFirstResumePause = false;
2200 }
2201 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002202
2203 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_PAUSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 mCalled = true;
2205 }
2206
2207 /**
2208 * Called as part of the activity lifecycle when an activity is about to go
2209 * into the background as the result of user choice. For example, when the
2210 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
2211 * when an incoming phone call causes the in-call Activity to be automatically
2212 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
2213 * the activity being interrupted. In cases when it is invoked, this method
2214 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07002215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 * <p>This callback and {@link #onUserInteraction} are intended to help
2217 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002218 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07002219 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 * @see #onUserInteraction()
Louis Changc2e193a2019-01-09 15:33:57 +08002221 * @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 */
2223 protected void onUserLeaveHint() {
2224 }
RoboErik55011652014-07-09 15:05:53 -07002225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002227 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002229 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002231 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
2233
2234 /**
2235 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002236 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07002238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 * <p>The default implementation returns null, which will cause you to
2240 * inherit the description from the previous activity. If all activities
2241 * return null, generally the label of the top activity will be used as the
2242 * description.
RoboErik55011652014-07-09 15:05:53 -07002243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 * @return A description of what the user is doing. It should be short and
2245 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07002246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002248 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002250 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 public CharSequence onCreateDescription() {
2252 return null;
2253 }
2254
2255 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002256 * This is called when the user is requesting an assist, to build a full
2257 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
2258 * application. You can override this method to place into the bundle anything
2259 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002260 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002261 *
2262 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07002263 * been registered with {@link Application#registerOnProvideAssistDataListener
2264 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002265 */
2266 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002267 }
2268
2269 /**
2270 * This is called when the user is requesting an assist, to provide references
2271 * to content related to the current activity. Before being called, the
2272 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
2273 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
2274 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
2275 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
2276 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
2277 *
2278 * <p>Custom implementation may adjust the content intent to better reflect the top-level
2279 * context of the activity, and fill in its ClipData with additional content of
2280 * interest that the user is currently viewing. For example, an image gallery application
2281 * that has launched in to an activity allowing the user to swipe through pictures should
2282 * modify the intent to reference the current image they are looking it; such an
2283 * application when showing a list of pictures should add a ClipData that has
2284 * references to all of the pictures currently visible on screen.</p>
2285 *
2286 * @param outContent The assist content to return.
2287 */
2288 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002289 }
2290
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002291 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01002292 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002293 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
2294 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01002295 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002296 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002297 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002298 sendBroadcastAsUser(intent, Process.myUserHandle());
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002299 }
2300
2301 /**
2302 * Dismiss the Keyboard Shortcuts screen.
2303 */
2304 public final void dismissKeyboardShortcutsHelper() {
2305 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002306 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002307 sendBroadcastAsUser(intent, Process.myUserHandle());
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002308 }
2309
Clara Bayarri75e09792015-07-29 16:20:40 +01002310 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00002311 public void onProvideKeyboardShortcuts(
2312 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01002313 if (menu == null) {
2314 return;
2315 }
2316 KeyboardShortcutGroup group = null;
2317 int menuSize = menu.size();
2318 for (int i = 0; i < menuSize; ++i) {
2319 final MenuItem item = menu.getItem(i);
2320 final CharSequence title = item.getTitle();
2321 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01002322 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01002323 if (title != null && alphaShortcut != MIN_VALUE) {
2324 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00002325 final int resource = mApplication.getApplicationInfo().labelRes;
2326 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01002327 }
2328 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01002329 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01002330 }
2331 }
2332 if (group != null) {
2333 data.add(group);
2334 }
2335 }
2336
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002337 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07002338 * Ask to have the current assistant shown to the user. This only works if the calling
2339 * activity is the current foreground activity. It is the same as calling
2340 * {@link android.service.voice.VoiceInteractionService#showSession
2341 * VoiceInteractionService.showSession} and requesting all of the possible context.
2342 * The receiver will always see
2343 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
2344 * @return Returns true if the assistant was successfully invoked, else false. For example
2345 * false will be returned if the caller is not the current top activity.
2346 */
2347 public boolean showAssist(Bundle args) {
2348 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002349 return ActivityTaskManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07002350 } catch (RemoteException e) {
2351 }
2352 return false;
2353 }
2354
2355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 * Called when you are no longer visible to the user. You will next
2357 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
2358 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07002359 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 * <p><em>Derived classes must call through to the super class's
2361 * implementation of this method. If they do not, an exception will be
2362 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 * @see #onRestart
2365 * @see #onResume
2366 * @see #onSaveInstanceState
2367 * @see #onDestroy
2368 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002369 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002371 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08002372 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07002373 mActivityTransitionState.onStop();
Ian Lake8a88cd62018-09-28 13:25:20 -07002374 dispatchActivityStopped();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002375 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07002377
Dake Gu67decfa2017-12-27 11:48:08 -08002378 if (mAutoFillResetNeeded) {
2379 getAutofillManager().onInvisibleForAutofill();
2380 }
2381
Felipe Lemec24a56a2017-08-03 14:27:57 -07002382 if (isFinishing()) {
2383 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08002384 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07002385 } else if (mIntent != null
2386 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
2387 // Activity was launched when user tapped a link in the Autofill Save UI - since
2388 // user launched another activity, the Save UI should not be restored when this
2389 // activity is finished.
2390 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
2391 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
2392 }
Felipe Lemebab851c2017-02-03 18:45:08 -08002393 }
chaviwfeb2e1e2018-12-19 17:24:11 -08002394 mEnterAnimationComplete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
2396
2397 /**
2398 * Perform any final cleanup before an activity is destroyed. This can
2399 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07002400 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 * this instance of the activity to save space. You can distinguish
2402 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07002403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 * <p><em>Note: do not count on this method being called as a place for
2405 * saving data! For example, if an activity is editing data in a content
2406 * provider, those edits should be committed in either {@link #onPause} or
2407 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
2408 * free resources like threads that are associated with an activity, so
2409 * that a destroyed activity does not leave such things around while the
2410 * rest of its application is still running. There are situations where
2411 * the system will simply kill the activity's hosting process without
2412 * calling this method (or any others) in it, so it should not be used to
2413 * do things that are intended to remain around after the process goes
2414 * away.
RoboErik55011652014-07-09 15:05:53 -07002415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 * <p><em>Derived classes must call through to the super class's
2417 * implementation of this method. If they do not, an exception will be
2418 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002419 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 * @see #onPause
2421 * @see #onStop
2422 * @see #finish
2423 * @see #isFinishing
2424 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002425 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002427 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 mCalled = true;
2429
2430 // dismiss any dialogs we are managing.
2431 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 final int numDialogs = mManagedDialogs.size();
2433 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002434 final ManagedDialog md = mManagedDialogs.valueAt(i);
2435 if (md.mDialog.isShowing()) {
2436 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002439 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441
2442 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002443 synchronized (mManagedCursors) {
2444 int numCursors = mManagedCursors.size();
2445 for (int i = 0; i < numCursors; i++) {
2446 ManagedCursor c = mManagedCursors.get(i);
2447 if (c != null) {
2448 c.mCursor.close();
2449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002451 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 }
Amith Yamasani49860442010-03-17 20:54:10 -07002453
2454 // Close any open search dialog
2455 if (mSearchManager != null) {
2456 mSearchManager.stopSearch();
2457 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002458
Chris Banes21b25772016-01-04 20:41:59 +00002459 if (mActionBar != null) {
2460 mActionBar.onDestroy();
2461 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002462
Ian Lake8a88cd62018-09-28 13:25:20 -07002463 dispatchActivityDestroyed();
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002464
2465 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_STOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
2467
2468 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002469 * Report to the system that your app is now fully drawn, purely for diagnostic
2470 * purposes (calling it does not impact the visible behavior of the activity).
2471 * This is only used to help instrument application launch times, so that the
2472 * app can report when it is fully in a usable state; without this, the only thing
2473 * the system itself can determine is the point at which the activity's window
2474 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002475 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002476 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002477 * entirely drawn your UI and populated with all of the significant data. You
2478 * can safely call this method any time after first launch as well, in which case
2479 * it will simply be ignored.
2480 */
2481 public void reportFullyDrawn() {
2482 if (mDoReportFullyDrawn) {
2483 mDoReportFullyDrawn = false;
2484 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002485 ActivityTaskManager.getService().reportActivityFullyDrawn(
2486 mToken, mRestoredFromBundle);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002487 } catch (RemoteException e) {
2488 }
2489 }
2490 }
2491
2492 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002493 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002494 * visa-versa. This method provides the same configuration that will be sent in the following
2495 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2496 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002497 * @see android.R.attr#resizeableActivity
2498 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002499 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002500 * @param newConfig The new configuration of the activity with the state
2501 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002502 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002503 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2504 // Left deliberately empty. There should be no side effects if a direct
2505 // subclass of Activity does not call super.
2506 onMultiWindowModeChanged(isInMultiWindowMode);
2507 }
2508
2509 /**
2510 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2511 * visa-versa.
2512 *
2513 * @see android.R.attr#resizeableActivity
2514 *
2515 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2516 *
2517 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2518 */
2519 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002520 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002521 // Left deliberately empty. There should be no side effects if a direct
2522 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002523 }
2524
2525 /**
2526 * Returns true if the activity is currently in multi-window mode.
2527 * @see android.R.attr#resizeableActivity
2528 *
2529 * @return True if the activity is in multi-window mode.
2530 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002531 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002532 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002533 return ActivityTaskManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002534 } catch (RemoteException e) {
2535 }
2536 return false;
2537 }
2538
2539 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002540 * Called by the system when the activity changes to and from picture-in-picture mode. This
2541 * method provides the same configuration that will be sent in the following
2542 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2543 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002544 * @see android.R.attr#supportsPictureInPicture
2545 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002546 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002547 * @param newConfig The new configuration of the activity with the state
2548 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002549 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002550 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2551 Configuration newConfig) {
2552 // Left deliberately empty. There should be no side effects if a direct
2553 // subclass of Activity does not call super.
2554 onPictureInPictureModeChanged(isInPictureInPictureMode);
2555 }
2556
2557 /**
2558 * Called by the system when the activity changes to and from picture-in-picture mode.
2559 *
2560 * @see android.R.attr#supportsPictureInPicture
2561 *
2562 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2563 *
2564 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2565 */
2566 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002567 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002568 // Left deliberately empty. There should be no side effects if a direct
2569 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002570 }
2571
2572 /**
2573 * Returns true if the activity is currently in picture-in-picture mode.
2574 * @see android.R.attr#supportsPictureInPicture
2575 *
2576 * @return True if the activity is in picture-in-picture mode.
2577 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002578 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002579 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002580 return ActivityTaskManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002581 } catch (RemoteException e) {
2582 }
2583 return false;
2584 }
2585
2586 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002587 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002588 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2589 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002590 *
Winson Chung709904f2017-04-25 11:00:48 -07002591 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002592 * @see android.R.attr#supportsPictureInPicture
2593 */
Winson Chung709904f2017-04-25 11:00:48 -07002594 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002595 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002596 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002597 }
2598
Jeff Sharkey000ce802017-04-29 13:13:27 -06002599 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002600 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002601 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002602 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002603 }
2604
2605 /**
Winson Chung709904f2017-04-25 11:00:48 -07002606 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2607 * set parameters in {@param params} will be combined with the parameters from prior calls to
2608 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2609 *
2610 * The system may disallow entering picture-in-picture in various cases, including when the
2611 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2612 *
2613 * @see android.R.attr#supportsPictureInPicture
2614 * @see PictureInPictureParams
2615 *
2616 * @param params non-null parameters to be combined with previously set parameters when entering
2617 * picture-in-picture.
2618 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002619 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002620 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002621 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002622 */
2623 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2624 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002625 if (!deviceSupportsPictureInPictureMode()) {
2626 return false;
2627 }
Winson Chung709904f2017-04-25 11:00:48 -07002628 if (params == null) {
2629 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2630 }
Winson Chung298f95b2017-08-10 15:57:18 -07002631 if (!mCanEnterPictureInPicture) {
2632 throw new IllegalStateException("Activity must be resumed to enter"
2633 + " picture-in-picture");
2634 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002635 return ActivityTaskManager.getService().enterPictureInPictureMode(mToken, params);
Winson Chung709904f2017-04-25 11:00:48 -07002636 } catch (RemoteException e) {
2637 return false;
2638 }
2639 }
2640
Jeff Sharkey000ce802017-04-29 13:13:27 -06002641 /** @removed */
2642 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002643 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002644 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002645 }
2646
2647 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002648 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2649 * {@link #enterPictureInPictureMode()} is called.
2650 *
Winson Chung709904f2017-04-25 11:00:48 -07002651 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002652 */
Winson Chung709904f2017-04-25 11:00:48 -07002653 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002654 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002655 if (!deviceSupportsPictureInPictureMode()) {
2656 return;
2657 }
Winson Chung709904f2017-04-25 11:00:48 -07002658 if (params == null) {
2659 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002660 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002661 ActivityTaskManager.getService().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002662 } catch (RemoteException e) {
2663 }
2664 }
2665
Winson Chung8802eac2017-04-17 14:21:44 -07002666 /**
Winson Chung709904f2017-04-25 11:00:48 -07002667 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2668 * user interacts with the activity currently in picture-in-picture mode. This number may change
2669 * if the global configuration changes (ie. if the device is plugged into an external display),
2670 * but will always be larger than three.
2671 */
2672 public int getMaxNumPictureInPictureActions() {
2673 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002674 return ActivityTaskManager.getService().getMaxNumPictureInPictureActions(mToken);
Winson Chung709904f2017-04-25 11:00:48 -07002675 } catch (RemoteException e) {
2676 return 0;
2677 }
2678 }
2679
Winson Chung80fa2d82018-01-25 13:58:20 -08002680 /**
2681 * @return Whether this device supports picture-in-picture.
2682 */
2683 private boolean deviceSupportsPictureInPictureMode() {
2684 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2685 }
2686
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002687 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002688 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002689 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002690 }
2691
2692 /**
2693 * Called by the system when the activity is moved from one display to another without
2694 * recreation. This means that this activity is declared to handle all changes to configuration
2695 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002696 * again.
2697 *
2698 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2699 * applied configuration actually changed. It is up to app developer to choose whether to handle
2700 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2701 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002702 *
2703 * <p>Use this callback to track changes to the displays if some activity functionality relies
2704 * on an association with some display properties.
2705 *
2706 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002707 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002708 *
2709 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002710 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002711 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002712 */
Tiger Huangfc218452018-09-27 00:38:50 +08002713 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002714 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002715 }
2716
Winson Chungb5c41b72016-12-07 15:00:47 -08002717 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 * Called by the system when the device configuration changes while your
2719 * activity is running. Note that this will <em>only</em> be called if
2720 * you have selected configurations you would like to handle with the
2721 * {@link android.R.attr#configChanges} attribute in your manifest. If
2722 * any configuration change occurs that is not selected to be reported
2723 * by that attribute, then instead of reporting it the system will stop
2724 * and restart the activity (to have it launched with the new
2725 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002726 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 * <p>At the time that this function has been called, your Resources
2728 * object will have been updated to return resource values matching the
2729 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 * @param newConfig The new device configuration.
2732 */
Ian Lake666a9652018-09-12 15:13:19 -07002733 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002734 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002736
Dianne Hackborn9d071802010-12-08 14:49:15 -08002737 mFragments.dispatchConfigurationChanged(newConfig);
2738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 if (mWindow != null) {
2740 // Pass the configuration changed event to the window
2741 mWindow.onConfigurationChanged(newConfig);
2742 }
Adam Powell45c0b192011-07-28 15:11:57 -07002743
2744 if (mActionBar != null) {
2745 // Do this last; the action bar will need to access
2746 // view changes from above.
2747 mActionBar.onConfigurationChanged(newConfig);
2748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
RoboErik55011652014-07-09 15:05:53 -07002750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 /**
2752 * If this activity is being destroyed because it can not handle a
2753 * configuration parameter being changed (and thus its
2754 * {@link #onConfigurationChanged(Configuration)} method is
2755 * <em>not</em> being called), then you can use this method to discover
2756 * the set of changes that have occurred while in the process of being
2757 * destroyed. Note that there is no guarantee that these will be
2758 * accurate (other changes could have happened at any time), so you should
2759 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002760 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 * @return Returns a bit field of the configuration parameters that are
2762 * changing, as defined by the {@link android.content.res.Configuration}
2763 * class.
2764 */
2765 public int getChangingConfigurations() {
2766 return mConfigChangeFlags;
2767 }
RoboErik55011652014-07-09 15:05:53 -07002768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 /**
2770 * Retrieve the non-configuration instance data that was previously
2771 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2772 * be available from the initial {@link #onCreate} and
2773 * {@link #onStart} calls to the new instance, allowing you to extract
2774 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002775 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 * <p>Note that the data you retrieve here should <em>only</em> be used
2777 * as an optimization for handling configuration changes. You should always
2778 * be able to handle getting a null pointer back, and an activity must
2779 * still be able to restore itself to its previous state (through the
2780 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2781 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002782 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002783 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002784 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002785 * available on older platforms through the Android support libraries.
2786 *
2787 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002789 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002791 return mLastNonConfigurationInstances != null
2792 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
RoboErik55011652014-07-09 15:05:53 -07002794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 /**
2796 * Called by the system, as part of destroying an
2797 * activity due to a configuration change, when it is known that a new
2798 * instance will immediately be created for the new configuration. You
2799 * can return any object you like here, including the activity instance
2800 * itself, which can later be retrieved by calling
2801 * {@link #getLastNonConfigurationInstance()} in the new activity
2802 * instance.
RoboErik55011652014-07-09 15:05:53 -07002803 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002804 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2805 * or later, consider instead using a {@link Fragment} with
2806 * {@link Fragment#setRetainInstance(boolean)
2807 * Fragment.setRetainInstance(boolean}.</em>
2808 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 * <p>This function is called purely as an optimization, and you must
2810 * not rely on it being called. When it is called, a number of guarantees
2811 * will be made to help optimize configuration switching:
2812 * <ul>
2813 * <li> The function will be called between {@link #onStop} and
2814 * {@link #onDestroy}.
2815 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002816 * created after this one's {@link #onDestroy()} is called. In particular,
2817 * <em>no</em> messages will be dispatched during this time (when the returned
2818 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 * <li> The object you return here will <em>always</em> be available from
2820 * the {@link #getLastNonConfigurationInstance()} method of the following
2821 * activity instance as described there.
2822 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 * <p>These guarantees are designed so that an activity can use this API
2825 * to propagate extensive state from the old to new activity instance, from
2826 * loaded bitmaps, to network connections, to evenly actively running
2827 * threads. Note that you should <em>not</em> propagate any data that
2828 * may change based on the configuration, including any data loaded from
2829 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002830 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002831 * <p>The guarantee of no message handling during the switch to the next
2832 * activity simplifies use with active objects. For example if your retained
2833 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2834 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2835 * not be called from the call here until you execute the next instance's
2836 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2837 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2838 * running in a separate thread.)
2839 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002840 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002841 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002842 * available on older platforms through the Android support libraries.
2843 *
2844 * @return any Object holding the desired state to propagate to the
2845 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 */
2847 public Object onRetainNonConfigurationInstance() {
2848 return null;
2849 }
RoboErik55011652014-07-09 15:05:53 -07002850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 /**
2852 * Retrieve the non-configuration instance data that was previously
2853 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2854 * be available from the initial {@link #onCreate} and
2855 * {@link #onStart} calls to the new instance, allowing you to extract
2856 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002857 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 * <p>Note that the data you retrieve here should <em>only</em> be used
2859 * as an optimization for handling configuration changes. You should always
2860 * be able to handle getting a null pointer back, and an activity must
2861 * still be able to restore itself to its previous state (through the
2862 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2863 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 * @return Returns the object previously returned by
2866 * {@link #onRetainNonConfigurationChildInstances()}
2867 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002868 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002869 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2870 return mLastNonConfigurationInstances != null
2871 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
RoboErik55011652014-07-09 15:05:53 -07002873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 /**
2875 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2876 * it should return either a mapping from child activity id strings to arbitrary objects,
2877 * or null. This method is intended to be used by Activity framework subclasses that control a
2878 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2879 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2880 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002881 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2883 return null;
2884 }
RoboErik55011652014-07-09 15:05:53 -07002885
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002886 NonConfigurationInstances retainNonConfigurationInstances() {
2887 Object activity = onRetainNonConfigurationInstance();
2888 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002889 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002890
2891 // We're already stopped but we've been asked to retain.
2892 // Our fragments are taken care of but we need to mark the loaders for retention.
2893 // In order to do this correctly we need to restart the loaders first before
2894 // handing them off to the next activity.
2895 mFragments.doLoaderStart();
2896 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002897 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002898
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002899 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002900 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002901 return null;
2902 }
RoboErik55011652014-07-09 15:05:53 -07002903
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002904 NonConfigurationInstances nci = new NonConfigurationInstances();
2905 nci.activity = activity;
2906 nci.children = children;
2907 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002908 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002909 if (mVoiceInteractor != null) {
2910 mVoiceInteractor.retainInstance();
2911 nci.voiceInteractor = mVoiceInteractor;
2912 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002913 return nci;
2914 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002917 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002919 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002921
2922 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002923 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002924 mCalled = true;
2925 mFragments.dispatchTrimMemory(level);
2926 }
2927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002929 * Return the FragmentManager for interacting with fragments associated
2930 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08002931 *
2932 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002933 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002934 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002935 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002936 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002937 }
RoboErik55011652014-07-09 15:05:53 -07002938
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002939 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002940 * Called when a Fragment is being attached to this activity, immediately
2941 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2942 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08002943 *
2944 * @deprecated Use {@link
2945 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07002946 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002947 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07002948 public void onAttachFragment(Fragment fragment) {
2949 }
RoboErik55011652014-07-09 15:05:53 -07002950
Dianne Hackbornc8017682010-07-06 13:34:38 -07002951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 * Wrapper around
2953 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2954 * that gives the resulting {@link Cursor} to call
2955 * {@link #startManagingCursor} so that the activity will manage its
2956 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002957 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002958 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2959 * or later, consider instead using {@link LoaderManager} instead, available
2960 * via {@link #getLoaderManager()}.</em>
2961 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002962 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2963 * this method, because the activity will do that for you at the appropriate time. However, if
2964 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2965 * not</em> automatically close the cursor and, in that case, you must call
2966 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002967 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 * @param uri The URI of the content provider to query.
2969 * @param projection List of columns to return.
2970 * @param selection SQL WHERE clause.
2971 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002972 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2976 * @see #startManagingCursor
2977 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002978 *
2979 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 */
Jason parks6ed50de2010-08-25 10:18:50 -05002981 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002982 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07002983 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2984 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2986 if (c != null) {
2987 startManagingCursor(c);
2988 }
2989 return c;
2990 }
2991
2992 /**
2993 * Wrapper around
2994 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2995 * that gives the resulting {@link Cursor} to call
2996 * {@link #startManagingCursor} so that the activity will manage its
2997 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002998 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002999 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3000 * or later, consider instead using {@link LoaderManager} instead, available
3001 * via {@link #getLoaderManager()}.</em>
3002 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003003 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3004 * this method, because the activity will do that for you at the appropriate time. However, if
3005 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3006 * not</em> automatically close the cursor and, in that case, you must call
3007 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 * @param uri The URI of the content provider to query.
3010 * @param projection List of columns to return.
3011 * @param selection SQL WHERE clause.
3012 * @param selectionArgs The arguments to selection, if any ?s are pesent
3013 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003014 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003016 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3018 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05003019 *
3020 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 */
Jason parks6ed50de2010-08-25 10:18:50 -05003022 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07003023 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3024 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
3026 if (c != null) {
3027 startManagingCursor(c);
3028 }
3029 return c;
3030 }
3031
3032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 * This method allows the activity to take care of managing the given
3034 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
3035 * That is, when the activity is stopped it will automatically call
3036 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
3037 * it will call {@link Cursor#requery} for you. When the activity is
3038 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07003039 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003040 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3041 * or later, consider instead using {@link LoaderManager} instead, available
3042 * via {@link #getLoaderManager()}.</em>
3043 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003044 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
3045 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
3046 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
3047 * <em>will not</em> automatically close the cursor and, in that case, you must call
3048 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07003051 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
3053 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003054 *
3055 * @deprecated Use the new {@link android.content.CursorLoader} class with
3056 * {@link LoaderManager} instead; this is also
3057 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 */
Jason parks6ed50de2010-08-25 10:18:50 -05003059 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 public void startManagingCursor(Cursor c) {
3061 synchronized (mManagedCursors) {
3062 mManagedCursors.add(new ManagedCursor(c));
3063 }
3064 }
3065
3066 /**
3067 * Given a Cursor that was previously given to
3068 * {@link #startManagingCursor}, stop the activity's management of that
3069 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003070 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003071 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003072 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003073 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003078 *
3079 * @deprecated Use the new {@link android.content.CursorLoader} class with
3080 * {@link LoaderManager} instead; this is also
3081 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 */
Jason parks6ed50de2010-08-25 10:18:50 -05003083 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 public void stopManagingCursor(Cursor c) {
3085 synchronized (mManagedCursors) {
3086 final int N = mManagedCursors.size();
3087 for (int i=0; i<N; i++) {
3088 ManagedCursor mc = mManagedCursors.get(i);
3089 if (mc.mCursor == c) {
3090 mManagedCursors.remove(i);
3091 break;
3092 }
3093 }
3094 }
3095 }
3096
3097 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003098 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3099 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003100 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003102 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003103 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 }
3106
3107 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003108 * Finds a view that was identified by the {@code android:id} XML attribute
3109 * that was processed in {@link #onCreate}.
3110 * <p>
3111 * <strong>Note:</strong> In most cases -- depending on compiler support --
3112 * the resulting view is automatically cast to the target class type. If
3113 * the target class type is unconstrained, an explicit cast may be
3114 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003116 * @param id the ID to search for
3117 * @return a view with given ID if found, or {@code null} otherwise
3118 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003119 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003121 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003122 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 return getWindow().findViewById(id);
3124 }
RoboErik55011652014-07-09 15:05:53 -07003125
Adam Powell33b97432010-04-20 10:01:14 -07003126 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003127 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3128 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3129 * no matching view in the hierarchy.
3130 * <p>
3131 * <strong>Note:</strong> In most cases -- depending on compiler support --
3132 * the resulting view is automatically cast to the target class type. If
3133 * the target class type is unconstrained, an explicit cast may be
3134 * necessary.
3135 *
3136 * @param id the ID to search for
3137 * @return a view with given ID
3138 * @see View#requireViewById(int)
3139 * @see Activity#findViewById(int)
3140 */
3141 @NonNull
3142 public final <T extends View> T requireViewById(@IdRes int id) {
3143 T view = findViewById(id);
3144 if (view == null) {
3145 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3146 }
3147 return view;
3148 }
3149
3150 /**
Adam Powell33b97432010-04-20 10:01:14 -07003151 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003152 *
Adam Powell33b97432010-04-20 10:01:14 -07003153 * @return The Activity's ActionBar, or null if it does not have one.
3154 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003155 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003156 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003157 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003158 return mActionBar;
3159 }
Adam Powelle43340c2014-03-17 19:10:43 -07003160
3161 /**
3162 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3163 * Activity window.
3164 *
3165 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3166 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3167 * a traditional window decor action bar. The toolbar's menu will be populated with the
3168 * Activity's options menu and the navigation button will be wired through the standard
3169 * {@link android.R.id#home home} menu select action.</p>
3170 *
3171 * <p>In order to use a Toolbar within the Activity's window content the application
3172 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3173 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003174 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003175 */
Adam Powell37780142014-06-01 13:31:00 -07003176 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003177 final ActionBar ab = getActionBar();
3178 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003179 throw new IllegalStateException("This Activity already has an action bar supplied " +
3180 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3181 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3182 }
Chris Banes21b25772016-01-04 20:41:59 +00003183
3184 // If we reach here then we're setting a new action bar
3185 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003186 mMenuInflater = null;
3187
Chris Banes21b25772016-01-04 20:41:59 +00003188 // If we have an action bar currently, destroy it
3189 if (ab != null) {
3190 ab.onDestroy();
3191 }
3192
Chris Banesc7d6c322016-01-27 14:09:16 +00003193 if (toolbar != null) {
3194 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3195 mActionBar = tbab;
3196 mWindow.setCallback(tbab.getWrappedWindowCallback());
3197 } else {
3198 mActionBar = null;
3199 // Re-set the original window callback since we may have already set a Toolbar wrapper
3200 mWindow.setCallback(this);
3201 }
3202
3203 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003204 }
RoboErik55011652014-07-09 15:05:53 -07003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 /**
Adam Powell33b97432010-04-20 10:01:14 -07003207 * Creates a new ActionBar, locates the inflated ActionBarView,
3208 * initializes the ActionBar with the view, and sets mActionBar.
3209 */
Adam Powelle43340c2014-03-17 19:10:43 -07003210 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003211 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003212
3213 // Initializing the window decor can change window feature flags.
3214 // Make sure that we have the correct set before performing the test below.
3215 window.getDecorView();
3216
Adam Powell9b4c8042010-08-10 15:36:44 -07003217 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003218 return;
3219 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003220
Adam Powelle43340c2014-03-17 19:10:43 -07003221 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003222 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003223
3224 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3225 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003226 }
RoboErik55011652014-07-09 15:05:53 -07003227
Adam Powell33b97432010-04-20 10:01:14 -07003228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 * Set the activity content from a layout resource. The resource will be
3230 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003233 *
Romain Guy482b34a62011-01-20 10:59:28 -08003234 * @see #setContentView(android.view.View)
3235 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003237 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003239 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
3241
3242 /**
3243 * Set the activity content to an explicit view. This view is placed
3244 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003245 * view hierarchy. When calling this method, the layout parameters of the
3246 * specified view are ignored. Both the width and the height of the view are
3247 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3248 * your own layout parameters, invoke
3249 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3250 * instead.
RoboErik55011652014-07-09 15:05:53 -07003251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003253 *
3254 * @see #setContentView(int)
3255 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 */
3257 public void setContentView(View view) {
3258 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003259 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 }
3261
3262 /**
3263 * Set the activity content to an explicit view. This view is placed
3264 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003265 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 * @param view The desired content to display.
3268 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003269 *
3270 * @see #setContentView(android.view.View)
3271 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 */
3273 public void setContentView(View view, ViewGroup.LayoutParams params) {
3274 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003275 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
3277
3278 /**
3279 * Add an additional content view to the activity. Added after any existing
3280 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003281 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 * @param view The desired content to display.
3283 * @param params Layout parameters for the view.
3284 */
3285 public void addContentView(View view, ViewGroup.LayoutParams params) {
3286 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003287 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 }
3289
3290 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003291 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3292 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3293 *
3294 * <p>This method will return non-null after content has been initialized (e.g. by using
3295 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3296 *
3297 * @return This window's content TransitionManager or null if none is set.
3298 */
3299 public TransitionManager getContentTransitionManager() {
3300 return getWindow().getTransitionManager();
3301 }
3302
3303 /**
3304 * Set the {@link TransitionManager} to use for default transitions in this window.
3305 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3306 *
3307 * @param tm The TransitionManager to use for scene changes.
3308 */
3309 public void setContentTransitionManager(TransitionManager tm) {
3310 getWindow().setTransitionManager(tm);
3311 }
3312
3313 /**
3314 * Retrieve the {@link Scene} representing this window's current content.
3315 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3316 *
3317 * <p>This method will return null if the current content is not represented by a Scene.</p>
3318 *
3319 * @return Current Scene being shown or null
3320 */
3321 public Scene getContentScene() {
3322 return getWindow().getContentScene();
3323 }
3324
3325 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003326 * Sets whether this activity is finished when touched outside its window's
3327 * bounds.
3328 */
3329 public void setFinishOnTouchOutside(boolean finish) {
3330 mWindow.setCloseOnTouchOutside(finish);
3331 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003332
3333 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003334 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003335 DEFAULT_KEYS_DISABLE,
3336 DEFAULT_KEYS_DIALER,
3337 DEFAULT_KEYS_SHORTCUT,
3338 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003339 DEFAULT_KEYS_SEARCH_GLOBAL
3340 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003341 @Retention(RetentionPolicy.SOURCE)
3342 @interface DefaultKeyMode {}
3343
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003344 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3346 * keys.
RoboErik55011652014-07-09 15:05:53 -07003347 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 * @see #setDefaultKeyMode
3349 */
3350 static public final int DEFAULT_KEYS_DISABLE = 0;
3351 /**
3352 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3353 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 * @see #setDefaultKeyMode
3356 */
3357 static public final int DEFAULT_KEYS_DIALER = 1;
3358 /**
3359 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3360 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003361 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 * @see #setDefaultKeyMode
3365 */
3366 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3367 /**
3368 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3369 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003370 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003371 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 * @see #setDefaultKeyMode
3375 */
3376 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3377
3378 /**
3379 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3380 * will start a global search (typically web search, but some platforms may define alternate
3381 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 * @see #setDefaultKeyMode
3386 */
3387 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3388
3389 /**
3390 * Select the default key handling for this activity. This controls what
3391 * will happen to key events that are not otherwise handled. The default
3392 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3393 * floor. Other modes allow you to launch the dialer
3394 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3395 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003396 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003398 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 * <p>Note that the mode selected here does not impact the default
3400 * handling of system keys, such as the "back" and "menu" keys, and your
3401 * activity and its views always get a first chance to receive and handle
3402 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 * @see #onKeyDown
3407 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003408 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 // Some modes use a SpannableStringBuilder to track & dispatch input events
3412 // This list must remain in sync with the switch in onKeyDown()
3413 switch (mode) {
3414 case DEFAULT_KEYS_DISABLE:
3415 case DEFAULT_KEYS_SHORTCUT:
3416 mDefaultKeySsb = null; // not used in these modes
3417 break;
3418 case DEFAULT_KEYS_DIALER:
3419 case DEFAULT_KEYS_SEARCH_LOCAL:
3420 case DEFAULT_KEYS_SEARCH_GLOBAL:
3421 mDefaultKeySsb = new SpannableStringBuilder();
3422 Selection.setSelection(mDefaultKeySsb,0);
3423 break;
3424 default:
3425 throw new IllegalArgumentException();
3426 }
3427 }
3428
3429 /**
3430 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003431 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 * is inside a TextView will not trigger the event (unless it is a navigation
3433 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003434 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 * <p>If the focused view didn't want this event, this method is called.
3436 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003437 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3438 * by calling {@link #onBackPressed()}, though the behavior varies based
3439 * on the application compatibility mode: for
3440 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3441 * it will set up the dispatch to call {@link #onKeyUp} where the action
3442 * will be performed; for earlier applications, it will perform the
3443 * action immediately in on-down, as those versions of the platform
3444 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003445 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003446 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003447 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003450 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 * this event and it should continue to be propagated.
3452 * @see #onKeyUp
3453 * @see android.view.KeyEvent
3454 */
3455 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003456 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003457 if (getApplicationInfo().targetSdkVersion
3458 >= Build.VERSION_CODES.ECLAIR) {
3459 event.startTracking();
3460 } else {
3461 onBackPressed();
3462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 return true;
3464 }
RoboErik55011652014-07-09 15:05:53 -07003465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3467 return false;
3468 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003469 Window w = getWindow();
3470 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3471 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3472 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003473 return true;
3474 }
3475 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003476 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3477 // Don't consume TAB here since it's used for navigation. Arrow keys
3478 // aren't considered "typing keys" so they already won't get consumed.
3479 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 } else {
3481 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3482 boolean clearSpannable = false;
3483 boolean handled;
3484 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3485 clearSpannable = true;
3486 handled = false;
3487 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003488 handled = TextKeyListener.getInstance().onKeyDown(
3489 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 if (handled && mDefaultKeySsb.length() > 0) {
3491 // something useable has been typed - dispatch it now.
3492
3493 final String str = mDefaultKeySsb.toString();
3494 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 switch (mDefaultKeyMode) {
3497 case DEFAULT_KEYS_DIALER:
3498 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3499 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003500 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 break;
3502 case DEFAULT_KEYS_SEARCH_LOCAL:
3503 startSearch(str, false, null, false);
3504 break;
3505 case DEFAULT_KEYS_SEARCH_GLOBAL:
3506 startSearch(str, false, null, true);
3507 break;
3508 }
3509 }
3510 }
3511 if (clearSpannable) {
3512 mDefaultKeySsb.clear();
3513 mDefaultKeySsb.clearSpans();
3514 Selection.setSelection(mDefaultKeySsb,0);
3515 }
3516 return handled;
3517 }
3518 }
3519
3520 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003521 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3522 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3523 * the event).
Siarhei Vishniakouccf973f2018-10-30 14:04:57 -07003524 *
3525 * To receive this callback, you must return true from onKeyDown for the current
3526 * event stream.
3527 *
3528 * @see KeyEvent.Callback#onKeyLongPress()
3529 * @see KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003530 */
3531 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3532 return false;
3533 }
3534
3535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003537 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 * is inside a TextView will not trigger the event (unless it is a navigation
3539 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003540 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003541 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3542 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003543 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003545 * further, or <code>false</code> to indicate that you have not handled
3546 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 * @see #onKeyDown
3548 * @see KeyEvent
3549 */
3550 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003551 if (getApplicationInfo().targetSdkVersion
3552 >= Build.VERSION_CODES.ECLAIR) {
3553 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3554 && !event.isCanceled()) {
3555 onBackPressed();
3556 return true;
3557 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 return false;
3560 }
3561
3562 /**
3563 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3564 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3565 * the event).
3566 */
3567 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3568 return false;
3569 }
RoboErik55011652014-07-09 15:05:53 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003572 * Called when the activity has detected the user's press of the back
3573 * key. The default implementation simply finishes the current activity,
3574 * but you can override this to do whatever you want.
3575 */
3576 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003577 if (mActionBar != null && mActionBar.collapseActionView()) {
3578 return;
3579 }
3580
George Mount27b0dc02017-02-21 10:24:09 -08003581 FragmentManager fragmentManager = mFragments.getFragmentManager();
3582
3583 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003584 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003585 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003586 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003587
3588 /**
3589 * Called when a key shortcut event is not handled by any of the views in the Activity.
3590 * Override this method to implement global key shortcuts for the Activity.
3591 * Key shortcuts can also be implemented by setting the
3592 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3593 *
3594 * @param keyCode The value in event.getKeyCode().
3595 * @param event Description of the key event.
3596 * @return True if the key shortcut was handled.
3597 */
3598 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003599 // Let the Action Bar have a chance at handling the shortcut.
3600 ActionBar actionBar = getActionBar();
3601 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003602 }
3603
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 * Called when a touch screen event was not handled by any of the views
3606 * under it. This is most useful to process touch events that happen
3607 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 * @return Return true if you have consumed the event, false if you haven't.
3612 * The default implementation always returns false.
3613 */
3614 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003615 if (mWindow.shouldCloseOnTouch(this, event)) {
3616 finish();
3617 return true;
3618 }
RoboErik55011652014-07-09 15:05:53 -07003619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 return false;
3621 }
RoboErik55011652014-07-09 15:05:53 -07003622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 /**
3624 * Called when the trackball was moved and not handled by any of the
3625 * views inside of the activity. So, for example, if the trackball moves
3626 * while focus is on a button, you will receive a call here because
3627 * buttons do not normally do anything with trackball events. The call
3628 * here happens <em>before</em> trackball movements are converted to
3629 * DPAD key events, which then get sent back to the view hierarchy, and
3630 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 * @return Return true if you have consumed the event, false if you haven't.
3635 * The default implementation always returns false.
3636 */
3637 public boolean onTrackballEvent(MotionEvent event) {
3638 return false;
3639 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003640
3641 /**
3642 * Called when a generic motion event was not handled by any of the
3643 * views inside of the activity.
3644 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003645 * Generic motion events describe joystick movements, mouse hovers, track pad
3646 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003647 * {@link MotionEvent#getSource() source} of the motion event specifies
3648 * the class of input that was received. Implementations of this method
3649 * must examine the bits in the source before processing the event.
3650 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003651 * </p><p>
3652 * Generic motion events with source class
3653 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3654 * are delivered to the view under the pointer. All other generic motion events are
3655 * delivered to the focused view.
3656 * </p><p>
3657 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3658 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003659 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003660 *
3661 * @param event The generic motion event being processed.
3662 *
3663 * @return Return true if you have consumed the event, false if you haven't.
3664 * The default implementation always returns false.
3665 */
3666 public boolean onGenericMotionEvent(MotionEvent event) {
3667 return false;
3668 }
3669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 /**
3671 * Called whenever a key, touch, or trackball event is dispatched to the
3672 * activity. Implement this method if you wish to know that the user has
3673 * interacted with the device in some way while your activity is running.
3674 * This callback and {@link #onUserLeaveHint} are intended to help
3675 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003676 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003677 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3679 * be accompanied by calls to {@link #onUserInteraction}. This
3680 * ensures that your activity will be told of relevant user activity such
3681 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003682 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 * <p>Note that this callback will be invoked for the touch down action
3684 * that begins a touch gesture, but may not be invoked for the touch-moved
3685 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 * @see #onUserLeaveHint()
3688 */
3689 public void onUserInteraction() {
3690 }
RoboErik55011652014-07-09 15:05:53 -07003691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3693 // Update window manager if: we have a view, that view is
3694 // attached to its parent (which will be a RootView), and
3695 // this activity is not embedded.
3696 if (mParent == null) {
3697 View decor = mDecor;
3698 if (decor != null && decor.getParent() != null) {
3699 getWindowManager().updateViewLayout(decor, params);
3700 }
3701 }
3702 }
3703
3704 public void onContentChanged() {
3705 }
3706
3707 /**
3708 * Called when the current {@link Window} of the activity gains or loses
3709 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003710 * to the user. The default implementation clears the key tracking
3711 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003712 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003713 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 * is managed independently of activity lifecycles. As such, while focus
3715 * changes will generally have some relation to lifecycle changes (an
3716 * activity that is stopped will not generally get window focus), you
3717 * should not rely on any particular order between the callbacks here and
3718 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 * <p>As a general rule, however, a resumed activity will have window
3721 * focus... unless it has displayed other dialogs or popups that take
3722 * input focus, in which case the activity itself will not have focus
3723 * when the other windows have it. Likewise, the system may display
3724 * system-level windows (such as the status bar notification panel or
3725 * a system alert) which will temporarily take window input focus without
3726 * pausing the foreground activity.
3727 *
3728 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003729 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 * @see #hasWindowFocus()
3731 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003732 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 */
3734 public void onWindowFocusChanged(boolean hasFocus) {
3735 }
RoboErik55011652014-07-09 15:05:53 -07003736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003738 * Called when the main window associated with the activity has been
3739 * attached to the window manager.
3740 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3741 * for more information.
3742 * @see View#onAttachedToWindow
3743 */
3744 public void onAttachedToWindow() {
3745 }
RoboErik55011652014-07-09 15:05:53 -07003746
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003747 /**
3748 * Called when the main window associated with the activity has been
3749 * detached from the window manager.
3750 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3751 * for more information.
3752 * @see View#onDetachedFromWindow
3753 */
3754 public void onDetachedFromWindow() {
3755 }
RoboErik55011652014-07-09 15:05:53 -07003756
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 * Returns true if this activity's <em>main</em> window currently has window focus.
3759 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003760 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003762 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3764 */
3765 public boolean hasWindowFocus() {
3766 Window w = getWindow();
3767 if (w != null) {
3768 View d = w.getDecorView();
3769 if (d != null) {
3770 return d.hasWindowFocus();
3771 }
3772 }
3773 return false;
3774 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003775
3776 /**
3777 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003778 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003779 */
Adam Powell117b6952014-05-05 18:14:56 -07003780 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003781 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003782 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003783 if (suppressWindowTransition) {
3784 overridePendingTransition(0, 0);
3785 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003786 }
RoboErik55011652014-07-09 15:05:53 -07003787
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003788
Skuhnece2faa52015-08-11 10:36:38 -07003789 /**
Yunfan Chend967af82019-01-17 18:30:18 +09003790 * Moves the activity between {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode
3791 * and {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003792 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003793 * @hide
3794 */
3795 @Override
Yunfan Chend967af82019-01-17 18:30:18 +09003796 public void toggleFreeformWindowingMode() throws RemoteException {
3797 ActivityTaskManager.getService().toggleFreeformWindowingMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003798 }
3799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003801 * Puts the activity in picture-in-picture mode if the activity supports.
3802 * @see android.R.attr#supportsPictureInPicture
3803 * @hide
3804 */
3805 @Override
3806 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003807 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003808 enterPictureInPictureMode();
3809 }
3810 }
3811
3812 /**
RoboErik55011652014-07-09 15:05:53 -07003813 * Called to process key events. You can override this to intercept all
3814 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 * @return boolean Return true if this event was consumed.
3820 */
3821 public boolean dispatchKeyEvent(KeyEvent event) {
3822 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003823
3824 // Let action bars open menus in response to the menu key prioritized over
3825 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003826 final int keyCode = event.getKeyCode();
3827 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003828 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3829 return true;
3830 }
3831
Dianne Hackborn8d374262009-09-14 21:21:52 -07003832 Window win = getWindow();
3833 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 return true;
3835 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003836 View decor = mDecor;
3837 if (decor == null) decor = win.getDecorView();
3838 return event.dispatch(this, decor != null
3839 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 }
3841
3842 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003843 * Called to process a key shortcut event.
3844 * You can override this to intercept all key shortcut events before they are
3845 * dispatched to the window. Be sure to call this implementation for key shortcut
3846 * events that should be handled normally.
3847 *
3848 * @param event The key shortcut event.
3849 * @return True if this event was consumed.
3850 */
3851 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3852 onUserInteraction();
3853 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3854 return true;
3855 }
3856 return onKeyShortcut(event.getKeyCode(), event);
3857 }
3858
3859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 * Called to process touch screen events. You can override this to
3861 * intercept all touch screen events before they are dispatched to the
3862 * window. Be sure to call this implementation for touch screen events
3863 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003866 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 * @return boolean Return true if this event was consumed.
3868 */
3869 public boolean dispatchTouchEvent(MotionEvent ev) {
3870 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3871 onUserInteraction();
3872 }
3873 if (getWindow().superDispatchTouchEvent(ev)) {
3874 return true;
3875 }
3876 return onTouchEvent(ev);
3877 }
RoboErik55011652014-07-09 15:05:53 -07003878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 /**
3880 * Called to process trackball events. You can override this to
3881 * intercept all trackball events before they are dispatched to the
3882 * window. Be sure to call this implementation for trackball events
3883 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003884 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 * @return boolean Return true if this event was consumed.
3888 */
3889 public boolean dispatchTrackballEvent(MotionEvent ev) {
3890 onUserInteraction();
3891 if (getWindow().superDispatchTrackballEvent(ev)) {
3892 return true;
3893 }
3894 return onTrackballEvent(ev);
3895 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003896
Jeff Browncb1404e2011-01-15 18:14:15 -08003897 /**
3898 * Called to process generic motion events. You can override this to
3899 * intercept all generic motion events before they are dispatched to the
3900 * window. Be sure to call this implementation for generic motion events
3901 * that should be handled normally.
3902 *
3903 * @param ev The generic motion event.
3904 *
3905 * @return boolean Return true if this event was consumed.
3906 */
3907 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3908 onUserInteraction();
3909 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3910 return true;
3911 }
3912 return onGenericMotionEvent(ev);
3913 }
3914
svetoslavganov75986cf2009-05-14 22:28:01 -07003915 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3916 event.setClassName(getClass().getName());
3917 event.setPackageName(getPackageName());
3918
3919 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003920 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3921 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003922 event.setFullScreen(isFullScreen);
3923
3924 CharSequence title = getTitle();
3925 if (!TextUtils.isEmpty(title)) {
3926 event.getText().add(title);
3927 }
3928
3929 return true;
3930 }
3931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 /**
3933 * Default implementation of
3934 * {@link android.view.Window.Callback#onCreatePanelView}
3935 * for activities. This
3936 * simply returns null so that all panel sub-windows will have the default
3937 * menu behavior.
3938 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003939 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 public View onCreatePanelView(int featureId) {
3941 return null;
3942 }
3943
3944 /**
3945 * Default implementation of
3946 * {@link android.view.Window.Callback#onCreatePanelMenu}
3947 * for activities. This calls through to the new
3948 * {@link #onCreateOptionsMenu} method for the
3949 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3950 * so that subclasses of Activity don't need to deal with feature codes.
3951 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003952 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003954 boolean show = onCreateOptionsMenu(menu);
3955 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3956 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 }
3958 return false;
3959 }
3960
3961 /**
3962 * Default implementation of
3963 * {@link android.view.Window.Callback#onPreparePanel}
3964 * for activities. This
3965 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3966 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3967 * panel, so that subclasses of
3968 * Activity don't need to deal with feature codes.
3969 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003970 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
3971 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003973 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003974 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 }
3976 return true;
3977 }
3978
3979 /**
3980 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003981 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 * @return The default implementation returns true.
3983 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003984 @Override
3985 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003986 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003987 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003988 if (mActionBar != null) {
3989 mActionBar.dispatchMenuVisibilityChanged(true);
3990 } else {
3991 Log.e(TAG, "Tried to open action bar menu with no action bar");
3992 }
Adam Powell8515ee82010-11-30 14:09:55 -08003993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 return true;
3995 }
3996
3997 /**
3998 * Default implementation of
3999 * {@link android.view.Window.Callback#onMenuItemSelected}
4000 * for activities. This calls through to the new
4001 * {@link #onOptionsItemSelected} method for the
4002 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4003 * panel, so that subclasses of
4004 * Activity don't need to deal with feature codes.
4005 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004006 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09004007 CharSequence titleCondensed = item.getTitleCondensed();
4008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 switch (featureId) {
4010 case Window.FEATURE_OPTIONS_PANEL:
4011 // Put event logging here so it gets called even if subclass
4012 // doesn't call through to superclass's implmeentation of each
4013 // of these methods below
zobject686a8052012-12-14 21:11:08 +09004014 if(titleCondensed != null) {
4015 EventLog.writeEvent(50000, 0, titleCondensed.toString());
4016 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004017 if (onOptionsItemSelected(item)) {
4018 return true;
4019 }
Adam Powell04d58112012-04-09 10:22:12 -07004020 if (mFragments.dispatchOptionsItemSelected(item)) {
4021 return true;
4022 }
4023 if (item.getItemId() == android.R.id.home && mActionBar != null &&
4024 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
4025 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07004026 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07004027 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07004028 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07004029 }
Adam Powell04d58112012-04-09 10:22:12 -07004030 }
4031 return false;
RoboErik55011652014-07-09 15:05:53 -07004032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09004034 if(titleCondensed != null) {
4035 EventLog.writeEvent(50000, 1, titleCondensed.toString());
4036 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07004037 if (onContextItemSelected(item)) {
4038 return true;
4039 }
4040 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08004041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 default:
4043 return false;
4044 }
4045 }
RoboErik55011652014-07-09 15:05:53 -07004046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 /**
4048 * Default implementation of
4049 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
4050 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
4051 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4052 * so that subclasses of Activity don't need to deal with feature codes.
4053 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
4054 * {@link #onContextMenuClosed(Menu)} will be called.
4055 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004056 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 switch (featureId) {
4058 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004059 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 onOptionsMenuClosed(menu);
4061 break;
RoboErik55011652014-07-09 15:05:53 -07004062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 case Window.FEATURE_CONTEXT_MENU:
4064 onContextMenuClosed(menu);
4065 break;
Adam Powell8515ee82010-11-30 14:09:55 -08004066
4067 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07004068 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08004069 mActionBar.dispatchMenuVisibilityChanged(false);
4070 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 }
4072 }
4073
4074 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004075 * Declare that the options menu has changed, so should be recreated.
4076 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4077 * time it needs to be displayed.
4078 */
4079 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004080 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4081 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004082 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4083 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004084 }
RoboErik55011652014-07-09 15:05:53 -07004085
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 * Initialize the contents of the Activity's standard options menu. You
4088 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 * <p>This is only called once, the first time the options menu is
4091 * displayed. To update the menu every time it is displayed, see
4092 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004093 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004095 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4096 * they will be correctly ordered with application-defined menu items.
4097 * Deriving classes should always call through to the base implementation.
4098 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 * <p>You can safely hold on to <var>menu</var> (and any items created
4100 * from it), making modifications to it as desired, until the next
4101 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 * <p>When you add items to the menu, you can implement the Activity's
4104 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 * @return You must return true for the menu to be displayed;
4109 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 * @see #onPrepareOptionsMenu
4112 * @see #onOptionsItemSelected
4113 */
4114 public boolean onCreateOptionsMenu(Menu menu) {
4115 if (mParent != null) {
4116 return mParent.onCreateOptionsMenu(menu);
4117 }
4118 return true;
4119 }
4120
4121 /**
4122 * Prepare the Screen's standard options menu to be displayed. This is
4123 * called right before the menu is shown, every time it is shown. You can
4124 * use this method to efficiently enable/disable items or otherwise
4125 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 * <p>The default implementation updates the system menu items based on the
4128 * activity's state. Deriving classes should always call through to the
4129 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 * @param menu The options menu as last shown or first initialized by
4132 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * @return You must return true for the menu to be displayed;
4135 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004136 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 * @see #onCreateOptionsMenu
4138 */
4139 public boolean onPrepareOptionsMenu(Menu menu) {
4140 if (mParent != null) {
4141 return mParent.onPrepareOptionsMenu(menu);
4142 }
4143 return true;
4144 }
4145
4146 /**
4147 * This hook is called whenever an item in your options menu is selected.
4148 * The default implementation simply returns false to have the normal
4149 * processing happen (calling the item's Runnable or sending a message to
4150 * its Handler as appropriate). You can use this method for any items
4151 * for which you would like to do processing without those other
4152 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004155 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 * @return boolean Return false to allow normal menu processing to
4160 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 * @see #onCreateOptionsMenu
4163 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004164 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 if (mParent != null) {
4166 return mParent.onOptionsItemSelected(item);
4167 }
4168 return false;
4169 }
4170
4171 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004172 * This method is called whenever the user chooses to navigate Up within your application's
4173 * activity hierarchy from the action bar.
4174 *
4175 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4176 * was specified in the manifest for this activity or an activity-alias to it,
4177 * default Up navigation will be handled automatically. If any activity
4178 * along the parent chain requires extra Intent arguments, the Activity subclass
4179 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4180 * to supply those arguments.</p>
4181 *
Mark Lufa434852016-08-11 17:40:33 -07004182 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004183 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4184 * from the design guide for more information about navigating within your app.</p>
4185 *
4186 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4187 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4188 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4189 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4190 *
4191 * @return true if Up navigation completed successfully and this Activity was finished,
4192 * false otherwise.
4193 */
4194 public boolean onNavigateUp() {
4195 // Automatically handle hierarchical Up navigation if the proper
4196 // metadata is available.
4197 Intent upIntent = getParentActivityIntent();
4198 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004199 if (mActivityInfo.taskAffinity == null) {
4200 // Activities with a null affinity are special; they really shouldn't
4201 // specify a parent activity intent in the first place. Just finish
4202 // the current activity and call it a day.
4203 finish();
4204 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004205 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004206 onCreateNavigateUpTaskStack(b);
4207 onPrepareNavigateUpTaskStack(b);
4208 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004209
4210 // We can't finishAffinity if we have a result.
4211 // Fall back and simply finish the current activity instead.
4212 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4213 // Tell the developer what's going on to avoid hair-pulling.
4214 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4215 finish();
4216 } else {
4217 finishAffinity();
4218 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004219 } else {
4220 navigateUpTo(upIntent);
4221 }
4222 return true;
4223 }
4224 return false;
4225 }
4226
4227 /**
4228 * This is called when a child activity of this one attempts to navigate up.
4229 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4230 *
4231 * @param child The activity making the call.
4232 */
4233 public boolean onNavigateUpFromChild(Activity child) {
4234 return onNavigateUp();
4235 }
4236
4237 /**
4238 * Define the synthetic task stack that will be generated during Up navigation from
4239 * a different task.
4240 *
4241 * <p>The default implementation of this method adds the parent chain of this activity
4242 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4243 * may choose to override this method to construct the desired task stack in a different
4244 * way.</p>
4245 *
Adam Powellf0195952012-05-02 21:38:54 -07004246 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4247 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4248 * returned by {@link #getParentActivityIntent()}.</p>
4249 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004250 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4251 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4252 *
4253 * @param builder An empty TaskStackBuilder - the application should add intents representing
4254 * the desired task stack
4255 */
4256 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4257 builder.addParentStack(this);
4258 }
4259
4260 /**
4261 * Prepare the synthetic task stack that will be generated during Up navigation
4262 * from a different task.
4263 *
4264 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4265 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4266 * If any extra data should be added to these intents before launching the new task,
4267 * the application should override this method and add that data here.</p>
4268 *
4269 * @param builder A TaskStackBuilder that has been populated with Intents by
4270 * onCreateNavigateUpTaskStack.
4271 */
4272 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4273 }
4274
4275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 * This hook is called whenever the options menu is being closed (either by the user canceling
4277 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 * @param menu The options menu as last shown or first initialized by
4280 * onCreateOptionsMenu().
4281 */
4282 public void onOptionsMenuClosed(Menu menu) {
4283 if (mParent != null) {
4284 mParent.onOptionsMenuClosed(menu);
4285 }
4286 }
RoboErik55011652014-07-09 15:05:53 -07004287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 /**
4289 * Programmatically opens the options menu. If the options menu is already
4290 * open, this method does nothing.
4291 */
4292 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004293 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4294 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004295 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 }
RoboErik55011652014-07-09 15:05:53 -07004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 /**
4300 * Progammatically closes the options menu. If the options menu is already
4301 * closed, this method does nothing.
4302 */
4303 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004304 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4305 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004306 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 }
4309
4310 /**
4311 * Called when a context menu for the {@code view} is about to be shown.
4312 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4313 * time the context menu is about to be shown and should be populated for
4314 * the view (or item inside the view for {@link AdapterView} subclasses,
4315 * this can be found in the {@code menuInfo})).
4316 * <p>
4317 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4318 * item has been selected.
4319 * <p>
4320 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004321 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 */
4323 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4324 }
4325
4326 /**
4327 * Registers a context menu to be shown for the given view (multiple views
4328 * can show the context menu). This method will set the
4329 * {@link OnCreateContextMenuListener} on the view to this activity, so
4330 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4331 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 * @see #unregisterForContextMenu(View)
4334 * @param view The view that should show a context menu.
4335 */
4336 public void registerForContextMenu(View view) {
4337 view.setOnCreateContextMenuListener(this);
4338 }
RoboErik55011652014-07-09 15:05:53 -07004339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 /**
4341 * Prevents a context menu to be shown for the given view. This method will remove the
4342 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004343 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 * @see #registerForContextMenu(View)
4345 * @param view The view that should stop showing a context menu.
4346 */
4347 public void unregisterForContextMenu(View view) {
4348 view.setOnCreateContextMenuListener(null);
4349 }
RoboErik55011652014-07-09 15:05:53 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 /**
4352 * Programmatically opens the context menu for a particular {@code view}.
4353 * The {@code view} should have been added via
4354 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 * @param view The view to show the context menu for.
4357 */
4358 public void openContextMenu(View view) {
4359 view.showContextMenu();
4360 }
RoboErik55011652014-07-09 15:05:53 -07004361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 /**
4363 * Programmatically closes the most recently opened context menu, if showing.
4364 */
4365 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004366 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4367 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 }
RoboErik55011652014-07-09 15:05:53 -07004370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 /**
4372 * This hook is called whenever an item in a context menu is selected. The
4373 * default implementation simply returns false to have the normal processing
4374 * happen (calling the item's Runnable or sending a message to its Handler
4375 * as appropriate). You can use this method for any items for which you
4376 * would like to do processing without those other facilities.
4377 * <p>
4378 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4379 * View that added this menu item.
4380 * <p>
4381 * Derived classes should call through to the base class for it to perform
4382 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004383 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 * @param item The context menu item that was selected.
4385 * @return boolean Return false to allow normal context menu processing to
4386 * proceed, true to consume it here.
4387 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004388 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 if (mParent != null) {
4390 return mParent.onContextItemSelected(item);
4391 }
4392 return false;
4393 }
4394
4395 /**
4396 * This hook is called whenever the context menu is being closed (either by
4397 * the user canceling the menu with the back/menu button, or when an item is
4398 * selected).
RoboErik55011652014-07-09 15:05:53 -07004399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 * @param menu The context menu that is being closed.
4401 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004402 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 if (mParent != null) {
4404 mParent.onContextMenuClosed(menu);
4405 }
4406 }
4407
4408 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004409 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 protected Dialog onCreateDialog(int id) {
4413 return null;
4414 }
4415
4416 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004417 * Callback for creating dialogs that are managed (saved and restored) for you
4418 * by the activity. The default implementation calls through to
4419 * {@link #onCreateDialog(int)} for compatibility.
4420 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004421 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4422 * or later, consider instead using a {@link DialogFragment} instead.</em>
4423 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004424 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4425 * this method the first time, and hang onto it thereafter. Any dialog
4426 * that is created by this method will automatically be saved and restored
4427 * for you, including whether it is showing.
4428 *
4429 * <p>If you would like the activity to manage saving and restoring dialogs
4430 * for you, you should override this method and handle any ids that are
4431 * passed to {@link #showDialog}.
4432 *
4433 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4434 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4435 *
4436 * @param id The id of the dialog.
4437 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4438 * @return The dialog. If you return null, the dialog will not be created.
4439 *
4440 * @see #onPrepareDialog(int, Dialog, Bundle)
4441 * @see #showDialog(int, Bundle)
4442 * @see #dismissDialog(int)
4443 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004444 *
4445 * @deprecated Use the new {@link DialogFragment} class with
4446 * {@link FragmentManager} instead; this is also
4447 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004448 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004449 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004450 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004451 protected Dialog onCreateDialog(int id, Bundle args) {
4452 return onCreateDialog(id);
4453 }
4454
4455 /**
4456 * @deprecated Old no-arguments version of
4457 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4458 */
4459 @Deprecated
4460 protected void onPrepareDialog(int id, Dialog dialog) {
4461 dialog.setOwnerActivity(this);
4462 }
4463
4464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004466 * shown. The default implementation calls through to
4467 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 * <p>
4470 * Override this if you need to update a managed dialog based on the state
4471 * of the application each time it is shown. For example, a time picker
4472 * dialog might want to be updated with the current time. You should call
4473 * through to the superclass's implementation. The default implementation
4474 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 * @param id The id of the managed dialog.
4477 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004478 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4479 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 * @see #showDialog(int)
4481 * @see #dismissDialog(int)
4482 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004483 *
4484 * @deprecated Use the new {@link DialogFragment} class with
4485 * {@link FragmentManager} instead; this is also
4486 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004488 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004489 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4490 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 }
4492
4493 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004494 * Simple version of {@link #showDialog(int, Bundle)} that does not
4495 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4496 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004497 *
4498 * @deprecated Use the new {@link DialogFragment} class with
4499 * {@link FragmentManager} instead; this is also
4500 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004501 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004502 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004503 public final void showDialog(int id) {
4504 showDialog(id, null);
4505 }
4506
4507 /**
4508 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 * will be made with the same id the first time this is called for a given
4510 * id. From thereafter, the dialog will be automatically saved and restored.
4511 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004512 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4513 * or later, consider instead using a {@link DialogFragment} instead.</em>
4514 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004515 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 * be made to provide an opportunity to do any timely preparation.
4517 *
4518 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004519 * @param args Arguments to pass through to the dialog. These will be saved
4520 * and restored for you. Note that if the dialog is already created,
4521 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4522 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004523 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004524 * @return Returns true if the Dialog was created; false is returned if
4525 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004526 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004527 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004528 * @see #onCreateDialog(int, Bundle)
4529 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 * @see #dismissDialog(int)
4531 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004532 *
4533 * @deprecated Use the new {@link DialogFragment} class with
4534 * {@link FragmentManager} instead; this is also
4535 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004537 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004538 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004540 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004542 ManagedDialog md = mManagedDialogs.get(id);
4543 if (md == null) {
4544 md = new ManagedDialog();
4545 md.mDialog = createDialog(id, null, args);
4546 if (md.mDialog == null) {
4547 return false;
4548 }
4549 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 }
RoboErik55011652014-07-09 15:05:53 -07004551
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004552 md.mArgs = args;
4553 onPrepareDialog(id, md.mDialog, args);
4554 md.mDialog.show();
4555 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 }
4557
4558 /**
4559 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4560 *
4561 * @param id The id of the managed dialog.
4562 *
4563 * @throws IllegalArgumentException if the id was not previously shown via
4564 * {@link #showDialog(int)}.
4565 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004566 * @see #onCreateDialog(int, Bundle)
4567 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 * @see #showDialog(int)
4569 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004570 *
4571 * @deprecated Use the new {@link DialogFragment} class with
4572 * {@link FragmentManager} instead; this is also
4573 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004575 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 public final void dismissDialog(int id) {
4577 if (mManagedDialogs == null) {
4578 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 }
RoboErik55011652014-07-09 15:05:53 -07004580
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004581 final ManagedDialog md = mManagedDialogs.get(id);
4582 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 throw missingDialog(id);
4584 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004585 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
4587
4588 /**
4589 * Creates an exception to throw if a user passed in a dialog id that is
4590 * unexpected.
4591 */
4592 private IllegalArgumentException missingDialog(int id) {
4593 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4594 + "shown via Activity#showDialog");
4595 }
4596
4597 /**
4598 * Removes any internal references to a dialog managed by this Activity.
4599 * If the dialog is showing, it will dismiss it as part of the clean up.
4600 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004601 * <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 -08004602 * want to avoid the overhead of saving and restoring it in the future.
4603 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004604 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4605 * will not throw an exception if you try to remove an ID that does not
4606 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004607 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 * @param id The id of the managed dialog.
4609 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004610 * @see #onCreateDialog(int, Bundle)
4611 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 * @see #showDialog(int)
4613 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004614 *
4615 * @deprecated Use the new {@link DialogFragment} class with
4616 * {@link FragmentManager} instead; this is also
4617 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004619 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004621 if (mManagedDialogs != null) {
4622 final ManagedDialog md = mManagedDialogs.get(id);
4623 if (md != null) {
4624 md.mDialog.dismiss();
4625 mManagedDialogs.remove(id);
4626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 }
4629
4630 /**
4631 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004632 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004633 * <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 -07004634 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004635 * calling this function is the same as calling
4636 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4637 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 * <p>You can override this function to force global search, e.g. in response to a dedicated
4640 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004641 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004642 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4643 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4644 * return false and you must supply your own custom implementation if you want to support
4645 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004646 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004647 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004648 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4649 * not respond to search. The default implementation always returns {@code true}, except
4650 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004651 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 * @see android.app.SearchManager
4653 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004654 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4655 mSearchEvent = searchEvent;
4656 boolean result = onSearchRequested();
4657 mSearchEvent = null;
4658 return result;
4659 }
4660
4661 /**
4662 * @see #onSearchRequested(SearchEvent)
4663 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004665 final int uiMode = getResources().getConfiguration().uiMode
4666 & Configuration.UI_MODE_TYPE_MASK;
4667 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4668 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004669 startSearch(null, false, null, false);
4670 return true;
4671 } else {
4672 return false;
4673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 }
RoboErik55011652014-07-09 15:05:53 -07004675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004677 * During the onSearchRequested() callbacks, this function will return the
4678 * {@link SearchEvent} that triggered the callback, if it exists.
4679 *
4680 * @return SearchEvent The SearchEvent that triggered the {@link
4681 * #onSearchRequested} callback.
4682 */
4683 public final SearchEvent getSearchEvent() {
4684 return mSearchEvent;
4685 }
4686
4687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004689 *
4690 * <p>It is typically called from onSearchRequested(), either directly from
4691 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004693 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 * is to inject specific data such as context data, it is preferred to <i>override</i>
4695 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004696 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004697 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4698 * not supported.
4699 *
RoboErik55011652014-07-09 15:05:53 -07004700 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004702 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 * any further typing will replace it. This is useful for cases where an entire pre-formed
4704 * query is being inserted. If false, the selection point will be placed at the end of the
4705 * inserted query. This is useful when the inserted query is text that the user entered,
4706 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4707 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004708 * @param appSearchData An application can insert application-specific
4709 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 * searches. This data will be returned with SEARCH intent(s). Null if
4711 * no extra data is required.
4712 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004713 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004714 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 * @see android.app.SearchManager
4718 * @see #onSearchRequested
4719 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004720 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4721 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004722 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004723 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004724 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 }
4726
4727 /**
krosaend2d60142009-08-17 08:56:48 -07004728 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4729 * the search dialog. Made available for testing purposes.
4730 *
4731 * @param query The query to trigger. If empty, the request will be ignored.
4732 * @param appSearchData An application can insert application-specific
4733 * context here, in order to improve quality or specificity of its own
4734 * searches. This data will be returned with SEARCH intent(s). Null if
4735 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004736 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004737 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004738 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004739 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004740 }
4741
4742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 * Request that key events come to this activity. Use this if your
4744 * activity has no views with focus, but the activity still wants
4745 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004746 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 * @see android.view.Window#takeKeyEvents
4748 */
4749 public void takeKeyEvents(boolean get) {
4750 getWindow().takeKeyEvents(get);
4751 }
4752
4753 /**
4754 * Enable extended window features. This is a convenience for calling
4755 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 * @param featureId The desired feature as defined in
4758 * {@link android.view.Window}.
4759 * @return Returns true if the requested feature is supported and now
4760 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 * @see android.view.Window#requestFeature
4763 */
4764 public final boolean requestWindowFeature(int featureId) {
4765 return getWindow().requestFeature(featureId);
4766 }
4767
4768 /**
4769 * Convenience for calling
4770 * {@link android.view.Window#setFeatureDrawableResource}.
4771 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004772 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 getWindow().setFeatureDrawableResource(featureId, resId);
4774 }
4775
4776 /**
4777 * Convenience for calling
4778 * {@link android.view.Window#setFeatureDrawableUri}.
4779 */
4780 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4781 getWindow().setFeatureDrawableUri(featureId, uri);
4782 }
4783
4784 /**
4785 * Convenience for calling
4786 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4787 */
4788 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4789 getWindow().setFeatureDrawable(featureId, drawable);
4790 }
4791
4792 /**
4793 * Convenience for calling
4794 * {@link android.view.Window#setFeatureDrawableAlpha}.
4795 */
4796 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4797 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4798 }
4799
4800 /**
4801 * Convenience for calling
4802 * {@link android.view.Window#getLayoutInflater}.
4803 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004804 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 public LayoutInflater getLayoutInflater() {
4806 return getWindow().getLayoutInflater();
4807 }
4808
4809 /**
4810 * Returns a {@link MenuInflater} with this context.
4811 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004812 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004814 // Make sure that action views can get an appropriate theme.
4815 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004816 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004817 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004818 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004819 } else {
4820 mMenuInflater = new MenuInflater(this);
4821 }
4822 }
4823 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 }
4825
4826 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004827 public void setTheme(int resid) {
4828 super.setTheme(resid);
4829 mWindow.setTheme(resid);
4830 }
4831
4832 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004833 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004834 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 if (mParent == null) {
4836 super.onApplyThemeResource(theme, resid, first);
4837 } else {
4838 try {
4839 theme.setTo(mParent.getTheme());
4840 } catch (Exception e) {
4841 // Empty
4842 }
4843 theme.applyStyle(resid, false);
4844 }
Winson073a5262016-02-29 16:04:40 -08004845
4846 // Get the primary color and update the TaskDescription for this activity
4847 TypedArray a = theme.obtainStyledAttributes(
4848 com.android.internal.R.styleable.ActivityTaskDescription);
4849 if (mTaskDescription.getPrimaryColor() == 0) {
4850 int colorPrimary = a.getColor(
4851 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4852 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4853 mTaskDescription.setPrimaryColor(colorPrimary);
4854 }
4855 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004856
4857 int colorBackground = a.getColor(
4858 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4859 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4860 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004861 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004862
4863 final int statusBarColor = a.getColor(
4864 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4865 if (statusBarColor != 0) {
4866 mTaskDescription.setStatusBarColor(statusBarColor);
4867 }
4868
4869 final int navigationBarColor = a.getColor(
4870 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4871 if (navigationBarColor != 0) {
4872 mTaskDescription.setNavigationBarColor(navigationBarColor);
4873 }
4874
Winson073a5262016-02-29 16:04:40 -08004875 a.recycle();
4876 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004877 }
4878
4879 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004880 * Requests permissions to be granted to this application. These permissions
4881 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01004882 * and they should have protection level {@link
4883 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
4884 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08004885 * <p>
4886 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4887 * are granted at install time if requested in the manifest. Signature permissions
4888 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4889 * install time if requested in the manifest and the signature of your app matches
4890 * the signature of the app declaring the permissions.
4891 * </p>
4892 * <p>
4893 * If your app does not have the requested permissions the user will be presented
4894 * with UI for accepting them. After the user has accepted or rejected the
4895 * requested permissions you will receive a callback on {@link
4896 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4897 * permissions were granted or not.
4898 * </p>
4899 * <p>
4900 * Note that requesting a permission does not guarantee it will be granted and
4901 * your app should be able to run without having this permission.
4902 * </p>
4903 * <p>
4904 * This method may start an activity allowing the user to choose which permissions
4905 * to grant and which to reject. Hence, you should be prepared that your activity
4906 * may be paused and resumed. Further, granting some permissions may require
4907 * a restart of you application. In such a case, the system will recreate the
4908 * activity stack before delivering the result to {@link
4909 * #onRequestPermissionsResult(int, String[], int[])}.
4910 * </p>
4911 * <p>
4912 * When checking whether you have a permission you should use {@link
4913 * #checkSelfPermission(String)}.
4914 * </p>
4915 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004916 * Calling this API for permissions already granted to your app would show UI
4917 * to the user to decide whether the app can still hold these permissions. This
4918 * can be useful if the way your app uses data guarded by the permissions
4919 * changes significantly.
4920 * </p>
4921 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004922 * You cannot request a permission if your activity sets {@link
4923 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4924 * <code>true</code> because in this case the activity would not receive
4925 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4926 * </p>
4927 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01004928 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07004929 * RuntimePermissions</a> sample app demonstrates how to use this method to
4930 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004931 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004932 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004933 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004934 * @param requestCode Application specific request code to match with a result
4935 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004936 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004937 *
Paul Miller77a0e132016-02-11 13:46:49 -08004938 * @throws IllegalArgumentException if requestCode is negative.
4939 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004940 * @see #onRequestPermissionsResult(int, String[], int[])
4941 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004942 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004943 */
4944 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004945 if (requestCode < 0) {
4946 throw new IllegalArgumentException("requestCode should be >= 0");
4947 }
Svetoslavffb32b12015-10-15 16:54:00 -07004948 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00004949 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07004950 // Dispatch the callback with empty arrays which means a cancellation.
4951 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4952 return;
4953 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004954 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004955 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004956 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004957 }
4958
4959 /**
4960 * Callback for the result from requesting permissions. This method
4961 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004962 * <p>
4963 * <strong>Note:</strong> It is possible that the permissions request interaction
4964 * with the user is interrupted. In this case you will receive empty permissions
4965 * and results arrays which should be treated as a cancellation.
4966 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004967 *
4968 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4969 * @param permissions The requested permissions. Never null.
4970 * @param grantResults The grant results for the corresponding permissions
4971 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4972 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4973 *
4974 * @see #requestPermissions(String[], int)
4975 */
4976 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4977 @NonNull int[] grantResults) {
4978 /* callback - no nothing */
4979 }
4980
4981 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004982 * Gets whether you should show UI with rationale for requesting a permission.
4983 * You should do this only if you do not have the permission and the context in
4984 * which the permission is requested does not clearly communicate to the user
4985 * what would be the benefit from granting this permission.
4986 * <p>
4987 * For example, if you write a camera app, requesting the camera permission
4988 * would be expected by the user and no rationale for why it is requested is
4989 * needed. If however, the app needs location for tagging photos then a non-tech
4990 * savvy user may wonder how location is related to taking photos. In this case
4991 * you may choose to show UI with rationale of requesting this permission.
4992 * </p>
4993 *
4994 * @param permission A permission your app wants to request.
4995 * @return Whether you can show permission rationale UI.
4996 *
4997 * @see #checkSelfPermission(String)
4998 * @see #requestPermissions(String[], int)
4999 * @see #onRequestPermissionsResult(int, String[], int[])
5000 */
Svetoslav9ce9a152015-06-02 17:59:31 -07005001 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07005002 return getPackageManager().shouldShowRequestPermissionRationale(permission);
5003 }
5004
5005 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005006 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
5007 * with no options.
5008 *
5009 * @param intent The intent to start.
5010 * @param requestCode If >= 0, this code will be returned in
5011 * onActivityResult() when the activity exits.
5012 *
5013 * @throws android.content.ActivityNotFoundException
5014 *
George Mount0a778ed2013-12-13 13:35:36 -08005015 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07005016 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005017 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005018 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005019 }
5020
5021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 * Launch an activity for which you would like a result when it finished.
5023 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08005024 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07005025 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07005027 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 * <p>Note that this method should only be used with Intent protocols
5029 * that are defined to return a result. In other protocols (such as
5030 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
5031 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05005032 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
5033 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005034 *
RoboErik55011652014-07-09 15:05:53 -07005035 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08005037 * activity, then your window will not be displayed until a result is
5038 * returned back from the started activity. This is to avoid visible
5039 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005040 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 * <p>This method throws {@link android.content.ActivityNotFoundException}
5042 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005043 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 * @param intent The intent to start.
5045 * @param requestCode If >= 0, this code will be returned in
5046 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005047 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005048 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005049 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005050 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005052 *
George Mount0a778ed2013-12-13 13:35:36 -08005053 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005055 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
5056 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07005058 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 Instrumentation.ActivityResult ar =
5060 mInstrumentation.execStartActivity(
5061 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005062 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 if (ar != null) {
5064 mMainThread.sendActivityResult(
5065 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
5066 ar.getResultData());
5067 }
5068 if (requestCode >= 0) {
5069 // If this start is requesting a result, we can avoid making
5070 // the activity visible until the result is received. Setting
5071 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5072 // activity hidden during this time, to avoid flickering.
5073 // This can only be done when a result is requested because
5074 // that guarantees we will get information back when the
5075 // activity is finished, no matter what happens to it.
5076 mStartedActivity = true;
5077 }
Adam Powell14874662013-07-18 19:42:41 -07005078
George Mount41725de2015-04-09 08:23:05 -07005079 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005080 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005082 if (options != null) {
5083 mParent.startActivityFromChild(this, intent, requestCode, options);
5084 } else {
5085 // Note we want to go through this method for compatibility with
5086 // existing applications that may have overridden it.
5087 mParent.startActivityFromChild(this, intent, requestCode);
5088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 }
George Mount41725de2015-04-09 08:23:05 -07005090 }
5091
5092 /**
5093 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5094 *
5095 * @param options The ActivityOptions bundle used to start an Activity.
5096 */
5097 private void cancelInputsAndStartExitTransition(Bundle options) {
5098 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5099 if (decor != null) {
5100 decor.cancelPendingInputEvents();
5101 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005102 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005103 mActivityTransitionState.startExitOutTransition(this, options);
5104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 }
5106
Chet Haaseb64e777f2016-12-09 07:17:10 -08005107 /**
5108 * Returns whether there are any activity transitions currently running on this
5109 * activity. A return value of {@code true} can mean that either an enter or
5110 * exit transition is running, including whether the background of the activity
5111 * is animating as a part of that transition.
5112 *
5113 * @return true if a transition is currently running on this activity, false otherwise.
5114 */
5115 public boolean isActivityTransitionRunning() {
5116 return mActivityTransitionState.isTransitionRunning();
5117 }
5118
George Mount413739e2016-06-08 07:13:37 -07005119 private Bundle transferSpringboardActivityOptions(Bundle options) {
5120 if (options == null && (mWindow != null && !mWindow.isActive())) {
5121 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005122 if (activityOptions != null &&
5123 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005124 return activityOptions.toBundle();
5125 }
5126 }
5127 return options;
5128 }
5129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005131 * @hide Implement to provide correct calling token.
5132 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005133 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005134 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5135 startActivityForResultAsUser(intent, requestCode, null, user);
5136 }
5137
5138 /**
5139 * @hide Implement to provide correct calling token.
5140 */
5141 public void startActivityForResultAsUser(Intent intent, int requestCode,
5142 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005143 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5144 }
5145
5146 /**
5147 * @hide Implement to provide correct calling token.
5148 */
5149 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5150 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005151 if (mParent != null) {
5152 throw new RuntimeException("Can't be called from a child");
5153 }
George Mount413739e2016-06-08 07:13:37 -07005154 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005155 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005156 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005157 options, user);
5158 if (ar != null) {
5159 mMainThread.sendActivityResult(
5160 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5161 }
5162 if (requestCode >= 0) {
5163 // If this start is requesting a result, we can avoid making
5164 // the activity visible until the result is received. Setting
5165 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5166 // activity hidden during this time, to avoid flickering.
5167 // This can only be done when a result is requested because
5168 // that guarantees we will get information back when the
5169 // activity is finished, no matter what happens to it.
5170 mStartedActivity = true;
5171 }
5172
George Mount41725de2015-04-09 08:23:05 -07005173 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005174 }
5175
5176 /**
5177 * @hide Implement to provide correct calling token.
5178 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005179 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005180 public void startActivityAsUser(Intent intent, UserHandle user) {
5181 startActivityAsUser(intent, null, user);
5182 }
5183
5184 /**
5185 * @hide Implement to provide correct calling token.
5186 */
5187 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5188 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005189 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005190 }
George Mount413739e2016-06-08 07:13:37 -07005191 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005192 Instrumentation.ActivityResult ar =
5193 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005194 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005195 intent, -1, options, user);
5196 if (ar != null) {
5197 mMainThread.sendActivityResult(
5198 mToken, mEmbeddedID, -1, ar.getResultCode(),
5199 ar.getResultData());
5200 }
George Mount41725de2015-04-09 08:23:05 -07005201 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005202 }
5203
5204 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005205 * Start a new activity as if it was started by the activity that started our
5206 * current activity. This is for the resolver and chooser activities, which operate
5207 * as intermediaries that dispatch their intent to the target the user selects -- to
5208 * do this, they must perform all security checks including permission grants as if
5209 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005210 * @param intent The Intent to start.
5211 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005212 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005213 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5214 * caller it is doing the start is, is actually allowed to start the target activity.
5215 * If you set this to true, you must set an explicit component in the Intent and do any
5216 * appropriate security checks yourself.
5217 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005218 * @hide
5219 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005220 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005221 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005222 if (mParent != null) {
5223 throw new RuntimeException("Can't be called from a child");
5224 }
George Mount413739e2016-06-08 07:13:37 -07005225 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005226 Instrumentation.ActivityResult ar =
5227 mInstrumentation.execStartActivityAsCaller(
5228 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005229 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005230 if (ar != null) {
5231 mMainThread.sendActivityResult(
5232 mToken, mEmbeddedID, -1, ar.getResultCode(),
5233 ar.getResultData());
5234 }
George Mount41725de2015-04-09 08:23:05 -07005235 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005236 }
5237
5238 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005239 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5240 * Intent, int, int, int, Bundle)} with no options.
5241 *
5242 * @param intent The IntentSender to launch.
5243 * @param requestCode If >= 0, this code will be returned in
5244 * onActivityResult() when the activity exits.
5245 * @param fillInIntent If non-null, this will be provided as the
5246 * intent parameter to {@link IntentSender#sendIntent}.
5247 * @param flagsMask Intent flags in the original IntentSender that you
5248 * would like to change.
5249 * @param flagsValues Desired values for any bits set in
5250 * <var>flagsMask</var>
5251 * @param extraFlags Always set to 0.
5252 */
5253 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005254 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005255 throws IntentSender.SendIntentException {
5256 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5257 flagsValues, extraFlags, null);
5258 }
5259
5260 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005261 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005262 * to use a IntentSender to describe the activity to be started. If
5263 * the IntentSender is for an activity, that activity will be started
5264 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5265 * here; otherwise, its associated action will be executed (such as
5266 * sending a broadcast) as if you had called
5267 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005268 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005269 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005270 * @param requestCode If >= 0, this code will be returned in
5271 * onActivityResult() when the activity exits.
5272 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005273 * intent parameter to {@link IntentSender#sendIntent}.
5274 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005275 * would like to change.
5276 * @param flagsValues Desired values for any bits set in
5277 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005278 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005279 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005280 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005281 * Context.startActivity(Intent, Bundle)} for more details. If options
5282 * have also been supplied by the IntentSender, options given here will
5283 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005284 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005285 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005286 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005287 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005288 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005289 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5290 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005291 } else if (options != null) {
5292 mParent.startIntentSenderFromChild(this, intent, requestCode,
5293 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005294 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005295 // Note we want to go through this call for compatibility with
5296 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005297 mParent.startIntentSenderFromChild(this, intent, requestCode,
5298 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005299 }
5300 }
5301
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005302 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5303 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005304 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005305 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005306 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005307 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005308 String resolvedType = null;
5309 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005310 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005311 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005312 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5313 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005314 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005315 .startActivityIntentSender(mMainThread.getApplicationThread(),
5316 intent != null ? intent.getTarget() : null,
5317 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005318 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005319 requestCode, flagsMask, flagsValues, options);
5320 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005321 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005322 }
5323 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005324
5325 if (options != null) {
5326 // Only when the options are not null, as the intent can point to something other
5327 // than an Activity.
5328 cancelInputsAndStartExitTransition(options);
5329 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005330 } catch (RemoteException e) {
5331 }
5332 if (requestCode >= 0) {
5333 // If this start is requesting a result, we can avoid making
5334 // the activity visible until the result is received. Setting
5335 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5336 // activity hidden during this time, to avoid flickering.
5337 // This can only be done when a result is requested because
5338 // that guarantees we will get information back when the
5339 // activity is finished, no matter what happens to it.
5340 mStartedActivity = true;
5341 }
5342 }
5343
5344 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005345 * Same as {@link #startActivity(Intent, Bundle)} with no options
5346 * specified.
5347 *
5348 * @param intent The intent to start.
5349 *
5350 * @throws android.content.ActivityNotFoundException
5351 *
Mark Lufa434852016-08-11 17:40:33 -07005352 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005353 * @see #startActivityForResult
5354 */
5355 @Override
5356 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005357 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005358 }
5359
5360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 * Launch a new activity. You will not receive any information about when
5362 * the activity exits. This implementation overrides the base version,
5363 * providing information about
5364 * the activity performing the launch. Because of this additional
5365 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5366 * required; if not specified, the new activity will be added to the
5367 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 * <p>This method throws {@link android.content.ActivityNotFoundException}
5370 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005371 *
5372 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005373 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005374 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005375 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005378 *
Mark Lufa434852016-08-11 17:40:33 -07005379 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005380 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 */
5382 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005383 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005384 if (options != null) {
5385 startActivityForResult(intent, -1, options);
5386 } else {
5387 // Note we want to go through this call for compatibility with
5388 // applications that may have overridden the method.
5389 startActivityForResult(intent, -1);
5390 }
5391 }
5392
5393 /**
5394 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5395 * specified.
5396 *
5397 * @param intents The intents to start.
5398 *
5399 * @throws android.content.ActivityNotFoundException
5400 *
Mark Lufa434852016-08-11 17:40:33 -07005401 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005402 * @see #startActivityForResult
5403 */
5404 @Override
5405 public void startActivities(Intent[] intents) {
5406 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 }
5408
5409 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005410 * Launch a new activity. You will not receive any information about when
5411 * the activity exits. This implementation overrides the base version,
5412 * providing information about
5413 * the activity performing the launch. Because of this additional
5414 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5415 * required; if not specified, the new activity will be added to the
5416 * task of the caller.
5417 *
5418 * <p>This method throws {@link android.content.ActivityNotFoundException}
5419 * if there was no Activity found to run the given Intent.
5420 *
5421 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005422 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005423 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005424 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005425 *
5426 * @throws android.content.ActivityNotFoundException
5427 *
Mark Lufa434852016-08-11 17:40:33 -07005428 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005429 * @see #startActivityForResult
5430 */
5431 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005432 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005433 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005434 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005435 }
5436
5437 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005438 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5439 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005440 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005441 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005442 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005443 * intent parameter to {@link IntentSender#sendIntent}.
5444 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005445 * would like to change.
5446 * @param flagsValues Desired values for any bits set in
5447 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005448 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005449 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005450 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005451 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005452 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005453 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5454 extraFlags, null);
5455 }
5456
5457 /**
5458 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5459 * to start; see
5460 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5461 * for more information.
5462 *
5463 * @param intent The IntentSender to launch.
5464 * @param fillInIntent If non-null, this will be provided as the
5465 * intent parameter to {@link IntentSender#sendIntent}.
5466 * @param flagsMask Intent flags in the original IntentSender that you
5467 * would like to change.
5468 * @param flagsValues Desired values for any bits set in
5469 * <var>flagsMask</var>
5470 * @param extraFlags Always set to 0.
5471 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005472 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005473 * Context.startActivity(Intent, Bundle)} for more details. If options
5474 * have also been supplied by the IntentSender, options given here will
5475 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005476 */
5477 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005478 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005479 Bundle options) throws IntentSender.SendIntentException {
5480 if (options != null) {
5481 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5482 flagsValues, extraFlags, options);
5483 } else {
5484 // Note we want to go through this call for compatibility with
5485 // applications that may have overridden the method.
5486 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5487 flagsValues, extraFlags);
5488 }
5489 }
5490
5491 /**
5492 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5493 * with no options.
5494 *
5495 * @param intent The intent to start.
5496 * @param requestCode If >= 0, this code will be returned in
5497 * onActivityResult() when the activity exits, as described in
5498 * {@link #startActivityForResult}.
5499 *
5500 * @return If a new activity was launched then true is returned; otherwise
5501 * false is returned and you must handle the Intent yourself.
5502 *
5503 * @see #startActivity
5504 * @see #startActivityForResult
5505 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005506 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5507 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005508 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005509 }
5510
5511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 * A special variation to launch an activity only if a new activity
5513 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005514 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005516 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005518 * and the activity
5519 * that handles <var>intent</var> is the same as your currently running
5520 * activity, then a new instance is not needed. In this case, instead of
5521 * the normal behavior of calling {@link #onNewIntent} this function will
5522 * return and you can handle the Intent yourself.
5523 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 * <p>This function can only be called from a top-level activity; if it is
5525 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005526 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 * @param intent The intent to start.
5528 * @param requestCode If >= 0, this code will be returned in
5529 * onActivityResult() when the activity exits, as described in
5530 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005531 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005532 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005533 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005534 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 * @return If a new activity was launched then true is returned; otherwise
5536 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005537 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 * @see #startActivity
5539 * @see #startActivityForResult
5540 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005541 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5542 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005544 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005546 Uri referrer = onProvideReferrer();
5547 if (referrer != null) {
5548 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5549 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005550 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005551 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005552 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005553 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005554 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5555 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5556 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 } catch (RemoteException e) {
5558 // Empty
5559 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 if (requestCode >= 0) {
5564 // If this start is requesting a result, we can avoid making
5565 // the activity visible until the result is received. Setting
5566 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5567 // activity hidden during this time, to avoid flickering.
5568 // This can only be done when a result is requested because
5569 // that guarantees we will get information back when the
5570 // activity is finished, no matter what happens to it.
5571 mStartedActivity = true;
5572 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005573 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 }
5575
5576 throw new UnsupportedOperationException(
5577 "startActivityIfNeeded can only be called from a top-level activity");
5578 }
5579
5580 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005581 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5582 * no options.
5583 *
5584 * @param intent The intent to dispatch to the next activity. For
5585 * correct behavior, this must be the same as the Intent that started
5586 * your own activity; the only changes you can make are to the extras
5587 * inside of it.
5588 *
5589 * @return Returns a boolean indicating whether there was another Activity
5590 * to start: true if there was a next activity to start, false if there
5591 * wasn't. In general, if true is returned you will then want to call
5592 * finish() on yourself.
5593 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005594 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005595 return startNextMatchingActivity(intent, null);
5596 }
5597
5598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 * Special version of starting an activity, for use when you are replacing
5600 * other activity components. You can use this to hand the Intent off
5601 * to the next Activity that can handle it. You typically call this in
5602 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 * @param intent The intent to dispatch to the next activity. For
5605 * correct behavior, this must be the same as the Intent that started
5606 * your own activity; the only changes you can make are to the extras
5607 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005608 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005609 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005610 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 * @return Returns a boolean indicating whether there was another Activity
5613 * to start: true if there was a next activity to start, false if there
5614 * wasn't. In general, if true is returned you will then want to call
5615 * finish() on yourself.
5616 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005617 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5618 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 if (mParent == null) {
5620 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005621 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005622 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005623 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005624 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 } catch (RemoteException e) {
5626 // Empty
5627 }
5628 return false;
5629 }
5630
5631 throw new UnsupportedOperationException(
5632 "startNextMatchingActivity can only be called from a top-level activity");
5633 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005634
5635 /**
5636 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5637 * with no options.
5638 *
5639 * @param child The activity making the call.
5640 * @param intent The intent to start.
5641 * @param requestCode Reply request code. < 0 if reply is not requested.
5642 *
5643 * @throws android.content.ActivityNotFoundException
5644 *
5645 * @see #startActivity
5646 * @see #startActivityForResult
5647 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005648 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005649 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005650 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005651 }
5652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 /**
RoboErik55011652014-07-09 15:05:53 -07005654 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 * <p>This method throws {@link android.content.ActivityNotFoundException}
5658 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 * @param child The activity making the call.
5661 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005662 * @param requestCode Reply request code. < 0 if reply is not requested.
5663 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005664 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005665 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005668 *
5669 * @see #startActivity
5670 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005672 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005673 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005674 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 Instrumentation.ActivityResult ar =
5676 mInstrumentation.execStartActivity(
5677 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005678 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 if (ar != null) {
5680 mMainThread.sendActivityResult(
5681 mToken, child.mEmbeddedID, requestCode,
5682 ar.getResultCode(), ar.getResultData());
5683 }
George Mount41725de2015-04-09 08:23:05 -07005684 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 }
5686
5687 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005688 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5689 * with no options.
5690 *
5691 * @param fragment The fragment making the call.
5692 * @param intent The intent to start.
5693 * @param requestCode Reply request code. < 0 if reply is not requested.
5694 *
5695 * @throws android.content.ActivityNotFoundException
5696 *
5697 * @see Fragment#startActivity
5698 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005699 *
5700 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5701 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005702 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005703 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005704 public void startActivityFromFragment(@NonNull Fragment fragment,
5705 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005706 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005707 }
5708
5709 /**
RoboErik55011652014-07-09 15:05:53 -07005710 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005711 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5712 * method.
RoboErik55011652014-07-09 15:05:53 -07005713 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005714 * <p>This method throws {@link android.content.ActivityNotFoundException}
5715 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005716 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005717 * @param fragment The fragment making the call.
5718 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005719 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005720 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005721 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005722 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005723 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005724 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005725 *
5726 * @see Fragment#startActivity
5727 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005728 *
5729 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5730 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005731 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005732 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005733 public void startActivityFromFragment(@NonNull Fragment fragment,
5734 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005735 startActivityForResult(fragment.mWho, intent, requestCode, options);
5736 }
5737
5738 /**
5739 * @hide
5740 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005741 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5742 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5743 UserHandle user) {
5744 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5745 }
5746
5747 /**
5748 * @hide
5749 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005750 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005751 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005752 public void startActivityForResult(
5753 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005754 Uri referrer = onProvideReferrer();
5755 if (referrer != null) {
5756 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5757 }
George Mount413739e2016-06-08 07:13:37 -07005758 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005759 Instrumentation.ActivityResult ar =
5760 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005761 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005762 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005763 if (ar != null) {
5764 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005765 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005766 ar.getResultCode(), ar.getResultData());
5767 }
George Mount41725de2015-04-09 08:23:05 -07005768 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005769 }
5770
5771 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005772 * @hide
5773 */
5774 @Override
5775 public boolean canStartActivityForResult() {
5776 return true;
5777 }
5778
5779 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005780 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5781 * int, Intent, int, int, int, Bundle)} with no options.
5782 */
5783 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5784 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5785 int extraFlags)
5786 throws IntentSender.SendIntentException {
5787 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5788 flagsMask, flagsValues, extraFlags, null);
5789 }
5790
5791 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005792 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005793 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005794 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005795 * for more information.
5796 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005797 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5798 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005799 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005800 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005801 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5802 flagsMask, flagsValues, options);
5803 }
5804
5805 /**
5806 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5807 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5808 * for more information.
5809 *
5810 * @hide
5811 */
5812 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5813 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5814 int extraFlags, @Nullable Bundle options)
5815 throws IntentSender.SendIntentException {
5816 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5817 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005818 }
5819
5820 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005821 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5822 * or {@link #finish} to specify an explicit transition animation to
5823 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005824 *
5825 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5826 * to using this with starting activities is to supply the desired animation
5827 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005828 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005829 * you to specify a custom animation even when starting an activity from
5830 * outside the context of the current top activity.
5831 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005832 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005833 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005834 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005835 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005836 */
5837 public void overridePendingTransition(int enterAnim, int exitAnim) {
5838 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005839 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005840 mToken, getPackageName(), enterAnim, exitAnim);
5841 } catch (RemoteException e) {
5842 }
5843 }
RoboErik55011652014-07-09 15:05:53 -07005844
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 * Call this to set the result that your activity will return to its
5847 * caller.
RoboErik55011652014-07-09 15:05:53 -07005848 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 * @param resultCode The result code to propagate back to the originating
5850 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 * @see #RESULT_CANCELED
5853 * @see #RESULT_OK
5854 * @see #RESULT_FIRST_USER
5855 * @see #setResult(int, Intent)
5856 */
5857 public final void setResult(int resultCode) {
5858 synchronized (this) {
5859 mResultCode = resultCode;
5860 mResultData = null;
5861 }
5862 }
5863
5864 /**
5865 * Call this to set the result that your activity will return to its
5866 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005867 *
5868 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5869 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5870 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5871 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5872 * Activity receiving the result access to the specific URIs in the Intent.
5873 * Access will remain until the Activity has finished (it will remain across the hosting
5874 * process being killed and other temporary destruction) and will be added
5875 * to any existing set of URI permissions it already holds.
5876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 * @param resultCode The result code to propagate back to the originating
5878 * activity, often RESULT_CANCELED or RESULT_OK
5879 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005880 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 * @see #RESULT_CANCELED
5882 * @see #RESULT_OK
5883 * @see #RESULT_FIRST_USER
5884 * @see #setResult(int)
5885 */
5886 public final void setResult(int resultCode, Intent data) {
5887 synchronized (this) {
5888 mResultCode = resultCode;
5889 mResultData = data;
5890 }
5891 }
5892
5893 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005894 * Return information about who launched this activity. If the launching Intent
5895 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5896 * that will be returned as-is; otherwise, if known, an
5897 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5898 * package name that started the Intent will be returned. This may return null if no
5899 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5900 * application package was involved.
5901 *
5902 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5903 * return the referrer that submitted that new intent to the activity. Otherwise, it
5904 * always returns the referrer of the original Intent.</p>
5905 *
5906 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5907 * referrer information, applications can spoof it.</p>
5908 */
5909 @Nullable
5910 public Uri getReferrer() {
5911 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005912 try {
5913 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5914 if (referrer != null) {
5915 return referrer;
5916 }
5917 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5918 if (referrerName != null) {
5919 return Uri.parse(referrerName);
5920 }
5921 } catch (BadParcelableException e) {
5922 Log.w(TAG, "Cannot read referrer from intent;"
5923 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005924 }
5925 if (mReferrer != null) {
5926 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5927 }
5928 return null;
5929 }
5930
5931 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005932 * Override to generate the desired referrer for the content currently being shown
5933 * by the app. The default implementation returns null, meaning the referrer will simply
5934 * be the android-app: of the package name of this activity. Return a non-null Uri to
5935 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5936 */
5937 public Uri onProvideReferrer() {
5938 return null;
5939 }
5940
5941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 * Return the name of the package that invoked this activity. This is who
5943 * the data in {@link #setResult setResult()} will be sent to. You can
5944 * use this information to validate that the recipient is allowed to
5945 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005946 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005947 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005948 * did not use the {@link #startActivityForResult}
5949 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005950 * null.</p>
5951 *
5952 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5953 * the result from this method was unstable. If the process hosting the calling
5954 * package was no longer running, it would return null instead of the proper package
5955 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5956 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 * @return The package of the activity that will receive your
5959 * reply, or null if none.
5960 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005961 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 public String getCallingPackage() {
5963 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005964 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 } catch (RemoteException e) {
5966 return null;
5967 }
5968 }
5969
5970 /**
5971 * Return the name of the activity that invoked this activity. This is
5972 * who the data in {@link #setResult setResult()} will be sent to. You
5973 * can use this information to validate that the recipient is allowed to
5974 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005975 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005976 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005977 * did not use the {@link #startActivityForResult}
5978 * form that includes a request code), then the calling package will be
5979 * null.
5980 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005981 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 * reply, or null if none.
5983 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005984 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 public ComponentName getCallingActivity() {
5986 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005987 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 } catch (RemoteException e) {
5989 return null;
5990 }
5991 }
5992
5993 /**
5994 * Control whether this activity's main window is visible. This is intended
5995 * only for the special case of an activity that is not going to show a
5996 * UI itself, but can't just finish prior to onResume() because it needs
5997 * to wait for a service binding or such. Setting this to false allows
5998 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005999 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 * <p>The default value for this is taken from the
6001 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
6002 */
6003 public void setVisible(boolean visible) {
6004 if (mVisibleFromClient != visible) {
6005 mVisibleFromClient = visible;
6006 if (mVisibleFromServer) {
6007 if (visible) makeVisible();
6008 else mDecor.setVisibility(View.INVISIBLE);
6009 }
6010 }
6011 }
RoboErik55011652014-07-09 15:05:53 -07006012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 void makeVisible() {
6014 if (!mWindowAdded) {
6015 ViewManager wm = getWindowManager();
6016 wm.addView(mDecor, getWindow().getAttributes());
6017 mWindowAdded = true;
6018 }
6019 mDecor.setVisibility(View.VISIBLE);
6020 }
RoboErik55011652014-07-09 15:05:53 -07006021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 /**
6023 * Check to see whether this activity is in the process of finishing,
6024 * either because you called {@link #finish} on it or someone else
6025 * has requested that it finished. This is often used in
6026 * {@link #onPause} to determine whether the activity is simply pausing or
6027 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07006028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07006030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 * @see #finish
6032 */
6033 public boolean isFinishing() {
6034 return mFinished;
6035 }
6036
6037 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006038 * Returns true if the final {@link #onDestroy()} call has been made
6039 * on the Activity, so this instance is now dead.
6040 */
6041 public boolean isDestroyed() {
6042 return mDestroyed;
6043 }
6044
6045 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006046 * Check to see whether this activity is in the process of being destroyed in order to be
6047 * recreated with a new configuration. This is often used in
6048 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
6049 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07006050 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006051 * @return If the activity is being torn down in order to be recreated with a new configuration,
6052 * returns true; else returns false.
6053 */
6054 public boolean isChangingConfigurations() {
6055 return mChangingConfigurations;
6056 }
6057
6058 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006059 * Cause this Activity to be recreated with a new instance. This results
6060 * in essentially the same flow as when the Activity is created due to
6061 * a configuration change -- the current instance will go through its
6062 * lifecycle to {@link #onDestroy} and a new instance then created after it.
6063 */
6064 public void recreate() {
6065 if (mParent != null) {
6066 throw new IllegalStateException("Can only be called on top-level activity");
6067 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07006068 if (Looper.myLooper() != mMainThread.getLooper()) {
6069 throw new IllegalStateException("Must be called from main thread");
6070 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006071 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006072 }
6073
6074 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006075 * Finishes the current activity and specifies whether to remove the task associated with this
6076 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006078 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006079 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 if (mParent == null) {
6081 int resultCode;
6082 Intent resultData;
6083 synchronized (this) {
6084 resultCode = mResultCode;
6085 resultData = mResultData;
6086 }
Joe Onorato43a17652011-04-06 19:22:23 -07006087 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006089 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006090 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006091 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006092 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006093 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 mFinished = true;
6095 }
6096 } catch (RemoteException e) {
6097 // Empty
6098 }
6099 } else {
6100 mParent.finishFromChild(this);
6101 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006102
6103 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6104 // be restored now.
6105 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
6106 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6107 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
6108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 }
6110
6111 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006112 * Call this when your activity is done and should be closed. The
6113 * ActivityResult is propagated back to whoever launched you via
6114 * onActivityResult().
6115 */
6116 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006117 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006118 }
6119
6120 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006121 * Finish this activity as well as all activities immediately below it
6122 * in the current task that have the same affinity. This is typically
6123 * used when an application can be launched on to another task (such as
6124 * from an ACTION_VIEW of a content type it understands) and the user
6125 * has used the up navigation to switch out of the current task and in
6126 * to its own task. In this case, if the user has navigated down into
6127 * any other activities of the second application, all of those should
6128 * be removed from the original task as part of the task switch.
6129 *
6130 * <p>Note that this finish does <em>not</em> allow you to deliver results
6131 * to the previous activity, and an exception will be thrown if you are trying
6132 * to do so.</p>
6133 */
6134 public void finishAffinity() {
6135 if (mParent != null) {
6136 throw new IllegalStateException("Can not be called from an embedded activity");
6137 }
6138 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6139 throw new IllegalStateException("Can not be called to deliver a result");
6140 }
6141 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006142 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006143 mFinished = true;
6144 }
6145 } catch (RemoteException e) {
6146 // Empty
6147 }
6148 }
6149
6150 /**
RoboErik55011652014-07-09 15:05:53 -07006151 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 * {@link #finish} method. The default implementation simply calls
6153 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 * @see #finish
6158 */
6159 public void finishFromChild(Activity child) {
6160 finish();
6161 }
6162
6163 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006164 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6165 * to reverse its exit Transition. When the exit Transition completes,
6166 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6167 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006168 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006169 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006170 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006171 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006172 finish();
6173 }
George Mountcb4b7d92014-02-25 10:47:55 -08006174 }
6175
6176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 * Force finish another activity that you had previously started with
6178 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 * @param requestCode The request code of the activity that you had
6181 * given to startActivityForResult(). If there are multiple
6182 * activities started with this request code, they
6183 * will all be finished.
6184 */
6185 public void finishActivity(int requestCode) {
6186 if (mParent == null) {
6187 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006188 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6190 } catch (RemoteException e) {
6191 // Empty
6192 }
6193 } else {
6194 mParent.finishActivityFromChild(this, requestCode);
6195 }
6196 }
6197
6198 /**
6199 * This is called when a child activity of this one calls its
6200 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006202 * @param child The activity making the call.
6203 * @param requestCode Request code that had been used to start the
6204 * activity.
6205 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006206 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006208 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6210 } catch (RemoteException e) {
6211 // Empty
6212 }
6213 }
6214
6215 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006216 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006217 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006218 */
6219 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006220 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006221 }
6222
6223 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006224 * Ask that the local app instance of this activity be released to free up its memory.
6225 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6226 * a new instance of the activity will later be re-created if needed due to the user
6227 * navigating back to it.
6228 *
6229 * @return Returns true if the activity was in a state that it has started the process
6230 * of destroying its current instance; returns false if for any reason this could not
6231 * be done: it is currently visible to the user, it is already being destroyed, it is
6232 * being finished, it hasn't yet saved its state, etc.
6233 */
6234 public boolean releaseInstance() {
6235 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006236 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006237 } catch (RemoteException e) {
6238 // Empty
6239 }
6240 return false;
6241 }
6242
6243 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 * Called when an activity you launched exits, giving you the requestCode
6245 * you started it with, the resultCode it returned, and any additional
6246 * data from it. The <var>resultCode</var> will be
6247 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6248 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 * <p>You will receive this call immediately before onResume() when your
6251 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006252 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006253 * <p>This method is never invoked if your activity sets
6254 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6255 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 * @param requestCode The integer request code originally supplied to
6258 * startActivityForResult(), allowing you to identify who this
6259 * result came from.
6260 * @param resultCode The integer result code returned by the child activity
6261 * through its setResult().
6262 * @param data An Intent, which can return result data to the caller
6263 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 * @see #startActivityForResult
6266 * @see #createPendingResult
6267 * @see #setResult(int)
6268 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006269 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 }
6271
6272 /**
George Mount62ab9b72014-05-02 13:51:17 -07006273 * Called when an activity you launched with an activity transition exposes this
6274 * Activity through a returning activity transition, giving you the resultCode
6275 * and any additional data from it. This method will only be called if the activity
6276 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006277 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006278 *
6279 * <p>The purpose of this function is to let the called Activity send a hint about
6280 * its state so that this underlying Activity can prepare to be exposed. A call to
6281 * this method does not guarantee that the called Activity has or will be exiting soon.
6282 * It only indicates that it will expose this Activity's Window and it has
6283 * some data to pass to prepare it.</p>
6284 *
6285 * @param resultCode The integer result code returned by the child activity
6286 * through its setResult().
6287 * @param data An Intent, which can return result data to the caller
6288 * (various data can be attached to Intent "extras").
6289 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006290 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006291 }
6292
6293 /**
RoboErik55011652014-07-09 15:05:53 -07006294 * Create a new PendingIntent object which you can hand to others
6295 * for them to use to send result data back to your
6296 * {@link #onActivityResult} callback. The created object will be either
6297 * one-shot (becoming invalid after a result is sent back) or multiple
6298 * (allowing any number of results to be sent through it).
6299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 * @param requestCode Private request code for the sender that will be
6301 * associated with the result data when it is returned. The sender can not
6302 * modify this value, allowing you to identify incoming results.
6303 * @param data Default data to supply in the result, which may be modified
6304 * by the sender.
6305 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6306 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6307 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6308 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6309 * or any of the flags as supported by
6310 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6311 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 * @return Returns an existing or new PendingIntent matching the given
6314 * parameters. May return null only if
6315 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6316 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 * @see PendingIntent
6319 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006320 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6321 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 String packageName = getPackageName();
6323 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006324 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006326 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07006327 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07006329 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006330 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 return target != null ? new PendingIntent(target) : null;
6332 } catch (RemoteException e) {
6333 // Empty
6334 }
6335 return null;
6336 }
6337
6338 /**
6339 * Change the desired orientation of this activity. If the activity
6340 * is currently in the foreground or otherwise impacting the screen
6341 * orientation, the screen will immediately be changed (possibly causing
6342 * the activity to be restarted). Otherwise, this will be used the next
6343 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 * @param requestedOrientation An orientation constant as used in
6346 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6347 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006348 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 if (mParent == null) {
6350 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006351 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 mToken, requestedOrientation);
6353 } catch (RemoteException e) {
6354 // Empty
6355 }
6356 } else {
6357 mParent.setRequestedOrientation(requestedOrientation);
6358 }
6359 }
RoboErik55011652014-07-09 15:05:53 -07006360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 /**
6362 * Return the current requested orientation of the activity. This will
6363 * either be the orientation requested in its component's manifest, or
6364 * the last requested orientation given to
6365 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 * @return Returns an orientation constant as used in
6368 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6369 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006370 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 public int getRequestedOrientation() {
6372 if (mParent == null) {
6373 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006374 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 .getRequestedOrientation(mToken);
6376 } catch (RemoteException e) {
6377 // Empty
6378 }
6379 } else {
6380 return mParent.getRequestedOrientation();
6381 }
6382 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6383 }
RoboErik55011652014-07-09 15:05:53 -07006384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 /**
6386 * Return the identifier of the task this activity is in. This identifier
6387 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006388 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 * @return Task identifier, an opaque integer.
6390 */
6391 public int getTaskId() {
6392 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006393 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 } catch (RemoteException e) {
6395 return -1;
6396 }
6397 }
6398
6399 /**
6400 * Return whether this activity is the root of a task. The root is the
6401 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 * @return True if this is the root activity, else false.
6404 */
Winson Chung4d8681f2017-05-23 16:22:08 -07006405 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 public boolean isTaskRoot() {
6407 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006408 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 } catch (RemoteException e) {
6410 return false;
6411 }
6412 }
6413
6414 /**
6415 * Move the task containing this activity to the back of the activity
6416 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 * @param nonRoot If false then this only works if the activity is the root
6419 * of a task; if true it will work for any activity in
6420 * a task.
RoboErik55011652014-07-09 15:05:53 -07006421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 * @return If the task was moved (or it was already at the
6423 * back) true is returned, else false.
6424 */
6425 public boolean moveTaskToBack(boolean nonRoot) {
6426 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006427 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006428 } catch (RemoteException e) {
6429 // Empty
6430 }
6431 return false;
6432 }
6433
6434 /**
6435 * Returns class name for this activity with the package prefix removed.
6436 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006437 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 * @return The local class name.
6439 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006440 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006441 public String getLocalClassName() {
6442 final String pkg = getPackageName();
6443 final String cls = mComponent.getClassName();
6444 int packageLen = pkg.length();
6445 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6446 || cls.charAt(packageLen) != '.') {
6447 return cls;
6448 }
6449 return cls.substring(packageLen+1);
6450 }
RoboErik55011652014-07-09 15:05:53 -07006451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006453 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 * @return Returns the complete component name for this activity
6456 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006457 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 return mComponent;
6459 }
6460
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006461 /** @hide */
6462 @Override
6463 public final ComponentName autofillClientGetComponentName() {
6464 return getComponentName();
6465 }
6466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 /**
6468 * Retrieve a {@link SharedPreferences} object for accessing preferences
6469 * that are private to this activity. This simply calls the underlying
6470 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6471 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006472 *
6473 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006474 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 *
6476 * @return Returns the single SharedPreferences instance that can be used
6477 * to retrieve and modify the preference values.
6478 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006479 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006480 return getSharedPreferences(getLocalClassName(), mode);
6481 }
RoboErik55011652014-07-09 15:05:53 -07006482
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006483 private void ensureSearchManager() {
6484 if (mSearchManager != null) {
6485 return;
6486 }
RoboErik55011652014-07-09 15:05:53 -07006487
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006488 try {
6489 mSearchManager = new SearchManager(this, null);
6490 } catch (ServiceNotFoundException e) {
6491 throw new IllegalStateException(e);
6492 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006493 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006496 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 if (getBaseContext() == null) {
6498 throw new IllegalStateException(
6499 "System services not available to Activities before onCreate()");
6500 }
6501
6502 if (WINDOW_SERVICE.equals(name)) {
6503 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006504 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006505 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006506 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 }
6508 return super.getSystemService(name);
6509 }
6510
6511 /**
6512 * Change the title associated with this activity. If this is a
6513 * top-level activity, the title for its window will change. If it
6514 * is an embedded activity, the parent can do whatever it wants
6515 * with it.
6516 */
6517 public void setTitle(CharSequence title) {
6518 mTitle = title;
6519 onTitleChanged(title, mTitleColor);
6520
6521 if (mParent != null) {
6522 mParent.onChildTitleChanged(this, title);
6523 }
6524 }
6525
6526 /**
6527 * Change the title associated with this activity. If this is a
6528 * top-level activity, the title for its window will change. If it
6529 * is an embedded activity, the parent can do whatever it wants
6530 * with it.
6531 */
6532 public void setTitle(int titleId) {
6533 setTitle(getText(titleId));
6534 }
6535
Alan Viverette2525d9c2013-11-15 14:42:19 -08006536 /**
6537 * Change the color of the title associated with this activity.
6538 * <p>
6539 * This method is deprecated starting in API Level 11 and replaced by action
6540 * bar styles. For information on styling the Action Bar, read the <a
6541 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6542 * guide.
6543 *
6544 * @deprecated Use action bar styles instead.
6545 */
6546 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 public void setTitleColor(int textColor) {
6548 mTitleColor = textColor;
6549 onTitleChanged(mTitle, textColor);
6550 }
6551
6552 public final CharSequence getTitle() {
6553 return mTitle;
6554 }
6555
6556 public final int getTitleColor() {
6557 return mTitleColor;
6558 }
6559
6560 protected void onTitleChanged(CharSequence title, int color) {
6561 if (mTitleReady) {
6562 final Window win = getWindow();
6563 if (win != null) {
6564 win.setTitle(title);
6565 if (color != 0) {
6566 win.setTitleColor(color);
6567 }
6568 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006569 if (mActionBar != null) {
6570 mActionBar.setWindowTitle(title);
6571 }
Adam Powella557fdc2014-08-21 18:05:53 -07006572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573 }
6574
6575 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6576 }
6577
6578 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006579 * Sets information describing the task with this activity for presentation inside the Recents
6580 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6581 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6582 * for each property until a suitable value is found. For each task the taskDescription will be
6583 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006584 *
6585 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006586 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006587 *
Winson Chunga449dc02014-05-16 11:15:04 -07006588 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006589 */
Winson Chunga449dc02014-05-16 11:15:04 -07006590 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006591 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006592 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006593 // Scale the icon down to something reasonable if it is provided
6594 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6595 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6596 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6597 true);
6598 mTaskDescription.setIcon(icon);
6599 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006600 }
6601 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006602 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006603 } catch (RemoteException e) {
6604 }
6605 }
6606
6607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 * Sets the visibility of the progress bar in the title.
6609 * <p>
6610 * In order for the progress bar to be shown, the feature must be requested
6611 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006612 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006614 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006616 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 public final void setProgressBarVisibility(boolean visible) {
6618 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6619 Window.PROGRESS_VISIBILITY_OFF);
6620 }
6621
6622 /**
6623 * Sets the visibility of the indeterminate progress bar in the title.
6624 * <p>
6625 * In order for the progress bar to be shown, the feature must be requested
6626 * via {@link #requestWindowFeature(int)}.
6627 *
6628 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006629 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006631 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6633 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6634 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6635 }
RoboErik55011652014-07-09 15:05:53 -07006636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 /**
6638 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6639 * is always indeterminate).
6640 * <p>
6641 * In order for the progress bar to be shown, the feature must be requested
6642 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006645 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006647 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 public final void setProgressBarIndeterminate(boolean indeterminate) {
6649 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006650 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6651 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
RoboErik55011652014-07-09 15:05:53 -07006653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654 /**
6655 * Sets the progress for the progress bars in the title.
6656 * <p>
6657 * In order for the progress bar to be shown, the feature must be requested
6658 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 * @param progress The progress for the progress bar. Valid ranges are from
6661 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6662 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006663 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006665 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006666 public final void setProgress(int progress) {
6667 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6668 }
RoboErik55011652014-07-09 15:05:53 -07006669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 /**
6671 * Sets the secondary progress for the progress bar in the title. This
6672 * progress is drawn between the primary progress (set via
6673 * {@link #setProgress(int)} and the background. It can be ideal for media
6674 * scenarios such as showing the buffering progress while the default
6675 * progress shows the play progress.
6676 * <p>
6677 * In order for the progress bar to be shown, the feature must be requested
6678 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006679 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6681 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006682 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006684 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 public final void setSecondaryProgress(int secondaryProgress) {
6686 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6687 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6688 }
6689
6690 /**
6691 * Suggests an audio stream whose volume should be changed by the hardware
6692 * volume controls.
6693 * <p>
6694 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006695 * Volume requests which are received while the Activity is in the
6696 * foreground will affect this stream.
6697 * <p>
6698 * It is not guaranteed that the hardware volume controls will always change
6699 * this stream's volume (for example, if a call is in progress, its stream's
6700 * volume may be changed instead). To reset back to the default, use
6701 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006704 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 */
6706 public final void setVolumeControlStream(int streamType) {
6707 getWindow().setVolumeControlStream(streamType);
6708 }
6709
6710 /**
6711 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006712 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006713 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006714 * @return The suggested audio stream type whose volume should be changed by
6715 * the hardware volume controls.
6716 * @see #setVolumeControlStream(int)
6717 */
6718 public final int getVolumeControlStream() {
6719 return getWindow().getVolumeControlStream();
6720 }
RoboErik55011652014-07-09 15:05:53 -07006721
6722 /**
6723 * Sets a {@link MediaController} to send media keys and volume changes to.
6724 * <p>
6725 * The controller will be tied to the window of this Activity. Media key and
6726 * volume events which are received while the Activity is in the foreground
6727 * will be forwarded to the controller and used to invoke transport controls
6728 * or adjust the volume. This may be used instead of or in addition to
6729 * {@link #setVolumeControlStream} to affect a specific session instead of a
6730 * specific stream.
6731 * <p>
6732 * It is not guaranteed that the hardware volume controls will always change
6733 * this session's volume (for example, if a call is in progress, its
6734 * stream's volume may be changed instead). To reset back to the default use
6735 * null as the controller.
6736 *
6737 * @param controller The controller for the session which should receive
6738 * media keys and volume changes.
6739 */
6740 public final void setMediaController(MediaController controller) {
6741 getWindow().setMediaController(controller);
6742 }
6743
6744 /**
6745 * Gets the controller which should be receiving media key and volume events
6746 * while this activity is in the foreground.
6747 *
6748 * @return The controller which should receive events.
6749 * @see #setMediaController(android.media.session.MediaController)
6750 */
6751 public final MediaController getMediaController() {
6752 return getWindow().getMediaController();
6753 }
6754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 /**
6756 * Runs the specified action on the UI thread. If the current thread is the UI
6757 * thread, then the action is executed immediately. If the current thread is
6758 * not the UI thread, the action is posted to the event queue of the UI thread.
6759 *
6760 * @param action the action to run on the UI thread
6761 */
6762 public final void runOnUiThread(Runnable action) {
6763 if (Thread.currentThread() != mUiThread) {
6764 mHandler.post(action);
6765 } else {
6766 action.run();
6767 }
6768 }
6769
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006770 /** @hide */
6771 @Override
6772 public final void autofillClientRunOnUiThread(Runnable action) {
6773 runOnUiThread(action);
6774 }
6775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006777 * Standard implementation of
6778 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6779 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006780 * This implementation does nothing and is for
6781 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6782 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6783 *
6784 * @see android.view.LayoutInflater#createView
6785 * @see android.view.Window#getLayoutInflater
6786 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006787 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07006788 public View onCreateView(@NonNull String name, @NonNull Context context,
6789 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006790 return null;
6791 }
6792
6793 /**
6794 * Standard implementation of
6795 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6796 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006797 * This implementation handles <fragment> tags to embed fragments inside
6798 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 *
6800 * @see android.view.LayoutInflater#createView
6801 * @see android.view.Window#getLayoutInflater
6802 */
Ian Lake0c807f42018-09-12 14:03:27 -07006803 @Nullable
6804 public View onCreateView(@Nullable View parent, @NonNull String name,
6805 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006806 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006807 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006808 }
RoboErik55011652014-07-09 15:05:53 -07006809
Adam Powell371a8092014-06-20 12:51:12 -07006810 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 }
6812
Daniel Sandler69a48172010-06-23 16:29:36 -04006813 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006814 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006815 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006816 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006817 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006818 * @param fd The raw file descriptor that the dump is being sent to.
6819 * @param writer The PrintWriter to which you should dump your state. This will be
6820 * closed for you after you return.
6821 * @param args additional arguments to the dump request.
6822 */
Ian Lake140fe122018-09-13 14:59:38 -07006823 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
6824 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006825 dumpInner(prefix, fd, writer, args);
6826 }
6827
Ian Lake140fe122018-09-13 14:59:38 -07006828 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
6829 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08006830 if (args != null && args.length > 0) {
6831 // Handle special cases
6832 switch (args[0]) {
6833 case "--autofill":
6834 dumpAutofillManager(prefix, writer);
6835 return;
Felipe Lemeb718e032018-12-10 09:44:23 -08006836 case "--contentcapture":
6837 dumpContentCaptureManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006838 return;
6839 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07006840 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006841 writer.print(prefix); writer.print("Local Activity ");
6842 writer.print(Integer.toHexString(System.identityHashCode(this)));
6843 writer.println(" State:");
6844 String innerPrefix = prefix + " ";
6845 writer.print(innerPrefix); writer.print("mResumed=");
6846 writer.print(mResumed); writer.print(" mStopped=");
6847 writer.print(mStopped); writer.print(" mFinished=");
6848 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006849 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6850 writer.println(mChangingConfigurations);
6851 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6852 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006853
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006854 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6855 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006856 if (mVoiceInteractor != null) {
6857 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6858 }
Jeff Brown5182c782013-10-15 20:31:52 -07006859
Michael Wright5f48dc72013-11-01 12:42:49 -07006860 if (getWindow() != null &&
6861 getWindow().peekDecorView() != null &&
6862 getWindow().peekDecorView().getViewRootImpl() != null) {
6863 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6864 }
Jeff Brown5182c782013-10-15 20:31:52 -07006865
6866 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07006867
Felipe Lemeb546ca72018-08-15 08:44:12 -07006868 dumpAutofillManager(prefix, writer);
Felipe Lemeb718e032018-12-10 09:44:23 -08006869 dumpContentCaptureManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07006870
6871 ResourcesManager.getInstance().dump(prefix, writer);
6872 }
6873
6874 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08006875 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006876 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07006877 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08006878 writer.print(prefix); writer.print("Autofill Compat Mode: ");
6879 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07006880 } else {
6881 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07006882 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07006883 }
6884
Felipe Lemeb718e032018-12-10 09:44:23 -08006885 void dumpContentCaptureManager(String prefix, PrintWriter writer) {
6886 final ContentCaptureManager cm = getContentCaptureManager();
6887 if (cm != null) {
6888 cm.dump(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006889 } else {
Felipe Lemeb718e032018-12-10 09:44:23 -08006890 writer.print(prefix); writer.println("No ContentCaptureManager");
Felipe Lemee348dc32018-11-05 12:35:29 -08006891 }
6892 }
6893
Dianne Hackborn625ac272010-09-17 18:29:22 -07006894 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006895 * Bit indicating that this activity is "immersive" and should not be
6896 * interrupted by notifications if possible.
6897 *
6898 * This value is initially set by the manifest property
6899 * <code>android:immersive</code> but may be changed at runtime by
6900 * {@link #setImmersive}.
6901 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006902 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006903 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6904 */
6905 public boolean isImmersive() {
6906 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006907 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006908 } catch (RemoteException e) {
6909 return false;
6910 }
6911 }
6912
6913 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006914 * Indication of whether this is the highest level activity in this task. Can be used to
6915 * determine whether an activity launched by this activity was placed in the same task or
6916 * another task.
6917 *
6918 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006919 */
Sunny Goyald85bed52018-09-25 12:01:01 -07006920 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006921 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006922 return false;
6923 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006924 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006925 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006926 } catch (RemoteException e) {
6927 return false;
6928 }
6929 }
6930
6931 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006932 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6933 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006934 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006935 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006936 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6937 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006938 * This call has no effect on non-translucent activities or on activities with the
6939 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006940 *
George Mount62ab9b72014-05-02 13:51:17 -07006941 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6942 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006943 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006944 *
6945 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006946 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006947 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006948 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006949 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006950 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006951 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006952 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6953 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006954 } catch (RemoteException e) {
6955 // pass
6956 }
6957 }
6958
6959 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006960 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6961 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6962 * <p>
6963 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6964 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6965 * be called indicating that it is safe to make this activity translucent again. Until
6966 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6967 * behind the frontmost Activity will be indeterminate.
6968 * <p>
6969 * This call has no effect on non-translucent activities or on activities with the
6970 * {@link android.R.attr#windowIsFloating} attribute.
6971 *
6972 * @param callback the method to call when all visible Activities behind this one have been
6973 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006974 * @param options activity options delivered to the activity below this one. The options
6975 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006976 * @return <code>true</code> if Window was opaque and will become translucent or
6977 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006978 *
6979 * @see #convertFromTranslucent()
6980 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006981 *
6982 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006983 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006984 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006985 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006986 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006987 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006988 try {
6989 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006990 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006991 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006992 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006993 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006994 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006995 // Make callback return as though it timed out.
6996 mChangeCanvasToTranslucent = false;
6997 drawComplete = false;
6998 }
6999 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
7000 // Window is already translucent.
7001 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07007002 }
George Mount3cc716c2014-06-12 16:35:35 -07007003 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007004 }
7005
7006 /** @hide */
7007 void onTranslucentConversionComplete(boolean drawComplete) {
7008 if (mTranslucentCallback != null) {
7009 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
7010 mTranslucentCallback = null;
7011 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007012 if (mChangeCanvasToTranslucent) {
7013 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
7014 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007015 }
7016
Craig Mautnereb8abf72014-07-02 15:04:09 -07007017 /** @hide */
7018 public void onNewActivityOptions(ActivityOptions options) {
7019 mActivityTransitionState.setEnterActivityOptions(this, options);
7020 if (!mStopped) {
7021 mActivityTransitionState.enterReady(this);
7022 }
7023 }
7024
Craig Mautner5eda9b32013-07-02 11:58:16 -07007025 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07007026 * Retrieve the ActivityOptions passed in from the launching activity or passed back
7027 * from an activity launched by this activity in its call to {@link
7028 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
7029 *
7030 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
7031 * @hide
7032 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007033 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07007034 ActivityOptions getActivityOptions() {
7035 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007036 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007037 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07007038 } catch (RemoteException e) {
7039 }
7040 return null;
7041 }
7042
7043 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007044 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07007045 * this method anytime between the start of {@link #onResume()} and the return from
7046 * {@link #onPause()}. If this call is successful then the activity will remain visible after
7047 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
7048 *
7049 * <p>The actions of this call are reset each time that this activity is brought to the
7050 * front. That is, every time {@link #onResume()} is called the activity will be assumed
7051 * to not have requested visible behind. Therefore, if you want this activity to continue to
7052 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007053 *
7054 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
7055 * for dialog and translucent activities.
7056 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007057 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
7058 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
7059 *
7060 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07007061 * the next call to onResume.
7062 *
Bryce Leed59629e2017-04-18 14:35:29 -07007063 * @deprecated This method's functionality is no longer supported as of
7064 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7065 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007066 * @param visible true to notify the system that the activity wishes to be visible behind other
7067 * translucent activities, false to indicate otherwise. Resources must be
7068 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07007069 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007070 * @return the resulting visibiity state. If true the activity will remain visible beyond
7071 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7072 * then the activity may not count on being visible behind other translucent activities,
7073 * and must stop any media playback and release resources.
7074 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7075 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007076 *
Jose Limafcf70832014-08-27 23:09:05 -07007077 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007078 */
Bryce Leed59629e2017-04-18 14:35:29 -07007079 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007080 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007081 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007082 }
7083
7084 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007085 * Called when a translucent activity over this activity is becoming opaque or another
7086 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007087 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007088 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007089 * <p>When this method is called the activity has 500 msec to release any resources it may be
7090 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007091 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007092 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007093 * process. Otherwise {@link #onStop()} will be called following return.
7094 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007095 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007096 *
7097 * @deprecated This method's functionality is no longer supported as of
7098 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007099 */
Bryce Leed59629e2017-04-18 14:35:29 -07007100 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007101 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007102 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007103 mCalled = true;
7104 }
7105
7106 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007107 * Translucent activities may call this to determine if there is an activity below them that
7108 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007109 *
Bryce Leed59629e2017-04-18 14:35:29 -07007110 * @deprecated This method's functionality is no longer supported as of
7111 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7112 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007113 * @return true if an activity below is set to visible according to the most recent call to
7114 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007115 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007116 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007117 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007118 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007119 * @hide
7120 */
Bryce Leed59629e2017-04-18 14:35:29 -07007121 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007122 @SystemApi
7123 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007124 return false;
7125 }
7126
7127 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007128 * The topmost foreground activity will receive this call when the background visibility state
7129 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007130 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007131 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007132 * due to a background activity finishing itself.
7133 *
Bryce Leed59629e2017-04-18 14:35:29 -07007134 * @deprecated This method's functionality is no longer supported as of
7135 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7136 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007137 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007138 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007139 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007140 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007141 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007142 */
Bryce Leed59629e2017-04-18 14:35:29 -07007143 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007144 @SystemApi
7145 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007146 }
7147
7148 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007149 * Activities cannot draw during the period that their windows are animating in. In order
7150 * to know when it is safe to begin drawing they can override this method which will be
7151 * called when the entering animation has completed.
7152 */
7153 public void onEnterAnimationComplete() {
7154 }
7155
7156 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007157 * @hide
7158 */
7159 public void dispatchEnterAnimationComplete() {
chaviwfeb2e1e2018-12-19 17:24:11 -08007160 mEnterAnimationComplete = true;
7161 mInstrumentation.onEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007162 onEnterAnimationComplete();
7163 if (getWindow() != null && getWindow().getDecorView() != null) {
Felipe Leme01297692019-01-29 18:16:23 -08007164 View decorView = getWindow().getDecorView();
7165 decorView.getViewTreeObserver().dispatchOnEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007166 }
7167 }
7168
7169 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007170 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007171 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007172 * Note that changing this value will have no effect on the activity's
7173 * {@link android.content.pm.ActivityInfo} structure; that is, if
7174 * <code>android:immersive</code> is set to <code>true</code>
7175 * in the application's manifest entry for this activity, the {@link
7176 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7177 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7178 * FLAG_IMMERSIVE} bit set.
7179 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007180 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007181 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7182 */
7183 public void setImmersive(boolean i) {
7184 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007185 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007186 } catch (RemoteException e) {
7187 // pass
7188 }
7189 }
7190
Adam Powell6e346362010-07-23 10:18:23 -07007191 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007192 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007193 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007194 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7195 * while this Activity has user focus.</p>
7196 *
7197 * <p>It is recommended that applications additionally declare
7198 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7199 * transitions when switching between VR activities.</p>
7200 *
7201 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7202 * VR mode will not be started. Developers can handle this case as follows:</p>
7203 *
7204 * <pre>
7205 * String servicePackage = "com.whatever.app";
7206 * String serviceClass = "com.whatever.app.MyVrListenerService";
7207 *
7208 * // Name of the component of the VrListenerService to start.
7209 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7210 *
7211 * try {
7212 * setVrModeEnabled(true, myComponentName);
7213 * } catch (PackageManager.NameNotFoundException e) {
7214 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7215 * boolean isInstalled = false;
7216 * for (ApplicationInfo app : installed) {
7217 * if (app.packageName.equals(servicePackage)) {
7218 * isInstalled = true;
7219 * break;
7220 * }
7221 * }
7222 * if (isInstalled) {
7223 * // Package is installed, but not enabled in Settings. Let user enable it.
7224 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7225 * } else {
7226 * // Package is not installed. Send an intent to download this.
7227 * sentIntentToLaunchAppStore(servicePackage);
7228 * }
7229 * }
7230 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007231 *
7232 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007233 * @param requestedComponent the name of the component to use as a
7234 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7235 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007236 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7237 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7238 * not been enabled in user settings.
7239 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007240 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7241 * @see android.service.vr.VrListenerService
7242 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7243 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007244 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007245 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7246 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007247 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007248 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007249 != 0) {
7250 throw new PackageManager.NameNotFoundException(
7251 requestedComponent.flattenToString());
7252 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007253 } catch (RemoteException e) {
7254 // pass
7255 }
7256 }
7257
7258 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007259 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007260 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007261 * @param callback Callback that will manage lifecycle events for this action mode
7262 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007263 *
7264 * @see ActionMode
7265 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007266 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007267 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007268 return mWindow.getDecorView().startActionMode(callback);
7269 }
7270
Adam Powelldebf3be2010-11-15 18:58:48 -08007271 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007272 * Start an action mode of the given type.
7273 *
7274 * @param callback Callback that will manage lifecycle events for this action mode
7275 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7276 * @return The ActionMode that was started, or null if it was canceled
7277 *
7278 * @see ActionMode
7279 */
7280 @Nullable
7281 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7282 return mWindow.getDecorView().startActionMode(callback, type);
7283 }
7284
7285 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007286 * Give the Activity a chance to control the UI for an action mode requested
7287 * by the system.
7288 *
7289 * <p>Note: If you are looking for a notification callback that an action mode
7290 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7291 *
7292 * @param callback The callback that should control the new action mode
7293 * @return The new action mode, or <code>null</code> if the activity does not want to
7294 * provide special handling for this action mode. (It will be handled by the system.)
7295 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007296 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007297 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007298 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007299 // Only Primary ActionModes are represented in the ActionBar.
7300 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7301 initWindowDecorActionBar();
7302 if (mActionBar != null) {
7303 return mActionBar.startActionMode(callback);
7304 }
Adam Powell6e346362010-07-23 10:18:23 -07007305 }
7306 return null;
7307 }
7308
Adam Powelldebf3be2010-11-15 18:58:48 -08007309 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007310 * {@inheritDoc}
7311 */
7312 @Nullable
7313 @Override
7314 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7315 try {
7316 mActionModeTypeStarting = type;
7317 return onWindowStartingActionMode(callback);
7318 } finally {
7319 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7320 }
7321 }
7322
7323 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007324 * Notifies the Activity that an action mode has been started.
7325 * Activity subclasses overriding this method should call the superclass implementation.
7326 *
7327 * @param mode The new action mode.
7328 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007329 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007330 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007331 public void onActionModeStarted(ActionMode mode) {
7332 }
7333
7334 /**
7335 * Notifies the activity that an action mode has finished.
7336 * Activity subclasses overriding this method should call the superclass implementation.
7337 *
7338 * @param mode The action mode that just finished.
7339 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007340 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007341 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007342 public void onActionModeFinished(ActionMode mode) {
7343 }
7344
Adam Powelldd8fab22012-03-22 17:47:27 -07007345 /**
7346 * Returns true if the app should recreate the task when navigating 'up' from this activity
7347 * by using targetIntent.
7348 *
7349 * <p>If this method returns false the app can trivially call
7350 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7351 * up navigation. If this method returns false, the app should synthesize a new task stack
7352 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7353 *
7354 * @param targetIntent An intent representing the target destination for up navigation
7355 * @return true if navigating up should recreate a new task stack, false if the same task
7356 * should be used for the destination
7357 */
7358 public boolean shouldUpRecreateTask(Intent targetIntent) {
7359 try {
7360 PackageManager pm = getPackageManager();
7361 ComponentName cn = targetIntent.getComponent();
7362 if (cn == null) {
7363 cn = targetIntent.resolveActivity(pm);
7364 }
7365 ActivityInfo info = pm.getActivityInfo(cn, 0);
7366 if (info.taskAffinity == null) {
7367 return false;
7368 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007369 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007370 } catch (RemoteException e) {
7371 return false;
7372 } catch (NameNotFoundException e) {
7373 return false;
7374 }
7375 }
7376
7377 /**
7378 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7379 * in the process. If the activity indicated by upIntent already exists in the task's history,
7380 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007381 * finished.
7382 *
7383 * <p>If the indicated activity does not appear in the history stack, this will finish
7384 * each activity in this task until the root activity of the task is reached, resulting in
7385 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7386 * when an activity may be reached by a path not passing through a canonical parent
7387 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007388 *
7389 * <p>This method should be used when performing up navigation from within the same task
7390 * as the destination. If up navigation should cross tasks in some cases, see
7391 * {@link #shouldUpRecreateTask(Intent)}.</p>
7392 *
7393 * @param upIntent An intent representing the target destination for up navigation
7394 *
7395 * @return true if up navigation successfully reached the activity indicated by upIntent and
7396 * upIntent was delivered to it. false if an instance of the indicated activity could
7397 * not be found and this activity was simply finished normally.
7398 */
7399 public boolean navigateUpTo(Intent upIntent) {
7400 if (mParent == null) {
7401 ComponentName destInfo = upIntent.getComponent();
7402 if (destInfo == null) {
7403 destInfo = upIntent.resolveActivity(getPackageManager());
7404 if (destInfo == null) {
7405 return false;
7406 }
7407 upIntent = new Intent(upIntent);
7408 upIntent.setComponent(destInfo);
7409 }
7410 int resultCode;
7411 Intent resultData;
7412 synchronized (this) {
7413 resultCode = mResultCode;
7414 resultData = mResultData;
7415 }
7416 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007417 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007418 }
7419 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007420 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007421 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007422 resultCode, resultData);
7423 } catch (RemoteException e) {
7424 return false;
7425 }
7426 } else {
7427 return mParent.navigateUpToFromChild(this, upIntent);
7428 }
7429 }
7430
7431 /**
7432 * This is called when a child activity of this one calls its
7433 * {@link #navigateUpTo} method. The default implementation simply calls
7434 * navigateUpTo(upIntent) on this activity (the parent).
7435 *
7436 * @param child The activity making the call.
7437 * @param upIntent An intent representing the target destination for up navigation
7438 *
7439 * @return true if up navigation successfully reached the activity indicated by upIntent and
7440 * upIntent was delivered to it. false if an instance of the indicated activity could
7441 * not be found and this activity was simply finished normally.
7442 */
7443 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7444 return navigateUpTo(upIntent);
7445 }
7446
7447 /**
7448 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7449 * this activity's logical parent. The logical parent is named in the application's manifest
7450 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007451 * Activity subclasses may override this method to modify the Intent returned by
7452 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7453 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007454 *
Adam Powell04d58112012-04-09 10:22:12 -07007455 * @return a new Intent targeting the defined parent of this activity or null if
7456 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007457 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007458 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007459 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007460 final String parentName = mActivityInfo.parentActivityName;
7461 if (TextUtils.isEmpty(parentName)) {
7462 return null;
7463 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007464
7465 // If the parent itself has no parent, generate a main activity intent.
7466 final ComponentName target = new ComponentName(this, parentName);
7467 try {
7468 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7469 final String parentActivity = parentInfo.parentActivityName;
7470 final Intent parentIntent = parentActivity == null
7471 ? Intent.makeMainActivity(target)
7472 : new Intent().setComponent(target);
7473 return parentIntent;
7474 } catch (NameNotFoundException e) {
7475 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7476 "' in manifest");
7477 return null;
7478 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007479 }
7480
George Mount31a21722014-03-24 17:44:36 -07007481 /**
George Mount62ab9b72014-05-02 13:51:17 -07007482 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007483 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007484 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007485 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007486 *
George Mount65580562014-08-29 08:15:48 -07007487 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007488 */
George Mount65580562014-08-29 08:15:48 -07007489 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7490 if (callback == null) {
7491 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007492 }
George Mount65580562014-08-29 08:15:48 -07007493 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007494 }
7495
7496 /**
7497 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007498 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007499 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7500 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007501 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007502 *
George Mount65580562014-08-29 08:15:48 -07007503 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007504 */
George Mount65580562014-08-29 08:15:48 -07007505 public void setExitSharedElementCallback(SharedElementCallback callback) {
7506 if (callback == null) {
7507 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007508 }
George Mount65580562014-08-29 08:15:48 -07007509 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007510 }
7511
George Mount8c2614c2014-06-10 11:12:01 -07007512 /**
7513 * Postpone the entering activity transition when Activity was started with
7514 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7515 * android.util.Pair[])}.
7516 * <p>This method gives the Activity the ability to delay starting the entering and
7517 * shared element transitions until all data is loaded. Until then, the Activity won't
7518 * draw into its window, leaving the window transparent. This may also cause the
7519 * returning animation to be delayed until data is ready. This method should be
7520 * called in {@link #onCreate(android.os.Bundle)} or in
7521 * {@link #onActivityReenter(int, android.content.Intent)}.
7522 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7523 * start the transitions. If the Activity did not use
7524 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7525 * android.util.Pair[])}, then this method does nothing.</p>
7526 */
7527 public void postponeEnterTransition() {
7528 mActivityTransitionState.postponeEnterTransition();
7529 }
7530
7531 /**
7532 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7533 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7534 * to have your Activity start drawing.
7535 */
7536 public void startPostponedEnterTransition() {
7537 mActivityTransitionState.startPostponedEnterTransition();
7538 }
7539
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007540 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007541 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7542 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007543 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007544 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7545 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007546 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007547 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7548 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7549 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7550 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007551 }
7552 return null;
7553 }
7554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007556
Mathew Inwood8c854f82018-09-14 12:35:36 +01007557 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 final void setParent(Activity parent) {
7559 mParent = parent;
7560 }
7561
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007562 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007563 final void attach(Context context, ActivityThread aThread,
7564 Instrumentation instr, IBinder token, int ident,
7565 Application application, Intent intent, ActivityInfo info,
7566 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007567 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007568 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07007569 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 attachBaseContext(context);
7571
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007572 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007573
Andrii Kulian51c1b672017-04-07 18:39:32 -07007574 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007575 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007576 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007577 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007578 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7580 mWindow.setSoftInputMode(info.softInputMode);
7581 }
Adam Powell269248d2011-08-02 10:26:54 -07007582 if (info.uiOptions != 0) {
7583 mWindow.setUiOptions(info.uiOptions);
7584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 mMainThread = aThread;
7588 mInstrumentation = instr;
7589 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007590 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007591 mApplication = application;
7592 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007593 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 mComponent = intent.getComponent();
7595 mActivityInfo = info;
7596 mTitle = title;
7597 mParent = parent;
7598 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007599 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007600 if (voiceInteractor != null) {
7601 if (lastNonConfigurationInstances != null) {
7602 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7603 } else {
7604 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7605 Looper.myLooper());
7606 }
7607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007608
Jeff Brown98365d72012-08-19 20:30:52 -07007609 mWindow.setWindowManager(
7610 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7611 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007612 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007613 if (mParent != null) {
7614 mWindow.setContainer(mParent.getWindow());
7615 }
7616 mWindowManager = mWindow.getWindowManager();
7617 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007618
7619 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007620
Felipe Lemea4f39cd2019-02-19 15:08:59 -08007621 setAutofillOptions(application.getAutofillOptions());
Felipe Leme326f15a2019-02-19 09:42:24 -08007622 setContentCaptureOptions(application.getContentCaptureOptions());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007623 }
7624
7625 private void enableAutofillCompatibilityIfNeeded() {
7626 if (isAutofillCompatibilityEnabled()) {
7627 final AutofillManager afm = getSystemService(AutofillManager.class);
7628 if (afm != null) {
7629 afm.enableCompatibilityMode();
7630 }
7631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 }
7633
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007634 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007635 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007636 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 return mParent != null ? mParent.getActivityToken() : mToken;
7638 }
7639
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007640 /** @hide */
7641 @VisibleForTesting
7642 public final ActivityThread getActivityThread() {
7643 return mMainThread;
7644 }
7645
Winson Chung298f95b2017-08-10 15:57:18 -07007646 final void performCreate(Bundle icicle) {
7647 performCreate(icicle, null);
7648 }
7649
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007650 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007651 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007652 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007653 mCanEnterPictureInPicture = true;
7654 restoreHasCurrentPermissionRequest(icicle);
7655 if (persistentState != null) {
7656 onCreate(icicle, persistentState);
7657 } else {
7658 onCreate(icicle);
7659 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007660 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007661 mActivityTransitionState.readState(icicle);
7662
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007663 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7664 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007665 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007666 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007667 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007668 }
George Mount0a778ed2013-12-13 13:35:36 -08007669
Ian Lake0b71b8e2018-09-13 13:56:26 -07007670 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07007671 mCanEnterPictureInPicture = true;
7672 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007673 }
7674
Andrii Kuliand25680c2018-02-21 15:16:58 -08007675 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007676 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07007677 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007678 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007680 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007682 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 if (!mCalled) {
7685 throw new SuperNotCalledException(
7686 "Activity " + mComponent.toShortString() +
7687 " did not call through to super.onStart()");
7688 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007689 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007690 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007691
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007692 boolean isAppDebuggable =
7693 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
7694
David Brazdil795cbfb2018-01-26 12:23:15 +00007695 // This property is set for all non-user builds except final release
David Brazdil927d6de2018-01-24 19:54:30 +00007696 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
7697
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007698 if (isAppDebuggable || isDlwarningEnabled) {
7699 String dlwarning = getDlWarning();
7700 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007701 String appName = getApplicationInfo().loadLabel(getPackageManager())
7702 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007703 String warning = "Detected problems with app native libraries\n" +
7704 "(please consult log for detail):\n" + dlwarning;
7705 if (isAppDebuggable) {
7706 new AlertDialog.Builder(this).
7707 setTitle(appName).
7708 setMessage(warning).
7709 setPositiveButton(android.R.string.ok, null).
7710 setCancelable(false).
7711 show();
7712 } else {
7713 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7714 }
7715 }
7716 }
7717
Tim Van Pattenddc43912018-12-18 17:47:52 -07007718 GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
7719
George Mount62ab9b72014-05-02 13:51:17 -07007720 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07007721 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 }
RoboErik55011652014-07-09 15:05:53 -07007723
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007724 /**
7725 * Restart the activity.
7726 * @param start Indicates whether the activity should also be started after restart.
7727 * The option to not start immediately is needed in case a transaction with
7728 * multiple lifecycle transitions is in progress.
7729 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007730 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007731 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007732 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007733
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007734 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007735 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007736 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7737 }
7738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 if (mStopped) {
7740 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007741
7742 synchronized (mManagedCursors) {
7743 final int N = mManagedCursors.size();
7744 for (int i=0; i<N; i++) {
7745 ManagedCursor mc = mManagedCursors.get(i);
7746 if (mc.mReleased || mc.mUpdated) {
7747 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007748 if (getApplicationInfo().targetSdkVersion
7749 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7750 throw new IllegalStateException(
7751 "trying to requery an already closed cursor "
7752 + mc.mCursor);
7753 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007754 }
7755 mc.mReleased = false;
7756 mc.mUpdated = false;
7757 }
7758 }
7759 }
7760
7761 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007763 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007764 if (!mCalled) {
7765 throw new SuperNotCalledException(
7766 "Activity " + mComponent.toShortString() +
7767 " did not call through to super.onRestart()");
7768 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007769 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007770 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 }
7773 }
RoboErik55011652014-07-09 15:05:53 -07007774
Andrii Kuliand25680c2018-02-21 15:16:58 -08007775 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007776 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007777 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007778
Dianne Hackborn445646c2010-06-25 15:52:59 -07007779 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007780
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007781 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007782
Dake Gu67decfa2017-12-27 11:48:08 -08007783 if (mAutoFillResetNeeded) {
7784 // When Activity is destroyed in paused state, and relaunch activity, there will be
7785 // extra onResume and onPause event, ignore the first onResume and onPause.
7786 // see ActivityThread.handleRelaunchActivity()
7787 mAutoFillIgnoreFirstResumePause = followedByPause;
7788 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7789 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7790 }
7791 }
7792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007794 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007796 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 if (!mCalled) {
7798 throw new SuperNotCalledException(
7799 "Activity " + mComponent.toShortString() +
7800 " did not call through to super.onResume()");
7801 }
7802
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007803 // invisible activities must be finished before onResume() completes
7804 if (!mVisibleFromClient && !mFinished) {
7805 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7806 if (getApplicationInfo().targetSdkVersion
7807 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7808 throw new IllegalStateException(
7809 "Activity " + mComponent.toShortString() +
7810 " did not call finish() prior to onResume() completing");
7811 }
7812 }
7813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007814 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007816
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007817 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007818 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 onPostResume();
7821 if (!mCalled) {
7822 throw new SuperNotCalledException(
7823 "Activity " + mComponent.toShortString() +
7824 " did not call through to super.onPostResume()");
7825 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007826 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 }
7828
7829 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007830 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007831 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007832 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007833 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007835 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007836 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007837 if (!mCalled && getApplicationInfo().targetSdkVersion
7838 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7839 throw new SuperNotCalledException(
7840 "Activity " + mComponent.toShortString() +
7841 " did not call through to super.onPause()");
7842 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007843 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 }
RoboErik55011652014-07-09 15:05:53 -07007845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 final void performUserLeaving() {
7847 onUserInteraction();
7848 onUserLeaveHint();
7849 }
RoboErik55011652014-07-09 15:05:53 -07007850
Andrii Kuliand25680c2018-02-21 15:16:58 -08007851 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007852 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007853 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007854
Winson Chung298f95b2017-08-10 15:57:18 -07007855 // Disallow entering picture-in-picture after the activity has been stopped
7856 mCanEnterPictureInPicture = false;
7857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007859 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 if (mWindow != null) {
7861 mWindow.closeAllPanels();
7862 }
7863
Chong Zhang7687f252016-02-26 12:03:33 -08007864 // If we're preserving the window, don't setStoppedState to true, since we
7865 // need the window started immediately again. Stopping the window will
7866 // destroys hardware resources and causes flicker.
7867 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07007868 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08007869 }
RoboErik55011652014-07-09 15:05:53 -07007870
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007871 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 mCalled = false;
7874 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007875 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 if (!mCalled) {
7877 throw new SuperNotCalledException(
7878 "Activity " + mComponent.toShortString() +
7879 " did not call through to super.onStop()");
7880 }
RoboErik55011652014-07-09 15:05:53 -07007881
Makoto Onuki2f6a0182010-02-22 13:26:59 -08007882 synchronized (mManagedCursors) {
7883 final int N = mManagedCursors.size();
7884 for (int i=0; i<N; i++) {
7885 ManagedCursor mc = mManagedCursors.get(i);
7886 if (!mc.mReleased) {
7887 mc.mCursor.deactivate();
7888 mc.mReleased = true;
7889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 }
7891 }
George Mount0a778ed2013-12-13 13:35:36 -08007892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07007894 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 }
7896 mResumed = false;
7897 }
7898
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007899 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007900 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07007901 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07007902 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007903 mFragments.dispatchDestroy();
7904 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007905 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007906 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07007907 if (mVoiceInteractor != null) {
7908 mVoiceInteractor.detachActivity();
7909 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007910 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007911 }
George Mount0a778ed2013-12-13 13:35:36 -08007912
Winson Chung5af42fc2017-03-24 17:11:33 -07007913 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
7914 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007915 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007916 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
7917 + " " + newConfig);
7918 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007919 if (mWindow != null) {
7920 mWindow.onMultiWindowModeChanged();
7921 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007922 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007923 }
7924
Winson Chung5af42fc2017-03-24 17:11:33 -07007925 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
7926 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007927 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007928 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
7929 + " " + newConfig);
7930 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07007931 if (mWindow != null) {
7932 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
7933 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007934 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007935 }
7936
George Mount0a778ed2013-12-13 13:35:36 -08007937 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06007938 * @hide
7939 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007940 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06007941 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007942 return mResumed;
7943 }
7944
Svetoslavffb32b12015-10-15 16:54:00 -07007945 private void storeHasCurrentPermissionRequest(Bundle bundle) {
7946 if (bundle != null && mHasCurrentPermissionsRequest) {
7947 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
7948 }
7949 }
7950
7951 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
7952 if (bundle != null) {
7953 mHasCurrentPermissionsRequest = bundle.getBoolean(
7954 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
7955 }
7956 }
7957
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007958 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07007959 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
7960 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07007961 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
7963 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007964 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007966 onActivityResult(requestCode, resultCode, data);
7967 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
7968 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
7969 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08007970 dispatchRequestPermissionsResult(requestCode, data);
7971 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00007972 Fragment frag = mFragments.findFragmentByWho(who);
7973 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007974 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08007975 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07007976 }
Svetoslav970b59c2015-06-09 16:05:21 -07007977 } else if (who.startsWith("@android:view:")) {
7978 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
7979 getActivityToken());
7980 for (ViewRootImpl viewRoot : views) {
7981 if (viewRoot.getView() != null
7982 && viewRoot.getView().dispatchActivityResult(
7983 who, requestCode, resultCode, data)) {
7984 return;
7985 }
7986 }
Svet Ganov782043c2017-02-11 00:52:02 +00007987 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08007988 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08007989 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07007990 } else {
7991 Fragment frag = mFragments.findFragmentByWho(who);
7992 if (frag != null) {
7993 frag.onActivityResult(requestCode, resultCode, data);
7994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07007996 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007998
Jason Monka57e5e02014-05-07 10:06:48 -04007999 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008000 * Request to put this activity in a mode where the user is locked to a restricted set of
8001 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04008002 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008003 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
8004 * for this component, the current task will be launched directly into LockTask mode. Only apps
8005 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
8006 * be launched while LockTask mode is active. The user will not be able to leave this mode
8007 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
8008 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04008009 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008010 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
8011 * system will prompt the user with a dialog requesting permission to use this mode.
8012 * The user can exit at any time through instructions shown on the request dialog. Calling
8013 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04008014 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008015 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
8016 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008017 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008018 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008019 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04008020 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008021 public void startLockTask() {
8022 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008023 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008024 } catch (RemoteException e) {
8025 }
8026 }
8027
Jason Monka57e5e02014-05-07 10:06:48 -04008028 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008029 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04008030 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008031 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
8032 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04008033 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008034 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
8035 * by this activity, then calling this method will not terminate the LockTask mode, but only
8036 * finish its own task. The device will remain in LockTask mode, until the activity which
8037 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
8038 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008039 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008040 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008041 * @see android.R.attr#lockTaskMode
8042 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04008043 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008044 public void stopLockTask() {
8045 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008046 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008047 } catch (RemoteException e) {
8048 }
8049 }
8050
Craig Mautner5eda9b32013-07-02 11:58:16 -07008051 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008052 * Shows the user the system defined message for telling the user how to exit
8053 * lock task mode. The task containing this activity must be in lock task mode at the time
8054 * of this call for the message to be displayed.
8055 */
8056 public void showLockTaskEscapeMessage() {
8057 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008058 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008059 } catch (RemoteException e) {
8060 }
8061 }
8062
8063 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07008064 * Check whether the caption on freeform windows is displayed directly on the content.
8065 *
8066 * @return True if caption is displayed on content, false if it pushes the content down.
8067 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08008068 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07008069 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07008070 */
8071 public boolean isOverlayWithDecorCaptionEnabled() {
8072 return mWindow.isOverlayWithDecorCaptionEnabled();
8073 }
8074
8075 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008076 * Set whether the caption should displayed directly on the content rather than push it down.
8077 *
8078 * This affects only freeform windows since they display the caption and only the main
8079 * window of the activity. The caption is used to drag the window around and also shows
8080 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008081 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008082 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008083 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8084 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008085 }
8086
8087 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008088 * Interface for informing a translucent {@link Activity} once all visible activities below it
8089 * have completed drawing. This is necessary only after an {@link Activity} has been made
8090 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8091 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008092 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8093 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008094 *
8095 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008096 */
Jose Lima14914852014-08-14 09:14:12 -07008097 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008098 public interface TranslucentConversionListener {
8099 /**
8100 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8101 * below the top one have been redrawn. Following this callback it is safe to make the top
8102 * Activity translucent because the underlying Activity has been drawn.
8103 *
8104 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8105 * occurred waiting for the Activity to complete drawing.
8106 *
8107 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008108 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008109 */
8110 public void onTranslucentConversionComplete(boolean drawComplete);
8111 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008112
8113 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008114 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008115 // If the package installer crashed we may have not data - best effort.
8116 String[] permissions = (data != null) ? data.getStringArrayExtra(
8117 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8118 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8119 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008120 onRequestPermissionsResult(requestCode, permissions, grantResults);
8121 }
8122
8123 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008124 Fragment fragment) {
8125 // If the package installer crashed we may have not data - best effort.
8126 String[] permissions = (data != null) ? data.getStringArrayExtra(
8127 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8128 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8129 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8130 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008131 }
8132
Svet Ganov782043c2017-02-11 00:52:02 +00008133 /** @hide */
8134 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008135 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008136 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00008137 try {
8138 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008139 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008140 } catch (IntentSender.SendIntentException e) {
8141 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8142 }
8143 }
8144
Svet Ganov17db9dc2017-02-21 19:54:31 -08008145 /** @hide */
8146 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008147 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008148 mAutoFillResetNeeded = true;
8149 }
8150
Felipe Leme4753bb02017-03-22 20:24:00 -07008151 /** @hide */
8152 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008153 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008154 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008155 final boolean wasShowing;
8156
8157 if (mAutofillPopupWindow == null) {
8158 wasShowing = false;
8159 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8160 } else {
8161 wasShowing = mAutofillPopupWindow.isShowing();
8162 }
Svet Ganov374cae12017-05-10 13:42:33 -07008163 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008164
8165 return !wasShowing && mAutofillPopupWindow.isShowing();
8166 }
8167
8168 /** @hide */
8169 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008170 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8171 @NonNull KeyEvent keyEvent) {
8172 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8173 if (rootImpl != null) {
8174 // dont care if anchorView is current focus, for example a custom view may only receive
8175 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8176 // might be inside parent of the custom view.
8177 rootImpl.dispatchKeyFromAutofill(keyEvent);
8178 }
8179 }
8180
8181 /** @hide */
8182 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008183 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008184 if (mAutofillPopupWindow == null) {
8185 return false;
8186 }
8187 mAutofillPopupWindow.dismiss();
8188 mAutofillPopupWindow = null;
8189 return true;
8190 }
8191
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008192 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008193 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008194 public final boolean autofillClientIsFillUiShowing() {
8195 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8196 }
8197
8198 /** @hide */
8199 @Override
8200 @NonNull
8201 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8202 @NonNull AutofillId[] autofillId) {
8203 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008204 final ArrayList<ViewRootImpl> roots =
8205 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8206
8207 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8208 final View rootView = roots.get(rootNum).getView();
8209
8210 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008211 final int viewCount = autofillId.length;
8212 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008213 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008214 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008215 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008216 }
8217 }
8218 }
8219 }
8220
8221 return views;
8222 }
8223
8224 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008225 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008226 @Nullable
8227 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008228 final ArrayList<ViewRootImpl> roots =
8229 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8230 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8231 final View rootView = roots.get(rootNum).getView();
8232
8233 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008234 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008235 if (view != null) {
8236 return view;
8237 }
8238 }
8239 }
8240
8241 return null;
8242 }
8243
8244 /** @hide */
8245 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008246 public final @NonNull boolean[] autofillClientGetViewVisibility(
8247 @NonNull AutofillId[] autofillIds) {
8248 final int autofillIdCount = autofillIds.length;
8249 final boolean[] visible = new boolean[autofillIdCount];
8250 for (int i = 0; i < autofillIdCount; i++) {
8251 final AutofillId autofillId = autofillIds[i];
8252 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8253 if (view != null) {
Felipe Lemea75333c2019-01-22 15:45:32 -08008254 if (!autofillId.isVirtualInt()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008255 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008256 } else {
Felipe Lemea75333c2019-01-22 15:45:32 -08008257 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildIntId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008258 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008259 }
8260 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008261 if (android.view.autofill.Helper.sVerbose) {
8262 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8263 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008264 return visible;
8265 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008266
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008267 /** @hide */
8268 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8269 int windowId) {
8270 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8271 .getRootViews(getActivityToken());
8272 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8273 final View rootView = roots.get(rootNum).getView();
8274 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8275 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8276 if (view != null) {
8277 return view;
8278 }
8279 }
8280 }
8281 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008282 }
8283
8284 /** @hide */
8285 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008286 public final @Nullable IBinder autofillClientGetActivityToken() {
8287 return getActivityToken();
8288 }
8289
8290 /** @hide */
8291 @Override
8292 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008293 return !mStopped;
8294 }
8295
Dake Gu67decfa2017-12-27 11:48:08 -08008296 /** @hide */
8297 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008298 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008299 return isAutofillCompatibilityEnabled();
8300 }
8301
8302 /** @hide */
8303 @Override
8304 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008305 return mAutoFillIgnoreFirstResumePause || !mResumed;
8306 }
8307
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008308 /**
8309 * If set to true, this indicates to the system that it should never take a
8310 * screenshot of the activity to be used as a representation while it is not in a started state.
8311 * <p>
8312 * Note that the system may use the window background of the theme instead to represent
8313 * the window when it is not running.
8314 * <p>
8315 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8316 * this only affects the behavior when the activity's screenshot would be used as a
8317 * representation when the activity is not in a started state, i.e. in Overview. The system may
8318 * still take screenshots of the activity in other contexts; for example, when the user takes a
8319 * screenshot of the entire screen, or when the active
8320 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8321 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8322 *
8323 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8324 * @hide
8325 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008326 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008327 public void setDisablePreviewScreenshots(boolean disable) {
8328 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008329 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008330 } catch (RemoteException e) {
8331 Log.e(TAG, "Failed to call setDisablePreviewScreenshots", e);
8332 }
8333 }
8334
chaviw59b98852017-06-13 12:05:44 -07008335 /**
koprivaa1a78482018-10-09 10:09:23 -07008336 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008337 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8338 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8339 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8340 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8341 *
8342 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8343 * {@code false} otherwise.
8344 * @see #setTurnScreenOn(boolean)
8345 * @see android.R.attr#turnScreenOn
8346 * @see android.R.attr#showWhenLocked
8347 */
8348 public void setShowWhenLocked(boolean showWhenLocked) {
8349 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008350 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008351 } catch (RemoteException e) {
8352 Log.e(TAG, "Failed to call setShowWhenLocked", e);
8353 }
8354 }
8355
8356 /**
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008357 * Specifies whether this {@link Activity} should be shown on top of the lock screen whenever
8358 * the lockscreen is up and this activity has another activity behind it with the showWhenLock
8359 * attribute set. That is, this activity is only visible on the lock screen if there is another
8360 * activity with the showWhenLock attribute visible at the same time on the lock screen. A use
8361 * case for this is permission dialogs, that should only be visible on the lock screen if their
8362 * requesting activity is also visible. This value can be set as a manifest attribute using
8363 * android.R.attr#inheritShowWhenLocked.
8364 *
8365 * @param inheritShowWhenLocked {@code true} to show the {@link Activity} on top of the lock
8366 * screen when this activity has another activity behind it with
8367 * the showWhenLock attribute set; {@code false} otherwise.
8368 * @see #setShowWhenLocked(boolean)
8369 * See android.R.attr#inheritShowWhenLocked
8370 * @hide
8371 */
8372 @SystemApi
8373 @TestApi
8374 public void setInheritShowWhenLocked(boolean inheritShowWhenLocked) {
8375 try {
8376 ActivityTaskManager.getService().setInheritShowWhenLocked(
8377 mToken, inheritShowWhenLocked);
8378 } catch (RemoteException e) {
8379 Log.e(TAG, "Failed to call setInheritShowWhenLocked", e);
8380 }
8381 }
8382
8383 /**
chaviw59b98852017-06-13 12:05:44 -07008384 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8385 * Normally an activity will be transitioned to the stopped state if it is started while the
8386 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8387 * activity will be visible and resumed due to the screen coming on. The screen will not be
8388 * turned on if the activity won't be visible after the screen is turned on. This flag is
8389 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8390 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8391 * if this flag is set and the activity calls {@link
8392 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8393 * the screen will turn on.
8394 *
8395 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8396 *
8397 * @see #setShowWhenLocked(boolean)
8398 * @see android.R.attr#turnScreenOn
8399 * @see android.R.attr#showWhenLocked
8400 */
8401 public void setTurnScreenOn(boolean turnScreenOn) {
8402 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008403 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008404 } catch (RemoteException e) {
8405 Log.e(TAG, "Failed to call setTurnScreenOn", e);
8406 }
8407 }
8408
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008409 /**
8410 * Registers remote animations per transition type for this activity.
8411 *
8412 * @param definition The remote animation definition that defines which transition whould run
8413 * which remote animation.
8414 * @hide
8415 */
8416 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008417 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008418 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8419 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008420 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008421 } catch (RemoteException e) {
8422 Log.e(TAG, "Failed to call registerRemoteAnimations", e);
8423 }
8424 }
8425
Andrii Kuliand25680c2018-02-21 15:16:58 -08008426 /** Log a lifecycle event for current user id and component class. */
8427 private void writeEventLog(int event, String reason) {
8428 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
8429 reason);
8430 }
8431
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008432 class HostCallbacks extends FragmentHostCallback<Activity> {
8433 public HostCallbacks() {
8434 super(Activity.this /*activity*/);
8435 }
8436
8437 @Override
8438 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8439 Activity.this.dump(prefix, fd, writer, args);
8440 }
8441
8442 @Override
8443 public boolean onShouldSaveFragmentState(Fragment fragment) {
8444 return !isFinishing();
8445 }
8446
8447 @Override
8448 public LayoutInflater onGetLayoutInflater() {
8449 final LayoutInflater result = Activity.this.getLayoutInflater();
8450 if (onUseFragmentManagerInflaterFactory()) {
8451 return result.cloneInContext(Activity.this);
8452 }
8453 return result;
8454 }
8455
8456 @Override
8457 public boolean onUseFragmentManagerInflaterFactory() {
8458 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8459 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8460 }
8461
8462 @Override
8463 public Activity onGetHost() {
8464 return Activity.this;
8465 }
8466
8467 @Override
8468 public void onInvalidateOptionsMenu() {
8469 Activity.this.invalidateOptionsMenu();
8470 }
8471
8472 @Override
8473 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8474 Bundle options) {
8475 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8476 }
8477
8478 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008479 public void onStartActivityAsUserFromFragment(
8480 Fragment fragment, Intent intent, int requestCode, Bundle options,
8481 UserHandle user) {
8482 Activity.this.startActivityAsUserFromFragment(
8483 fragment, intent, requestCode, options, user);
8484 }
8485
8486 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008487 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8488 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8489 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8490 if (mParent == null) {
8491 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8492 flagsMask, flagsValues, options);
8493 } else if (options != null) {
8494 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
8495 fillInIntent, flagsMask, flagsValues, extraFlags, options);
8496 }
8497 }
8498
8499 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008500 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8501 int requestCode) {
8502 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8503 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8504 startActivityForResult(who, intent, requestCode, null);
8505 }
8506
8507 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008508 public boolean onHasWindowAnimations() {
8509 return getWindow() != null;
8510 }
8511
8512 @Override
8513 public int onGetWindowAnimations() {
8514 final Window w = getWindow();
8515 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8516 }
8517
Todd Kennedy434bd652015-05-04 12:29:50 -07008518 @Override
8519 public void onAttachFragment(Fragment fragment) {
8520 Activity.this.onAttachFragment(fragment);
8521 }
8522
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008523 @Nullable
8524 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008525 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008526 return Activity.this.findViewById(id);
8527 }
8528
8529 @Override
8530 public boolean onHasView() {
8531 final Window w = getWindow();
8532 return (w != null && w.peekDecorView() != null);
8533 }
8534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535}