blob: 56bf8fa8ffceaf1eb9d3a7509201d9bb60e208c2 [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;
Adam He6240eab2019-02-25 13:34:45 -0800130import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800132import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000133import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100134
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700135import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800136import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700137import com.android.internal.app.IVoiceInteractor;
138import com.android.internal.app.ToolbarActionBar;
139import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700140import com.android.internal.policy.PhoneWindow;
141
Dianne Hackborn625ac272010-09-17 18:29:22 -0700142import java.io.FileDescriptor;
143import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700144import java.lang.annotation.Retention;
145import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700146import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800147import java.util.Arrays;
Adam Powell6e346362010-07-23 10:18:23 -0700148import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700149import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Bryce Leee83f34cd2017-10-31 19:50:54 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152/**
153 * An activity is a single, focused thing that the user can do. Almost all
154 * activities interact with the user, so the Activity class takes care of
155 * creating a window for you in which you can place your UI with
156 * {@link #setContentView}. While activities are often presented to the user
157 * as full-screen windows, they can also be used in other ways: as floating
158 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
159 * or embedded inside of another activity (using {@link ActivityGroup}).
160 *
161 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * <ul>
164 * <li> {@link #onCreate} is where you initialize your activity. Most
165 * importantly, here you will usually call {@link #setContentView(int)}
166 * with a layout resource defining your UI, and using {@link #findViewById}
167 * to retrieve the widgets in that UI that you need to interact with
168 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * <li> {@link #onPause} is where you deal with the user leaving your
171 * activity. Most importantly, any changes made by the user should at this
172 * point be committed (usually to the
173 * {@link android.content.ContentProvider} holding the data).
174 * </ul>
175 *
176 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
177 * activity classes must have a corresponding
178 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
179 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <p>Topics covered here:
182 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700183 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
185 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
186 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
187 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
188 * <li><a href="#Permissions">Permissions</a>
189 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
190 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700191 *
192 * <div class="special reference">
193 * <h3>Developer Guides</h3>
194 * <p>The Activity class is an important part of an application's overall lifecycle,
195 * and the way activities are launched and put together is a fundamental
196 * part of the platform's application model. For a detailed perspective on the structure of an
197 * Android application and how activities behave, please read the
198 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700199 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700200 * developer guides.</p>
201 *
202 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700203 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700204 * developer guide.</p>
205 * </div>
206 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700207 * <a name="Fragments"></a>
208 * <h3>Fragments</h3>
209 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800210 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
211 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700212 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800213 * screens, and help scale their application between small and large screens.</p>
214 *
215 * <p>For more information about using fragments, read the
216 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700217 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <a name="ActivityLifecycle"></a>
219 * <h3>Activity Lifecycle</h3>
220 *
221 * <p>Activities in the system are managed as an <em>activity stack</em>.
222 * When a new activity is started, it is placed on the top of the stack
223 * and becomes the running activity -- the previous activity always remains
224 * below it in the stack, and will not come to the foreground again until
225 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700226 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * <p>An activity has essentially four states:</p>
228 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700229 * <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 -0800230 * the stack),
231 * it is <em>active</em> or <em>running</em>. </li>
232 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700233 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * is <em>paused</em>. A paused activity is completely alive (it
235 * maintains all state and member information and remains attached to
236 * the window manager), but can be killed by the system in extreme
237 * low memory situations.
238 * <li>If an activity is completely obscured by another activity,
239 * it is <em>stopped</em>. It still retains all state and member information,
240 * however, it is no longer visible to the user so its window is hidden
241 * and it will often be killed by the system when memory is needed
242 * elsewhere.</li>
243 * <li>If an activity is paused or stopped, the system can drop the activity
244 * from memory by either asking it to finish, or simply killing its
245 * process. When it is displayed again to the user, it must be
246 * completely restarted and restored to its previous state.</li>
247 * </ul>
248 *
249 * <p>The following diagram shows the important state paths of an Activity.
250 * The square rectangles represent callback methods you can implement to
251 * perform operations when the Activity moves between states. The colored
252 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <p><img src="../../../images/activity_lifecycle.png"
255 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * <p>There are three key loops you may be interested in monitoring within your
258 * activity:
RoboErik55011652014-07-09 15:05:53 -0700259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * <ul>
261 * <li>The <b>entire lifetime</b> of an activity happens between the first call
262 * to {@link android.app.Activity#onCreate} through to a single final call
263 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
264 * of "global" state in onCreate(), and release all remaining resources in
265 * onDestroy(). For example, if it has a thread running in the background
266 * to download data from the network, it may create that thread in onCreate()
267 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * <li>The <b>visible lifetime</b> of an activity happens between a call to
270 * {@link android.app.Activity#onStart} until a corresponding call to
271 * {@link android.app.Activity#onStop}. During this time the user can see the
272 * activity on-screen, though it may not be in the foreground and interacting
273 * with the user. Between these two methods you can maintain resources that
274 * are needed to show the activity to the user. For example, you can register
275 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900276 * that impact your UI, and unregister it in onStop() when the user no
277 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 * can be called multiple times, as the activity becomes visible and hidden
279 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
282 * {@link android.app.Activity#onResume} until a corresponding call to
283 * {@link android.app.Activity#onPause}. During this time the activity is
284 * in front of all other activities and interacting with the user. An activity
285 * can frequently go between the resumed and paused states -- for example when
286 * the device goes to sleep, when an activity result is delivered, when a new
287 * intent is delivered -- so the code in these methods should be fairly
288 * lightweight.
289 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 * <p>The entire lifecycle of an activity is defined by the following
292 * Activity methods. All of these are hooks that you can override
293 * to do appropriate work when the activity changes state. All
294 * activities will implement {@link android.app.Activity#onCreate}
295 * to do their initial setup; many will also implement
296 * {@link android.app.Activity#onPause} to commit changes to data and
297 * otherwise prepare to stop interacting with the user. You should always
298 * call up to your superclass when implementing these methods.</p>
299 *
300 * </p>
301 * <pre class="prettyprint">
302 * public class Activity extends ApplicationContext {
303 * protected void onCreate(Bundle savedInstanceState);
304 *
305 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 * protected void onRestart();
308 *
309 * protected void onResume();
310 *
311 * protected void onPause();
312 *
313 * protected void onStop();
314 *
315 * protected void onDestroy();
316 * }
317 * </pre>
318 *
319 * <p>In general the movement through an activity's lifecycle looks like
320 * this:</p>
321 *
322 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
323 * <colgroup align="left" span="3" />
324 * <colgroup align="left" />
325 * <colgroup align="center" />
326 * <colgroup align="center" />
327 *
328 * <thead>
329 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
330 * </thead>
331 *
332 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700333 * <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 -0800334 * <td>Called when the activity is first created.
335 * This is where you should do all of your normal static set up:
336 * create views, bind data to lists, etc. This method also
337 * provides you with a Bundle containing the activity's previously
338 * frozen state, if there was one.
339 * <p>Always followed by <code>onStart()</code>.</td>
340 * <td align="center">No</td>
341 * <td align="center"><code>onStart()</code></td>
342 * </tr>
343 *
344 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700345 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * <td>Called after your activity has been stopped, prior to it being
347 * started again.
348 * <p>Always followed by <code>onStart()</code></td>
349 * <td align="center">No</td>
350 * <td align="center"><code>onStart()</code></td>
351 * </tr>
352 *
smain@google.com10e29b82016-06-23 12:09:30 -0700353 * <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 -0800354 * <td>Called when the activity is becoming visible to the user.
355 * <p>Followed by <code>onResume()</code> if the activity comes
356 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
357 * <td align="center">No</td>
358 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
359 * </tr>
360 *
361 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700362 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 * <td>Called when the activity will start
364 * interacting with the user. At this point your activity is at
365 * the top of the activity stack, with user input going to it.
366 * <p>Always followed by <code>onPause()</code>.</td>
367 * <td align="center">No</td>
368 * <td align="center"><code>onPause()</code></td>
369 * </tr>
370 *
smain@google.com10e29b82016-06-23 12:09:30 -0700371 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 * <td>Called when the system is about to start resuming a previous
373 * activity. This is typically used to commit unsaved changes to
374 * persistent data, stop animations and other things that may be consuming
375 * CPU, etc. Implementations of this method must be very quick because
376 * the next activity will not be resumed until this method returns.
377 * <p>Followed by either <code>onResume()</code> if the activity
378 * returns back to the front, or <code>onStop()</code> if it becomes
379 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800380 * <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 -0800381 * <td align="center"><code>onResume()</code> or<br>
382 * <code>onStop()</code></td>
383 * </tr>
384 *
smain@google.com10e29b82016-06-23 12:09:30 -0700385 * <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 -0800386 * <td>Called when the activity is no longer visible to the user, because
387 * another activity has been resumed and is covering this one. This
388 * may happen either because a new activity is being started, an existing
389 * one is being brought in front of this one, or this one is being
390 * destroyed.
391 * <p>Followed by either <code>onRestart()</code> if
392 * this activity is coming back to interact with the user, or
393 * <code>onDestroy()</code> if this activity is going away.</td>
394 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
395 * <td align="center"><code>onRestart()</code> or<br>
396 * <code>onDestroy()</code></td>
397 * </tr>
398 *
smain@google.com10e29b82016-06-23 12:09:30 -0700399 * <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 -0800400 * <td>The final call you receive before your
401 * activity is destroyed. This can happen either because the
402 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700403 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * instance of the activity to save space. You can distinguish
405 * between these two scenarios with the {@link
406 * Activity#isFinishing} method.</td>
407 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
408 * <td align="center"><em>nothing</em></td>
409 * </tr>
410 * </tbody>
411 * </table>
412 *
413 * <p>Note the "Killable" column in the above table -- for those methods that
414 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800415 * 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 -0800416 * of its code being executed. Because of this, you should use the
417 * {@link #onPause} method to write any persistent data (such as user edits)
418 * to storage. In addition, the method
419 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
420 * in such a background state, allowing you to save away any dynamic instance
421 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700422 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
424 * section for more information on how the lifecycle of a process is tied
425 * to the activities it is hosting. Note that it is important to save
426 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800427 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * be called in every situation as described in its documentation.</p>
429 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800430 * <p class="note">Be aware that these semantics will change slightly between
431 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
432 * vs. those targeting prior platforms. Starting with Honeycomb, an application
433 * is not in the killable state until its {@link #onStop} has returned. This
434 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700435 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800436 * wait until {@link #onStop()} to save persistent state.</p>
437 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800438 * <p class="note">For applications targeting platforms starting with
439 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
440 * will always be called after {@link #onStop}, so an application may safely
441 * perform fragment transactions in {@link #onStop} and will be able to save
442 * persistent state later.</p>
443 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 * <p>For those methods that are not marked as being killable, the activity's
445 * process will not be killed by the system starting from the time the method
446 * is called and continuing after it returns. Thus an activity is in the killable
447 * state, for example, between after <code>onPause()</code> to the start of
448 * <code>onResume()</code>.</p>
449 *
450 * <a name="ConfigurationChanges"></a>
451 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * <p>If the configuration of the device (as defined by the
454 * {@link Configuration Resources.Configuration} class) changes,
455 * then anything displaying a user interface will need to update to match that
456 * configuration. Because Activity is the primary mechanism for interacting
457 * with the user, it includes special support for handling configuration
458 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700459 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * <p>Unless you specify otherwise, a configuration change (such as a change
461 * in screen orientation, language, input devices, etc) will cause your
462 * current activity to be <em>destroyed</em>, going through the normal activity
463 * lifecycle process of {@link #onPause},
464 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
465 * had been in the foreground or visible to the user, once {@link #onDestroy} is
466 * called in that instance then a new instance of the activity will be
467 * created, with whatever savedInstanceState the previous instance had generated
468 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 * <p>This is done because any application resource,
471 * including layout files, can change based on any configuration value. Thus
472 * the only safe way to handle a configuration change is to re-retrieve all
473 * resources, including layouts, drawables, and strings. Because activities
474 * must already know how to save their state and re-create themselves from
475 * that state, this is a convenient way to have an activity restart itself
476 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 * <p>In some special cases, you may want to bypass restarting of your
479 * activity based on one or more types of configuration changes. This is
480 * done with the {@link android.R.attr#configChanges android:configChanges}
481 * attribute in its manifest. For any types of configuration changes you say
482 * that you handle there, you will receive a call to your current activity's
483 * {@link #onConfigurationChanged} method instead of being restarted. If
484 * a configuration change involves any that you do not handle, however, the
485 * activity will still be restarted and {@link #onConfigurationChanged}
486 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 * <a name="StartingActivities"></a>
489 * <h3>Starting Activities and Getting Results</h3>
490 *
491 * <p>The {@link android.app.Activity#startActivity}
492 * method is used to start a
493 * new activity, which will be placed at the top of the activity stack. It
494 * takes a single argument, an {@link android.content.Intent Intent},
495 * which describes the activity
496 * to be executed.</p>
497 *
498 * <p>Sometimes you want to get a result back from an activity when it
499 * ends. For example, you may start an activity that lets the user pick
500 * a person in a list of contacts; when it ends, it returns the person
501 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700502 * {@link android.app.Activity#startActivityForResult(Intent, int)}
503 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700505 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 *
507 * <p>When an activity exits, it can call
508 * {@link android.app.Activity#setResult(int)}
509 * to return data back to its parent. It must always supply a result code,
510 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
511 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
512 * return back an Intent containing any additional data it wants. All of this
513 * information appears back on the
514 * parent's <code>Activity.onActivityResult()</code>, along with the integer
515 * identifier it originally supplied.</p>
516 *
517 * <p>If a child activity fails for any reason (such as crashing), the parent
518 * activity will receive a result with the code RESULT_CANCELED.</p>
519 *
520 * <pre class="prettyprint">
521 * public class MyActivity extends Activity {
522 * ...
523 *
524 * static final int PICK_CONTACT_REQUEST = 0;
525 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700526 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
528 * // When the user center presses, let them pick a contact.
529 * startActivityForResult(
530 * new Intent(Intent.ACTION_PICK,
531 * new Uri("content://contacts")),
532 * PICK_CONTACT_REQUEST);
533 * return true;
534 * }
535 * return false;
536 * }
537 *
538 * protected void onActivityResult(int requestCode, int resultCode,
539 * Intent data) {
540 * if (requestCode == PICK_CONTACT_REQUEST) {
541 * if (resultCode == RESULT_OK) {
542 * // A contact was picked. Here we will just display it
543 * // to the user.
544 * startActivity(new Intent(Intent.ACTION_VIEW, data));
545 * }
546 * }
547 * }
548 * }
549 * </pre>
550 *
551 * <a name="SavingPersistentState"></a>
552 * <h3>Saving Persistent State</h3>
553 *
koprivafc3949e2018-09-17 11:35:13 -0700554 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * will deal with: shared document-like data (typically stored in a SQLite
556 * database using a {@linkplain android.content.ContentProvider content provider})
557 * and internal state such as user preferences.</p>
558 *
koprivafc3949e2018-09-17 11:35:13 -0700559 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 * "edit in place" user model. That is, any edits a user makes are effectively
561 * made immediately without requiring an additional confirmation step.
562 * Supporting this model is generally a simple matter of following two rules:</p>
563 *
564 * <ul>
565 * <li> <p>When creating a new document, the backing database entry or file for
566 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700567 * 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 -0800568 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700569 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
571 * commit to the backing content provider or file any changes the user
572 * has made. This ensures that those changes will be seen by any other
573 * activity that is about to run. You will probably want to commit
574 * your data even more aggressively at key times during your
575 * activity's lifecycle: for example before starting a new
576 * activity, before finishing your own activity, when the user
577 * switches between input fields, etc.</p>
578 * </ul>
579 *
580 * <p>This model is designed to prevent data loss when a user is navigating
581 * between activities, and allows the system to safely kill an activity (because
582 * system resources are needed somewhere else) at any time after it has been
583 * paused. Note this implies
584 * that the user pressing BACK from your activity does <em>not</em>
585 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800586 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
588 *
589 * <p>See the {@linkplain android.content.ContentProvider content package} for
590 * more information about content providers. These are a key aspect of how
591 * different activities invoke and propagate data between themselves.</p>
592 *
593 * <p>The Activity class also provides an API for managing internal persistent state
594 * associated with an activity. This can be used, for example, to remember
595 * the user's preferred initial display in a calendar (day view or week view)
596 * or the user's default home page in a web browser.</p>
597 *
598 * <p>Activity persistent state is managed
599 * with the method {@link #getPreferences},
600 * allowing you to retrieve and
601 * modify a set of name/value pairs associated with the activity. To use
602 * preferences that are shared across multiple application components
603 * (activities, receivers, services, providers), you can use the underlying
604 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
605 * to retrieve a preferences
606 * object stored under a specific name.
607 * (Note that it is not possible to share settings data across application
608 * packages -- for that you will need a content provider.)</p>
609 *
610 * <p>Here is an excerpt from a calendar activity that stores the user's
611 * preferred view mode in its persistent settings:</p>
612 *
613 * <pre class="prettyprint">
614 * public class CalendarActivity extends Activity {
615 * ...
616 *
617 * static final int DAY_VIEW_MODE = 0;
618 * static final int WEEK_VIEW_MODE = 1;
619 *
620 * private SharedPreferences mPrefs;
621 * private int mCurViewMode;
622 *
623 * protected void onCreate(Bundle savedInstanceState) {
624 * super.onCreate(savedInstanceState);
625 *
626 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700627 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 * }
629 *
630 * protected void onPause() {
631 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * SharedPreferences.Editor ed = mPrefs.edit();
634 * ed.putInt("view_mode", mCurViewMode);
635 * ed.commit();
636 * }
637 * }
638 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * <a name="Permissions"></a>
641 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * <p>The ability to start a particular Activity can be enforced when it is
644 * declared in its
645 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
646 * tag. By doing so, other applications will need to declare a corresponding
647 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
648 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800649 *
650 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
651 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
652 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
653 * Activity access to the specific URIs in the Intent. Access will remain
654 * until the Activity has finished (it will remain across the hosting
655 * process being killed and other temporary destruction). As of
656 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
657 * was already created and a new Intent is being delivered to
658 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
659 * to the existing ones it holds.
660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
662 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700663 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 * <a name="ProcessLifecycle"></a>
665 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700666 *
kopriva4dddc632018-03-19 14:13:08 -0700667 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700669 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700671 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700673 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 * processes (the last ones) before it resorts to killing more important
675 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * <ol>
678 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
679 * that the user is currently interacting with) is considered the most important.
680 * Its process will only be killed as a last resort, if it uses more memory
681 * than is available on the device. Generally at this point the device has
682 * reached a memory paging state, so this is required in order to keep the user
683 * interface responsive.
684 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
685 * but not in the foreground, such as one sitting behind a foreground dialog)
686 * is considered extremely important and will not be killed unless that is
687 * required to keep the foreground activity running.
688 * <li> <p>A <b>background activity</b> (an activity that is not visible to
689 * the user and has been paused) is no longer critical, so the system may
690 * safely kill its process to reclaim memory for other foreground or
691 * visible processes. If its process needs to be killed, when the user navigates
692 * back to the activity (making it visible on the screen again), its
693 * {@link #onCreate} method will be called with the savedInstanceState it had previously
694 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
695 * state as the user last left it.
696 * <li> <p>An <b>empty process</b> is one hosting no activities or other
697 * application components (such as {@link Service} or
698 * {@link android.content.BroadcastReceiver} classes). These are killed very
699 * quickly by the system as memory becomes low. For this reason, any
700 * background operation you do outside of an activity must be executed in the
701 * context of an activity BroadcastReceiver or Service to ensure that the system
702 * knows it needs to keep your process around.
703 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700704 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 * <p>Sometimes an Activity may need to do a long-running operation that exists
706 * independently of the activity lifecycle itself. An example may be a camera
707 * application that allows you to upload a picture to a web site. The upload
708 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700709 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 * should start a {@link Service} in which the upload takes place. This allows
711 * the system to properly prioritize your process (considering it to be more
712 * important than other non-visible applications) for the duration of the
713 * upload, independent of whether the original activity is paused, stopped,
714 * or finished.
715 */
716public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700717 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700719 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000720 Window.OnWindowDismissedCallback, WindowControllerCallback,
Adam He6240eab2019-02-25 13:34:45 -0800721 AutofillManager.AutofillClient, ContentCaptureManager.ContentCaptureClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700723 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
725 /** Standard activity result: operation canceled. */
726 public static final int RESULT_CANCELED = 0;
727 /** Standard activity result: operation succeeded. */
728 public static final int RESULT_OK = -1;
729 /** Start of user-defined activity results. */
730 public static final int RESULT_FIRST_USER = 1;
731
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700732 /** @hide Task isn't finished when activity is finished */
733 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700734 /**
735 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
736 * past behavior the task is also removed from recents.
737 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700738 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700739 /**
740 * @hide Task is finished along with the finishing activity, but it is not removed from
741 * recents.
742 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700743 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
744
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100745 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700746 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700747 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700748
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700749 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
751 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
752 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
753 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800754 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700755 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
756 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757
Svetoslav970b59c2015-06-09 16:05:21 -0700758 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000759 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700760
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100761 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100762
Andrii Kuliand25680c2018-02-21 15:16:58 -0800763 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
764 private static final int LOG_AM_ON_START_CALLED = 30059;
765 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
766 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
767 private static final int LOG_AM_ON_STOP_CALLED = 30049;
768 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
769 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700770 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800771
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800772 private static class ManagedDialog {
773 Dialog mDialog;
774 Bundle mArgs;
775 }
776 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777
778 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100779 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 private Instrumentation mInstrumentation;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100781 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 private IBinder mToken;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100783 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700784 private int mIdent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100785 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 /*package*/ String mEmbeddedID;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100787 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 private Application mApplication;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100789 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700790 /*package*/ Intent mIntent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100791 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800792 /*package*/ String mReferrer;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100793 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 private ComponentName mComponent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100795 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100797 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 /*package*/ ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100799 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 Activity mParent;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100801 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 boolean mCalled;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100803 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600804 /*package*/ boolean mResumed;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100805 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700806 /*package*/ boolean mStopped;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100807 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 boolean mFinished;
809 boolean mStartedActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100810 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700811 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700812 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200813 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700814
815 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
816 * This only affects the client-side exception, the actual state check still happens in AMS. */
817 private boolean mCanEnterPictureInPicture = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700818 /** true if the activity is going through a transient pause */
819 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500820 /** true if the activity is being destroyed in order to recreate it with a new configuration */
821 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100822 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 /*package*/ int mConfigChangeFlags;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100824 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100826 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700827 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700829 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
830 @Nullable private AutofillManager mAutofillManager;
831
Felipe Lemeecb08be2018-11-27 15:48:47 -0800832 /** The content capture manager. Always access via {@link #getContentCaptureManager()}. */
833 @Nullable private ContentCaptureManager mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800834
Ian Lake8a88cd62018-09-28 13:25:20 -0700835 private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
836 new ArrayList<Application.ActivityLifecycleCallbacks>();
Felipe Lemee348dc32018-11-05 12:35:29 -0800837
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700838 static final class NonConfigurationInstances {
839 Object activity;
840 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800841 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700842 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700843 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700844 }
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100845 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700846 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700847
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100848 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 private Window mWindow;
850
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100851 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 private WindowManager mWindowManager;
853 /*package*/ View mDecor = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100854 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 /*package*/ boolean mWindowAdded = false;
856 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood31755f92018-12-20 13:53:36 +0000857 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700859 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700860 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861
Mathew Inwood8c854f82018-09-14 12:35:36 +0100862 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn91097de2014-04-04 18:02:06 -0700863 private VoiceInteractor mVoiceInteractor;
864
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100865 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 private CharSequence mTitle;
867 private int mTitleColor = 0;
868
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700869 // we must have a handler before the FragmentController is constructed
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100870 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700871 final Handler mHandler = new Handler();
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100872 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700873 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 private static final class ManagedCursor {
876 ManagedCursor(Cursor cursor) {
877 mCursor = cursor;
878 mReleased = false;
879 mUpdated = false;
880 }
881
882 private final Cursor mCursor;
883 private boolean mReleased;
884 private boolean mUpdated;
885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700887 @GuardedBy("mManagedCursors")
888 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
889
890 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100891 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700893 @GuardedBy("this")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100894 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700896
Craig Mautner5eda9b32013-07-02 11:58:16 -0700897 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700898 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700900 private SearchEvent mSearchEvent;
901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000903 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904
905 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
906 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700907
Winsonb6403152016-02-23 13:32:09 -0800908 private ActivityManager.TaskDescription mTaskDescription =
909 new ActivityManager.TaskDescription();
910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
912
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700913 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700914 private final Object mInstanceTracker = StrictMode.trackActivity(this);
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700917
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100918 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700919 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700920 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
921 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
Svetoslavffb32b12015-10-15 16:54:00 -0700923 private boolean mHasCurrentPermissionsRequest;
924
Svet Ganov17db9dc2017-02-21 19:54:31 -0800925 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800926 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800927
Phil Weaver846cda932017-06-15 10:10:06 -0700928 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
929 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700930
Felipe Leme4753bb02017-03-22 20:24:00 -0700931 private AutofillPopupWindow mAutofillPopupWindow;
932
chaviwfeb2e1e2018-12-19 17:24:11 -0800933 /** @hide */
934 boolean mEnterAnimationComplete;
935
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800936 private static native String getDlWarning();
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 /** Return the intent that started this activity. */
939 public Intent getIntent() {
940 return mIntent;
941 }
942
RoboErik55011652014-07-09 15:05:53 -0700943 /**
944 * Change the intent returned by {@link #getIntent}. This holds a
945 * reference to the given intent; it does not copy it. Often used in
946 * conjunction with {@link #onNewIntent}.
947 *
948 * @param newIntent The new Intent object to return from getIntent
949 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 * @see #getIntent
951 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700952 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public void setIntent(Intent newIntent) {
954 mIntent = newIntent;
955 }
956
957 /** Return the application that owns this activity. */
958 public final Application getApplication() {
959 return mApplication;
960 }
961
962 /** Is this activity embedded inside of another activity? */
963 public final boolean isChild() {
964 return mParent != null;
965 }
RoboErik55011652014-07-09 15:05:53 -0700966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 /** Return the parent activity if this view is an embedded child. */
968 public final Activity getParent() {
969 return mParent;
970 }
971
972 /** Retrieve the window manager for showing custom windows. */
973 public WindowManager getWindowManager() {
974 return mWindowManager;
975 }
976
977 /**
978 * Retrieve the current {@link android.view.Window} for the activity.
979 * This can be used to directly access parts of the Window API that
980 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700981 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 * @return Window The current window, or null if the activity is not
983 * visual.
984 */
985 public Window getWindow() {
986 return mWindow;
987 }
988
989 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800990 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -0800991 *
992 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -0700993 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800994 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -0700995 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700996 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700997 }
RoboErik55011652014-07-09 15:05:53 -0700998
Dianne Hackbornc8017682010-07-06 13:34:38 -0700999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 * Calls {@link android.view.Window#getCurrentFocus} on the
1001 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -07001002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -07001004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 * @see #getWindow
1006 * @see android.view.Window#getCurrentFocus
1007 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001008 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 public View getCurrentFocus() {
1010 return mWindow != null ? mWindow.getCurrentFocus() : null;
1011 }
1012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 /**
Felipe Lemee348dc32018-11-05 12:35:29 -08001014 * (Creates, sets and) returns the autofill manager
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001015 *
1016 * @return The autofill manager
1017 */
1018 @NonNull private AutofillManager getAutofillManager() {
1019 if (mAutofillManager == null) {
1020 mAutofillManager = getSystemService(AutofillManager.class);
1021 }
1022
1023 return mAutofillManager;
1024 }
1025
Felipe Lemee348dc32018-11-05 12:35:29 -08001026 /**
Felipe Lemeecb08be2018-11-27 15:48:47 -08001027 * (Creates, sets, and ) returns the content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001028 *
Felipe Lemeecb08be2018-11-27 15:48:47 -08001029 * @return The content capture manager
Felipe Lemee348dc32018-11-05 12:35:29 -08001030 */
Adam Heb66babb2019-01-08 15:43:53 -08001031 @Nullable private ContentCaptureManager getContentCaptureManager() {
1032 // ContextCapture disabled for system apps
Felipe Leme0c6b23d2019-01-15 17:02:49 -08001033 if (!UserHandle.isApp(myUid())) return null;
Felipe Lemeecb08be2018-11-27 15:48:47 -08001034 if (mContentCaptureManager == null) {
1035 mContentCaptureManager = getSystemService(ContentCaptureManager.class);
Felipe Lemee348dc32018-11-05 12:35:29 -08001036 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001037 return mContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -08001038 }
1039
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001040 /** @hide */ private static final int CONTENT_CAPTURE_START = 1;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001041 /** @hide */ private static final int CONTENT_CAPTURE_RESUME = 2;
1042 /** @hide */ private static final int CONTENT_CAPTURE_PAUSE = 3;
1043 /** @hide */ private static final int CONTENT_CAPTURE_STOP = 4;
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001044
1045 /** @hide */
1046 @IntDef(prefix = { "CONTENT_CAPTURE_" }, value = {
1047 CONTENT_CAPTURE_START,
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001048 CONTENT_CAPTURE_RESUME,
1049 CONTENT_CAPTURE_PAUSE,
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001050 CONTENT_CAPTURE_STOP
1051 })
1052 @Retention(RetentionPolicy.SOURCE)
1053 @interface ContentCaptureNotificationType{}
1054
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001055 private String getContentCaptureTypeAsString(@ContentCaptureNotificationType int type) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001056 switch (type) {
1057 case CONTENT_CAPTURE_START:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001058 return "START";
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001059 case CONTENT_CAPTURE_RESUME:
1060 return "RESUME";
1061 case CONTENT_CAPTURE_PAUSE:
1062 return "PAUSE";
Felipe Lemeaa5088e2018-12-10 14:53:58 -08001063 case CONTENT_CAPTURE_STOP:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001064 return "STOP";
Felipe Leme7a534082018-11-05 15:03:04 -08001065 default:
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001066 return "UNKNOW-" + type;
1067 }
1068 }
1069
1070 private void notifyContentCaptureManagerIfNeeded(@ContentCaptureNotificationType int type) {
1071 if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
1072 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1073 "notifyContentCapture(" + getContentCaptureTypeAsString(type) + ") for "
1074 + mComponent.toShortString());
1075 }
1076 try {
1077 final ContentCaptureManager cm = getContentCaptureManager();
1078 if (cm == null) return;
1079
1080 switch (type) {
1081 case CONTENT_CAPTURE_START:
1082 //TODO(b/111276913): decide whether the InteractionSessionId should be
1083 // saved / restored in the activity bundle - probably not
1084 int flags = 0;
1085 if ((getWindow().getAttributes().flags
1086 & WindowManager.LayoutParams.FLAG_SECURE) != 0) {
1087 flags |= ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE;
1088 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001089 cm.onActivityCreated(mToken, getComponentName(), flags);
1090 break;
1091 case CONTENT_CAPTURE_RESUME:
1092 cm.onActivityResumed();
1093 break;
1094 case CONTENT_CAPTURE_PAUSE:
1095 cm.onActivityPaused();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001096 break;
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001097 case CONTENT_CAPTURE_STOP:
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001098 cm.onActivityDestroyed();
Felipe Lemeb337e1c2019-01-18 13:06:17 -08001099 break;
1100 default:
1101 Log.wtf(TAG, "Invalid @ContentCaptureNotificationType: " + type);
1102 }
1103 } finally {
1104 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Felipe Leme7a534082018-11-05 15:03:04 -08001105 }
1106 }
1107
Felipe Lemebb567ae2017-10-04 09:56:21 -07001108 @Override
1109 protected void attachBaseContext(Context newBase) {
1110 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001111 if (newBase != null) {
1112 newBase.setAutofillClient(this);
Felipe Leme326f15a2019-02-19 09:42:24 -08001113 newBase.setContentCaptureOptions(getContentCaptureOptions());
Felipe Lemed247de82018-05-14 17:51:58 -07001114 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001115 }
1116
1117 /** @hide */
1118 @Override
1119 public final AutofillClient getAutofillClient() {
1120 return this;
1121 }
1122
Adam He6240eab2019-02-25 13:34:45 -08001123 /** @hide */
1124 @Override
1125 public final ContentCaptureClient getContentCaptureClient() {
1126 return this;
1127 }
1128
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001129 /**
Ian Lake8a88cd62018-09-28 13:25:20 -07001130 * Register an {@link Application.ActivityLifecycleCallbacks} instance that receives
1131 * lifecycle callbacks for only this Activity.
1132 * <p>
1133 * In relation to any
1134 * {@link Application#registerActivityLifecycleCallbacks Application registered callbacks},
1135 * the callbacks registered here will always occur nested within those callbacks. This means:
1136 * <ul>
1137 * <li>Pre events will first be sent to Application registered callbacks, then to callbacks
1138 * registered here.</li>
1139 * <li>{@link Application.ActivityLifecycleCallbacks#onActivityCreated(Activity, Bundle)},
1140 * {@link Application.ActivityLifecycleCallbacks#onActivityStarted(Activity)}, and
1141 * {@link Application.ActivityLifecycleCallbacks#onActivityResumed(Activity)} will
1142 * be sent first to Application registered callbacks, then to callbacks registered here.
1143 * For all other events, callbacks registered here will be sent first.</li>
1144 * <li>Post events will first be sent to callbacks registered here, then to
1145 * Application registered callbacks.</li>
1146 * </ul>
1147 * <p>
1148 * If multiple callbacks are registered here, they receive events in a first in (up through
1149 * {@link Application.ActivityLifecycleCallbacks#onActivityPostResumed}, last out
1150 * ordering.
1151 * <p>
1152 * It is strongly recommended to register this in the constructor of your Activity to ensure
1153 * you get all available callbacks. As this callback is associated with only this Activity,
1154 * it is not usually necessary to {@link #unregisterActivityLifecycleCallbacks unregister} it
1155 * unless you specifically do not want to receive further lifecycle callbacks.
1156 *
1157 * @param callback The callback instance to register
1158 */
1159 public void registerActivityLifecycleCallbacks(
1160 @NonNull Application.ActivityLifecycleCallbacks callback) {
1161 synchronized (mActivityLifecycleCallbacks) {
1162 mActivityLifecycleCallbacks.add(callback);
1163 }
1164 }
1165
1166 /**
1167 * Unregister an {@link Application.ActivityLifecycleCallbacks} previously registered
1168 * with {@link #registerActivityLifecycleCallbacks}. It will not receive any further
1169 * callbacks.
1170 *
1171 * @param callback The callback instance to unregister
1172 * @see #registerActivityLifecycleCallbacks
1173 */
1174 public void unregisterActivityLifecycleCallbacks(
1175 @NonNull Application.ActivityLifecycleCallbacks callback) {
1176 synchronized (mActivityLifecycleCallbacks) {
1177 mActivityLifecycleCallbacks.remove(callback);
1178 }
1179 }
1180
1181 private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
1182 getApplication().dispatchActivityPreCreated(this, savedInstanceState);
1183 Object[] callbacks = collectActivityLifecycleCallbacks();
1184 if (callbacks != null) {
1185 for (int i = 0; i < callbacks.length; i++) {
1186 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
1187 savedInstanceState);
1188 }
1189 }
1190 }
1191
1192 private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
1193 getApplication().dispatchActivityCreated(this, savedInstanceState);
1194 Object[] callbacks = collectActivityLifecycleCallbacks();
1195 if (callbacks != null) {
1196 for (int i = 0; i < callbacks.length; i++) {
1197 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
1198 savedInstanceState);
1199 }
1200 }
1201 }
1202
1203 private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
1204 Object[] callbacks = collectActivityLifecycleCallbacks();
1205 if (callbacks != null) {
1206 for (int i = 0; i < callbacks.length; i++) {
1207 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
1208 savedInstanceState);
1209 }
1210 }
1211 getApplication().dispatchActivityPostCreated(this, savedInstanceState);
1212 }
1213
1214 private void dispatchActivityPreStarted() {
1215 getApplication().dispatchActivityPreStarted(this);
1216 Object[] callbacks = collectActivityLifecycleCallbacks();
1217 if (callbacks != null) {
1218 for (int i = 0; i < callbacks.length; i++) {
1219 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStarted(this);
1220 }
1221 }
1222 }
1223
1224 private void dispatchActivityStarted() {
1225 getApplication().dispatchActivityStarted(this);
1226 Object[] callbacks = collectActivityLifecycleCallbacks();
1227 if (callbacks != null) {
1228 for (int i = 0; i < callbacks.length; i++) {
1229 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
1230 }
1231 }
1232 }
1233
1234 private void dispatchActivityPostStarted() {
1235 Object[] callbacks = collectActivityLifecycleCallbacks();
1236 if (callbacks != null) {
1237 for (int i = 0; i < callbacks.length; i++) {
1238 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1239 .onActivityPostStarted(this);
1240 }
1241 }
1242 getApplication().dispatchActivityPostStarted(this);
1243 }
1244
1245 private void dispatchActivityPreResumed() {
1246 getApplication().dispatchActivityPreResumed(this);
1247 Object[] callbacks = collectActivityLifecycleCallbacks();
1248 if (callbacks != null) {
1249 for (int i = 0; i < callbacks.length; i++) {
1250 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreResumed(this);
1251 }
1252 }
1253 }
1254
1255 private void dispatchActivityResumed() {
1256 getApplication().dispatchActivityResumed(this);
1257 Object[] callbacks = collectActivityLifecycleCallbacks();
1258 if (callbacks != null) {
1259 for (int i = 0; i < callbacks.length; i++) {
1260 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
1261 }
1262 }
1263 }
1264
1265 private void dispatchActivityPostResumed() {
1266 Object[] callbacks = collectActivityLifecycleCallbacks();
1267 if (callbacks != null) {
1268 for (int i = 0; i < callbacks.length; i++) {
1269 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostResumed(this);
1270 }
1271 }
1272 getApplication().dispatchActivityPostResumed(this);
1273 }
1274
1275 private void dispatchActivityPrePaused() {
1276 getApplication().dispatchActivityPrePaused(this);
1277 Object[] callbacks = collectActivityLifecycleCallbacks();
1278 if (callbacks != null) {
1279 for (int i = callbacks.length - 1; i >= 0; i--) {
1280 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPrePaused(this);
1281 }
1282 }
1283 }
1284
1285 private void dispatchActivityPaused() {
1286 Object[] callbacks = collectActivityLifecycleCallbacks();
1287 if (callbacks != null) {
1288 for (int i = callbacks.length - 1; i >= 0; i--) {
1289 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(this);
1290 }
1291 }
1292 getApplication().dispatchActivityPaused(this);
1293 }
1294
1295 private void dispatchActivityPostPaused() {
1296 Object[] callbacks = collectActivityLifecycleCallbacks();
1297 if (callbacks != null) {
1298 for (int i = callbacks.length - 1; i >= 0; i--) {
1299 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostPaused(this);
1300 }
1301 }
1302 getApplication().dispatchActivityPostPaused(this);
1303 }
1304
1305 private void dispatchActivityPreStopped() {
1306 getApplication().dispatchActivityPreStopped(this);
1307 Object[] callbacks = collectActivityLifecycleCallbacks();
1308 if (callbacks != null) {
1309 for (int i = callbacks.length - 1; i >= 0; i--) {
1310 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreStopped(this);
1311 }
1312 }
1313 }
1314
1315 private void dispatchActivityStopped() {
1316 Object[] callbacks = collectActivityLifecycleCallbacks();
1317 if (callbacks != null) {
1318 for (int i = callbacks.length - 1; i >= 0; i--) {
1319 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(this);
1320 }
1321 }
1322 getApplication().dispatchActivityStopped(this);
1323 }
1324
1325 private void dispatchActivityPostStopped() {
1326 Object[] callbacks = collectActivityLifecycleCallbacks();
1327 if (callbacks != null) {
1328 for (int i = callbacks.length - 1; i >= 0; i--) {
1329 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1330 .onActivityPostStopped(this);
1331 }
1332 }
1333 getApplication().dispatchActivityPostStopped(this);
1334 }
1335
1336 private void dispatchActivityPreSaveInstanceState(@NonNull Bundle outState) {
1337 getApplication().dispatchActivityPreSaveInstanceState(this, outState);
1338 Object[] callbacks = collectActivityLifecycleCallbacks();
1339 if (callbacks != null) {
1340 for (int i = callbacks.length - 1; i >= 0; i--) {
1341 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1342 .onActivityPreSaveInstanceState(this, outState);
1343 }
1344 }
1345 }
1346
1347 private void dispatchActivitySaveInstanceState(@NonNull Bundle outState) {
1348 Object[] callbacks = collectActivityLifecycleCallbacks();
1349 if (callbacks != null) {
1350 for (int i = callbacks.length - 1; i >= 0; i--) {
1351 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1352 .onActivitySaveInstanceState(this, outState);
1353 }
1354 }
1355 getApplication().dispatchActivitySaveInstanceState(this, outState);
1356 }
1357
1358 private void dispatchActivityPostSaveInstanceState(@NonNull Bundle outState) {
1359 Object[] callbacks = collectActivityLifecycleCallbacks();
1360 if (callbacks != null) {
1361 for (int i = callbacks.length - 1; i >= 0; i--) {
1362 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1363 .onActivityPostSaveInstanceState(this, outState);
1364 }
1365 }
1366 getApplication().dispatchActivityPostSaveInstanceState(this, outState);
1367 }
1368
1369 private void dispatchActivityPreDestroyed() {
1370 getApplication().dispatchActivityPreDestroyed(this);
1371 Object[] callbacks = collectActivityLifecycleCallbacks();
1372 if (callbacks != null) {
1373 for (int i = callbacks.length - 1; i >= 0; i--) {
1374 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1375 .onActivityPreDestroyed(this);
1376 }
1377 }
1378 }
1379
1380 private void dispatchActivityDestroyed() {
1381 Object[] callbacks = collectActivityLifecycleCallbacks();
1382 if (callbacks != null) {
1383 for (int i = callbacks.length - 1; i >= 0; i--) {
1384 ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(this);
1385 }
1386 }
1387 getApplication().dispatchActivityDestroyed(this);
1388 }
1389
1390 private void dispatchActivityPostDestroyed() {
1391 Object[] callbacks = collectActivityLifecycleCallbacks();
1392 if (callbacks != null) {
1393 for (int i = callbacks.length - 1; i >= 0; i--) {
1394 ((Application.ActivityLifecycleCallbacks) callbacks[i])
1395 .onActivityPostDestroyed(this);
1396 }
1397 }
1398 getApplication().dispatchActivityPostDestroyed(this);
1399 }
1400
1401 private Object[] collectActivityLifecycleCallbacks() {
1402 Object[] callbacks = null;
1403 synchronized (mActivityLifecycleCallbacks) {
1404 if (mActivityLifecycleCallbacks.size() > 0) {
1405 callbacks = mActivityLifecycleCallbacks.toArray();
1406 }
1407 }
1408 return callbacks;
1409 }
1410
1411 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 * Called when the activity is starting. This is where most initialization
1413 * should go: calling {@link #setContentView(int)} to inflate the
1414 * activity's UI, using {@link #findViewById} to programmatically interact
1415 * with widgets in the UI, calling
1416 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1417 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001420 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1421 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1422 * executing.
RoboErik55011652014-07-09 15:05:53 -07001423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * <p><em>Derived classes must call through to the super class's
1425 * implementation of this method. If they do not, an exception will be
1426 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 * @param savedInstanceState If the activity is being re-initialized after
1429 * previously being shut down then this Bundle contains the data it most
1430 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * @see #onStart
1433 * @see #onSaveInstanceState
1434 * @see #onRestoreInstanceState
1435 * @see #onPostCreate
1436 */
Tor Norbye83c68962015-03-10 20:55:31 -07001437 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001438 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001439 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001440 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001441
Dianne Hackborn2707d602010-07-09 18:01:20 -07001442 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001443 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001444 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001445 if (mActivityInfo.parentActivityName != null) {
1446 if (mActionBar == null) {
1447 mEnableDefaultActionBarUp = true;
1448 } else {
1449 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1450 }
1451 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001452 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001453 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001454 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1455 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001456
1457 if (mAutoFillResetNeeded) {
1458 getAutofillManager().onCreate(savedInstanceState);
1459 }
1460
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001461 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1462 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1463 ? mLastNonConfigurationInstances.fragments : null);
1464 }
1465 mFragments.dispatchCreate();
Ian Lake8a88cd62018-09-28 13:25:20 -07001466 dispatchActivityCreated(savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001467 if (mVoiceInteractor != null) {
1468 mVoiceInteractor.attachActivity(this);
1469 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001470 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mCalled = true;
Felipe Lemee348dc32018-11-05 12:35:29 -08001472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 }
1474
1475 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001476 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001477 * the attribute {@link android.R.attr#persistableMode} set to
1478 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001479 *
1480 * @param savedInstanceState if the activity is being re-initialized after
1481 * previously being shut down then this Bundle contains the data it most
1482 * recently supplied in {@link #onSaveInstanceState}.
1483 * <b><i>Note: Otherwise it is null.</i></b>
1484 * @param persistentState if the activity is being re-initialized after
1485 * previously being shut down or powered off then this Bundle contains the data it most
1486 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1487 * <b><i>Note: Otherwise it is null.</i></b>
1488 *
1489 * @see #onCreate(android.os.Bundle)
1490 * @see #onStart
1491 * @see #onSaveInstanceState
1492 * @see #onRestoreInstanceState
1493 * @see #onPostCreate
1494 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001495 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001496 @Nullable PersistableBundle persistentState) {
1497 onCreate(savedInstanceState);
1498 }
1499
1500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 * The hook for {@link ActivityThread} to restore the state of this activity.
1502 *
1503 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1504 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1505 *
1506 * @param savedInstanceState contains the saved state
1507 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001508 final void performRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 onRestoreInstanceState(savedInstanceState);
1510 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512
1513 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001514 * The hook for {@link ActivityThread} to restore the state of this activity.
1515 *
1516 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1517 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1518 *
1519 * @param savedInstanceState contains the saved state
1520 * @param persistentState contains the persistable saved state
1521 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001522 final void performRestoreInstanceState(@Nullable Bundle savedInstanceState,
1523 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001524 onRestoreInstanceState(savedInstanceState, persistentState);
1525 if (savedInstanceState != null) {
1526 restoreManagedDialogs(savedInstanceState);
1527 }
1528 }
1529
1530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 * This method is called after {@link #onStart} when the activity is
1532 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001533 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * to restore their state, but it is sometimes convenient to do it here
1535 * after all of the initialization has been done or to allow subclasses to
1536 * decide whether to use your default implementation. The default
1537 * implementation of this method performs a restore of any view state that
1538 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001539 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * <p>This method is called between {@link #onStart} and
1541 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 * @see #onCreate
1546 * @see #onPostCreate
1547 * @see #onResume
1548 * @see #onSaveInstanceState
1549 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001550 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (mWindow != null) {
1552 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1553 if (windowState != null) {
1554 mWindow.restoreHierarchyState(windowState);
1555 }
1556 }
1557 }
Craig Mautnera0026042014-04-23 11:45:37 -07001558
1559 /**
1560 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001561 * created with the attribute {@link android.R.attr#persistableMode} set to
1562 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1563 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001564 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1565 *
1566 * <p>This method is called between {@link #onStart} and
1567 * {@link #onPostCreate}.
1568 *
1569 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1570 *
Jake Wharton63f4d892018-07-10 12:27:59 -04001571 * <p>At least one of {@code savedInstanceState} or {@code persistentState} will not be null.
1572 *
1573 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}
1574 * or null.
1575 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}
1576 * or null.
Craig Mautnera0026042014-04-23 11:45:37 -07001577 *
1578 * @see #onRestoreInstanceState(Bundle)
1579 * @see #onCreate
1580 * @see #onPostCreate
1581 * @see #onResume
1582 * @see #onSaveInstanceState
1583 */
Jake Wharton63f4d892018-07-10 12:27:59 -04001584 public void onRestoreInstanceState(@Nullable Bundle savedInstanceState,
1585 @Nullable PersistableBundle persistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001586 if (savedInstanceState != null) {
1587 onRestoreInstanceState(savedInstanceState);
1588 }
1589 }
1590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 /**
1592 * Restore the state of any saved managed dialogs.
1593 *
1594 * @param savedInstanceState The bundle to restore from.
1595 */
1596 private void restoreManagedDialogs(Bundle savedInstanceState) {
1597 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1598 if (b == null) {
1599 return;
1600 }
1601
1602 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1603 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001604 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 for (int i = 0; i < numDialogs; i++) {
1606 final Integer dialogId = ids[i];
1607 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1608 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001609 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1610 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001611 final ManagedDialog md = new ManagedDialog();
1612 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1613 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1614 if (md.mDialog != null) {
1615 mManagedDialogs.put(dialogId, md);
1616 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1617 md.mDialog.onRestoreInstanceState(dialogState);
1618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
1620 }
1621 }
1622
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001623 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1624 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001625 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001626 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001627 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001628 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001629 return dialog;
1630 }
1631
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001632 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 return SAVED_DIALOG_KEY_PREFIX + key;
1634 }
1635
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001636 private static String savedDialogArgsKeyFor(int key) {
1637 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639
1640 /**
1641 * Called when activity start-up is complete (after {@link #onStart}
1642 * and {@link #onRestoreInstanceState} have been called). Applications will
1643 * generally not implement this method; it is intended for system
1644 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 * <p><em>Derived classes must call through to the super class's
1647 * implementation of this method. If they do not, an exception will be
1648 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 * @param savedInstanceState If the activity is being re-initialized after
1651 * previously being shut down then this Bundle contains the data it most
1652 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1653 * @see #onCreate
1654 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001655 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001656 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 if (!isChild()) {
1658 mTitleReady = true;
1659 onTitleChanged(getTitle(), getTitleColor());
1660 }
Winsonb6403152016-02-23 13:32:09 -08001661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 mCalled = true;
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001663
1664 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_START);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666
1667 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001668 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001669 * created with the attribute {@link android.R.attr#persistableMode} set to
1670 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001671 *
1672 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1673 * @param persistentState The data caming from the PersistableBundle first
1674 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1675 *
1676 * @see #onCreate
1677 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001678 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001679 @Nullable PersistableBundle persistentState) {
1680 onPostCreate(savedInstanceState);
1681 }
1682
1683 /**
RoboErik55011652014-07-09 15:05:53 -07001684 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1685 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001686 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 *
1688 * <p><em>Derived classes must call through to the super class's
1689 * implementation of this method. If they do not, an exception will be
1690 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001691 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 * @see #onCreate
1693 * @see #onStop
1694 * @see #onResume
1695 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001696 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001698 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001700
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001701 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001702
Ian Lake8a88cd62018-09-28 13:25:20 -07001703 dispatchActivityStarted();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001704
1705 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001706 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
1709
1710 /**
1711 * Called after {@link #onStop} when the current activity is being
1712 * re-displayed to the user (the user has navigated back to it). It will
1713 * be followed by {@link #onStart} and then {@link #onResume}.
1714 *
1715 * <p>For activities that are using raw {@link Cursor} objects (instead of
1716 * creating them through
1717 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1718 * this is usually the place
1719 * where the cursor should be requeried (because you had deactivated it in
1720 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 * <p><em>Derived classes must call through to the super class's
1723 * implementation of this method. If they do not, an exception will be
1724 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001725 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 * @see #onStop
1727 * @see #onStart
1728 * @see #onResume
1729 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001730 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 protected void onRestart() {
1732 mCalled = true;
1733 }
1734
1735 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001736 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1737 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1738 * to give the activity a hint that its state is no longer saved -- it will generally
1739 * be called after {@link #onSaveInstanceState} and prior to the activity being
1740 * resumed/started again.
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001741 *
1742 * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
1743 * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
1744 * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001745 */
Sergey Vasilinets0de0eca2018-08-23 19:28:57 -07001746 @Deprecated
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001747 public void onStateNotSaved() {
1748 }
1749
1750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1752 * {@link #onPause}, for your activity to start interacting with the user.
1753 * This is a good place to begin animations, open exclusive-access devices
1754 * (such as the camera), etc.
1755 *
1756 * <p>Keep in mind that onResume is not the best indicator that your activity
1757 * is visible to the user; a system window such as the keyguard may be in
1758 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1759 * activity is visible to the user (for example, to resume a game).
1760 *
1761 * <p><em>Derived classes must call through to the super class's
1762 * implementation of this method. If they do not, an exception will be
1763 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001764 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 * @see #onRestoreInstanceState
1766 * @see #onRestart
1767 * @see #onPostResume
1768 * @see #onPause
1769 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001770 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001772 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07001773 dispatchActivityResumed();
Sunny Goyal64643f62019-01-31 15:15:33 -08001774 mActivityTransitionState.onResume(this);
Felipe Lemece404982018-09-17 09:46:13 -07001775 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001776 if (mAutoFillResetNeeded) {
1777 if (!mAutoFillIgnoreFirstResumePause) {
1778 View focus = getCurrentFocus();
Ben Line55cd3e2018-12-07 20:26:37 +00001779 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
Dake Gu67decfa2017-12-27 11:48:08 -08001780 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1781 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1782 // window visibility after recreation is INVISIBLE in onResume() and next frame
1783 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1784 // So we cannot call View.notifyEnterOrExited() which will do nothing
1785 // when View.isVisibleToUser() is false.
1786 getAutofillManager().notifyViewEntered(focus);
1787 }
1788 }
1789 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08001790
1791 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);
1792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 mCalled = true;
1794 }
1795
1796 /**
1797 * Called when activity resume is complete (after {@link #onResume} has
1798 * been called). Applications will generally not implement this method;
1799 * it is intended for system classes to do final setup after application
1800 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 * <p><em>Derived classes must call through to the super class's
1803 * implementation of this method. If they do not, an exception will be
1804 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001805 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 * @see #onResume
1807 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001808 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 protected void onPostResume() {
1810 final Window win = getWindow();
1811 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001812 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 mCalled = true;
1814 }
1815
Andrii Kuliand70cdb92019-01-08 15:03:50 -08001816 /**
1817 * Called when activity gets or looses the top resumed position in the system.
1818 *
1819 * <p>Starting with {@link android.os.Build.VERSION_CODES#Q} multiple activities can be resumed
1820 * at the same time in multi-window and multi-display modes. This callback should be used
1821 * instead of {@link #onResume()} as an indication that the activity can try to open
1822 * exclusive-access devices like camera.</p>
1823 *
1824 * <p>It will always be delivered after the activity was resumed and before it is paused. In
1825 * some cases it might be skipped and activity can go straight from {@link #onResume()} to
1826 * {@link #onPause()} without receiving the top resumed state.</p>
1827 *
1828 * @param isTopResumedActivity {@code true} if it's the topmost resumed activity in the system,
1829 * {@code false} otherwise. A call with this as {@code true} will
1830 * always be followed by another one with {@code false}.
1831 *
1832 * @see #onResume()
1833 * @see #onPause()
1834 * @see #onWindowFocusChanged(boolean)
1835 */
1836 public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
1837 }
1838
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001839 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001840 if (mVoiceInteractor != null) {
1841 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1842 activeRequest.cancel();
1843 activeRequest.clear();
1844 }
1845 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001846 if (voiceInteractor == null) {
1847 mVoiceInteractor = null;
1848 } else {
1849 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1850 Looper.myLooper());
1851 }
1852 }
1853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001855 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001856 *
Phil Weaver846cda932017-06-15 10:10:06 -07001857 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001858 * will be unique.
1859 *
1860 * @return A ID that is unique in the activity
1861 *
1862 * {@hide}
1863 */
Felipe Leme42b97932018-02-20 13:04:31 -08001864 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001865 public int getNextAutofillId() {
1866 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1867 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001868 }
1869
Phil Weaver846cda932017-06-15 10:10:06 -07001870 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001871
Phil Weaver846cda932017-06-15 10:10:06 -07001872 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001873 }
1874
1875 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001876 * @hide
1877 */
1878 @Override
1879 public AutofillId autofillClientGetNextAutofillId() {
Felipe Leme305fd402018-09-11 18:20:42 +00001880 return new AutofillId(getNextAutofillId());
Felipe Leme42b97932018-02-20 13:04:31 -08001881 }
1882
1883 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001884 * Check whether this activity is running as part of a voice interaction with the user.
1885 * If true, it should perform its interaction with the user through the
1886 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1887 */
1888 public boolean isVoiceInteraction() {
1889 return mVoiceInteractor != null;
1890 }
1891
1892 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001893 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1894 * of a voice interaction. That is, returns true if this activity was directly
1895 * started by the voice interaction service as the initiation of a voice interaction.
1896 * Otherwise, for example if it was started by another activity while under voice
1897 * interaction, returns false.
1898 */
1899 public boolean isVoiceInteractionRoot() {
1900 try {
1901 return mVoiceInteractor != null
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001902 && ActivityTaskManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001903 } catch (RemoteException e) {
1904 }
1905 return false;
1906 }
1907
1908 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001909 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1910 * interact with this activity.
1911 */
1912 public VoiceInteractor getVoiceInteractor() {
1913 return mVoiceInteractor;
1914 }
1915
1916 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001917 * Queries whether the currently enabled voice interaction service supports returning
1918 * a voice interactor for use by the activity. This is valid only for the duration of the
1919 * activity.
1920 *
1921 * @return whether the current voice interaction service supports local voice interaction
1922 */
1923 public boolean isLocalVoiceInteractionSupported() {
1924 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001925 return ActivityTaskManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001926 } catch (RemoteException re) {
1927 }
1928 return false;
1929 }
1930
1931 /**
1932 * Starts a local voice interaction session. When ready,
1933 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1934 * to the registered voice interaction service.
1935 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1936 */
1937 public void startLocalVoiceInteraction(Bundle privateOptions) {
1938 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001939 ActivityTaskManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001940 } catch (RemoteException re) {
1941 }
1942 }
1943
1944 /**
1945 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1946 * voice interaction session being started. You can now retrieve a voice interactor using
1947 * {@link #getVoiceInteractor()}.
1948 */
1949 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001950 }
1951
1952 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001953 * Callback to indicate that the local voice interaction has stopped either
1954 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1955 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1956 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001957 */
1958 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001959 }
1960
1961 /**
1962 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001963 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1964 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001965 */
1966 public void stopLocalVoiceInteraction() {
1967 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001968 ActivityTaskManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001969 } catch (RemoteException re) {
1970 }
1971 }
1972
1973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 * This is called for activities that set launchMode to "singleTop" in
1975 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1976 * flag when calling {@link #startActivity}. In either case, when the
1977 * activity is re-launched while at the top of the activity stack instead
1978 * of a new instance of the activity being started, onNewIntent() will be
1979 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001980 * re-launch it.
1981 *
1982 * <p>An activity will always be paused before receiving a new intent, so
1983 * you can count on {@link #onResume} being called after this method.
1984 *
1985 * <p>Note that {@link #getIntent} still returns the original Intent. You
1986 * can use {@link #setIntent} to update it to this new Intent.
1987 *
1988 * @param intent The new intent that was started for the activity.
1989 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001991 * @see #setIntent
1992 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 */
1994 protected void onNewIntent(Intent intent) {
1995 }
1996
1997 /**
1998 * The hook for {@link ActivityThread} to save the state of this activity.
1999 *
2000 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2001 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2002 *
2003 * @param outState The bundle to save the state to.
2004 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002005 final void performSaveInstanceState(@NonNull Bundle outState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002006 dispatchActivityPreSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 onSaveInstanceState(outState);
2008 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07002009 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002010 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002011 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002012 dispatchActivityPostSaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
2014
2015 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002016 * The hook for {@link ActivityThread} to save the state of this activity.
2017 *
2018 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
2019 * and {@link #saveManagedDialogs(android.os.Bundle)}.
2020 *
2021 * @param outState The bundle to save the state to.
2022 * @param outPersistentState The bundle to save persistent state to.
2023 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002024 final void performSaveInstanceState(@NonNull Bundle outState,
2025 @NonNull PersistableBundle outPersistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07002026 dispatchActivityPreSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002027 onSaveInstanceState(outState, outPersistentState);
2028 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07002029 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002030 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
2031 ", " + outPersistentState);
Ian Lake8a88cd62018-09-28 13:25:20 -07002032 dispatchActivityPostSaveInstanceState(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07002033 }
2034
2035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 * Called to retrieve per-instance state from an activity before being killed
2037 * so that the state can be restored in {@link #onCreate} or
2038 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
2039 * will be passed to both).
2040 *
2041 * <p>This method is called before an activity may be killed so that when it
2042 * comes back some time in the future it can restore its state. For example,
2043 * if activity B is launched in front of activity A, and at some point activity
2044 * A is killed to reclaim resources, activity A will have a chance to save the
2045 * current state of its user interface via this method so that when the user
2046 * returns to activity A, the state of the user interface can be restored
2047 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
2048 *
2049 * <p>Do not confuse this method with activity lifecycle callbacks such as
2050 * {@link #onPause}, which is always called when an activity is being placed
2051 * in the background or on its way to destruction, or {@link #onStop} which
2052 * is called before destruction. One example of when {@link #onPause} and
2053 * {@link #onStop} is called and not this method is when a user navigates back
2054 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
2055 * on B because that particular instance will never be restored, so the
2056 * system avoids calling it. An example when {@link #onPause} is called and
2057 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
2058 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
2059 * killed during the lifetime of B since the state of the user interface of
2060 * A will stay intact.
2061 *
2062 * <p>The default implementation takes care of most of the UI per-instance
2063 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
2064 * view in the hierarchy that has an id, and by saving the id of the currently
2065 * focused view (all of which is restored by the default implementation of
2066 * {@link #onRestoreInstanceState}). If you override this method to save additional
2067 * information not captured by each individual view, you will likely want to
2068 * call through to the default implementation, otherwise be prepared to save
2069 * all of the state of each view yourself.
2070 *
Andrii Kulian391161f2018-01-29 10:50:02 -08002071 * <p>If called, this method will occur after {@link #onStop} for applications
2072 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
2073 * For applications targeting earlier platform versions this method will occur
2074 * before {@link #onStop} and there are no guarantees about whether it will
2075 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07002076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07002078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 * @see #onCreate
2080 * @see #onRestoreInstanceState
2081 * @see #onPause
2082 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002083 protected void onSaveInstanceState(@NonNull Bundle outState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002085
Phil Weaver846cda932017-06-15 10:10:06 -07002086 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002087 Parcelable p = mFragments.saveAllState();
2088 if (p != null) {
2089 outState.putParcelable(FRAGMENTS_TAG, p);
2090 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002091 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07002092 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
2093 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07002094 }
Ian Lake8a88cd62018-09-28 13:25:20 -07002095 dispatchActivitySaveInstanceState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097
2098 /**
Craig Mautnera0026042014-04-23 11:45:37 -07002099 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07002100 * created with the attribute {@link android.R.attr#persistableMode} set to
2101 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
2102 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
2103 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07002104 *
2105 * @param outState Bundle in which to place your saved state.
2106 * @param outPersistentState State which will be saved across reboots.
2107 *
2108 * @see #onSaveInstanceState(Bundle)
2109 * @see #onCreate
2110 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
2111 * @see #onPause
2112 */
Jake Wharton63f4d892018-07-10 12:27:59 -04002113 public void onSaveInstanceState(@NonNull Bundle outState,
2114 @NonNull PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07002115 onSaveInstanceState(outState);
2116 }
2117
2118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 * Save the state of any managed dialogs.
2120 *
2121 * @param outState place to store the saved state.
2122 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002123 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 private void saveManagedDialogs(Bundle outState) {
2125 if (mManagedDialogs == null) {
2126 return;
2127 }
2128
2129 final int numDialogs = mManagedDialogs.size();
2130 if (numDialogs == 0) {
2131 return;
2132 }
2133
2134 Bundle dialogState = new Bundle();
2135
2136 int[] ids = new int[mManagedDialogs.size()];
2137
2138 // save each dialog's bundle, gather the ids
2139 for (int i = 0; i < numDialogs; i++) {
2140 final int key = mManagedDialogs.keyAt(i);
2141 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002142 final ManagedDialog md = mManagedDialogs.valueAt(i);
2143 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
2144 if (md.mArgs != null) {
2145 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
2146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 }
2148
2149 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
2150 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
2151 }
2152
2153
2154 /**
2155 * Called as part of the activity lifecycle when an activity is going into
2156 * the background, but has not (yet) been killed. The counterpart to
2157 * {@link #onResume}.
2158 *
2159 * <p>When activity B is launched in front of activity A, this callback will
2160 * be invoked on A. B will not be created until A's {@link #onPause} returns,
2161 * so be sure to not do anything lengthy here.
2162 *
2163 * <p>This callback is mostly used for saving any persistent state the
2164 * activity is editing, to present a "edit in place" model to the user and
2165 * making sure nothing is lost if there are not enough resources to start
2166 * the new activity without first killing this one. This is also a good
2167 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08002168 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 * as fast as possible, or to close resources that are exclusive access
2170 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07002171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 * <p>In situations where the system needs more memory it may kill paused
2173 * processes to reclaim resources. Because of this, you should be sure
2174 * that all of your state is saved by the time you return from
2175 * this function. In general {@link #onSaveInstanceState} is used to save
2176 * per-instance state in the activity and this method is used to store
2177 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07002178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 * <p>After receiving this call you will usually receive a following call
2180 * to {@link #onStop} (after the next activity has been resumed and
2181 * displayed), however in some cases there will be a direct call back to
2182 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07002183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 * <p><em>Derived classes must call through to the super class's
2185 * implementation of this method. If they do not, an exception will be
2186 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 * @see #onResume
2189 * @see #onSaveInstanceState
2190 * @see #onStop
2191 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002192 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002194 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Ian Lake8a88cd62018-09-28 13:25:20 -07002195 dispatchActivityPaused();
Dake Gu67decfa2017-12-27 11:48:08 -08002196 if (mAutoFillResetNeeded) {
2197 if (!mAutoFillIgnoreFirstResumePause) {
2198 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
2199 View focus = getCurrentFocus();
2200 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
2201 getAutofillManager().notifyViewExited(focus);
2202 }
2203 } else {
2204 // reset after first pause()
2205 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
2206 mAutoFillIgnoreFirstResumePause = false;
2207 }
2208 }
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002209
2210 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_PAUSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 mCalled = true;
2212 }
2213
2214 /**
2215 * Called as part of the activity lifecycle when an activity is about to go
2216 * into the background as the result of user choice. For example, when the
2217 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
2218 * when an incoming phone call causes the in-call Activity to be automatically
2219 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
2220 * the activity being interrupted. In cases when it is invoked, this method
2221 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07002222 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 * <p>This callback and {@link #onUserInteraction} are intended to help
2224 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002225 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07002226 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 * @see #onUserInteraction()
Louis Changc2e193a2019-01-09 15:33:57 +08002228 * @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 */
2230 protected void onUserLeaveHint() {
2231 }
RoboErik55011652014-07-09 15:05:53 -07002232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002234 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002236 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002238 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
2240
2241 /**
2242 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002243 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07002245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 * <p>The default implementation returns null, which will cause you to
2247 * inherit the description from the previous activity. If all activities
2248 * return null, generally the label of the top activity will be used as the
2249 * description.
RoboErik55011652014-07-09 15:05:53 -07002250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 * @return A description of what the user is doing. It should be short and
2252 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07002253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08002255 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002257 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 public CharSequence onCreateDescription() {
2259 return null;
2260 }
2261
2262 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002263 * This is called when the user is requesting an assist, to build a full
2264 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
2265 * application. You can override this method to place into the bundle anything
2266 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002267 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002268 *
2269 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07002270 * been registered with {@link Application#registerOnProvideAssistDataListener
2271 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002272 */
2273 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002274 }
2275
2276 /**
2277 * This is called when the user is requesting an assist, to provide references
2278 * to content related to the current activity. Before being called, the
2279 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
2280 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
2281 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
2282 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
2283 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
2284 *
2285 * <p>Custom implementation may adjust the content intent to better reflect the top-level
2286 * context of the activity, and fill in its ClipData with additional content of
2287 * interest that the user is currently viewing. For example, an image gallery application
2288 * that has launched in to an activity allowing the user to swipe through pictures should
2289 * modify the intent to reference the current image they are looking it; such an
2290 * application when showing a list of pictures should add a ClipData that has
2291 * references to all of the pictures currently visible on screen.</p>
2292 *
2293 * @param outContent The assist content to return.
2294 */
2295 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002296 }
2297
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002298 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01002299 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002300 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
2301 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01002302 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002303 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002304 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002305 sendBroadcastAsUser(intent, Process.myUserHandle());
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01002306 }
2307
2308 /**
2309 * Dismiss the Keyboard Shortcuts screen.
2310 */
2311 public final void dismissKeyboardShortcutsHelper() {
2312 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01002313 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
Nicholas Sauer28348ed742019-02-15 11:58:32 -08002314 sendBroadcastAsUser(intent, Process.myUserHandle());
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01002315 }
2316
Clara Bayarri75e09792015-07-29 16:20:40 +01002317 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00002318 public void onProvideKeyboardShortcuts(
2319 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01002320 if (menu == null) {
2321 return;
2322 }
2323 KeyboardShortcutGroup group = null;
2324 int menuSize = menu.size();
2325 for (int i = 0; i < menuSize; ++i) {
2326 final MenuItem item = menu.getItem(i);
2327 final CharSequence title = item.getTitle();
2328 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01002329 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01002330 if (title != null && alphaShortcut != MIN_VALUE) {
2331 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00002332 final int resource = mApplication.getApplicationInfo().labelRes;
2333 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01002334 }
2335 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01002336 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01002337 }
2338 }
2339 if (group != null) {
2340 data.add(group);
2341 }
2342 }
2343
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002344 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07002345 * Ask to have the current assistant shown to the user. This only works if the calling
2346 * activity is the current foreground activity. It is the same as calling
2347 * {@link android.service.voice.VoiceInteractionService#showSession
2348 * VoiceInteractionService.showSession} and requesting all of the possible context.
2349 * The receiver will always see
2350 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
2351 * @return Returns true if the assistant was successfully invoked, else false. For example
2352 * false will be returned if the caller is not the current top activity.
2353 */
2354 public boolean showAssist(Bundle args) {
2355 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002356 return ActivityTaskManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07002357 } catch (RemoteException e) {
2358 }
2359 return false;
2360 }
2361
2362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 * Called when you are no longer visible to the user. You will next
2364 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
2365 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07002366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 * <p><em>Derived classes must call through to the super class's
2368 * implementation of this method. If they do not, an exception will be
2369 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 * @see #onRestart
2372 * @see #onResume
2373 * @see #onSaveInstanceState
2374 * @see #onDestroy
2375 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002376 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002378 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08002379 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07002380 mActivityTransitionState.onStop();
Ian Lake8a88cd62018-09-28 13:25:20 -07002381 dispatchActivityStopped();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002382 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07002384
Dake Gu67decfa2017-12-27 11:48:08 -08002385 if (mAutoFillResetNeeded) {
2386 getAutofillManager().onInvisibleForAutofill();
2387 }
2388
Felipe Lemec24a56a2017-08-03 14:27:57 -07002389 if (isFinishing()) {
2390 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08002391 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07002392 } else if (mIntent != null
2393 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
2394 // Activity was launched when user tapped a link in the Autofill Save UI - since
2395 // user launched another activity, the Save UI should not be restored when this
2396 // activity is finished.
2397 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
2398 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
2399 }
Felipe Lemebab851c2017-02-03 18:45:08 -08002400 }
chaviwfeb2e1e2018-12-19 17:24:11 -08002401 mEnterAnimationComplete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403
2404 /**
2405 * Perform any final cleanup before an activity is destroyed. This can
2406 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07002407 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 * this instance of the activity to save space. You can distinguish
2409 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07002410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 * <p><em>Note: do not count on this method being called as a place for
2412 * saving data! For example, if an activity is editing data in a content
2413 * provider, those edits should be committed in either {@link #onPause} or
2414 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
2415 * free resources like threads that are associated with an activity, so
2416 * that a destroyed activity does not leave such things around while the
2417 * rest of its application is still running. There are situations where
2418 * the system will simply kill the activity's hosting process without
2419 * calling this method (or any others) in it, so it should not be used to
2420 * do things that are intended to remain around after the process goes
2421 * away.
RoboErik55011652014-07-09 15:05:53 -07002422 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 * <p><em>Derived classes must call through to the super class's
2424 * implementation of this method. If they do not, an exception will be
2425 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07002426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 * @see #onPause
2428 * @see #onStop
2429 * @see #finish
2430 * @see #isFinishing
2431 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08002432 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002434 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 mCalled = true;
2436
2437 // dismiss any dialogs we are managing.
2438 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 final int numDialogs = mManagedDialogs.size();
2440 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002441 final ManagedDialog md = mManagedDialogs.valueAt(i);
2442 if (md.mDialog.isShowing()) {
2443 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
2445 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002446 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448
2449 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002450 synchronized (mManagedCursors) {
2451 int numCursors = mManagedCursors.size();
2452 for (int i = 0; i < numCursors; i++) {
2453 ManagedCursor c = mManagedCursors.get(i);
2454 if (c != null) {
2455 c.mCursor.close();
2456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002458 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 }
Amith Yamasani49860442010-03-17 20:54:10 -07002460
2461 // Close any open search dialog
2462 if (mSearchManager != null) {
2463 mSearchManager.stopSearch();
2464 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002465
Chris Banes21b25772016-01-04 20:41:59 +00002466 if (mActionBar != null) {
2467 mActionBar.onDestroy();
2468 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002469
Ian Lake8a88cd62018-09-28 13:25:20 -07002470 dispatchActivityDestroyed();
Felipe Lemeb0da18f2019-02-22 15:10:02 -08002471
2472 notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_STOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 }
2474
2475 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002476 * Report to the system that your app is now fully drawn, purely for diagnostic
2477 * purposes (calling it does not impact the visible behavior of the activity).
2478 * This is only used to help instrument application launch times, so that the
2479 * app can report when it is fully in a usable state; without this, the only thing
2480 * the system itself can determine is the point at which the activity's window
2481 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002482 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002483 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002484 * entirely drawn your UI and populated with all of the significant data. You
2485 * can safely call this method any time after first launch as well, in which case
2486 * it will simply be ignored.
2487 */
2488 public void reportFullyDrawn() {
2489 if (mDoReportFullyDrawn) {
2490 mDoReportFullyDrawn = false;
2491 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002492 ActivityTaskManager.getService().reportActivityFullyDrawn(
2493 mToken, mRestoredFromBundle);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002494 } catch (RemoteException e) {
2495 }
2496 }
2497 }
2498
2499 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002500 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002501 * visa-versa. This method provides the same configuration that will be sent in the following
2502 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2503 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002504 * @see android.R.attr#resizeableActivity
2505 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002506 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002507 * @param newConfig The new configuration of the activity with the state
2508 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002509 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002510 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2511 // Left deliberately empty. There should be no side effects if a direct
2512 // subclass of Activity does not call super.
2513 onMultiWindowModeChanged(isInMultiWindowMode);
2514 }
2515
2516 /**
2517 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2518 * visa-versa.
2519 *
2520 * @see android.R.attr#resizeableActivity
2521 *
2522 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2523 *
2524 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2525 */
2526 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002527 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002528 // Left deliberately empty. There should be no side effects if a direct
2529 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002530 }
2531
2532 /**
2533 * Returns true if the activity is currently in multi-window mode.
2534 * @see android.R.attr#resizeableActivity
2535 *
2536 * @return True if the activity is in multi-window mode.
2537 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002538 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002539 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002540 return ActivityTaskManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002541 } catch (RemoteException e) {
2542 }
2543 return false;
2544 }
2545
2546 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002547 * Called by the system when the activity changes to and from picture-in-picture mode. This
2548 * method provides the same configuration that will be sent in the following
2549 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2550 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002551 * @see android.R.attr#supportsPictureInPicture
2552 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002553 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002554 * @param newConfig The new configuration of the activity with the state
2555 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002556 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002557 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2558 Configuration newConfig) {
2559 // Left deliberately empty. There should be no side effects if a direct
2560 // subclass of Activity does not call super.
2561 onPictureInPictureModeChanged(isInPictureInPictureMode);
2562 }
2563
2564 /**
2565 * Called by the system when the activity changes to and from picture-in-picture mode.
2566 *
2567 * @see android.R.attr#supportsPictureInPicture
2568 *
2569 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2570 *
2571 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2572 */
2573 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002574 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002575 // Left deliberately empty. There should be no side effects if a direct
2576 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002577 }
2578
2579 /**
2580 * Returns true if the activity is currently in picture-in-picture mode.
2581 * @see android.R.attr#supportsPictureInPicture
2582 *
2583 * @return True if the activity is in picture-in-picture mode.
2584 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002585 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002586 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002587 return ActivityTaskManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002588 } catch (RemoteException e) {
2589 }
2590 return false;
2591 }
2592
2593 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002594 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002595 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2596 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002597 *
Winson Chung709904f2017-04-25 11:00:48 -07002598 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002599 * @see android.R.attr#supportsPictureInPicture
2600 */
Winson Chung709904f2017-04-25 11:00:48 -07002601 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002602 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002603 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002604 }
2605
Jeff Sharkey000ce802017-04-29 13:13:27 -06002606 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002607 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002608 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002609 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002610 }
2611
2612 /**
Winson Chung709904f2017-04-25 11:00:48 -07002613 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2614 * set parameters in {@param params} will be combined with the parameters from prior calls to
2615 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2616 *
2617 * The system may disallow entering picture-in-picture in various cases, including when the
2618 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2619 *
2620 * @see android.R.attr#supportsPictureInPicture
2621 * @see PictureInPictureParams
2622 *
2623 * @param params non-null parameters to be combined with previously set parameters when entering
2624 * picture-in-picture.
2625 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002626 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002627 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002628 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002629 */
2630 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2631 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002632 if (!deviceSupportsPictureInPictureMode()) {
2633 return false;
2634 }
Winson Chung709904f2017-04-25 11:00:48 -07002635 if (params == null) {
2636 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2637 }
Winson Chung298f95b2017-08-10 15:57:18 -07002638 if (!mCanEnterPictureInPicture) {
2639 throw new IllegalStateException("Activity must be resumed to enter"
2640 + " picture-in-picture");
2641 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002642 return ActivityTaskManager.getService().enterPictureInPictureMode(mToken, params);
Winson Chung709904f2017-04-25 11:00:48 -07002643 } catch (RemoteException e) {
2644 return false;
2645 }
2646 }
2647
Jeff Sharkey000ce802017-04-29 13:13:27 -06002648 /** @removed */
2649 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002650 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002651 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002652 }
2653
2654 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002655 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2656 * {@link #enterPictureInPictureMode()} is called.
2657 *
Winson Chung709904f2017-04-25 11:00:48 -07002658 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002659 */
Winson Chung709904f2017-04-25 11:00:48 -07002660 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002661 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002662 if (!deviceSupportsPictureInPictureMode()) {
2663 return;
2664 }
Winson Chung709904f2017-04-25 11:00:48 -07002665 if (params == null) {
2666 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002667 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002668 ActivityTaskManager.getService().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002669 } catch (RemoteException e) {
2670 }
2671 }
2672
Winson Chung8802eac2017-04-17 14:21:44 -07002673 /**
Winson Chung709904f2017-04-25 11:00:48 -07002674 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2675 * user interacts with the activity currently in picture-in-picture mode. This number may change
2676 * if the global configuration changes (ie. if the device is plugged into an external display),
2677 * but will always be larger than three.
2678 */
2679 public int getMaxNumPictureInPictureActions() {
2680 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002681 return ActivityTaskManager.getService().getMaxNumPictureInPictureActions(mToken);
Winson Chung709904f2017-04-25 11:00:48 -07002682 } catch (RemoteException e) {
2683 return 0;
2684 }
2685 }
2686
Winson Chung80fa2d82018-01-25 13:58:20 -08002687 /**
2688 * @return Whether this device supports picture-in-picture.
2689 */
2690 private boolean deviceSupportsPictureInPictureMode() {
2691 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2692 }
2693
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002694 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002695 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002696 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002697 }
2698
2699 /**
2700 * Called by the system when the activity is moved from one display to another without
2701 * recreation. This means that this activity is declared to handle all changes to configuration
2702 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002703 * again.
2704 *
2705 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2706 * applied configuration actually changed. It is up to app developer to choose whether to handle
2707 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2708 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002709 *
2710 * <p>Use this callback to track changes to the displays if some activity functionality relies
2711 * on an association with some display properties.
2712 *
2713 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002714 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002715 *
2716 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002717 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002718 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002719 */
Tiger Huangfc218452018-09-27 00:38:50 +08002720 @TestApi
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002721 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002722 }
2723
Winson Chungb5c41b72016-12-07 15:00:47 -08002724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 * Called by the system when the device configuration changes while your
2726 * activity is running. Note that this will <em>only</em> be called if
2727 * you have selected configurations you would like to handle with the
2728 * {@link android.R.attr#configChanges} attribute in your manifest. If
2729 * any configuration change occurs that is not selected to be reported
2730 * by that attribute, then instead of reporting it the system will stop
2731 * and restart the activity (to have it launched with the new
2732 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002733 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 * <p>At the time that this function has been called, your Resources
2735 * object will have been updated to return resource values matching the
2736 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002737 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 * @param newConfig The new device configuration.
2739 */
Ian Lake666a9652018-09-12 15:13:19 -07002740 public void onConfigurationChanged(@NonNull Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002741 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002743
Dianne Hackborn9d071802010-12-08 14:49:15 -08002744 mFragments.dispatchConfigurationChanged(newConfig);
2745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 if (mWindow != null) {
2747 // Pass the configuration changed event to the window
2748 mWindow.onConfigurationChanged(newConfig);
2749 }
Adam Powell45c0b192011-07-28 15:11:57 -07002750
2751 if (mActionBar != null) {
2752 // Do this last; the action bar will need to access
2753 // view changes from above.
2754 mActionBar.onConfigurationChanged(newConfig);
2755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
RoboErik55011652014-07-09 15:05:53 -07002757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 /**
2759 * If this activity is being destroyed because it can not handle a
2760 * configuration parameter being changed (and thus its
2761 * {@link #onConfigurationChanged(Configuration)} method is
2762 * <em>not</em> being called), then you can use this method to discover
2763 * the set of changes that have occurred while in the process of being
2764 * destroyed. Note that there is no guarantee that these will be
2765 * accurate (other changes could have happened at any time), so you should
2766 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002767 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 * @return Returns a bit field of the configuration parameters that are
2769 * changing, as defined by the {@link android.content.res.Configuration}
2770 * class.
2771 */
2772 public int getChangingConfigurations() {
2773 return mConfigChangeFlags;
2774 }
RoboErik55011652014-07-09 15:05:53 -07002775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 /**
2777 * Retrieve the non-configuration instance data that was previously
2778 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2779 * be available from the initial {@link #onCreate} and
2780 * {@link #onStart} calls to the new instance, allowing you to extract
2781 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002782 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 * <p>Note that the data you retrieve here should <em>only</em> be used
2784 * as an optimization for handling configuration changes. You should always
2785 * be able to handle getting a null pointer back, and an activity must
2786 * still be able to restore itself to its previous state (through the
2787 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2788 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002789 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002790 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002791 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002792 * available on older platforms through the Android support libraries.
2793 *
2794 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002796 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002798 return mLastNonConfigurationInstances != null
2799 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 }
RoboErik55011652014-07-09 15:05:53 -07002801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 /**
2803 * Called by the system, as part of destroying an
2804 * activity due to a configuration change, when it is known that a new
2805 * instance will immediately be created for the new configuration. You
2806 * can return any object you like here, including the activity instance
2807 * itself, which can later be retrieved by calling
2808 * {@link #getLastNonConfigurationInstance()} in the new activity
2809 * instance.
RoboErik55011652014-07-09 15:05:53 -07002810 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002811 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2812 * or later, consider instead using a {@link Fragment} with
2813 * {@link Fragment#setRetainInstance(boolean)
2814 * Fragment.setRetainInstance(boolean}.</em>
2815 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 * <p>This function is called purely as an optimization, and you must
2817 * not rely on it being called. When it is called, a number of guarantees
2818 * will be made to help optimize configuration switching:
2819 * <ul>
2820 * <li> The function will be called between {@link #onStop} and
2821 * {@link #onDestroy}.
2822 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002823 * created after this one's {@link #onDestroy()} is called. In particular,
2824 * <em>no</em> messages will be dispatched during this time (when the returned
2825 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 * <li> The object you return here will <em>always</em> be available from
2827 * the {@link #getLastNonConfigurationInstance()} method of the following
2828 * activity instance as described there.
2829 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002830 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 * <p>These guarantees are designed so that an activity can use this API
2832 * to propagate extensive state from the old to new activity instance, from
2833 * loaded bitmaps, to network connections, to evenly actively running
2834 * threads. Note that you should <em>not</em> propagate any data that
2835 * may change based on the configuration, including any data loaded from
2836 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002837 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002838 * <p>The guarantee of no message handling during the switch to the next
2839 * activity simplifies use with active objects. For example if your retained
2840 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2841 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2842 * not be called from the call here until you execute the next instance's
2843 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2844 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2845 * running in a separate thread.)
2846 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002847 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002848 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002849 * available on older platforms through the Android support libraries.
2850 *
2851 * @return any Object holding the desired state to propagate to the
2852 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 */
2854 public Object onRetainNonConfigurationInstance() {
2855 return null;
2856 }
RoboErik55011652014-07-09 15:05:53 -07002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 /**
2859 * Retrieve the non-configuration instance data that was previously
2860 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2861 * be available from the initial {@link #onCreate} and
2862 * {@link #onStart} calls to the new instance, allowing you to extract
2863 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 * <p>Note that the data you retrieve here should <em>only</em> be used
2866 * as an optimization for handling configuration changes. You should always
2867 * be able to handle getting a null pointer back, and an activity must
2868 * still be able to restore itself to its previous state (through the
2869 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2870 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002871 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 * @return Returns the object previously returned by
2873 * {@link #onRetainNonConfigurationChildInstances()}
2874 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002875 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002876 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2877 return mLastNonConfigurationInstances != null
2878 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
RoboErik55011652014-07-09 15:05:53 -07002880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 /**
2882 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2883 * it should return either a mapping from child activity id strings to arbitrary objects,
2884 * or null. This method is intended to be used by Activity framework subclasses that control a
2885 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2886 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2887 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002888 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2890 return null;
2891 }
RoboErik55011652014-07-09 15:05:53 -07002892
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002893 NonConfigurationInstances retainNonConfigurationInstances() {
2894 Object activity = onRetainNonConfigurationInstance();
2895 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002896 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002897
2898 // We're already stopped but we've been asked to retain.
2899 // Our fragments are taken care of but we need to mark the loaders for retention.
2900 // In order to do this correctly we need to restart the loaders first before
2901 // handing them off to the next activity.
2902 mFragments.doLoaderStart();
2903 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002904 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002905
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002906 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002907 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002908 return null;
2909 }
RoboErik55011652014-07-09 15:05:53 -07002910
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002911 NonConfigurationInstances nci = new NonConfigurationInstances();
2912 nci.activity = activity;
2913 nci.children = children;
2914 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002915 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002916 if (mVoiceInteractor != null) {
2917 mVoiceInteractor.retainInstance();
2918 nci.voiceInteractor = mVoiceInteractor;
2919 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002920 return nci;
2921 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002924 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002926 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002928
2929 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002930 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002931 mCalled = true;
2932 mFragments.dispatchTrimMemory(level);
2933 }
2934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002936 * Return the FragmentManager for interacting with fragments associated
2937 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08002938 *
2939 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002940 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002941 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002942 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002943 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002944 }
RoboErik55011652014-07-09 15:05:53 -07002945
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002946 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002947 * Called when a Fragment is being attached to this activity, immediately
2948 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2949 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08002950 *
2951 * @deprecated Use {@link
2952 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07002953 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002954 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07002955 public void onAttachFragment(Fragment fragment) {
2956 }
RoboErik55011652014-07-09 15:05:53 -07002957
Dianne Hackbornc8017682010-07-06 13:34:38 -07002958 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 * Wrapper around
2960 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2961 * that gives the resulting {@link Cursor} to call
2962 * {@link #startManagingCursor} so that the activity will manage its
2963 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002964 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002965 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2966 * or later, consider instead using {@link LoaderManager} instead, available
2967 * via {@link #getLoaderManager()}.</em>
2968 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002969 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2970 * this method, because the activity will do that for you at the appropriate time. However, if
2971 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2972 * not</em> automatically close the cursor and, in that case, you must call
2973 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 * @param uri The URI of the content provider to query.
2976 * @param projection List of columns to return.
2977 * @param selection SQL WHERE clause.
2978 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002979 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002981 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2983 * @see #startManagingCursor
2984 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002985 *
2986 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 */
Jason parks6ed50de2010-08-25 10:18:50 -05002988 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002989 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07002990 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2991 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2993 if (c != null) {
2994 startManagingCursor(c);
2995 }
2996 return c;
2997 }
2998
2999 /**
3000 * Wrapper around
3001 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
3002 * that gives the resulting {@link Cursor} to call
3003 * {@link #startManagingCursor} so that the activity will manage its
3004 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07003005 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003006 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3007 * or later, consider instead using {@link LoaderManager} instead, available
3008 * via {@link #getLoaderManager()}.</em>
3009 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003010 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
3011 * this method, because the activity will do that for you at the appropriate time. However, if
3012 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
3013 * not</em> automatically close the cursor and, in that case, you must call
3014 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 * @param uri The URI of the content provider to query.
3017 * @param projection List of columns to return.
3018 * @param selection SQL WHERE clause.
3019 * @param selectionArgs The arguments to selection, if any ?s are pesent
3020 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07003021 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07003023 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
3025 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05003026 *
3027 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 */
Jason parks6ed50de2010-08-25 10:18:50 -05003029 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07003030 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
3031 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
3033 if (c != null) {
3034 startManagingCursor(c);
3035 }
3036 return c;
3037 }
3038
3039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 * This method allows the activity to take care of managing the given
3041 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
3042 * That is, when the activity is stopped it will automatically call
3043 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
3044 * it will call {@link Cursor#requery} for you. When the activity is
3045 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07003046 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003047 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3048 * or later, consider instead using {@link LoaderManager} instead, available
3049 * via {@link #getLoaderManager()}.</em>
3050 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003051 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
3052 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
3053 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
3054 * <em>will not</em> automatically close the cursor and, in that case, you must call
3055 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07003058 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
3060 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003061 *
3062 * @deprecated Use the new {@link android.content.CursorLoader} class with
3063 * {@link LoaderManager} instead; this is also
3064 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 */
Jason parks6ed50de2010-08-25 10:18:50 -05003066 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 public void startManagingCursor(Cursor c) {
3068 synchronized (mManagedCursors) {
3069 mManagedCursors.add(new ManagedCursor(c));
3070 }
3071 }
3072
3073 /**
3074 * Given a Cursor that was previously given to
3075 * {@link #startManagingCursor}, stop the activity's management of that
3076 * cursor.
RoboErik55011652014-07-09 15:05:53 -07003077 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07003078 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07003079 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07003080 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07003081 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07003083 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003085 *
3086 * @deprecated Use the new {@link android.content.CursorLoader} class with
3087 * {@link LoaderManager} instead; this is also
3088 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 */
Jason parks6ed50de2010-08-25 10:18:50 -05003090 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 public void stopManagingCursor(Cursor c) {
3092 synchronized (mManagedCursors) {
3093 final int N = mManagedCursors.size();
3094 for (int i=0; i<N; i++) {
3095 ManagedCursor mc = mManagedCursors.get(i);
3096 if (mc.mCursor == c) {
3097 mManagedCursors.remove(i);
3098 break;
3099 }
3100 }
3101 }
3102 }
3103
3104 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003105 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
3106 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08003107 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07003109 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003110 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
3113
3114 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04003115 * Finds a view that was identified by the {@code android:id} XML attribute
3116 * that was processed in {@link #onCreate}.
3117 * <p>
3118 * <strong>Note:</strong> In most cases -- depending on compiler support --
3119 * the resulting view is automatically cast to the target class type. If
3120 * the target class type is unconstrained, an explicit cast may be
3121 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04003123 * @param id the ID to search for
3124 * @return a view with given ID if found, or {@code null} otherwise
3125 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08003126 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 */
Scott Kennedyc0519552015-02-11 15:33:10 -08003128 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04003129 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 return getWindow().findViewById(id);
3131 }
RoboErik55011652014-07-09 15:05:53 -07003132
Adam Powell33b97432010-04-20 10:01:14 -07003133 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08003134 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
3135 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
3136 * no matching view in the hierarchy.
3137 * <p>
3138 * <strong>Note:</strong> In most cases -- depending on compiler support --
3139 * the resulting view is automatically cast to the target class type. If
3140 * the target class type is unconstrained, an explicit cast may be
3141 * necessary.
3142 *
3143 * @param id the ID to search for
3144 * @return a view with given ID
3145 * @see View#requireViewById(int)
3146 * @see Activity#findViewById(int)
3147 */
3148 @NonNull
3149 public final <T extends View> T requireViewById(@IdRes int id) {
3150 T view = findViewById(id);
3151 if (view == null) {
3152 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
3153 }
3154 return view;
3155 }
3156
3157 /**
Adam Powell33b97432010-04-20 10:01:14 -07003158 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07003159 *
Adam Powell33b97432010-04-20 10:01:14 -07003160 * @return The Activity's ActionBar, or null if it does not have one.
3161 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003162 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07003163 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07003164 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07003165 return mActionBar;
3166 }
Adam Powelle43340c2014-03-17 19:10:43 -07003167
3168 /**
3169 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
3170 * Activity window.
3171 *
3172 * <p>When set to a non-null value the {@link #getActionBar()} method will return
3173 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
3174 * a traditional window decor action bar. The toolbar's menu will be populated with the
3175 * Activity's options menu and the navigation button will be wired through the standard
3176 * {@link android.R.id#home home} menu select action.</p>
3177 *
3178 * <p>In order to use a Toolbar within the Activity's window content the application
3179 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
3180 *
Chris Banesc7d6c322016-01-27 14:09:16 +00003181 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07003182 */
Adam Powell37780142014-06-01 13:31:00 -07003183 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00003184 final ActionBar ab = getActionBar();
3185 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07003186 throw new IllegalStateException("This Activity already has an action bar supplied " +
3187 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
3188 "android:windowActionBar to false in your theme to use a Toolbar instead.");
3189 }
Chris Banes21b25772016-01-04 20:41:59 +00003190
3191 // If we reach here then we're setting a new action bar
3192 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01003193 mMenuInflater = null;
3194
Chris Banes21b25772016-01-04 20:41:59 +00003195 // If we have an action bar currently, destroy it
3196 if (ab != null) {
3197 ab.onDestroy();
3198 }
3199
Chris Banesc7d6c322016-01-27 14:09:16 +00003200 if (toolbar != null) {
3201 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
3202 mActionBar = tbab;
3203 mWindow.setCallback(tbab.getWrappedWindowCallback());
3204 } else {
3205 mActionBar = null;
3206 // Re-set the original window callback since we may have already set a Toolbar wrapper
3207 mWindow.setCallback(this);
3208 }
3209
3210 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07003211 }
RoboErik55011652014-07-09 15:05:53 -07003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 /**
Adam Powell33b97432010-04-20 10:01:14 -07003214 * Creates a new ActionBar, locates the inflated ActionBarView,
3215 * initializes the ActionBar with the view, and sets mActionBar.
3216 */
Adam Powelle43340c2014-03-17 19:10:43 -07003217 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07003218 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07003219
3220 // Initializing the window decor can change window feature flags.
3221 // Make sure that we have the correct set before performing the test below.
3222 window.getDecorView();
3223
Adam Powell9b4c8042010-08-10 15:36:44 -07003224 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07003225 return;
3226 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07003227
Adam Powelle43340c2014-03-17 19:10:43 -07003228 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003229 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07003230
3231 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
3232 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07003233 }
RoboErik55011652014-07-09 15:05:53 -07003234
Adam Powell33b97432010-04-20 10:01:14 -07003235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 * Set the activity content from a layout resource. The resource will be
3237 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08003238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07003240 *
Romain Guy482b34a62011-01-20 10:59:28 -08003241 * @see #setContentView(android.view.View)
3242 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003244 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07003246 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
3248
3249 /**
3250 * Set the activity content to an explicit view. This view is placed
3251 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003252 * view hierarchy. When calling this method, the layout parameters of the
3253 * specified view are ignored. Both the width and the height of the view are
3254 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
3255 * your own layout parameters, invoke
3256 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
3257 * instead.
RoboErik55011652014-07-09 15:05:53 -07003258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08003260 *
3261 * @see #setContentView(int)
3262 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 */
3264 public void setContentView(View view) {
3265 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07003266 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
3268
3269 /**
3270 * Set the activity content to an explicit view. This view is placed
3271 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08003272 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07003273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 * @param view The desired content to display.
3275 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08003276 *
3277 * @see #setContentView(android.view.View)
3278 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 */
3280 public void setContentView(View view, ViewGroup.LayoutParams params) {
3281 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003282 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 }
3284
3285 /**
3286 * Add an additional content view to the activity. Added after any existing
3287 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07003288 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 * @param view The desired content to display.
3290 * @param params Layout parameters for the view.
3291 */
3292 public void addContentView(View view, ViewGroup.LayoutParams params) {
3293 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07003294 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
3296
3297 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08003298 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
3299 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3300 *
3301 * <p>This method will return non-null after content has been initialized (e.g. by using
3302 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
3303 *
3304 * @return This window's content TransitionManager or null if none is set.
3305 */
3306 public TransitionManager getContentTransitionManager() {
3307 return getWindow().getTransitionManager();
3308 }
3309
3310 /**
3311 * Set the {@link TransitionManager} to use for default transitions in this window.
3312 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3313 *
3314 * @param tm The TransitionManager to use for scene changes.
3315 */
3316 public void setContentTransitionManager(TransitionManager tm) {
3317 getWindow().setTransitionManager(tm);
3318 }
3319
3320 /**
3321 * Retrieve the {@link Scene} representing this window's current content.
3322 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
3323 *
3324 * <p>This method will return null if the current content is not represented by a Scene.</p>
3325 *
3326 * @return Current Scene being shown or null
3327 */
3328 public Scene getContentScene() {
3329 return getWindow().getContentScene();
3330 }
3331
3332 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003333 * Sets whether this activity is finished when touched outside its window's
3334 * bounds.
3335 */
3336 public void setFinishOnTouchOutside(boolean finish) {
3337 mWindow.setCloseOnTouchOutside(finish);
3338 }
Tor Norbyed9273d62013-05-30 15:59:53 -07003339
3340 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003341 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07003342 DEFAULT_KEYS_DISABLE,
3343 DEFAULT_KEYS_DIALER,
3344 DEFAULT_KEYS_SHORTCUT,
3345 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06003346 DEFAULT_KEYS_SEARCH_GLOBAL
3347 })
Tor Norbyed9273d62013-05-30 15:59:53 -07003348 @Retention(RetentionPolicy.SOURCE)
3349 @interface DefaultKeyMode {}
3350
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 * Use with {@link #setDefaultKeyMode} to turn off default handling of
3353 * keys.
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_DISABLE = 0;
3358 /**
3359 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
3360 * key handling.
RoboErik55011652014-07-09 15:05:53 -07003361 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 * @see #setDefaultKeyMode
3363 */
3364 static public final int DEFAULT_KEYS_DIALER = 1;
3365 /**
3366 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
3367 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07003368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07003370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 * @see #setDefaultKeyMode
3372 */
3373 static public final int DEFAULT_KEYS_SHORTCUT = 2;
3374 /**
3375 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3376 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07003377 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07003378 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 * @see #setDefaultKeyMode
3382 */
3383 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
3384
3385 /**
3386 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
3387 * will start a global search (typically web search, but some platforms may define alternate
3388 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07003389 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07003391 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 * @see #setDefaultKeyMode
3393 */
3394 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
3395
3396 /**
3397 * Select the default key handling for this activity. This controls what
3398 * will happen to key events that are not otherwise handled. The default
3399 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
3400 * floor. Other modes allow you to launch the dialer
3401 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
3402 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07003403 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07003405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 * <p>Note that the mode selected here does not impact the default
3407 * handling of system keys, such as the "back" and "menu" keys, and your
3408 * activity and its views always get a first chance to receive and handle
3409 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07003410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07003412 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 * @see #onKeyDown
3414 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003415 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07003417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 // Some modes use a SpannableStringBuilder to track & dispatch input events
3419 // This list must remain in sync with the switch in onKeyDown()
3420 switch (mode) {
3421 case DEFAULT_KEYS_DISABLE:
3422 case DEFAULT_KEYS_SHORTCUT:
3423 mDefaultKeySsb = null; // not used in these modes
3424 break;
3425 case DEFAULT_KEYS_DIALER:
3426 case DEFAULT_KEYS_SEARCH_LOCAL:
3427 case DEFAULT_KEYS_SEARCH_GLOBAL:
3428 mDefaultKeySsb = new SpannableStringBuilder();
3429 Selection.setSelection(mDefaultKeySsb,0);
3430 break;
3431 default:
3432 throw new IllegalArgumentException();
3433 }
3434 }
3435
3436 /**
3437 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003438 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 * is inside a TextView will not trigger the event (unless it is a navigation
3440 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 * <p>If the focused view didn't want this event, this method is called.
3443 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003444 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3445 * by calling {@link #onBackPressed()}, though the behavior varies based
3446 * on the application compatibility mode: for
3447 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3448 * it will set up the dispatch to call {@link #onKeyUp} where the action
3449 * will be performed; for earlier applications, it will perform the
3450 * action immediately in on-down, as those versions of the platform
3451 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003452 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003453 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003454 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003457 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 * this event and it should continue to be propagated.
3459 * @see #onKeyUp
3460 * @see android.view.KeyEvent
3461 */
3462 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003463 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003464 if (getApplicationInfo().targetSdkVersion
3465 >= Build.VERSION_CODES.ECLAIR) {
3466 event.startTracking();
3467 } else {
3468 onBackPressed();
3469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 return true;
3471 }
RoboErik55011652014-07-09 15:05:53 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3474 return false;
3475 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003476 Window w = getWindow();
3477 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3478 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3479 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003480 return true;
3481 }
3482 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003483 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3484 // Don't consume TAB here since it's used for navigation. Arrow keys
3485 // aren't considered "typing keys" so they already won't get consumed.
3486 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 } else {
3488 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3489 boolean clearSpannable = false;
3490 boolean handled;
3491 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3492 clearSpannable = true;
3493 handled = false;
3494 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003495 handled = TextKeyListener.getInstance().onKeyDown(
3496 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 if (handled && mDefaultKeySsb.length() > 0) {
3498 // something useable has been typed - dispatch it now.
3499
3500 final String str = mDefaultKeySsb.toString();
3501 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 switch (mDefaultKeyMode) {
3504 case DEFAULT_KEYS_DIALER:
3505 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3506 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003507 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 break;
3509 case DEFAULT_KEYS_SEARCH_LOCAL:
3510 startSearch(str, false, null, false);
3511 break;
3512 case DEFAULT_KEYS_SEARCH_GLOBAL:
3513 startSearch(str, false, null, true);
3514 break;
3515 }
3516 }
3517 }
3518 if (clearSpannable) {
3519 mDefaultKeySsb.clear();
3520 mDefaultKeySsb.clearSpans();
3521 Selection.setSelection(mDefaultKeySsb,0);
3522 }
3523 return handled;
3524 }
3525 }
3526
3527 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003528 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3529 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3530 * the event).
Siarhei Vishniakouccf973f2018-10-30 14:04:57 -07003531 *
3532 * To receive this callback, you must return true from onKeyDown for the current
3533 * event stream.
3534 *
3535 * @see KeyEvent.Callback#onKeyLongPress()
3536 * @see KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003537 */
3538 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3539 return false;
3540 }
3541
3542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003544 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 * is inside a TextView will not trigger the event (unless it is a navigation
3546 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003547 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003548 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3549 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003552 * further, or <code>false</code> to indicate that you have not handled
3553 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 * @see #onKeyDown
3555 * @see KeyEvent
3556 */
3557 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003558 if (getApplicationInfo().targetSdkVersion
3559 >= Build.VERSION_CODES.ECLAIR) {
3560 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3561 && !event.isCanceled()) {
3562 onBackPressed();
3563 return true;
3564 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 return false;
3567 }
3568
3569 /**
3570 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3571 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3572 * the event).
3573 */
3574 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3575 return false;
3576 }
RoboErik55011652014-07-09 15:05:53 -07003577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003579 * Called when the activity has detected the user's press of the back
3580 * key. The default implementation simply finishes the current activity,
3581 * but you can override this to do whatever you want.
3582 */
3583 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003584 if (mActionBar != null && mActionBar.collapseActionView()) {
3585 return;
3586 }
3587
George Mount27b0dc02017-02-21 10:24:09 -08003588 FragmentManager fragmentManager = mFragments.getFragmentManager();
3589
3590 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003591 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003592 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003593 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003594
3595 /**
3596 * Called when a key shortcut event is not handled by any of the views in the Activity.
3597 * Override this method to implement global key shortcuts for the Activity.
3598 * Key shortcuts can also be implemented by setting the
3599 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3600 *
3601 * @param keyCode The value in event.getKeyCode().
3602 * @param event Description of the key event.
3603 * @return True if the key shortcut was handled.
3604 */
3605 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003606 // Let the Action Bar have a chance at handling the shortcut.
3607 ActionBar actionBar = getActionBar();
3608 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003609 }
3610
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 * Called when a touch screen event was not handled by any of the views
3613 * under it. This is most useful to process touch events that happen
3614 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003615 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 * @return Return true if you have consumed the event, false if you haven't.
3619 * The default implementation always returns false.
3620 */
3621 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003622 if (mWindow.shouldCloseOnTouch(this, event)) {
3623 finish();
3624 return true;
3625 }
RoboErik55011652014-07-09 15:05:53 -07003626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 return false;
3628 }
RoboErik55011652014-07-09 15:05:53 -07003629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 /**
3631 * Called when the trackball was moved and not handled by any of the
3632 * views inside of the activity. So, for example, if the trackball moves
3633 * while focus is on a button, you will receive a call here because
3634 * buttons do not normally do anything with trackball events. The call
3635 * here happens <em>before</em> trackball movements are converted to
3636 * DPAD key events, which then get sent back to the view hierarchy, and
3637 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 * @return Return true if you have consumed the event, false if you haven't.
3642 * The default implementation always returns false.
3643 */
3644 public boolean onTrackballEvent(MotionEvent event) {
3645 return false;
3646 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003647
3648 /**
3649 * Called when a generic motion event was not handled by any of the
3650 * views inside of the activity.
3651 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003652 * Generic motion events describe joystick movements, mouse hovers, track pad
3653 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003654 * {@link MotionEvent#getSource() source} of the motion event specifies
3655 * the class of input that was received. Implementations of this method
3656 * must examine the bits in the source before processing the event.
3657 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003658 * </p><p>
3659 * Generic motion events with source class
3660 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3661 * are delivered to the view under the pointer. All other generic motion events are
3662 * delivered to the focused view.
3663 * </p><p>
3664 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3665 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003666 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003667 *
3668 * @param event The generic motion event being processed.
3669 *
3670 * @return Return true if you have consumed the event, false if you haven't.
3671 * The default implementation always returns false.
3672 */
3673 public boolean onGenericMotionEvent(MotionEvent event) {
3674 return false;
3675 }
3676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 /**
3678 * Called whenever a key, touch, or trackball event is dispatched to the
3679 * activity. Implement this method if you wish to know that the user has
3680 * interacted with the device in some way while your activity is running.
3681 * This callback and {@link #onUserLeaveHint} are intended to help
3682 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003683 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3686 * be accompanied by calls to {@link #onUserInteraction}. This
3687 * ensures that your activity will be told of relevant user activity such
3688 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003689 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 * <p>Note that this callback will be invoked for the touch down action
3691 * that begins a touch gesture, but may not be invoked for the touch-moved
3692 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 * @see #onUserLeaveHint()
3695 */
3696 public void onUserInteraction() {
3697 }
RoboErik55011652014-07-09 15:05:53 -07003698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3700 // Update window manager if: we have a view, that view is
3701 // attached to its parent (which will be a RootView), and
3702 // this activity is not embedded.
3703 if (mParent == null) {
3704 View decor = mDecor;
3705 if (decor != null && decor.getParent() != null) {
3706 getWindowManager().updateViewLayout(decor, params);
3707 }
3708 }
3709 }
3710
3711 public void onContentChanged() {
3712 }
3713
3714 /**
3715 * Called when the current {@link Window} of the activity gains or loses
3716 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003717 * to the user. The default implementation clears the key tracking
3718 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003719 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003720 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 * is managed independently of activity lifecycles. As such, while focus
3722 * changes will generally have some relation to lifecycle changes (an
3723 * activity that is stopped will not generally get window focus), you
3724 * should not rely on any particular order between the callbacks here and
3725 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003726 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 * <p>As a general rule, however, a resumed activity will have window
3728 * focus... unless it has displayed other dialogs or popups that take
3729 * input focus, in which case the activity itself will not have focus
3730 * when the other windows have it. Likewise, the system may display
3731 * system-level windows (such as the status bar notification panel or
3732 * a system alert) which will temporarily take window input focus without
3733 * pausing the foreground activity.
3734 *
3735 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 * @see #hasWindowFocus()
3738 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003739 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 */
3741 public void onWindowFocusChanged(boolean hasFocus) {
3742 }
RoboErik55011652014-07-09 15:05:53 -07003743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003745 * Called when the main window associated with the activity has been
3746 * attached to the window manager.
3747 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3748 * for more information.
3749 * @see View#onAttachedToWindow
3750 */
3751 public void onAttachedToWindow() {
3752 }
RoboErik55011652014-07-09 15:05:53 -07003753
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003754 /**
3755 * Called when the main window associated with the activity has been
3756 * detached from the window manager.
3757 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3758 * for more information.
3759 * @see View#onDetachedFromWindow
3760 */
3761 public void onDetachedFromWindow() {
3762 }
RoboErik55011652014-07-09 15:05:53 -07003763
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 * Returns true if this activity's <em>main</em> window currently has window focus.
3766 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003767 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3771 */
3772 public boolean hasWindowFocus() {
3773 Window w = getWindow();
3774 if (w != null) {
3775 View d = w.getDecorView();
3776 if (d != null) {
3777 return d.hasWindowFocus();
3778 }
3779 }
3780 return false;
3781 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003782
3783 /**
3784 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003785 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003786 */
Adam Powell117b6952014-05-05 18:14:56 -07003787 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003788 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003789 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003790 if (suppressWindowTransition) {
3791 overridePendingTransition(0, 0);
3792 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003793 }
RoboErik55011652014-07-09 15:05:53 -07003794
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003795
Skuhnece2faa52015-08-11 10:36:38 -07003796 /**
Yunfan Chend967af82019-01-17 18:30:18 +09003797 * Moves the activity between {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode
3798 * and {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003799 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003800 * @hide
3801 */
3802 @Override
Yunfan Chend967af82019-01-17 18:30:18 +09003803 public void toggleFreeformWindowingMode() throws RemoteException {
3804 ActivityTaskManager.getService().toggleFreeformWindowingMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003805 }
3806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003808 * Puts the activity in picture-in-picture mode if the activity supports.
3809 * @see android.R.attr#supportsPictureInPicture
3810 * @hide
3811 */
3812 @Override
3813 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003814 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003815 enterPictureInPictureMode();
3816 }
3817 }
3818
3819 /**
RoboErik55011652014-07-09 15:05:53 -07003820 * Called to process key events. You can override this to intercept all
3821 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003825 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 * @return boolean Return true if this event was consumed.
3827 */
3828 public boolean dispatchKeyEvent(KeyEvent event) {
3829 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003830
3831 // Let action bars open menus in response to the menu key prioritized over
3832 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003833 final int keyCode = event.getKeyCode();
3834 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003835 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3836 return true;
3837 }
3838
Dianne Hackborn8d374262009-09-14 21:21:52 -07003839 Window win = getWindow();
3840 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 return true;
3842 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003843 View decor = mDecor;
3844 if (decor == null) decor = win.getDecorView();
3845 return event.dispatch(this, decor != null
3846 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 }
3848
3849 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003850 * Called to process a key shortcut event.
3851 * You can override this to intercept all key shortcut events before they are
3852 * dispatched to the window. Be sure to call this implementation for key shortcut
3853 * events that should be handled normally.
3854 *
3855 * @param event The key shortcut event.
3856 * @return True if this event was consumed.
3857 */
3858 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3859 onUserInteraction();
3860 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3861 return true;
3862 }
3863 return onKeyShortcut(event.getKeyCode(), event);
3864 }
3865
3866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 * Called to process touch screen events. You can override this to
3868 * intercept all touch screen events before they are dispatched to the
3869 * window. Be sure to call this implementation for touch screen events
3870 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003871 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003873 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 * @return boolean Return true if this event was consumed.
3875 */
3876 public boolean dispatchTouchEvent(MotionEvent ev) {
3877 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3878 onUserInteraction();
3879 }
3880 if (getWindow().superDispatchTouchEvent(ev)) {
3881 return true;
3882 }
3883 return onTouchEvent(ev);
3884 }
RoboErik55011652014-07-09 15:05:53 -07003885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 /**
3887 * Called to process trackball events. You can override this to
3888 * intercept all trackball events before they are dispatched to the
3889 * window. Be sure to call this implementation for trackball events
3890 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003891 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003893 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 * @return boolean Return true if this event was consumed.
3895 */
3896 public boolean dispatchTrackballEvent(MotionEvent ev) {
3897 onUserInteraction();
3898 if (getWindow().superDispatchTrackballEvent(ev)) {
3899 return true;
3900 }
3901 return onTrackballEvent(ev);
3902 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003903
Jeff Browncb1404e2011-01-15 18:14:15 -08003904 /**
3905 * Called to process generic motion events. You can override this to
3906 * intercept all generic motion events before they are dispatched to the
3907 * window. Be sure to call this implementation for generic motion events
3908 * that should be handled normally.
3909 *
3910 * @param ev The generic motion event.
3911 *
3912 * @return boolean Return true if this event was consumed.
3913 */
3914 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3915 onUserInteraction();
3916 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3917 return true;
3918 }
3919 return onGenericMotionEvent(ev);
3920 }
3921
svetoslavganov75986cf2009-05-14 22:28:01 -07003922 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3923 event.setClassName(getClass().getName());
3924 event.setPackageName(getPackageName());
3925
3926 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003927 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3928 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003929 event.setFullScreen(isFullScreen);
3930
3931 CharSequence title = getTitle();
3932 if (!TextUtils.isEmpty(title)) {
3933 event.getText().add(title);
3934 }
3935
3936 return true;
3937 }
3938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 /**
3940 * Default implementation of
3941 * {@link android.view.Window.Callback#onCreatePanelView}
3942 * for activities. This
3943 * simply returns null so that all panel sub-windows will have the default
3944 * menu behavior.
3945 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003946 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 public View onCreatePanelView(int featureId) {
3948 return null;
3949 }
3950
3951 /**
3952 * Default implementation of
3953 * {@link android.view.Window.Callback#onCreatePanelMenu}
3954 * for activities. This calls through to the new
3955 * {@link #onCreateOptionsMenu} method for the
3956 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3957 * so that subclasses of Activity don't need to deal with feature codes.
3958 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003959 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003961 boolean show = onCreateOptionsMenu(menu);
3962 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3963 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 }
3965 return false;
3966 }
3967
3968 /**
3969 * Default implementation of
3970 * {@link android.view.Window.Callback#onPreparePanel}
3971 * for activities. This
3972 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3973 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3974 * panel, so that subclasses of
3975 * Activity don't need to deal with feature codes.
3976 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003977 public boolean onPreparePanel(int featureId, @Nullable View view, @NonNull Menu menu) {
3978 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003980 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003981 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 }
3983 return true;
3984 }
3985
3986 /**
3987 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003988 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 * @return The default implementation returns true.
3990 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07003991 @Override
3992 public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003993 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003994 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003995 if (mActionBar != null) {
3996 mActionBar.dispatchMenuVisibilityChanged(true);
3997 } else {
3998 Log.e(TAG, "Tried to open action bar menu with no action bar");
3999 }
Adam Powell8515ee82010-11-30 14:09:55 -08004000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 return true;
4002 }
4003
4004 /**
4005 * Default implementation of
4006 * {@link android.view.Window.Callback#onMenuItemSelected}
4007 * for activities. This calls through to the new
4008 * {@link #onOptionsItemSelected} method for the
4009 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
4010 * panel, so that subclasses of
4011 * Activity don't need to deal with feature codes.
4012 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004013 public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09004014 CharSequence titleCondensed = item.getTitleCondensed();
4015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 switch (featureId) {
4017 case Window.FEATURE_OPTIONS_PANEL:
4018 // Put event logging here so it gets called even if subclass
4019 // doesn't call through to superclass's implmeentation of each
4020 // of these methods below
zobject686a8052012-12-14 21:11:08 +09004021 if(titleCondensed != null) {
4022 EventLog.writeEvent(50000, 0, titleCondensed.toString());
4023 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004024 if (onOptionsItemSelected(item)) {
4025 return true;
4026 }
Adam Powell04d58112012-04-09 10:22:12 -07004027 if (mFragments.dispatchOptionsItemSelected(item)) {
4028 return true;
4029 }
4030 if (item.getItemId() == android.R.id.home && mActionBar != null &&
4031 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
4032 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07004033 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07004034 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07004035 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07004036 }
Adam Powell04d58112012-04-09 10:22:12 -07004037 }
4038 return false;
RoboErik55011652014-07-09 15:05:53 -07004039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09004041 if(titleCondensed != null) {
4042 EventLog.writeEvent(50000, 1, titleCondensed.toString());
4043 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07004044 if (onContextItemSelected(item)) {
4045 return true;
4046 }
4047 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08004048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 default:
4050 return false;
4051 }
4052 }
RoboErik55011652014-07-09 15:05:53 -07004053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 /**
4055 * Default implementation of
4056 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
4057 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
4058 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
4059 * so that subclasses of Activity don't need to deal with feature codes.
4060 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
4061 * {@link #onContextMenuClosed(Menu)} will be called.
4062 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004063 public void onPanelClosed(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 switch (featureId) {
4065 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004066 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 onOptionsMenuClosed(menu);
4068 break;
RoboErik55011652014-07-09 15:05:53 -07004069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 case Window.FEATURE_CONTEXT_MENU:
4071 onContextMenuClosed(menu);
4072 break;
Adam Powell8515ee82010-11-30 14:09:55 -08004073
4074 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07004075 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08004076 mActionBar.dispatchMenuVisibilityChanged(false);
4077 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
4079 }
4080
4081 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004082 * Declare that the options menu has changed, so should be recreated.
4083 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
4084 * time it needs to be displayed.
4085 */
4086 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004087 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4088 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004089 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
4090 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004091 }
RoboErik55011652014-07-09 15:05:53 -07004092
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07004093 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 * Initialize the contents of the Activity's standard options menu. You
4095 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07004096 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 * <p>This is only called once, the first time the options menu is
4098 * displayed. To update the menu every time it is displayed, see
4099 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07004100 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07004102 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
4103 * they will be correctly ordered with application-defined menu items.
4104 * Deriving classes should always call through to the base implementation.
4105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 * <p>You can safely hold on to <var>menu</var> (and any items created
4107 * from it), making modifications to it as desired, until the next
4108 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07004109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 * <p>When you add items to the menu, you can implement the Activity's
4111 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07004112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07004114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 * @return You must return true for the menu to be displayed;
4116 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 * @see #onPrepareOptionsMenu
4119 * @see #onOptionsItemSelected
4120 */
4121 public boolean onCreateOptionsMenu(Menu menu) {
4122 if (mParent != null) {
4123 return mParent.onCreateOptionsMenu(menu);
4124 }
4125 return true;
4126 }
4127
4128 /**
4129 * Prepare the Screen's standard options menu to be displayed. This is
4130 * called right before the menu is shown, every time it is shown. You can
4131 * use this method to efficiently enable/disable items or otherwise
4132 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07004133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * <p>The default implementation updates the system menu items based on the
4135 * activity's state. Deriving classes should always call through to the
4136 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07004137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 * @param menu The options menu as last shown or first initialized by
4139 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07004140 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 * @return You must return true for the menu to be displayed;
4142 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07004143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 * @see #onCreateOptionsMenu
4145 */
4146 public boolean onPrepareOptionsMenu(Menu menu) {
4147 if (mParent != null) {
4148 return mParent.onPrepareOptionsMenu(menu);
4149 }
4150 return true;
4151 }
4152
4153 /**
4154 * This hook is called whenever an item in your options menu is selected.
4155 * The default implementation simply returns false to have the normal
4156 * processing happen (calling the item's Runnable or sending a message to
4157 * its Handler as appropriate). You can use this method for any items
4158 * for which you would like to do processing without those other
4159 * facilities.
RoboErik55011652014-07-09 15:05:53 -07004160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07004162 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07004163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07004165 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 * @return boolean Return false to allow normal menu processing to
4167 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07004168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 * @see #onCreateOptionsMenu
4170 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004171 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 if (mParent != null) {
4173 return mParent.onOptionsItemSelected(item);
4174 }
4175 return false;
4176 }
4177
4178 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07004179 * This method is called whenever the user chooses to navigate Up within your application's
4180 * activity hierarchy from the action bar.
4181 *
4182 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
4183 * was specified in the manifest for this activity or an activity-alias to it,
4184 * default Up navigation will be handled automatically. If any activity
4185 * along the parent chain requires extra Intent arguments, the Activity subclass
4186 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
4187 * to supply those arguments.</p>
4188 *
Mark Lufa434852016-08-11 17:40:33 -07004189 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07004190 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
4191 * from the design guide for more information about navigating within your app.</p>
4192 *
4193 * <p>See the {@link TaskStackBuilder} class and the Activity methods
4194 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
4195 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
4196 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
4197 *
4198 * @return true if Up navigation completed successfully and this Activity was finished,
4199 * false otherwise.
4200 */
4201 public boolean onNavigateUp() {
4202 // Automatically handle hierarchical Up navigation if the proper
4203 // metadata is available.
4204 Intent upIntent = getParentActivityIntent();
4205 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07004206 if (mActivityInfo.taskAffinity == null) {
4207 // Activities with a null affinity are special; they really shouldn't
4208 // specify a parent activity intent in the first place. Just finish
4209 // the current activity and call it a day.
4210 finish();
4211 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07004212 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07004213 onCreateNavigateUpTaskStack(b);
4214 onPrepareNavigateUpTaskStack(b);
4215 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07004216
4217 // We can't finishAffinity if we have a result.
4218 // Fall back and simply finish the current activity instead.
4219 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4220 // Tell the developer what's going on to avoid hair-pulling.
4221 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
4222 finish();
4223 } else {
4224 finishAffinity();
4225 }
Adam Powelldd8fab22012-03-22 17:47:27 -07004226 } else {
4227 navigateUpTo(upIntent);
4228 }
4229 return true;
4230 }
4231 return false;
4232 }
4233
4234 /**
4235 * This is called when a child activity of this one attempts to navigate up.
4236 * The default implementation simply calls onNavigateUp() on this activity (the parent).
4237 *
4238 * @param child The activity making the call.
4239 */
4240 public boolean onNavigateUpFromChild(Activity child) {
4241 return onNavigateUp();
4242 }
4243
4244 /**
4245 * Define the synthetic task stack that will be generated during Up navigation from
4246 * a different task.
4247 *
4248 * <p>The default implementation of this method adds the parent chain of this activity
4249 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
4250 * may choose to override this method to construct the desired task stack in a different
4251 * way.</p>
4252 *
Adam Powellf0195952012-05-02 21:38:54 -07004253 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
4254 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
4255 * returned by {@link #getParentActivityIntent()}.</p>
4256 *
Adam Powelldd8fab22012-03-22 17:47:27 -07004257 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
4258 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
4259 *
4260 * @param builder An empty TaskStackBuilder - the application should add intents representing
4261 * the desired task stack
4262 */
4263 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
4264 builder.addParentStack(this);
4265 }
4266
4267 /**
4268 * Prepare the synthetic task stack that will be generated during Up navigation
4269 * from a different task.
4270 *
4271 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
4272 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
4273 * If any extra data should be added to these intents before launching the new task,
4274 * the application should override this method and add that data here.</p>
4275 *
4276 * @param builder A TaskStackBuilder that has been populated with Intents by
4277 * onCreateNavigateUpTaskStack.
4278 */
4279 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
4280 }
4281
4282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 * This hook is called whenever the options menu is being closed (either by the user canceling
4284 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07004285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 * @param menu The options menu as last shown or first initialized by
4287 * onCreateOptionsMenu().
4288 */
4289 public void onOptionsMenuClosed(Menu menu) {
4290 if (mParent != null) {
4291 mParent.onOptionsMenuClosed(menu);
4292 }
4293 }
RoboErik55011652014-07-09 15:05:53 -07004294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 /**
4296 * Programmatically opens the options menu. If the options menu is already
4297 * open, this method does nothing.
4298 */
4299 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08004300 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4301 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07004302 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
4303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 }
RoboErik55011652014-07-09 15:05:53 -07004305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 /**
4307 * Progammatically closes the options menu. If the options menu is already
4308 * closed, this method does nothing.
4309 */
4310 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07004311 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
4312 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08004313 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
4314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 }
4316
4317 /**
4318 * Called when a context menu for the {@code view} is about to be shown.
4319 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
4320 * time the context menu is about to be shown and should be populated for
4321 * the view (or item inside the view for {@link AdapterView} subclasses,
4322 * this can be found in the {@code menuInfo})).
4323 * <p>
4324 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
4325 * item has been selected.
4326 * <p>
4327 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08004328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 */
4330 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
4331 }
4332
4333 /**
4334 * Registers a context menu to be shown for the given view (multiple views
4335 * can show the context menu). This method will set the
4336 * {@link OnCreateContextMenuListener} on the view to this activity, so
4337 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
4338 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07004339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 * @see #unregisterForContextMenu(View)
4341 * @param view The view that should show a context menu.
4342 */
4343 public void registerForContextMenu(View view) {
4344 view.setOnCreateContextMenuListener(this);
4345 }
RoboErik55011652014-07-09 15:05:53 -07004346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 /**
4348 * Prevents a context menu to be shown for the given view. This method will remove the
4349 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07004350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 * @see #registerForContextMenu(View)
4352 * @param view The view that should stop showing a context menu.
4353 */
4354 public void unregisterForContextMenu(View view) {
4355 view.setOnCreateContextMenuListener(null);
4356 }
RoboErik55011652014-07-09 15:05:53 -07004357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 /**
4359 * Programmatically opens the context menu for a particular {@code view}.
4360 * The {@code view} should have been added via
4361 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07004362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 * @param view The view to show the context menu for.
4364 */
4365 public void openContextMenu(View view) {
4366 view.showContextMenu();
4367 }
RoboErik55011652014-07-09 15:05:53 -07004368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 /**
4370 * Programmatically closes the most recently opened context menu, if showing.
4371 */
4372 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08004373 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
4374 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
4375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
RoboErik55011652014-07-09 15:05:53 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 /**
4379 * This hook is called whenever an item in a context menu is selected. The
4380 * default implementation simply returns false to have the normal processing
4381 * happen (calling the item's Runnable or sending a message to its Handler
4382 * as appropriate). You can use this method for any items for which you
4383 * would like to do processing without those other facilities.
4384 * <p>
4385 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
4386 * View that added this menu item.
4387 * <p>
4388 * Derived classes should call through to the base class for it to perform
4389 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07004390 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 * @param item The context menu item that was selected.
4392 * @return boolean Return false to allow normal context menu processing to
4393 * proceed, true to consume it here.
4394 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004395 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 if (mParent != null) {
4397 return mParent.onContextItemSelected(item);
4398 }
4399 return false;
4400 }
4401
4402 /**
4403 * This hook is called whenever the context menu is being closed (either by
4404 * the user canceling the menu with the back/menu button, or when an item is
4405 * selected).
RoboErik55011652014-07-09 15:05:53 -07004406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 * @param menu The context menu that is being closed.
4408 */
Ian Lake0b71b8e2018-09-13 13:56:26 -07004409 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 if (mParent != null) {
4411 mParent.onContextMenuClosed(menu);
4412 }
4413 }
4414
4415 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004416 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004418 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 protected Dialog onCreateDialog(int id) {
4420 return null;
4421 }
4422
4423 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004424 * Callback for creating dialogs that are managed (saved and restored) for you
4425 * by the activity. The default implementation calls through to
4426 * {@link #onCreateDialog(int)} for compatibility.
4427 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004428 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4429 * or later, consider instead using a {@link DialogFragment} instead.</em>
4430 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004431 * <p>If you use {@link #showDialog(int)}, the activity will call through to
4432 * this method the first time, and hang onto it thereafter. Any dialog
4433 * that is created by this method will automatically be saved and restored
4434 * for you, including whether it is showing.
4435 *
4436 * <p>If you would like the activity to manage saving and restoring dialogs
4437 * for you, you should override this method and handle any ids that are
4438 * passed to {@link #showDialog}.
4439 *
4440 * <p>If you would like an opportunity to prepare your dialog before it is shown,
4441 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
4442 *
4443 * @param id The id of the dialog.
4444 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4445 * @return The dialog. If you return null, the dialog will not be created.
4446 *
4447 * @see #onPrepareDialog(int, Dialog, Bundle)
4448 * @see #showDialog(int, Bundle)
4449 * @see #dismissDialog(int)
4450 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004451 *
4452 * @deprecated Use the new {@link DialogFragment} class with
4453 * {@link FragmentManager} instead; this is also
4454 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004455 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004456 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004457 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004458 protected Dialog onCreateDialog(int id, Bundle args) {
4459 return onCreateDialog(id);
4460 }
4461
4462 /**
4463 * @deprecated Old no-arguments version of
4464 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4465 */
4466 @Deprecated
4467 protected void onPrepareDialog(int id, Dialog dialog) {
4468 dialog.setOwnerActivity(this);
4469 }
4470
4471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004473 * shown. The default implementation calls through to
4474 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 * <p>
4477 * Override this if you need to update a managed dialog based on the state
4478 * of the application each time it is shown. For example, a time picker
4479 * dialog might want to be updated with the current time. You should call
4480 * through to the superclass's implementation. The default implementation
4481 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004482 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 * @param id The id of the managed dialog.
4484 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004485 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4486 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 * @see #showDialog(int)
4488 * @see #dismissDialog(int)
4489 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004490 *
4491 * @deprecated Use the new {@link DialogFragment} class with
4492 * {@link FragmentManager} instead; this is also
4493 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004495 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004496 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4497 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 }
4499
4500 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004501 * Simple version of {@link #showDialog(int, Bundle)} that does not
4502 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4503 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004504 *
4505 * @deprecated Use the new {@link DialogFragment} class with
4506 * {@link FragmentManager} instead; this is also
4507 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004508 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004509 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004510 public final void showDialog(int id) {
4511 showDialog(id, null);
4512 }
4513
4514 /**
4515 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 * will be made with the same id the first time this is called for a given
4517 * id. From thereafter, the dialog will be automatically saved and restored.
4518 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004519 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4520 * or later, consider instead using a {@link DialogFragment} instead.</em>
4521 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004522 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 * be made to provide an opportunity to do any timely preparation.
4524 *
4525 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004526 * @param args Arguments to pass through to the dialog. These will be saved
4527 * and restored for you. Note that if the dialog is already created,
4528 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4529 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004530 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004531 * @return Returns true if the Dialog was created; false is returned if
4532 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004533 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004534 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004535 * @see #onCreateDialog(int, Bundle)
4536 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 * @see #dismissDialog(int)
4538 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004539 *
4540 * @deprecated Use the new {@link DialogFragment} class with
4541 * {@link FragmentManager} instead; this is also
4542 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004544 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004545 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004547 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004549 ManagedDialog md = mManagedDialogs.get(id);
4550 if (md == null) {
4551 md = new ManagedDialog();
4552 md.mDialog = createDialog(id, null, args);
4553 if (md.mDialog == null) {
4554 return false;
4555 }
4556 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 }
RoboErik55011652014-07-09 15:05:53 -07004558
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004559 md.mArgs = args;
4560 onPrepareDialog(id, md.mDialog, args);
4561 md.mDialog.show();
4562 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 }
4564
4565 /**
4566 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4567 *
4568 * @param id The id of the managed dialog.
4569 *
4570 * @throws IllegalArgumentException if the id was not previously shown via
4571 * {@link #showDialog(int)}.
4572 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004573 * @see #onCreateDialog(int, Bundle)
4574 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 * @see #showDialog(int)
4576 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004577 *
4578 * @deprecated Use the new {@link DialogFragment} class with
4579 * {@link FragmentManager} instead; this is also
4580 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004582 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 public final void dismissDialog(int id) {
4584 if (mManagedDialogs == null) {
4585 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
RoboErik55011652014-07-09 15:05:53 -07004587
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004588 final ManagedDialog md = mManagedDialogs.get(id);
4589 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 throw missingDialog(id);
4591 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004592 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 }
4594
4595 /**
4596 * Creates an exception to throw if a user passed in a dialog id that is
4597 * unexpected.
4598 */
4599 private IllegalArgumentException missingDialog(int id) {
4600 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4601 + "shown via Activity#showDialog");
4602 }
4603
4604 /**
4605 * Removes any internal references to a dialog managed by this Activity.
4606 * If the dialog is showing, it will dismiss it as part of the clean up.
4607 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004608 * <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 -08004609 * want to avoid the overhead of saving and restoring it in the future.
4610 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004611 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4612 * will not throw an exception if you try to remove an ID that does not
4613 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615 * @param id The id of the managed dialog.
4616 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004617 * @see #onCreateDialog(int, Bundle)
4618 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 * @see #showDialog(int)
4620 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004621 *
4622 * @deprecated Use the new {@link DialogFragment} class with
4623 * {@link FragmentManager} instead; this is also
4624 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004626 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004628 if (mManagedDialogs != null) {
4629 final ManagedDialog md = mManagedDialogs.get(id);
4630 if (md != null) {
4631 md.mDialog.dismiss();
4632 mManagedDialogs.remove(id);
4633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 }
4636
4637 /**
4638 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004639 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004640 * <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 -07004641 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004642 * calling this function is the same as calling
4643 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4644 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 * <p>You can override this function to force global search, e.g. in response to a dedicated
4647 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004648 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004649 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4650 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4651 * return false and you must supply your own custom implementation if you want to support
4652 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004653 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004654 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004655 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4656 * not respond to search. The default implementation always returns {@code true}, except
4657 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004658 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659 * @see android.app.SearchManager
4660 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004661 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4662 mSearchEvent = searchEvent;
4663 boolean result = onSearchRequested();
4664 mSearchEvent = null;
4665 return result;
4666 }
4667
4668 /**
4669 * @see #onSearchRequested(SearchEvent)
4670 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004672 final int uiMode = getResources().getConfiguration().uiMode
4673 & Configuration.UI_MODE_TYPE_MASK;
4674 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4675 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004676 startSearch(null, false, null, false);
4677 return true;
4678 } else {
4679 return false;
4680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 }
RoboErik55011652014-07-09 15:05:53 -07004682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004684 * During the onSearchRequested() callbacks, this function will return the
4685 * {@link SearchEvent} that triggered the callback, if it exists.
4686 *
4687 * @return SearchEvent The SearchEvent that triggered the {@link
4688 * #onSearchRequested} callback.
4689 */
4690 public final SearchEvent getSearchEvent() {
4691 return mSearchEvent;
4692 }
4693
4694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004696 *
4697 * <p>It is typically called from onSearchRequested(), either directly from
4698 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004700 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 * is to inject specific data such as context data, it is preferred to <i>override</i>
4702 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004703 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004704 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4705 * not supported.
4706 *
RoboErik55011652014-07-09 15:05:53 -07004707 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004708 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004709 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 * any further typing will replace it. This is useful for cases where an entire pre-formed
4711 * query is being inserted. If false, the selection point will be placed at the end of the
4712 * inserted query. This is useful when the inserted query is text that the user entered,
4713 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4714 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004715 * @param appSearchData An application can insert application-specific
4716 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 * searches. This data will be returned with SEARCH intent(s). Null if
4718 * no extra data is required.
4719 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004720 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004721 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004723 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 * @see android.app.SearchManager
4725 * @see #onSearchRequested
4726 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004727 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4728 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004729 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004730 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004731 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 }
4733
4734 /**
krosaend2d60142009-08-17 08:56:48 -07004735 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4736 * the search dialog. Made available for testing purposes.
4737 *
4738 * @param query The query to trigger. If empty, the request will be ignored.
4739 * @param appSearchData An application can insert application-specific
4740 * context here, in order to improve quality or specificity of its own
4741 * searches. This data will be returned with SEARCH intent(s). Null if
4742 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004743 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004744 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004745 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004746 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004747 }
4748
4749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 * Request that key events come to this activity. Use this if your
4751 * activity has no views with focus, but the activity still wants
4752 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 * @see android.view.Window#takeKeyEvents
4755 */
4756 public void takeKeyEvents(boolean get) {
4757 getWindow().takeKeyEvents(get);
4758 }
4759
4760 /**
4761 * Enable extended window features. This is a convenience for calling
4762 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 * @param featureId The desired feature as defined in
4765 * {@link android.view.Window}.
4766 * @return Returns true if the requested feature is supported and now
4767 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004768 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004769 * @see android.view.Window#requestFeature
4770 */
4771 public final boolean requestWindowFeature(int featureId) {
4772 return getWindow().requestFeature(featureId);
4773 }
4774
4775 /**
4776 * Convenience for calling
4777 * {@link android.view.Window#setFeatureDrawableResource}.
4778 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004779 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 getWindow().setFeatureDrawableResource(featureId, resId);
4781 }
4782
4783 /**
4784 * Convenience for calling
4785 * {@link android.view.Window#setFeatureDrawableUri}.
4786 */
4787 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4788 getWindow().setFeatureDrawableUri(featureId, uri);
4789 }
4790
4791 /**
4792 * Convenience for calling
4793 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4794 */
4795 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4796 getWindow().setFeatureDrawable(featureId, drawable);
4797 }
4798
4799 /**
4800 * Convenience for calling
4801 * {@link android.view.Window#setFeatureDrawableAlpha}.
4802 */
4803 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4804 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4805 }
4806
4807 /**
4808 * Convenience for calling
4809 * {@link android.view.Window#getLayoutInflater}.
4810 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004811 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 public LayoutInflater getLayoutInflater() {
4813 return getWindow().getLayoutInflater();
4814 }
4815
4816 /**
4817 * Returns a {@link MenuInflater} with this context.
4818 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004819 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004821 // Make sure that action views can get an appropriate theme.
4822 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004823 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004824 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004825 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004826 } else {
4827 mMenuInflater = new MenuInflater(this);
4828 }
4829 }
4830 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 }
4832
4833 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004834 public void setTheme(int resid) {
4835 super.setTheme(resid);
4836 mWindow.setTheme(resid);
4837 }
4838
4839 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004840 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004841 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 if (mParent == null) {
4843 super.onApplyThemeResource(theme, resid, first);
4844 } else {
4845 try {
4846 theme.setTo(mParent.getTheme());
4847 } catch (Exception e) {
4848 // Empty
4849 }
4850 theme.applyStyle(resid, false);
4851 }
Winson073a5262016-02-29 16:04:40 -08004852
4853 // Get the primary color and update the TaskDescription for this activity
4854 TypedArray a = theme.obtainStyledAttributes(
4855 com.android.internal.R.styleable.ActivityTaskDescription);
4856 if (mTaskDescription.getPrimaryColor() == 0) {
4857 int colorPrimary = a.getColor(
4858 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4859 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4860 mTaskDescription.setPrimaryColor(colorPrimary);
4861 }
4862 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004863
4864 int colorBackground = a.getColor(
4865 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4866 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4867 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004868 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004869
4870 final int statusBarColor = a.getColor(
4871 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4872 if (statusBarColor != 0) {
4873 mTaskDescription.setStatusBarColor(statusBarColor);
4874 }
4875
4876 final int navigationBarColor = a.getColor(
4877 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4878 if (navigationBarColor != 0) {
4879 mTaskDescription.setNavigationBarColor(navigationBarColor);
4880 }
4881
Winson073a5262016-02-29 16:04:40 -08004882 a.recycle();
4883 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004884 }
4885
4886 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004887 * Requests permissions to be granted to this application. These permissions
4888 * must be requested in your manifest, they should not be granted to your app,
Peter Visontay2c6b6c42018-06-14 14:47:12 +01004889 * and they should have protection level {@link
4890 * android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless
4891 * whether they are declared by the platform or a third-party app.
Svetoslavc6d1c342015-02-26 14:44:43 -08004892 * <p>
4893 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4894 * are granted at install time if requested in the manifest. Signature permissions
4895 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4896 * install time if requested in the manifest and the signature of your app matches
4897 * the signature of the app declaring the permissions.
4898 * </p>
4899 * <p>
4900 * If your app does not have the requested permissions the user will be presented
4901 * with UI for accepting them. After the user has accepted or rejected the
4902 * requested permissions you will receive a callback on {@link
4903 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4904 * permissions were granted or not.
4905 * </p>
4906 * <p>
4907 * Note that requesting a permission does not guarantee it will be granted and
4908 * your app should be able to run without having this permission.
4909 * </p>
4910 * <p>
4911 * This method may start an activity allowing the user to choose which permissions
4912 * to grant and which to reject. Hence, you should be prepared that your activity
4913 * may be paused and resumed. Further, granting some permissions may require
4914 * a restart of you application. In such a case, the system will recreate the
4915 * activity stack before delivering the result to {@link
4916 * #onRequestPermissionsResult(int, String[], int[])}.
4917 * </p>
4918 * <p>
4919 * When checking whether you have a permission you should use {@link
4920 * #checkSelfPermission(String)}.
4921 * </p>
4922 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004923 * Calling this API for permissions already granted to your app would show UI
4924 * to the user to decide whether the app can still hold these permissions. This
4925 * can be useful if the way your app uses data guarded by the permissions
4926 * changes significantly.
4927 * </p>
4928 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004929 * You cannot request a permission if your activity sets {@link
4930 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4931 * <code>true</code> because in this case the activity would not receive
4932 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4933 * </p>
4934 * <p>
Peter Visontay2c6b6c42018-06-14 14:47:12 +01004935 * The <a href="https://github.com/googlesamples/android-RuntimePermissions">
Trevor Johns682c24e2016-04-12 10:13:47 -07004936 * RuntimePermissions</a> sample app demonstrates how to use this method to
4937 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004938 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004939 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004940 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004941 * @param requestCode Application specific request code to match with a result
4942 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004943 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004944 *
Paul Miller77a0e132016-02-11 13:46:49 -08004945 * @throws IllegalArgumentException if requestCode is negative.
4946 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004947 * @see #onRequestPermissionsResult(int, String[], int[])
4948 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004949 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004950 */
4951 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004952 if (requestCode < 0) {
4953 throw new IllegalArgumentException("requestCode should be >= 0");
4954 }
Svetoslavffb32b12015-10-15 16:54:00 -07004955 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00004956 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07004957 // Dispatch the callback with empty arrays which means a cancellation.
4958 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4959 return;
4960 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004961 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004962 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004963 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004964 }
4965
4966 /**
4967 * Callback for the result from requesting permissions. This method
4968 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004969 * <p>
4970 * <strong>Note:</strong> It is possible that the permissions request interaction
4971 * with the user is interrupted. In this case you will receive empty permissions
4972 * and results arrays which should be treated as a cancellation.
4973 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004974 *
4975 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4976 * @param permissions The requested permissions. Never null.
4977 * @param grantResults The grant results for the corresponding permissions
4978 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4979 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4980 *
4981 * @see #requestPermissions(String[], int)
4982 */
4983 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4984 @NonNull int[] grantResults) {
4985 /* callback - no nothing */
4986 }
4987
4988 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004989 * Gets whether you should show UI with rationale for requesting a permission.
4990 * You should do this only if you do not have the permission and the context in
4991 * which the permission is requested does not clearly communicate to the user
4992 * what would be the benefit from granting this permission.
4993 * <p>
4994 * For example, if you write a camera app, requesting the camera permission
4995 * would be expected by the user and no rationale for why it is requested is
4996 * needed. If however, the app needs location for tagging photos then a non-tech
4997 * savvy user may wonder how location is related to taking photos. In this case
4998 * you may choose to show UI with rationale of requesting this permission.
4999 * </p>
5000 *
5001 * @param permission A permission your app wants to request.
5002 * @return Whether you can show permission rationale UI.
5003 *
5004 * @see #checkSelfPermission(String)
5005 * @see #requestPermissions(String[], int)
5006 * @see #onRequestPermissionsResult(int, String[], int[])
5007 */
Svetoslav9ce9a152015-06-02 17:59:31 -07005008 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07005009 return getPackageManager().shouldShowRequestPermissionRationale(permission);
5010 }
5011
5012 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005013 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
5014 * with no options.
5015 *
5016 * @param intent The intent to start.
5017 * @param requestCode If >= 0, this code will be returned in
5018 * onActivityResult() when the activity exits.
5019 *
5020 * @throws android.content.ActivityNotFoundException
5021 *
George Mount0a778ed2013-12-13 13:35:36 -08005022 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07005023 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005024 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005025 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005026 }
5027
5028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 * Launch an activity for which you would like a result when it finished.
5030 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08005031 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07005032 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07005034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 * <p>Note that this method should only be used with Intent protocols
5036 * that are defined to return a result. In other protocols (such as
5037 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
5038 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05005039 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
5040 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005041 *
RoboErik55011652014-07-09 15:05:53 -07005042 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08005044 * activity, then your window will not be displayed until a result is
5045 * returned back from the started activity. This is to avoid visible
5046 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005047 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 * <p>This method throws {@link android.content.ActivityNotFoundException}
5049 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005050 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 * @param intent The intent to start.
5052 * @param requestCode If >= 0, this code will be returned in
5053 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005054 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005055 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005056 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005057 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005059 *
George Mount0a778ed2013-12-13 13:35:36 -08005060 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005062 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
5063 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07005065 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 Instrumentation.ActivityResult ar =
5067 mInstrumentation.execStartActivity(
5068 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005069 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 if (ar != null) {
5071 mMainThread.sendActivityResult(
5072 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
5073 ar.getResultData());
5074 }
5075 if (requestCode >= 0) {
5076 // If this start is requesting a result, we can avoid making
5077 // the activity visible until the result is received. Setting
5078 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5079 // activity hidden during this time, to avoid flickering.
5080 // This can only be done when a result is requested because
5081 // that guarantees we will get information back when the
5082 // activity is finished, no matter what happens to it.
5083 mStartedActivity = true;
5084 }
Adam Powell14874662013-07-18 19:42:41 -07005085
George Mount41725de2015-04-09 08:23:05 -07005086 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07005087 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005089 if (options != null) {
5090 mParent.startActivityFromChild(this, intent, requestCode, options);
5091 } else {
5092 // Note we want to go through this method for compatibility with
5093 // existing applications that may have overridden it.
5094 mParent.startActivityFromChild(this, intent, requestCode);
5095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 }
George Mount41725de2015-04-09 08:23:05 -07005097 }
5098
5099 /**
5100 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
5101 *
5102 * @param options The ActivityOptions bundle used to start an Activity.
5103 */
5104 private void cancelInputsAndStartExitTransition(Bundle options) {
5105 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
5106 if (decor != null) {
5107 decor.cancelPendingInputEvents();
5108 }
Sunny Goyald85bed52018-09-25 12:01:01 -07005109 if (options != null) {
George Mountff243282014-07-07 16:12:07 -07005110 mActivityTransitionState.startExitOutTransition(this, options);
5111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 }
5113
Chet Haaseb64e777f2016-12-09 07:17:10 -08005114 /**
5115 * Returns whether there are any activity transitions currently running on this
5116 * activity. A return value of {@code true} can mean that either an enter or
5117 * exit transition is running, including whether the background of the activity
5118 * is animating as a part of that transition.
5119 *
5120 * @return true if a transition is currently running on this activity, false otherwise.
5121 */
5122 public boolean isActivityTransitionRunning() {
5123 return mActivityTransitionState.isTransitionRunning();
5124 }
5125
George Mount413739e2016-06-08 07:13:37 -07005126 private Bundle transferSpringboardActivityOptions(Bundle options) {
5127 if (options == null && (mWindow != null && !mWindow.isActive())) {
5128 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07005129 if (activityOptions != null &&
5130 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07005131 return activityOptions.toBundle();
5132 }
5133 }
5134 return options;
5135 }
5136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005138 * @hide Implement to provide correct calling token.
5139 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005140 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01005141 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
5142 startActivityForResultAsUser(intent, requestCode, null, user);
5143 }
5144
5145 /**
5146 * @hide Implement to provide correct calling token.
5147 */
5148 public void startActivityForResultAsUser(Intent intent, int requestCode,
5149 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01005150 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
5151 }
5152
5153 /**
5154 * @hide Implement to provide correct calling token.
5155 */
5156 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
5157 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005158 if (mParent != null) {
5159 throw new RuntimeException("Can't be called from a child");
5160 }
George Mount413739e2016-06-08 07:13:37 -07005161 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005162 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005163 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01005164 options, user);
5165 if (ar != null) {
5166 mMainThread.sendActivityResult(
5167 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
5168 }
5169 if (requestCode >= 0) {
5170 // If this start is requesting a result, we can avoid making
5171 // the activity visible until the result is received. Setting
5172 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5173 // activity hidden during this time, to avoid flickering.
5174 // This can only be done when a result is requested because
5175 // that guarantees we will get information back when the
5176 // activity is finished, no matter what happens to it.
5177 mStartedActivity = true;
5178 }
5179
George Mount41725de2015-04-09 08:23:05 -07005180 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01005181 }
5182
5183 /**
5184 * @hide Implement to provide correct calling token.
5185 */
Philip P. Moltmann4e615e62018-08-28 14:57:49 -07005186 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005187 public void startActivityAsUser(Intent intent, UserHandle user) {
5188 startActivityAsUser(intent, null, user);
5189 }
5190
5191 /**
5192 * @hide Implement to provide correct calling token.
5193 */
5194 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
5195 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01005196 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005197 }
George Mount413739e2016-06-08 07:13:37 -07005198 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005199 Instrumentation.ActivityResult ar =
5200 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01005201 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005202 intent, -1, options, user);
5203 if (ar != null) {
5204 mMainThread.sendActivityResult(
5205 mToken, mEmbeddedID, -1, ar.getResultCode(),
5206 ar.getResultData());
5207 }
George Mount41725de2015-04-09 08:23:05 -07005208 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07005209 }
5210
5211 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005212 * Start a new activity as if it was started by the activity that started our
5213 * current activity. This is for the resolver and chooser activities, which operate
5214 * as intermediaries that dispatch their intent to the target the user selects -- to
5215 * do this, they must perform all security checks including permission grants as if
5216 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005217 * @param intent The Intent to start.
5218 * @param options ActivityOptions or null.
Alison Cichowlas3e340502018-08-07 17:15:01 -04005219 * @param permissionToken Token received from the system that permits this call to be made.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005220 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
5221 * caller it is doing the start is, is actually allowed to start the target activity.
5222 * If you set this to true, you must set an explicit component in the Intent and do any
5223 * appropriate security checks yourself.
5224 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005225 * @hide
5226 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005227 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005228 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005229 if (mParent != null) {
5230 throw new RuntimeException("Can't be called from a child");
5231 }
George Mount413739e2016-06-08 07:13:37 -07005232 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005233 Instrumentation.ActivityResult ar =
5234 mInstrumentation.execStartActivityAsCaller(
5235 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas3e340502018-08-07 17:15:01 -04005236 intent, -1, options, permissionToken, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005237 if (ar != null) {
5238 mMainThread.sendActivityResult(
5239 mToken, mEmbeddedID, -1, ar.getResultCode(),
5240 ar.getResultData());
5241 }
George Mount41725de2015-04-09 08:23:05 -07005242 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07005243 }
5244
5245 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005246 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
5247 * Intent, int, int, int, Bundle)} with no options.
5248 *
5249 * @param intent The IntentSender to launch.
5250 * @param requestCode If >= 0, this code will be returned in
5251 * onActivityResult() when the activity exits.
5252 * @param fillInIntent If non-null, this will be provided as the
5253 * intent parameter to {@link IntentSender#sendIntent}.
5254 * @param flagsMask Intent flags in the original IntentSender that you
5255 * would like to change.
5256 * @param flagsValues Desired values for any bits set in
5257 * <var>flagsMask</var>
5258 * @param extraFlags Always set to 0.
5259 */
5260 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005261 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005262 throws IntentSender.SendIntentException {
5263 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
5264 flagsValues, extraFlags, null);
5265 }
5266
5267 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005268 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005269 * to use a IntentSender to describe the activity to be started. If
5270 * the IntentSender is for an activity, that activity will be started
5271 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
5272 * here; otherwise, its associated action will be executed (such as
5273 * sending a broadcast) as if you had called
5274 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07005275 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005276 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005277 * @param requestCode If >= 0, this code will be returned in
5278 * onActivityResult() when the activity exits.
5279 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005280 * intent parameter to {@link IntentSender#sendIntent}.
5281 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005282 * would like to change.
5283 * @param flagsValues Desired values for any bits set in
5284 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005285 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005286 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005287 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005288 * Context.startActivity(Intent, Bundle)} for more details. If options
5289 * have also been supplied by the IntentSender, options given here will
5290 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005291 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005292 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07005293 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005294 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005295 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005296 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
5297 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005298 } else if (options != null) {
5299 mParent.startIntentSenderFromChild(this, intent, requestCode,
5300 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005301 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005302 // Note we want to go through this call for compatibility with
5303 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005304 mParent.startIntentSenderFromChild(this, intent, requestCode,
5305 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005306 }
5307 }
5308
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005309 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
5310 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005311 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005312 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005313 try {
Sunny Goyald85bed52018-09-25 12:01:01 -07005314 options = transferSpringboardActivityOptions(options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005315 String resolvedType = null;
5316 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005317 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005318 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005319 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
5320 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005321 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07005322 .startActivityIntentSender(mMainThread.getApplicationThread(),
5323 intent != null ? intent.getTarget() : null,
5324 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005325 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005326 requestCode, flagsMask, flagsValues, options);
5327 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005328 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005329 }
5330 Instrumentation.checkStartActivityResult(result, null);
Sunny Goyald85bed52018-09-25 12:01:01 -07005331
5332 if (options != null) {
5333 // Only when the options are not null, as the intent can point to something other
5334 // than an Activity.
5335 cancelInputsAndStartExitTransition(options);
5336 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005337 } catch (RemoteException e) {
5338 }
5339 if (requestCode >= 0) {
5340 // If this start is requesting a result, we can avoid making
5341 // the activity visible until the result is received. Setting
5342 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5343 // activity hidden during this time, to avoid flickering.
5344 // This can only be done when a result is requested because
5345 // that guarantees we will get information back when the
5346 // activity is finished, no matter what happens to it.
5347 mStartedActivity = true;
5348 }
5349 }
5350
5351 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005352 * Same as {@link #startActivity(Intent, Bundle)} with no options
5353 * specified.
5354 *
5355 * @param intent The intent to start.
5356 *
5357 * @throws android.content.ActivityNotFoundException
5358 *
Mark Lufa434852016-08-11 17:40:33 -07005359 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005360 * @see #startActivityForResult
5361 */
5362 @Override
5363 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00005364 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005365 }
5366
5367 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 * Launch a new activity. You will not receive any information about when
5369 * the activity exits. This implementation overrides the base version,
5370 * providing information about
5371 * the activity performing the launch. Because of this additional
5372 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5373 * required; if not specified, the new activity will be added to the
5374 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07005375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 * <p>This method throws {@link android.content.ActivityNotFoundException}
5377 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005378 *
5379 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005380 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005381 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005382 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005383 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07005385 *
Mark Lufa434852016-08-11 17:40:33 -07005386 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07005387 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 */
5389 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005390 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005391 if (options != null) {
5392 startActivityForResult(intent, -1, options);
5393 } else {
5394 // Note we want to go through this call for compatibility with
5395 // applications that may have overridden the method.
5396 startActivityForResult(intent, -1);
5397 }
5398 }
5399
5400 /**
5401 * Same as {@link #startActivities(Intent[], Bundle)} with no options
5402 * specified.
5403 *
5404 * @param intents The intents to start.
5405 *
5406 * @throws android.content.ActivityNotFoundException
5407 *
Mark Lufa434852016-08-11 17:40:33 -07005408 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07005409 * @see #startActivityForResult
5410 */
5411 @Override
5412 public void startActivities(Intent[] intents) {
5413 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 }
5415
5416 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005417 * Launch a new activity. You will not receive any information about when
5418 * the activity exits. This implementation overrides the base version,
5419 * providing information about
5420 * the activity performing the launch. Because of this additional
5421 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
5422 * required; if not specified, the new activity will be added to the
5423 * task of the caller.
5424 *
5425 * <p>This method throws {@link android.content.ActivityNotFoundException}
5426 * if there was no Activity found to run the given Intent.
5427 *
5428 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005429 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005430 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005431 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005432 *
5433 * @throws android.content.ActivityNotFoundException
5434 *
Mark Lufa434852016-08-11 17:40:33 -07005435 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005436 * @see #startActivityForResult
5437 */
5438 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005439 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005440 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07005441 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005442 }
5443
5444 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005445 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
5446 * with no options.
RoboErik55011652014-07-09 15:05:53 -07005447 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005448 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005449 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005450 * intent parameter to {@link IntentSender#sendIntent}.
5451 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005452 * would like to change.
5453 * @param flagsValues Desired values for any bits set in
5454 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005455 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005456 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005457 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005458 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005459 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005460 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5461 extraFlags, null);
5462 }
5463
5464 /**
5465 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5466 * to start; see
5467 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5468 * for more information.
5469 *
5470 * @param intent The IntentSender to launch.
5471 * @param fillInIntent If non-null, this will be provided as the
5472 * intent parameter to {@link IntentSender#sendIntent}.
5473 * @param flagsMask Intent flags in the original IntentSender that you
5474 * would like to change.
5475 * @param flagsValues Desired values for any bits set in
5476 * <var>flagsMask</var>
5477 * @param extraFlags Always set to 0.
5478 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005479 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005480 * Context.startActivity(Intent, Bundle)} for more details. If options
5481 * have also been supplied by the IntentSender, options given here will
5482 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005483 */
5484 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005485 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005486 Bundle options) throws IntentSender.SendIntentException {
5487 if (options != null) {
5488 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5489 flagsValues, extraFlags, options);
5490 } else {
5491 // Note we want to go through this call for compatibility with
5492 // applications that may have overridden the method.
5493 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5494 flagsValues, extraFlags);
5495 }
5496 }
5497
5498 /**
5499 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5500 * with no options.
5501 *
5502 * @param intent The intent to start.
5503 * @param requestCode If >= 0, this code will be returned in
5504 * onActivityResult() when the activity exits, as described in
5505 * {@link #startActivityForResult}.
5506 *
5507 * @return If a new activity was launched then true is returned; otherwise
5508 * false is returned and you must handle the Intent yourself.
5509 *
5510 * @see #startActivity
5511 * @see #startActivityForResult
5512 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005513 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5514 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005515 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005516 }
5517
5518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 * A special variation to launch an activity only if a new activity
5520 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005521 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005523 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005525 * and the activity
5526 * that handles <var>intent</var> is the same as your currently running
5527 * activity, then a new instance is not needed. In this case, instead of
5528 * the normal behavior of calling {@link #onNewIntent} this function will
5529 * return and you can handle the Intent yourself.
5530 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 * <p>This function can only be called from a top-level activity; if it is
5532 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 * @param intent The intent to start.
5535 * @param requestCode If >= 0, this code will be returned in
5536 * onActivityResult() when the activity exits, as described in
5537 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005538 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005539 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005540 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 * @return If a new activity was launched then true is returned; otherwise
5543 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 * @see #startActivity
5546 * @see #startActivityForResult
5547 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005548 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5549 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005551 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005553 Uri referrer = onProvideReferrer();
5554 if (referrer != null) {
5555 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5556 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005557 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005558 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005559 result = ActivityTaskManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005560 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005561 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5562 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5563 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 } catch (RemoteException e) {
5565 // Empty
5566 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 if (requestCode >= 0) {
5571 // If this start is requesting a result, we can avoid making
5572 // the activity visible until the result is received. Setting
5573 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5574 // activity hidden during this time, to avoid flickering.
5575 // This can only be done when a result is requested because
5576 // that guarantees we will get information back when the
5577 // activity is finished, no matter what happens to it.
5578 mStartedActivity = true;
5579 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005580 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 }
5582
5583 throw new UnsupportedOperationException(
5584 "startActivityIfNeeded can only be called from a top-level activity");
5585 }
5586
5587 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005588 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5589 * no options.
5590 *
5591 * @param intent The intent to dispatch to the next activity. For
5592 * correct behavior, this must be the same as the Intent that started
5593 * your own activity; the only changes you can make are to the extras
5594 * inside of it.
5595 *
5596 * @return Returns a boolean indicating whether there was another Activity
5597 * to start: true if there was a next activity to start, false if there
5598 * wasn't. In general, if true is returned you will then want to call
5599 * finish() on yourself.
5600 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005601 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005602 return startNextMatchingActivity(intent, null);
5603 }
5604
5605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 * Special version of starting an activity, for use when you are replacing
5607 * other activity components. You can use this to hand the Intent off
5608 * to the next Activity that can handle it. You typically call this in
5609 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 * @param intent The intent to dispatch to the next activity. For
5612 * correct behavior, this must be the same as the Intent that started
5613 * your own activity; the only changes you can make are to the extras
5614 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005615 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005616 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005617 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005618 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 * @return Returns a boolean indicating whether there was another Activity
5620 * to start: true if there was a next activity to start, false if there
5621 * wasn't. In general, if true is returned you will then want to call
5622 * finish() on yourself.
5623 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005624 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5625 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 if (mParent == null) {
5627 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005628 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005629 intent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005630 return ActivityTaskManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005631 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 } catch (RemoteException e) {
5633 // Empty
5634 }
5635 return false;
5636 }
5637
5638 throw new UnsupportedOperationException(
5639 "startNextMatchingActivity can only be called from a top-level activity");
5640 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005641
5642 /**
5643 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5644 * with no options.
5645 *
5646 * @param child The activity making the call.
5647 * @param intent The intent to start.
5648 * @param requestCode Reply request code. < 0 if reply is not requested.
5649 *
5650 * @throws android.content.ActivityNotFoundException
5651 *
5652 * @see #startActivity
5653 * @see #startActivityForResult
5654 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005655 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005656 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005657 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005658 }
5659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 /**
RoboErik55011652014-07-09 15:05:53 -07005661 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005663 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 * <p>This method throws {@link android.content.ActivityNotFoundException}
5665 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 * @param child The activity making the call.
5668 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005669 * @param requestCode Reply request code. < 0 if reply is not requested.
5670 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005671 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005672 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005675 *
5676 * @see #startActivity
5677 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005679 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005680 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005681 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 Instrumentation.ActivityResult ar =
5683 mInstrumentation.execStartActivity(
5684 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005685 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 if (ar != null) {
5687 mMainThread.sendActivityResult(
5688 mToken, child.mEmbeddedID, requestCode,
5689 ar.getResultCode(), ar.getResultData());
5690 }
George Mount41725de2015-04-09 08:23:05 -07005691 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 }
5693
5694 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005695 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5696 * with no options.
5697 *
5698 * @param fragment The fragment making the call.
5699 * @param intent The intent to start.
5700 * @param requestCode Reply request code. < 0 if reply is not requested.
5701 *
5702 * @throws android.content.ActivityNotFoundException
5703 *
5704 * @see Fragment#startActivity
5705 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005706 *
5707 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5708 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005709 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005710 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005711 public void startActivityFromFragment(@NonNull Fragment fragment,
5712 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005713 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005714 }
5715
5716 /**
RoboErik55011652014-07-09 15:05:53 -07005717 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005718 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5719 * method.
RoboErik55011652014-07-09 15:05:53 -07005720 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005721 * <p>This method throws {@link android.content.ActivityNotFoundException}
5722 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005723 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005724 * @param fragment The fragment making the call.
5725 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005726 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005727 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005728 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005729 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005730 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005731 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005732 *
5733 * @see Fragment#startActivity
5734 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005735 *
5736 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5737 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005738 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005739 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005740 public void startActivityFromFragment(@NonNull Fragment fragment,
5741 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005742 startActivityForResult(fragment.mWho, intent, requestCode, options);
5743 }
5744
5745 /**
5746 * @hide
5747 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005748 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5749 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5750 UserHandle user) {
5751 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5752 }
5753
5754 /**
5755 * @hide
5756 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005757 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005758 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005759 public void startActivityForResult(
5760 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005761 Uri referrer = onProvideReferrer();
5762 if (referrer != null) {
5763 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5764 }
George Mount413739e2016-06-08 07:13:37 -07005765 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005766 Instrumentation.ActivityResult ar =
5767 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005768 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005769 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005770 if (ar != null) {
5771 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005772 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005773 ar.getResultCode(), ar.getResultData());
5774 }
George Mount41725de2015-04-09 08:23:05 -07005775 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005776 }
5777
5778 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005779 * @hide
5780 */
5781 @Override
5782 public boolean canStartActivityForResult() {
5783 return true;
5784 }
5785
5786 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005787 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5788 * int, Intent, int, int, int, Bundle)} with no options.
5789 */
5790 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5791 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5792 int extraFlags)
5793 throws IntentSender.SendIntentException {
5794 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5795 flagsMask, flagsValues, extraFlags, null);
5796 }
5797
5798 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005799 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005800 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005801 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005802 * for more information.
5803 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005804 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5805 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005806 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005807 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005808 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5809 flagsMask, flagsValues, options);
5810 }
5811
5812 /**
5813 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5814 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5815 * for more information.
5816 *
5817 * @hide
5818 */
5819 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5820 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5821 int extraFlags, @Nullable Bundle options)
5822 throws IntentSender.SendIntentException {
5823 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5824 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005825 }
5826
5827 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005828 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5829 * or {@link #finish} to specify an explicit transition animation to
5830 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005831 *
5832 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5833 * to using this with starting activities is to supply the desired animation
5834 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005835 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005836 * you to specify a custom animation even when starting an activity from
5837 * outside the context of the current top activity.
5838 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005839 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005840 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005841 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005842 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005843 */
5844 public void overridePendingTransition(int enterAnim, int exitAnim) {
5845 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005846 ActivityTaskManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005847 mToken, getPackageName(), enterAnim, exitAnim);
5848 } catch (RemoteException e) {
5849 }
5850 }
RoboErik55011652014-07-09 15:05:53 -07005851
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 * Call this to set the result that your activity will return to its
5854 * caller.
RoboErik55011652014-07-09 15:05:53 -07005855 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 * @param resultCode The result code to propagate back to the originating
5857 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 * @see #RESULT_CANCELED
5860 * @see #RESULT_OK
5861 * @see #RESULT_FIRST_USER
5862 * @see #setResult(int, Intent)
5863 */
5864 public final void setResult(int resultCode) {
5865 synchronized (this) {
5866 mResultCode = resultCode;
5867 mResultData = null;
5868 }
5869 }
5870
5871 /**
5872 * Call this to set the result that your activity will return to its
5873 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005874 *
5875 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5876 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5877 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5878 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5879 * Activity receiving the result access to the specific URIs in the Intent.
5880 * Access will remain until the Activity has finished (it will remain across the hosting
5881 * process being killed and other temporary destruction) and will be added
5882 * to any existing set of URI permissions it already holds.
5883 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 * @param resultCode The result code to propagate back to the originating
5885 * activity, often RESULT_CANCELED or RESULT_OK
5886 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 * @see #RESULT_CANCELED
5889 * @see #RESULT_OK
5890 * @see #RESULT_FIRST_USER
5891 * @see #setResult(int)
5892 */
5893 public final void setResult(int resultCode, Intent data) {
5894 synchronized (this) {
5895 mResultCode = resultCode;
5896 mResultData = data;
5897 }
5898 }
5899
5900 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005901 * Return information about who launched this activity. If the launching Intent
5902 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5903 * that will be returned as-is; otherwise, if known, an
5904 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5905 * package name that started the Intent will be returned. This may return null if no
5906 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5907 * application package was involved.
5908 *
5909 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5910 * return the referrer that submitted that new intent to the activity. Otherwise, it
5911 * always returns the referrer of the original Intent.</p>
5912 *
5913 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5914 * referrer information, applications can spoof it.</p>
5915 */
5916 @Nullable
5917 public Uri getReferrer() {
5918 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005919 try {
5920 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5921 if (referrer != null) {
5922 return referrer;
5923 }
5924 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5925 if (referrerName != null) {
5926 return Uri.parse(referrerName);
5927 }
5928 } catch (BadParcelableException e) {
5929 Log.w(TAG, "Cannot read referrer from intent;"
5930 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005931 }
5932 if (mReferrer != null) {
5933 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5934 }
5935 return null;
5936 }
5937
5938 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005939 * Override to generate the desired referrer for the content currently being shown
5940 * by the app. The default implementation returns null, meaning the referrer will simply
5941 * be the android-app: of the package name of this activity. Return a non-null Uri to
5942 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5943 */
5944 public Uri onProvideReferrer() {
5945 return null;
5946 }
5947
5948 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 * Return the name of the package that invoked this activity. This is who
5950 * the data in {@link #setResult setResult()} will be sent to. You can
5951 * use this information to validate that the recipient is allowed to
5952 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005953 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005954 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005955 * did not use the {@link #startActivityForResult}
5956 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005957 * null.</p>
5958 *
5959 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5960 * the result from this method was unstable. If the process hosting the calling
5961 * package was no longer running, it would return null instead of the proper package
5962 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5963 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005964 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 * @return The package of the activity that will receive your
5966 * reply, or null if none.
5967 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005968 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 public String getCallingPackage() {
5970 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005971 return ActivityTaskManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 } catch (RemoteException e) {
5973 return null;
5974 }
5975 }
5976
5977 /**
5978 * Return the name of the activity that invoked this activity. This is
5979 * who the data in {@link #setResult setResult()} will be sent to. You
5980 * can use this information to validate that the recipient is allowed to
5981 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005982 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005983 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005984 * did not use the {@link #startActivityForResult}
5985 * form that includes a request code), then the calling package will be
5986 * null.
5987 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005988 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 * reply, or null if none.
5990 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005991 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 public ComponentName getCallingActivity() {
5993 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07005994 return ActivityTaskManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 } catch (RemoteException e) {
5996 return null;
5997 }
5998 }
5999
6000 /**
6001 * Control whether this activity's main window is visible. This is intended
6002 * only for the special case of an activity that is not going to show a
6003 * UI itself, but can't just finish prior to onResume() because it needs
6004 * to wait for a service binding or such. Setting this to false allows
6005 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07006006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 * <p>The default value for this is taken from the
6008 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
6009 */
6010 public void setVisible(boolean visible) {
6011 if (mVisibleFromClient != visible) {
6012 mVisibleFromClient = visible;
6013 if (mVisibleFromServer) {
6014 if (visible) makeVisible();
6015 else mDecor.setVisibility(View.INVISIBLE);
6016 }
6017 }
6018 }
RoboErik55011652014-07-09 15:05:53 -07006019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 void makeVisible() {
6021 if (!mWindowAdded) {
6022 ViewManager wm = getWindowManager();
6023 wm.addView(mDecor, getWindow().getAttributes());
6024 mWindowAdded = true;
6025 }
6026 mDecor.setVisibility(View.VISIBLE);
6027 }
RoboErik55011652014-07-09 15:05:53 -07006028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 /**
6030 * Check to see whether this activity is in the process of finishing,
6031 * either because you called {@link #finish} on it or someone else
6032 * has requested that it finished. This is often used in
6033 * {@link #onPause} to determine whether the activity is simply pausing or
6034 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07006035 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07006037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038 * @see #finish
6039 */
6040 public boolean isFinishing() {
6041 return mFinished;
6042 }
6043
6044 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006045 * Returns true if the final {@link #onDestroy()} call has been made
6046 * on the Activity, so this instance is now dead.
6047 */
6048 public boolean isDestroyed() {
6049 return mDestroyed;
6050 }
6051
6052 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006053 * Check to see whether this activity is in the process of being destroyed in order to be
6054 * recreated with a new configuration. This is often used in
6055 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
6056 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07006057 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05006058 * @return If the activity is being torn down in order to be recreated with a new configuration,
6059 * returns true; else returns false.
6060 */
6061 public boolean isChangingConfigurations() {
6062 return mChangingConfigurations;
6063 }
6064
6065 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006066 * Cause this Activity to be recreated with a new instance. This results
6067 * in essentially the same flow as when the Activity is created due to
6068 * a configuration change -- the current instance will go through its
6069 * lifecycle to {@link #onDestroy} and a new instance then created after it.
6070 */
6071 public void recreate() {
6072 if (mParent != null) {
6073 throw new IllegalStateException("Can only be called on top-level activity");
6074 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07006075 if (Looper.myLooper() != mMainThread.getLooper()) {
6076 throw new IllegalStateException("Must be called from main thread");
6077 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07006078 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006079 }
6080
6081 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006082 * Finishes the current activity and specifies whether to remove the task associated with this
6083 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006085 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006086 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 if (mParent == null) {
6088 int resultCode;
6089 Intent resultData;
6090 synchronized (this) {
6091 resultCode = mResultCode;
6092 resultData = mResultData;
6093 }
Joe Onorato43a17652011-04-06 19:22:23 -07006094 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006096 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006097 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04006098 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006099 if (ActivityTaskManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07006100 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 mFinished = true;
6102 }
6103 } catch (RemoteException e) {
6104 // Empty
6105 }
6106 } else {
6107 mParent.finishFromChild(this);
6108 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07006109
6110 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
6111 // be restored now.
6112 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
6113 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
6114 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
6115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 }
6117
6118 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006119 * Call this when your activity is done and should be closed. The
6120 * ActivityResult is propagated back to whoever launched you via
6121 * onActivityResult().
6122 */
6123 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006124 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006125 }
6126
6127 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006128 * Finish this activity as well as all activities immediately below it
6129 * in the current task that have the same affinity. This is typically
6130 * used when an application can be launched on to another task (such as
6131 * from an ACTION_VIEW of a content type it understands) and the user
6132 * has used the up navigation to switch out of the current task and in
6133 * to its own task. In this case, if the user has navigated down into
6134 * any other activities of the second application, all of those should
6135 * be removed from the original task as part of the task switch.
6136 *
6137 * <p>Note that this finish does <em>not</em> allow you to deliver results
6138 * to the previous activity, and an exception will be thrown if you are trying
6139 * to do so.</p>
6140 */
6141 public void finishAffinity() {
6142 if (mParent != null) {
6143 throw new IllegalStateException("Can not be called from an embedded activity");
6144 }
6145 if (mResultCode != RESULT_CANCELED || mResultData != null) {
6146 throw new IllegalStateException("Can not be called to deliver a result");
6147 }
6148 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006149 if (ActivityTaskManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07006150 mFinished = true;
6151 }
6152 } catch (RemoteException e) {
6153 // Empty
6154 }
6155 }
6156
6157 /**
RoboErik55011652014-07-09 15:05:53 -07006158 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 * {@link #finish} method. The default implementation simply calls
6160 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07006161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07006163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 * @see #finish
6165 */
6166 public void finishFromChild(Activity child) {
6167 finish();
6168 }
6169
6170 /**
George Mountcb4b7d92014-02-25 10:47:55 -08006171 * Reverses the Activity Scene entry Transition and triggers the calling Activity
6172 * to reverse its exit Transition. When the exit Transition completes,
6173 * {@link #finish()} is called. If no entry Transition was used, finish() is called
6174 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07006175 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08006176 */
Craig Mautner73f843d2014-05-19 09:42:28 -07006177 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07006178 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07006179 finish();
6180 }
George Mountcb4b7d92014-02-25 10:47:55 -08006181 }
6182
6183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184 * Force finish another activity that you had previously started with
6185 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07006186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 * @param requestCode The request code of the activity that you had
6188 * given to startActivityForResult(). If there are multiple
6189 * activities started with this request code, they
6190 * will all be finished.
6191 */
6192 public void finishActivity(int requestCode) {
6193 if (mParent == null) {
6194 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006195 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 .finishSubActivity(mToken, mEmbeddedID, requestCode);
6197 } catch (RemoteException e) {
6198 // Empty
6199 }
6200 } else {
6201 mParent.finishActivityFromChild(this, requestCode);
6202 }
6203 }
6204
6205 /**
6206 * This is called when a child activity of this one calls its
6207 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07006208 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 * @param child The activity making the call.
6210 * @param requestCode Request code that had been used to start the
6211 * activity.
6212 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006213 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006215 ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
6217 } catch (RemoteException e) {
6218 // Empty
6219 }
6220 }
6221
6222 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07006223 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006224 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07006225 */
6226 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07006227 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07006228 }
6229
6230 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006231 * Ask that the local app instance of this activity be released to free up its memory.
6232 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
6233 * a new instance of the activity will later be re-created if needed due to the user
6234 * navigating back to it.
6235 *
6236 * @return Returns true if the activity was in a state that it has started the process
6237 * of destroying its current instance; returns false if for any reason this could not
6238 * be done: it is currently visible to the user, it is already being destroyed, it is
6239 * being finished, it hasn't yet saved its state, etc.
6240 */
6241 public boolean releaseInstance() {
6242 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006243 return ActivityTaskManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006244 } catch (RemoteException e) {
6245 // Empty
6246 }
6247 return false;
6248 }
6249
6250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 * Called when an activity you launched exits, giving you the requestCode
6252 * you started it with, the resultCode it returned, and any additional
6253 * data from it. The <var>resultCode</var> will be
6254 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
6255 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07006256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 * <p>You will receive this call immediately before onResume() when your
6258 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07006259 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07006260 * <p>This method is never invoked if your activity sets
6261 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
6262 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08006263 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 * @param requestCode The integer request code originally supplied to
6265 * startActivityForResult(), allowing you to identify who this
6266 * result came from.
6267 * @param resultCode The integer result code returned by the child activity
6268 * through its setResult().
6269 * @param data An Intent, which can return result data to the caller
6270 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07006271 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 * @see #startActivityForResult
6273 * @see #createPendingResult
6274 * @see #setResult(int)
6275 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006276 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006277 }
6278
6279 /**
George Mount62ab9b72014-05-02 13:51:17 -07006280 * Called when an activity you launched with an activity transition exposes this
6281 * Activity through a returning activity transition, giving you the resultCode
6282 * and any additional data from it. This method will only be called if the activity
6283 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07006284 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07006285 *
6286 * <p>The purpose of this function is to let the called Activity send a hint about
6287 * its state so that this underlying Activity can prepare to be exposed. A call to
6288 * this method does not guarantee that the called Activity has or will be exiting soon.
6289 * It only indicates that it will expose this Activity's Window and it has
6290 * some data to pass to prepare it.</p>
6291 *
6292 * @param resultCode The integer result code returned by the child activity
6293 * through its setResult().
6294 * @param data An Intent, which can return result data to the caller
6295 * (various data can be attached to Intent "extras").
6296 */
Craig Mautner7a629c22014-09-04 14:57:04 -07006297 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07006298 }
6299
6300 /**
RoboErik55011652014-07-09 15:05:53 -07006301 * Create a new PendingIntent object which you can hand to others
6302 * for them to use to send result data back to your
6303 * {@link #onActivityResult} callback. The created object will be either
6304 * one-shot (becoming invalid after a result is sent back) or multiple
6305 * (allowing any number of results to be sent through it).
6306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 * @param requestCode Private request code for the sender that will be
6308 * associated with the result data when it is returned. The sender can not
6309 * modify this value, allowing you to identify incoming results.
6310 * @param data Default data to supply in the result, which may be modified
6311 * by the sender.
6312 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
6313 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
6314 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
6315 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
6316 * or any of the flags as supported by
6317 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
6318 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07006319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 * @return Returns an existing or new PendingIntent matching the given
6321 * parameters. May return null only if
6322 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
6323 * supplied.
RoboErik55011652014-07-09 15:05:53 -07006324 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 * @see PendingIntent
6326 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006327 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
6328 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 String packageName = getPackageName();
6330 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006331 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006333 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07006334 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07006336 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006337 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 return target != null ? new PendingIntent(target) : null;
6339 } catch (RemoteException e) {
6340 // Empty
6341 }
6342 return null;
6343 }
6344
6345 /**
6346 * Change the desired orientation of this activity. If the activity
6347 * is currently in the foreground or otherwise impacting the screen
6348 * orientation, the screen will immediately be changed (possibly causing
6349 * the activity to be restarted). Otherwise, this will be used the next
6350 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07006351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 * @param requestedOrientation An orientation constant as used in
6353 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6354 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006355 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 if (mParent == null) {
6357 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006358 ActivityTaskManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 mToken, requestedOrientation);
6360 } catch (RemoteException e) {
6361 // Empty
6362 }
6363 } else {
6364 mParent.setRequestedOrientation(requestedOrientation);
6365 }
6366 }
RoboErik55011652014-07-09 15:05:53 -07006367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 /**
6369 * Return the current requested orientation of the activity. This will
6370 * either be the orientation requested in its component's manifest, or
6371 * the last requested orientation given to
6372 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07006373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 * @return Returns an orientation constant as used in
6375 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
6376 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006377 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 public int getRequestedOrientation() {
6379 if (mParent == null) {
6380 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006381 return ActivityTaskManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006382 .getRequestedOrientation(mToken);
6383 } catch (RemoteException e) {
6384 // Empty
6385 }
6386 } else {
6387 return mParent.getRequestedOrientation();
6388 }
6389 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
6390 }
RoboErik55011652014-07-09 15:05:53 -07006391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 /**
6393 * Return the identifier of the task this activity is in. This identifier
6394 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07006395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 * @return Task identifier, an opaque integer.
6397 */
6398 public int getTaskId() {
6399 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006400 return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 } catch (RemoteException e) {
6402 return -1;
6403 }
6404 }
6405
6406 /**
6407 * Return whether this activity is the root of a task. The root is the
6408 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07006409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 * @return True if this is the root activity, else false.
6411 */
Winson Chung4d8681f2017-05-23 16:22:08 -07006412 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006413 public boolean isTaskRoot() {
6414 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006415 return ActivityTaskManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 } catch (RemoteException e) {
6417 return false;
6418 }
6419 }
6420
6421 /**
6422 * Move the task containing this activity to the back of the activity
6423 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07006424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 * @param nonRoot If false then this only works if the activity is the root
6426 * of a task; if true it will work for any activity in
6427 * a task.
RoboErik55011652014-07-09 15:05:53 -07006428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 * @return If the task was moved (or it was already at the
6430 * back) true is returned, else false.
6431 */
6432 public boolean moveTaskToBack(boolean nonRoot) {
6433 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006434 return ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435 } catch (RemoteException e) {
6436 // Empty
6437 }
6438 return false;
6439 }
6440
6441 /**
6442 * Returns class name for this activity with the package prefix removed.
6443 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07006444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 * @return The local class name.
6446 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006447 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 public String getLocalClassName() {
6449 final String pkg = getPackageName();
6450 final String cls = mComponent.getClassName();
6451 int packageLen = pkg.length();
6452 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6453 || cls.charAt(packageLen) != '.') {
6454 return cls;
6455 }
6456 return cls.substring(packageLen+1);
6457 }
RoboErik55011652014-07-09 15:05:53 -07006458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006460 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 * @return Returns the complete component name for this activity
6463 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006464 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 return mComponent;
6466 }
6467
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006468 /** @hide */
6469 @Override
6470 public final ComponentName autofillClientGetComponentName() {
6471 return getComponentName();
6472 }
6473
Adam He6240eab2019-02-25 13:34:45 -08006474 /** @hide */
6475 @Override
6476 public final ComponentName contentCaptureClientGetComponentName() {
6477 return getComponentName();
6478 }
6479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006480 /**
6481 * Retrieve a {@link SharedPreferences} object for accessing preferences
6482 * that are private to this activity. This simply calls the underlying
6483 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6484 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006485 *
6486 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006487 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 *
6489 * @return Returns the single SharedPreferences instance that can be used
6490 * to retrieve and modify the preference values.
6491 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006492 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 return getSharedPreferences(getLocalClassName(), mode);
6494 }
RoboErik55011652014-07-09 15:05:53 -07006495
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006496 private void ensureSearchManager() {
6497 if (mSearchManager != null) {
6498 return;
6499 }
RoboErik55011652014-07-09 15:05:53 -07006500
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006501 try {
6502 mSearchManager = new SearchManager(this, null);
6503 } catch (ServiceNotFoundException e) {
6504 throw new IllegalStateException(e);
6505 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006506 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006509 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 if (getBaseContext() == null) {
6511 throw new IllegalStateException(
6512 "System services not available to Activities before onCreate()");
6513 }
6514
6515 if (WINDOW_SERVICE.equals(name)) {
6516 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006517 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006518 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006519 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 }
6521 return super.getSystemService(name);
6522 }
6523
6524 /**
6525 * Change the title associated with this activity. If this is a
6526 * top-level activity, the title for its window will change. If it
6527 * is an embedded activity, the parent can do whatever it wants
6528 * with it.
6529 */
6530 public void setTitle(CharSequence title) {
6531 mTitle = title;
6532 onTitleChanged(title, mTitleColor);
6533
6534 if (mParent != null) {
6535 mParent.onChildTitleChanged(this, title);
6536 }
6537 }
6538
6539 /**
6540 * Change the title associated with this activity. If this is a
6541 * top-level activity, the title for its window will change. If it
6542 * is an embedded activity, the parent can do whatever it wants
6543 * with it.
6544 */
6545 public void setTitle(int titleId) {
6546 setTitle(getText(titleId));
6547 }
6548
Alan Viverette2525d9c2013-11-15 14:42:19 -08006549 /**
6550 * Change the color of the title associated with this activity.
6551 * <p>
6552 * This method is deprecated starting in API Level 11 and replaced by action
6553 * bar styles. For information on styling the Action Bar, read the <a
6554 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6555 * guide.
6556 *
6557 * @deprecated Use action bar styles instead.
6558 */
6559 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 public void setTitleColor(int textColor) {
6561 mTitleColor = textColor;
6562 onTitleChanged(mTitle, textColor);
6563 }
6564
6565 public final CharSequence getTitle() {
6566 return mTitle;
6567 }
6568
6569 public final int getTitleColor() {
6570 return mTitleColor;
6571 }
6572
6573 protected void onTitleChanged(CharSequence title, int color) {
6574 if (mTitleReady) {
6575 final Window win = getWindow();
6576 if (win != null) {
6577 win.setTitle(title);
6578 if (color != 0) {
6579 win.setTitleColor(color);
6580 }
6581 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006582 if (mActionBar != null) {
6583 mActionBar.setWindowTitle(title);
6584 }
Adam Powella557fdc2014-08-21 18:05:53 -07006585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 }
6587
6588 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6589 }
6590
6591 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006592 * Sets information describing the task with this activity for presentation inside the Recents
6593 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6594 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6595 * for each property until a suitable value is found. For each task the taskDescription will be
6596 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006597 *
6598 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006599 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006600 *
Winson Chunga449dc02014-05-16 11:15:04 -07006601 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006602 */
Winson Chunga449dc02014-05-16 11:15:04 -07006603 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006604 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006605 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006606 // Scale the icon down to something reasonable if it is provided
6607 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6608 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6609 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6610 true);
6611 mTaskDescription.setIcon(icon);
6612 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006613 }
6614 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006615 ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006616 } catch (RemoteException e) {
6617 }
6618 }
6619
6620 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 * Sets the visibility of the progress bar in the title.
6622 * <p>
6623 * In order for the progress bar to be shown, the feature must be requested
6624 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006627 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006629 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 public final void setProgressBarVisibility(boolean visible) {
6631 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6632 Window.PROGRESS_VISIBILITY_OFF);
6633 }
6634
6635 /**
6636 * Sets the visibility of the indeterminate progress bar in the title.
6637 * <p>
6638 * In order for the progress bar to be shown, the feature must be requested
6639 * via {@link #requestWindowFeature(int)}.
6640 *
6641 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006642 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006644 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6646 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6647 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6648 }
RoboErik55011652014-07-09 15:05:53 -07006649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650 /**
6651 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6652 * is always indeterminate).
6653 * <p>
6654 * In order for the progress bar to be shown, the feature must be requested
6655 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006658 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006660 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 public final void setProgressBarIndeterminate(boolean indeterminate) {
6662 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006663 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6664 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 }
RoboErik55011652014-07-09 15:05:53 -07006666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667 /**
6668 * Sets the progress for the progress bars in the title.
6669 * <p>
6670 * In order for the progress bar to be shown, the feature must be requested
6671 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006672 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 * @param progress The progress for the progress bar. Valid ranges are from
6674 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6675 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006676 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006678 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 public final void setProgress(int progress) {
6680 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6681 }
RoboErik55011652014-07-09 15:05:53 -07006682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 /**
6684 * Sets the secondary progress for the progress bar in the title. This
6685 * progress is drawn between the primary progress (set via
6686 * {@link #setProgress(int)} and the background. It can be ideal for media
6687 * scenarios such as showing the buffering progress while the default
6688 * progress shows the play progress.
6689 * <p>
6690 * In order for the progress bar to be shown, the feature must be requested
6691 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006692 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006693 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6694 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006695 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006696 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006697 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 public final void setSecondaryProgress(int secondaryProgress) {
6699 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6700 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6701 }
6702
6703 /**
6704 * Suggests an audio stream whose volume should be changed by the hardware
6705 * volume controls.
6706 * <p>
6707 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006708 * Volume requests which are received while the Activity is in the
6709 * foreground will affect this stream.
6710 * <p>
6711 * It is not guaranteed that the hardware volume controls will always change
6712 * this stream's volume (for example, if a call is in progress, its stream's
6713 * volume may be changed instead). To reset back to the default, use
6714 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6715 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006717 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 */
6719 public final void setVolumeControlStream(int streamType) {
6720 getWindow().setVolumeControlStream(streamType);
6721 }
6722
6723 /**
6724 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006725 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006726 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 * @return The suggested audio stream type whose volume should be changed by
6728 * the hardware volume controls.
6729 * @see #setVolumeControlStream(int)
6730 */
6731 public final int getVolumeControlStream() {
6732 return getWindow().getVolumeControlStream();
6733 }
RoboErik55011652014-07-09 15:05:53 -07006734
6735 /**
6736 * Sets a {@link MediaController} to send media keys and volume changes to.
6737 * <p>
6738 * The controller will be tied to the window of this Activity. Media key and
6739 * volume events which are received while the Activity is in the foreground
6740 * will be forwarded to the controller and used to invoke transport controls
6741 * or adjust the volume. This may be used instead of or in addition to
6742 * {@link #setVolumeControlStream} to affect a specific session instead of a
6743 * specific stream.
6744 * <p>
6745 * It is not guaranteed that the hardware volume controls will always change
6746 * this session's volume (for example, if a call is in progress, its
6747 * stream's volume may be changed instead). To reset back to the default use
6748 * null as the controller.
6749 *
6750 * @param controller The controller for the session which should receive
6751 * media keys and volume changes.
6752 */
6753 public final void setMediaController(MediaController controller) {
6754 getWindow().setMediaController(controller);
6755 }
6756
6757 /**
6758 * Gets the controller which should be receiving media key and volume events
6759 * while this activity is in the foreground.
6760 *
6761 * @return The controller which should receive events.
6762 * @see #setMediaController(android.media.session.MediaController)
6763 */
6764 public final MediaController getMediaController() {
6765 return getWindow().getMediaController();
6766 }
6767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 /**
6769 * Runs the specified action on the UI thread. If the current thread is the UI
6770 * thread, then the action is executed immediately. If the current thread is
6771 * not the UI thread, the action is posted to the event queue of the UI thread.
6772 *
6773 * @param action the action to run on the UI thread
6774 */
6775 public final void runOnUiThread(Runnable action) {
6776 if (Thread.currentThread() != mUiThread) {
6777 mHandler.post(action);
6778 } else {
6779 action.run();
6780 }
6781 }
6782
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006783 /** @hide */
6784 @Override
6785 public final void autofillClientRunOnUiThread(Runnable action) {
6786 runOnUiThread(action);
6787 }
6788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006790 * Standard implementation of
6791 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6792 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006793 * This implementation does nothing and is for
6794 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6795 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6796 *
6797 * @see android.view.LayoutInflater#createView
6798 * @see android.view.Window#getLayoutInflater
6799 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006800 @Nullable
Ian Lake0c807f42018-09-12 14:03:27 -07006801 public View onCreateView(@NonNull String name, @NonNull Context context,
6802 @NonNull AttributeSet attrs) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006803 return null;
6804 }
6805
6806 /**
6807 * Standard implementation of
6808 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6809 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006810 * This implementation handles <fragment> tags to embed fragments inside
6811 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 *
6813 * @see android.view.LayoutInflater#createView
6814 * @see android.view.Window#getLayoutInflater
6815 */
Ian Lake0c807f42018-09-12 14:03:27 -07006816 @Nullable
6817 public View onCreateView(@Nullable View parent, @NonNull String name,
6818 @NonNull Context context, @NonNull AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006819 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006820 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006821 }
RoboErik55011652014-07-09 15:05:53 -07006822
Adam Powell371a8092014-06-20 12:51:12 -07006823 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 }
6825
Daniel Sandler69a48172010-06-23 16:29:36 -04006826 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006827 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006828 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006829 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006830 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006831 * @param fd The raw file descriptor that the dump is being sent to.
6832 * @param writer The PrintWriter to which you should dump your state. This will be
6833 * closed for you after you return.
6834 * @param args additional arguments to the dump request.
6835 */
Ian Lake140fe122018-09-13 14:59:38 -07006836 public void dump(@NonNull String prefix, @Nullable FileDescriptor fd,
6837 @NonNull PrintWriter writer, @Nullable String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006838 dumpInner(prefix, fd, writer, args);
6839 }
6840
Ian Lake140fe122018-09-13 14:59:38 -07006841 void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd,
6842 @NonNull PrintWriter writer, @Nullable String[] args) {
Felipe Lemee348dc32018-11-05 12:35:29 -08006843 if (args != null && args.length > 0) {
6844 // Handle special cases
6845 switch (args[0]) {
6846 case "--autofill":
6847 dumpAutofillManager(prefix, writer);
6848 return;
Felipe Lemeb718e032018-12-10 09:44:23 -08006849 case "--contentcapture":
6850 dumpContentCaptureManager(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006851 return;
6852 }
Felipe Lemeb546ca72018-08-15 08:44:12 -07006853 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006854 writer.print(prefix); writer.print("Local Activity ");
6855 writer.print(Integer.toHexString(System.identityHashCode(this)));
6856 writer.println(" State:");
6857 String innerPrefix = prefix + " ";
6858 writer.print(innerPrefix); writer.print("mResumed=");
6859 writer.print(mResumed); writer.print(" mStopped=");
6860 writer.print(mStopped); writer.print(" mFinished=");
6861 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006862 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6863 writer.println(mChangingConfigurations);
6864 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6865 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006866
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006867 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6868 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006869 if (mVoiceInteractor != null) {
6870 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6871 }
Jeff Brown5182c782013-10-15 20:31:52 -07006872
Michael Wright5f48dc72013-11-01 12:42:49 -07006873 if (getWindow() != null &&
6874 getWindow().peekDecorView() != null &&
6875 getWindow().peekDecorView().getViewRootImpl() != null) {
6876 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6877 }
Jeff Brown5182c782013-10-15 20:31:52 -07006878
6879 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07006880
Felipe Lemeb546ca72018-08-15 08:44:12 -07006881 dumpAutofillManager(prefix, writer);
Felipe Lemeb718e032018-12-10 09:44:23 -08006882 dumpContentCaptureManager(prefix, writer);
Felipe Lemeb546ca72018-08-15 08:44:12 -07006883
6884 ResourcesManager.getInstance().dump(prefix, writer);
6885 }
6886
6887 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08006888 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006889 if (afm != null) {
Felipe Lemeb546ca72018-08-15 08:44:12 -07006890 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08006891 writer.print(prefix); writer.print("Autofill Compat Mode: ");
6892 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07006893 } else {
6894 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07006895 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07006896 }
6897
Felipe Lemeb718e032018-12-10 09:44:23 -08006898 void dumpContentCaptureManager(String prefix, PrintWriter writer) {
6899 final ContentCaptureManager cm = getContentCaptureManager();
6900 if (cm != null) {
6901 cm.dump(prefix, writer);
Felipe Lemee348dc32018-11-05 12:35:29 -08006902 } else {
Felipe Lemeb718e032018-12-10 09:44:23 -08006903 writer.print(prefix); writer.println("No ContentCaptureManager");
Felipe Lemee348dc32018-11-05 12:35:29 -08006904 }
6905 }
6906
Dianne Hackborn625ac272010-09-17 18:29:22 -07006907 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006908 * Bit indicating that this activity is "immersive" and should not be
6909 * interrupted by notifications if possible.
6910 *
6911 * This value is initially set by the manifest property
6912 * <code>android:immersive</code> but may be changed at runtime by
6913 * {@link #setImmersive}.
6914 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006915 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006916 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6917 */
6918 public boolean isImmersive() {
6919 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006920 return ActivityTaskManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006921 } catch (RemoteException e) {
6922 return false;
6923 }
6924 }
6925
6926 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006927 * Indication of whether this is the highest level activity in this task. Can be used to
6928 * determine whether an activity launched by this activity was placed in the same task or
6929 * another task.
6930 *
6931 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006932 */
Sunny Goyald85bed52018-09-25 12:01:01 -07006933 final boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006934 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006935 return false;
6936 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006937 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006938 return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006939 } catch (RemoteException e) {
6940 return false;
6941 }
6942 }
6943
6944 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006945 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6946 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006947 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006948 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006949 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6950 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006951 * This call has no effect on non-translucent activities or on activities with the
6952 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006953 *
George Mount62ab9b72014-05-02 13:51:17 -07006954 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6955 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006956 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006957 *
6958 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006959 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006960 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006961 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006962 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006963 mTranslucentCallback = null;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07006964 if (ActivityTaskManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006965 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6966 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006967 } catch (RemoteException e) {
6968 // pass
6969 }
6970 }
6971
6972 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006973 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6974 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6975 * <p>
6976 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6977 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6978 * be called indicating that it is safe to make this activity translucent again. Until
6979 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6980 * behind the frontmost Activity will be indeterminate.
6981 * <p>
6982 * This call has no effect on non-translucent activities or on activities with the
6983 * {@link android.R.attr#windowIsFloating} attribute.
6984 *
6985 * @param callback the method to call when all visible Activities behind this one have been
6986 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006987 * @param options activity options delivered to the activity below this one. The options
6988 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006989 * @return <code>true</code> if Window was opaque and will become translucent or
6990 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006991 *
6992 * @see #convertFromTranslucent()
6993 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006994 *
6995 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006996 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006997 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006998 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006999 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007000 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007001 try {
7002 mTranslucentCallback = callback;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007003 mChangeCanvasToTranslucent = ActivityTaskManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007004 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07007005 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07007006 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007007 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07007008 // Make callback return as though it timed out.
7009 mChangeCanvasToTranslucent = false;
7010 drawComplete = false;
7011 }
7012 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
7013 // Window is already translucent.
7014 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07007015 }
George Mount3cc716c2014-06-12 16:35:35 -07007016 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07007017 }
7018
7019 /** @hide */
7020 void onTranslucentConversionComplete(boolean drawComplete) {
7021 if (mTranslucentCallback != null) {
7022 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
7023 mTranslucentCallback = null;
7024 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07007025 if (mChangeCanvasToTranslucent) {
7026 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
7027 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007028 }
7029
Craig Mautnereb8abf72014-07-02 15:04:09 -07007030 /** @hide */
7031 public void onNewActivityOptions(ActivityOptions options) {
7032 mActivityTransitionState.setEnterActivityOptions(this, options);
7033 if (!mStopped) {
7034 mActivityTransitionState.enterReady(this);
7035 }
7036 }
7037
Craig Mautner5eda9b32013-07-02 11:58:16 -07007038 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07007039 * Retrieve the ActivityOptions passed in from the launching activity or passed back
7040 * from an activity launched by this activity in its call to {@link
7041 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
7042 *
7043 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
7044 * @hide
7045 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007046 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07007047 ActivityOptions getActivityOptions() {
7048 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07007049 return ActivityOptions.fromBundle(
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007050 ActivityTaskManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07007051 } catch (RemoteException e) {
7052 }
7053 return null;
7054 }
7055
7056 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007057 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07007058 * this method anytime between the start of {@link #onResume()} and the return from
7059 * {@link #onPause()}. If this call is successful then the activity will remain visible after
7060 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
7061 *
7062 * <p>The actions of this call are reset each time that this activity is brought to the
7063 * front. That is, every time {@link #onResume()} is called the activity will be assumed
7064 * to not have requested visible behind. Therefore, if you want this activity to continue to
7065 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007066 *
7067 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
7068 * for dialog and translucent activities.
7069 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007070 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
7071 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
7072 *
7073 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07007074 * the next call to onResume.
7075 *
Bryce Leed59629e2017-04-18 14:35:29 -07007076 * @deprecated This method's functionality is no longer supported as of
7077 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7078 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007079 * @param visible true to notify the system that the activity wishes to be visible behind other
7080 * translucent activities, false to indicate otherwise. Resources must be
7081 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07007082 *
Craig Mautner64ccb702014-10-01 09:38:40 -07007083 * @return the resulting visibiity state. If true the activity will remain visible beyond
7084 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
7085 * then the activity may not count on being visible behind other translucent activities,
7086 * and must stop any media playback and release resources.
7087 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
7088 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007089 *
Jose Limafcf70832014-08-27 23:09:05 -07007090 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07007091 */
Bryce Leed59629e2017-04-18 14:35:29 -07007092 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007093 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07007094 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07007095 }
7096
7097 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007098 * Called when a translucent activity over this activity is becoming opaque or another
7099 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07007100 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007101 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007102 * <p>When this method is called the activity has 500 msec to release any resources it may be
7103 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007104 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07007105 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07007106 * process. Otherwise {@link #onStop()} will be called following return.
7107 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007108 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07007109 *
7110 * @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.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007112 */
Bryce Leed59629e2017-04-18 14:35:29 -07007113 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08007114 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07007115 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007116 mCalled = true;
7117 }
7118
7119 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007120 * Translucent activities may call this to determine if there is an activity below them that
7121 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007122 *
Bryce Leed59629e2017-04-18 14:35:29 -07007123 * @deprecated This method's functionality is no longer supported as of
7124 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7125 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007126 * @return true if an activity below is set to visible according to the most recent call to
7127 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007128 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007129 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007130 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007131 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07007132 * @hide
7133 */
Bryce Leed59629e2017-04-18 14:35:29 -07007134 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007135 @SystemApi
7136 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007137 return false;
7138 }
7139
7140 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07007141 * The topmost foreground activity will receive this call when the background visibility state
7142 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007143 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007144 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07007145 * due to a background activity finishing itself.
7146 *
Bryce Leed59629e2017-04-18 14:35:29 -07007147 * @deprecated This method's functionality is no longer supported as of
7148 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
7149 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007150 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07007151 *
Jose Lima4b6c6692014-08-12 17:41:12 -07007152 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07007153 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07007154 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07007155 */
Bryce Leed59629e2017-04-18 14:35:29 -07007156 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07007157 @SystemApi
7158 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07007159 }
7160
7161 /**
Craig Mautner8746a472014-07-24 15:12:54 -07007162 * Activities cannot draw during the period that their windows are animating in. In order
7163 * to know when it is safe to begin drawing they can override this method which will be
7164 * called when the entering animation has completed.
7165 */
7166 public void onEnterAnimationComplete() {
7167 }
7168
7169 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007170 * @hide
7171 */
7172 public void dispatchEnterAnimationComplete() {
chaviwfeb2e1e2018-12-19 17:24:11 -08007173 mEnterAnimationComplete = true;
7174 mInstrumentation.onEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007175 onEnterAnimationComplete();
7176 if (getWindow() != null && getWindow().getDecorView() != null) {
Felipe Leme01297692019-01-29 18:16:23 -08007177 View decorView = getWindow().getDecorView();
7178 decorView.getViewTreeObserver().dispatchOnEnterAnimationComplete();
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08007179 }
7180 }
7181
7182 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04007183 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07007184 *
Daniel Sandler69a48172010-06-23 16:29:36 -04007185 * Note that changing this value will have no effect on the activity's
7186 * {@link android.content.pm.ActivityInfo} structure; that is, if
7187 * <code>android:immersive</code> is set to <code>true</code>
7188 * in the application's manifest entry for this activity, the {@link
7189 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
7190 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7191 * FLAG_IMMERSIVE} bit set.
7192 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07007193 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04007194 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
7195 */
7196 public void setImmersive(boolean i) {
7197 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007198 ActivityTaskManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04007199 } catch (RemoteException e) {
7200 // pass
7201 }
7202 }
7203
Adam Powell6e346362010-07-23 10:18:23 -07007204 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07007205 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007206 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007207 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
7208 * while this Activity has user focus.</p>
7209 *
7210 * <p>It is recommended that applications additionally declare
7211 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
7212 * transitions when switching between VR activities.</p>
7213 *
7214 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
7215 * VR mode will not be started. Developers can handle this case as follows:</p>
7216 *
7217 * <pre>
7218 * String servicePackage = "com.whatever.app";
7219 * String serviceClass = "com.whatever.app.MyVrListenerService";
7220 *
7221 * // Name of the component of the VrListenerService to start.
7222 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
7223 *
7224 * try {
7225 * setVrModeEnabled(true, myComponentName);
7226 * } catch (PackageManager.NameNotFoundException e) {
7227 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
7228 * boolean isInstalled = false;
7229 * for (ApplicationInfo app : installed) {
7230 * if (app.packageName.equals(servicePackage)) {
7231 * isInstalled = true;
7232 * break;
7233 * }
7234 * }
7235 * if (isInstalled) {
7236 * // Package is installed, but not enabled in Settings. Let user enable it.
7237 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
7238 * } else {
7239 * // Package is not installed. Send an intent to download this.
7240 * sentIntentToLaunchAppStore(servicePackage);
7241 * }
7242 * }
7243 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007244 *
7245 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08007246 * @param requestedComponent the name of the component to use as a
7247 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
7248 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007249 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
7250 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
7251 * not been enabled in user settings.
7252 *
Ruben Brunk927d3452016-05-02 19:30:51 -07007253 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
7254 * @see android.service.vr.VrListenerService
7255 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
7256 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007257 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08007258 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
7259 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007260 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007261 if (ActivityTaskManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08007262 != 0) {
7263 throw new PackageManager.NameNotFoundException(
7264 requestedComponent.flattenToString());
7265 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08007266 } catch (RemoteException e) {
7267 // pass
7268 }
7269 }
7270
7271 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007272 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07007273 *
Clara Bayarri4423d912015-03-02 19:42:48 +00007274 * @param callback Callback that will manage lifecycle events for this action mode
7275 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07007276 *
7277 * @see ActionMode
7278 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007279 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07007280 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07007281 return mWindow.getDecorView().startActionMode(callback);
7282 }
7283
Adam Powelldebf3be2010-11-15 18:58:48 -08007284 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007285 * Start an action mode of the given type.
7286 *
7287 * @param callback Callback that will manage lifecycle events for this action mode
7288 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
7289 * @return The ActionMode that was started, or null if it was canceled
7290 *
7291 * @see ActionMode
7292 */
7293 @Nullable
7294 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
7295 return mWindow.getDecorView().startActionMode(callback, type);
7296 }
7297
7298 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007299 * Give the Activity a chance to control the UI for an action mode requested
7300 * by the system.
7301 *
7302 * <p>Note: If you are looking for a notification callback that an action mode
7303 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
7304 *
7305 * @param callback The callback that should control the new action mode
7306 * @return The new action mode, or <code>null</code> if the activity does not want to
7307 * provide special handling for this action mode. (It will be handled by the system.)
7308 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007309 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07007310 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007311 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00007312 // Only Primary ActionModes are represented in the ActionBar.
7313 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
7314 initWindowDecorActionBar();
7315 if (mActionBar != null) {
7316 return mActionBar.startActionMode(callback);
7317 }
Adam Powell6e346362010-07-23 10:18:23 -07007318 }
7319 return null;
7320 }
7321
Adam Powelldebf3be2010-11-15 18:58:48 -08007322 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00007323 * {@inheritDoc}
7324 */
7325 @Nullable
7326 @Override
7327 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
7328 try {
7329 mActionModeTypeStarting = type;
7330 return onWindowStartingActionMode(callback);
7331 } finally {
7332 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
7333 }
7334 }
7335
7336 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08007337 * Notifies the Activity that an action mode has been started.
7338 * Activity subclasses overriding this method should call the superclass implementation.
7339 *
7340 * @param mode The new action mode.
7341 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007342 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007343 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007344 public void onActionModeStarted(ActionMode mode) {
7345 }
7346
7347 /**
7348 * Notifies the activity that an action mode has finished.
7349 * Activity subclasses overriding this method should call the superclass implementation.
7350 *
7351 * @param mode The action mode that just finished.
7352 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08007353 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07007354 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08007355 public void onActionModeFinished(ActionMode mode) {
7356 }
7357
Adam Powelldd8fab22012-03-22 17:47:27 -07007358 /**
7359 * Returns true if the app should recreate the task when navigating 'up' from this activity
7360 * by using targetIntent.
7361 *
7362 * <p>If this method returns false the app can trivially call
7363 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
7364 * up navigation. If this method returns false, the app should synthesize a new task stack
7365 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
7366 *
7367 * @param targetIntent An intent representing the target destination for up navigation
7368 * @return true if navigating up should recreate a new task stack, false if the same task
7369 * should be used for the destination
7370 */
7371 public boolean shouldUpRecreateTask(Intent targetIntent) {
7372 try {
7373 PackageManager pm = getPackageManager();
7374 ComponentName cn = targetIntent.getComponent();
7375 if (cn == null) {
7376 cn = targetIntent.resolveActivity(pm);
7377 }
7378 ActivityInfo info = pm.getActivityInfo(cn, 0);
7379 if (info.taskAffinity == null) {
7380 return false;
7381 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007382 return ActivityTaskManager.getService().shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07007383 } catch (RemoteException e) {
7384 return false;
7385 } catch (NameNotFoundException e) {
7386 return false;
7387 }
7388 }
7389
7390 /**
7391 * Navigate from this activity to the activity specified by upIntent, finishing this activity
7392 * in the process. If the activity indicated by upIntent already exists in the task's history,
7393 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07007394 * finished.
7395 *
7396 * <p>If the indicated activity does not appear in the history stack, this will finish
7397 * each activity in this task until the root activity of the task is reached, resulting in
7398 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
7399 * when an activity may be reached by a path not passing through a canonical parent
7400 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07007401 *
7402 * <p>This method should be used when performing up navigation from within the same task
7403 * as the destination. If up navigation should cross tasks in some cases, see
7404 * {@link #shouldUpRecreateTask(Intent)}.</p>
7405 *
7406 * @param upIntent An intent representing the target destination for up navigation
7407 *
7408 * @return true if up navigation successfully reached the activity indicated by upIntent and
7409 * upIntent was delivered to it. false if an instance of the indicated activity could
7410 * not be found and this activity was simply finished normally.
7411 */
7412 public boolean navigateUpTo(Intent upIntent) {
7413 if (mParent == null) {
7414 ComponentName destInfo = upIntent.getComponent();
7415 if (destInfo == null) {
7416 destInfo = upIntent.resolveActivity(getPackageManager());
7417 if (destInfo == null) {
7418 return false;
7419 }
7420 upIntent = new Intent(upIntent);
7421 upIntent.setComponent(destInfo);
7422 }
7423 int resultCode;
7424 Intent resultData;
7425 synchronized (this) {
7426 resultCode = mResultCode;
7427 resultData = mResultData;
7428 }
7429 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007430 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07007431 }
7432 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07007433 upIntent.prepareToLeaveProcess(this);
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07007434 return ActivityTaskManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07007435 resultCode, resultData);
7436 } catch (RemoteException e) {
7437 return false;
7438 }
7439 } else {
7440 return mParent.navigateUpToFromChild(this, upIntent);
7441 }
7442 }
7443
7444 /**
7445 * This is called when a child activity of this one calls its
7446 * {@link #navigateUpTo} method. The default implementation simply calls
7447 * navigateUpTo(upIntent) on this activity (the parent).
7448 *
7449 * @param child The activity making the call.
7450 * @param upIntent An intent representing the target destination for up navigation
7451 *
7452 * @return true if up navigation successfully reached the activity indicated by upIntent and
7453 * upIntent was delivered to it. false if an instance of the indicated activity could
7454 * not be found and this activity was simply finished normally.
7455 */
7456 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
7457 return navigateUpTo(upIntent);
7458 }
7459
7460 /**
7461 * Obtain an {@link Intent} that will launch an explicit target activity specified by
7462 * this activity's logical parent. The logical parent is named in the application's manifest
7463 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07007464 * Activity subclasses may override this method to modify the Intent returned by
7465 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
7466 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07007467 *
Adam Powell04d58112012-04-09 10:22:12 -07007468 * @return a new Intent targeting the defined parent of this activity or null if
7469 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07007470 */
Tor Norbyed9273d62013-05-30 15:59:53 -07007471 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07007472 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07007473 final String parentName = mActivityInfo.parentActivityName;
7474 if (TextUtils.isEmpty(parentName)) {
7475 return null;
7476 }
Adam Powell5a4010c2012-09-16 15:14:05 -07007477
7478 // If the parent itself has no parent, generate a main activity intent.
7479 final ComponentName target = new ComponentName(this, parentName);
7480 try {
7481 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7482 final String parentActivity = parentInfo.parentActivityName;
7483 final Intent parentIntent = parentActivity == null
7484 ? Intent.makeMainActivity(target)
7485 : new Intent().setComponent(target);
7486 return parentIntent;
7487 } catch (NameNotFoundException e) {
7488 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7489 "' in manifest");
7490 return null;
7491 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007492 }
7493
George Mount31a21722014-03-24 17:44:36 -07007494 /**
George Mount62ab9b72014-05-02 13:51:17 -07007495 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007496 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007497 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007498 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007499 *
George Mount65580562014-08-29 08:15:48 -07007500 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007501 */
George Mount65580562014-08-29 08:15:48 -07007502 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7503 if (callback == null) {
7504 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007505 }
George Mount65580562014-08-29 08:15:48 -07007506 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007507 }
7508
7509 /**
7510 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007511 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007512 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7513 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007514 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007515 *
George Mount65580562014-08-29 08:15:48 -07007516 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007517 */
George Mount65580562014-08-29 08:15:48 -07007518 public void setExitSharedElementCallback(SharedElementCallback callback) {
7519 if (callback == null) {
7520 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007521 }
George Mount65580562014-08-29 08:15:48 -07007522 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007523 }
7524
George Mount8c2614c2014-06-10 11:12:01 -07007525 /**
7526 * Postpone the entering activity transition when Activity was started with
7527 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7528 * android.util.Pair[])}.
7529 * <p>This method gives the Activity the ability to delay starting the entering and
7530 * shared element transitions until all data is loaded. Until then, the Activity won't
7531 * draw into its window, leaving the window transparent. This may also cause the
7532 * returning animation to be delayed until data is ready. This method should be
7533 * called in {@link #onCreate(android.os.Bundle)} or in
7534 * {@link #onActivityReenter(int, android.content.Intent)}.
7535 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7536 * start the transitions. If the Activity did not use
7537 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7538 * android.util.Pair[])}, then this method does nothing.</p>
7539 */
7540 public void postponeEnterTransition() {
7541 mActivityTransitionState.postponeEnterTransition();
7542 }
7543
7544 /**
7545 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7546 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7547 * to have your Activity start drawing.
7548 */
7549 public void startPostponedEnterTransition() {
7550 mActivityTransitionState.startPostponedEnterTransition();
7551 }
7552
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007553 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007554 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7555 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007556 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007557 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7558 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007559 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007560 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7561 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7562 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7563 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007564 }
7565 return null;
7566 }
7567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007569
Mathew Inwood8c854f82018-09-14 12:35:36 +01007570 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 final void setParent(Activity parent) {
7572 mParent = parent;
7573 }
7574
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007575 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007576 final void attach(Context context, ActivityThread aThread,
7577 Instrumentation instr, IBinder token, int ident,
7578 Application application, Intent intent, ActivityInfo info,
7579 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007580 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007581 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07007582 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 attachBaseContext(context);
7584
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007585 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007586
Andrii Kulian51c1b672017-04-07 18:39:32 -07007587 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007588 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007590 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007591 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7593 mWindow.setSoftInputMode(info.softInputMode);
7594 }
Adam Powell269248d2011-08-02 10:26:54 -07007595 if (info.uiOptions != 0) {
7596 mWindow.setUiOptions(info.uiOptions);
7597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 mMainThread = aThread;
7601 mInstrumentation = instr;
7602 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007603 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 mApplication = application;
7605 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007606 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 mComponent = intent.getComponent();
7608 mActivityInfo = info;
7609 mTitle = title;
7610 mParent = parent;
7611 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007612 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007613 if (voiceInteractor != null) {
7614 if (lastNonConfigurationInstances != null) {
7615 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7616 } else {
7617 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7618 Looper.myLooper());
7619 }
7620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621
Jeff Brown98365d72012-08-19 20:30:52 -07007622 mWindow.setWindowManager(
7623 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7624 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007625 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 if (mParent != null) {
7627 mWindow.setContainer(mParent.getWindow());
7628 }
7629 mWindowManager = mWindow.getWindowManager();
7630 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007631
7632 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007633
Felipe Lemea4f39cd2019-02-19 15:08:59 -08007634 setAutofillOptions(application.getAutofillOptions());
Felipe Leme326f15a2019-02-19 09:42:24 -08007635 setContentCaptureOptions(application.getContentCaptureOptions());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007636 }
7637
7638 private void enableAutofillCompatibilityIfNeeded() {
7639 if (isAutofillCompatibilityEnabled()) {
7640 final AutofillManager afm = getSystemService(AutofillManager.class);
7641 if (afm != null) {
7642 afm.enableCompatibilityMode();
7643 }
7644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 }
7646
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007647 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007648 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007649 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 return mParent != null ? mParent.getActivityToken() : mToken;
7651 }
7652
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007653 /** @hide */
7654 @VisibleForTesting
7655 public final ActivityThread getActivityThread() {
7656 return mMainThread;
7657 }
7658
Winson Chung298f95b2017-08-10 15:57:18 -07007659 final void performCreate(Bundle icicle) {
7660 performCreate(icicle, null);
7661 }
7662
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007663 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007664 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007665 dispatchActivityPreCreated(icicle);
Winson Chung298f95b2017-08-10 15:57:18 -07007666 mCanEnterPictureInPicture = true;
7667 restoreHasCurrentPermissionRequest(icicle);
7668 if (persistentState != null) {
7669 onCreate(icicle, persistentState);
7670 } else {
7671 onCreate(icicle);
7672 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007673 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007674 mActivityTransitionState.readState(icicle);
7675
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007676 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7677 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007678 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007679 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Ian Lake8a88cd62018-09-28 13:25:20 -07007680 dispatchActivityPostCreated(icicle);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007681 }
George Mount0a778ed2013-12-13 13:35:36 -08007682
Ian Lake0b71b8e2018-09-13 13:56:26 -07007683 final void performNewIntent(@NonNull Intent intent) {
Winson Chung298f95b2017-08-10 15:57:18 -07007684 mCanEnterPictureInPicture = true;
7685 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007686 }
7687
Andrii Kuliand25680c2018-02-21 15:16:58 -08007688 final void performStart(String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007689 dispatchActivityPreStarted();
George Mount62ab9b72014-05-02 13:51:17 -07007690 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007691 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007693 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007695 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 if (!mCalled) {
7698 throw new SuperNotCalledException(
7699 "Activity " + mComponent.toShortString() +
7700 " did not call through to super.onStart()");
7701 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007702 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007703 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007704
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007705 boolean isAppDebuggable =
7706 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
7707
David Brazdil795cbfb2018-01-26 12:23:15 +00007708 // This property is set for all non-user builds except final release
David Brazdil927d6de2018-01-24 19:54:30 +00007709 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
7710
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007711 if (isAppDebuggable || isDlwarningEnabled) {
7712 String dlwarning = getDlWarning();
7713 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007714 String appName = getApplicationInfo().loadLabel(getPackageManager())
7715 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007716 String warning = "Detected problems with app native libraries\n" +
7717 "(please consult log for detail):\n" + dlwarning;
7718 if (isAppDebuggable) {
7719 new AlertDialog.Builder(this).
7720 setTitle(appName).
7721 setMessage(warning).
7722 setPositiveButton(android.R.string.ok, null).
7723 setCancelable(false).
7724 show();
7725 } else {
7726 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7727 }
7728 }
7729 }
7730
Tim Van Pattenddc43912018-12-18 17:47:52 -07007731 GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
7732
George Mount62ab9b72014-05-02 13:51:17 -07007733 mActivityTransitionState.enterReady(this);
Ian Lake8a88cd62018-09-28 13:25:20 -07007734 dispatchActivityPostStarted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 }
RoboErik55011652014-07-09 15:05:53 -07007736
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007737 /**
7738 * Restart the activity.
7739 * @param start Indicates whether the activity should also be started after restart.
7740 * The option to not start immediately is needed in case a transaction with
7741 * multiple lifecycle transitions is in progress.
7742 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007743 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007744 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007745 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007746
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007747 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007748 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007749 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7750 }
7751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007752 if (mStopped) {
7753 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007754
7755 synchronized (mManagedCursors) {
7756 final int N = mManagedCursors.size();
7757 for (int i=0; i<N; i++) {
7758 ManagedCursor mc = mManagedCursors.get(i);
7759 if (mc.mReleased || mc.mUpdated) {
7760 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007761 if (getApplicationInfo().targetSdkVersion
7762 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7763 throw new IllegalStateException(
7764 "trying to requery an already closed cursor "
7765 + mc.mCursor);
7766 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007767 }
7768 mc.mReleased = false;
7769 mc.mUpdated = false;
7770 }
7771 }
7772 }
7773
7774 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007776 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 if (!mCalled) {
7778 throw new SuperNotCalledException(
7779 "Activity " + mComponent.toShortString() +
7780 " did not call through to super.onRestart()");
7781 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007782 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007783 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 }
7786 }
RoboErik55011652014-07-09 15:05:53 -07007787
Andrii Kuliand25680c2018-02-21 15:16:58 -08007788 final void performResume(boolean followedByPause, String reason) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007789 dispatchActivityPreResumed();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007790 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007791
Dianne Hackborn445646c2010-06-25 15:52:59 -07007792 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007793
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007794 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007795
Dake Gu67decfa2017-12-27 11:48:08 -08007796 if (mAutoFillResetNeeded) {
7797 // When Activity is destroyed in paused state, and relaunch activity, there will be
7798 // extra onResume and onPause event, ignore the first onResume and onPause.
7799 // see ActivityThread.handleRelaunchActivity()
7800 mAutoFillIgnoreFirstResumePause = followedByPause;
7801 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7802 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7803 }
7804 }
7805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007807 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007808 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007809 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 if (!mCalled) {
7811 throw new SuperNotCalledException(
7812 "Activity " + mComponent.toShortString() +
7813 " did not call through to super.onResume()");
7814 }
7815
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007816 // invisible activities must be finished before onResume() completes
7817 if (!mVisibleFromClient && !mFinished) {
7818 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7819 if (getApplicationInfo().targetSdkVersion
7820 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7821 throw new IllegalStateException(
7822 "Activity " + mComponent.toShortString() +
7823 " did not call finish() prior to onResume() completing");
7824 }
7825 }
7826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007829
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007830 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007831 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 onPostResume();
7834 if (!mCalled) {
7835 throw new SuperNotCalledException(
7836 "Activity " + mComponent.toShortString() +
7837 " did not call through to super.onPostResume()");
7838 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007839 dispatchActivityPostResumed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 }
7841
7842 final void performPause() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007843 dispatchActivityPrePaused();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007844 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007845 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007846 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007848 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007849 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007850 if (!mCalled && getApplicationInfo().targetSdkVersion
7851 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7852 throw new SuperNotCalledException(
7853 "Activity " + mComponent.toShortString() +
7854 " did not call through to super.onPause()");
7855 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007856 dispatchActivityPostPaused();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 }
RoboErik55011652014-07-09 15:05:53 -07007858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007859 final void performUserLeaving() {
7860 onUserInteraction();
7861 onUserLeaveHint();
7862 }
RoboErik55011652014-07-09 15:05:53 -07007863
Andrii Kuliand25680c2018-02-21 15:16:58 -08007864 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007865 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007866 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007867
Winson Chung298f95b2017-08-10 15:57:18 -07007868 // Disallow entering picture-in-picture after the activity has been stopped
7869 mCanEnterPictureInPicture = false;
7870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007871 if (!mStopped) {
Ian Lake8a88cd62018-09-28 13:25:20 -07007872 dispatchActivityPreStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 if (mWindow != null) {
7874 mWindow.closeAllPanels();
7875 }
7876
Chong Zhang7687f252016-02-26 12:03:33 -08007877 // If we're preserving the window, don't setStoppedState to true, since we
7878 // need the window started immediately again. Stopping the window will
7879 // destroys hardware resources and causes flicker.
7880 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07007881 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08007882 }
RoboErik55011652014-07-09 15:05:53 -07007883
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007884 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07007885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 mCalled = false;
7887 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007888 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 if (!mCalled) {
7890 throw new SuperNotCalledException(
7891 "Activity " + mComponent.toShortString() +
7892 " did not call through to super.onStop()");
7893 }
RoboErik55011652014-07-09 15:05:53 -07007894
Makoto Onuki2f6a0182010-02-22 13:26:59 -08007895 synchronized (mManagedCursors) {
7896 final int N = mManagedCursors.size();
7897 for (int i=0; i<N; i++) {
7898 ManagedCursor mc = mManagedCursors.get(i);
7899 if (!mc.mReleased) {
7900 mc.mCursor.deactivate();
7901 mc.mReleased = true;
7902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 }
7904 }
George Mount0a778ed2013-12-13 13:35:36 -08007905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 mStopped = true;
Ian Lake8a88cd62018-09-28 13:25:20 -07007907 dispatchActivityPostStopped();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 }
7909 mResumed = false;
7910 }
7911
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007912 final void performDestroy() {
Ian Lake8a88cd62018-09-28 13:25:20 -07007913 dispatchActivityPreDestroyed();
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07007914 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07007915 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007916 mFragments.dispatchDestroy();
7917 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007918 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007919 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07007920 if (mVoiceInteractor != null) {
7921 mVoiceInteractor.detachActivity();
7922 }
Ian Lake8a88cd62018-09-28 13:25:20 -07007923 dispatchActivityPostDestroyed();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007924 }
George Mount0a778ed2013-12-13 13:35:36 -08007925
Winson Chung5af42fc2017-03-24 17:11:33 -07007926 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
7927 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007928 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007929 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
7930 + " " + newConfig);
7931 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007932 if (mWindow != null) {
7933 mWindow.onMultiWindowModeChanged();
7934 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007935 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007936 }
7937
Winson Chung5af42fc2017-03-24 17:11:33 -07007938 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
7939 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007940 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007941 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
7942 + " " + newConfig);
7943 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07007944 if (mWindow != null) {
7945 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
7946 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007947 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007948 }
7949
George Mount0a778ed2013-12-13 13:35:36 -08007950 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06007951 * @hide
7952 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007953 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06007954 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955 return mResumed;
7956 }
7957
Svetoslavffb32b12015-10-15 16:54:00 -07007958 private void storeHasCurrentPermissionRequest(Bundle bundle) {
7959 if (bundle != null && mHasCurrentPermissionsRequest) {
7960 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
7961 }
7962 }
7963
7964 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
7965 if (bundle != null) {
7966 mHasCurrentPermissionsRequest = bundle.getBoolean(
7967 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
7968 }
7969 }
7970
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007971 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07007972 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
7973 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07007974 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
7976 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007977 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007979 onActivityResult(requestCode, resultCode, data);
7980 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
7981 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
7982 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08007983 dispatchRequestPermissionsResult(requestCode, data);
7984 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00007985 Fragment frag = mFragments.findFragmentByWho(who);
7986 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007987 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08007988 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07007989 }
Svetoslav970b59c2015-06-09 16:05:21 -07007990 } else if (who.startsWith("@android:view:")) {
7991 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
7992 getActivityToken());
7993 for (ViewRootImpl viewRoot : views) {
7994 if (viewRoot.getView() != null
7995 && viewRoot.getView().dispatchActivityResult(
7996 who, requestCode, resultCode, data)) {
7997 return;
7998 }
7999 }
Svet Ganov782043c2017-02-11 00:52:02 +00008000 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08008001 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08008002 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07008003 } else {
8004 Fragment frag = mFragments.findFragmentByWho(who);
8005 if (frag != null) {
8006 frag.onActivityResult(requestCode, resultCode, data);
8007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07008009 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07008011
Jason Monka57e5e02014-05-07 10:06:48 -04008012 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008013 * Request to put this activity in a mode where the user is locked to a restricted set of
8014 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04008015 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008016 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
8017 * for this component, the current task will be launched directly into LockTask mode. Only apps
8018 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
8019 * be launched while LockTask mode is active. The user will not be able to leave this mode
8020 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
8021 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04008022 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008023 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
8024 * system will prompt the user with a dialog requesting permission to use this mode.
8025 * The user can exit at any time through instructions shown on the request dialog. Calling
8026 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04008027 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008028 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
8029 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008030 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008031 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008032 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04008033 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008034 public void startLockTask() {
8035 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008036 ActivityTaskManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008037 } catch (RemoteException e) {
8038 }
8039 }
8040
Jason Monka57e5e02014-05-07 10:06:48 -04008041 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01008042 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04008043 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008044 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
8045 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04008046 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008047 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
8048 * by this activity, then calling this method will not terminate the LockTask mode, but only
8049 * finish its own task. The device will remain in LockTask mode, until the activity which
8050 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
8051 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07008052 *
Charles Heff9b4dff2017-09-22 10:18:37 +01008053 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07008054 * @see android.R.attr#lockTaskMode
8055 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04008056 */
Craig Mautneraea74a52014-03-08 14:23:10 -08008057 public void stopLockTask() {
8058 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008059 ActivityTaskManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08008060 } catch (RemoteException e) {
8061 }
8062 }
8063
Craig Mautner5eda9b32013-07-02 11:58:16 -07008064 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008065 * Shows the user the system defined message for telling the user how to exit
8066 * lock task mode. The task containing this activity must be in lock task mode at the time
8067 * of this call for the message to be displayed.
8068 */
8069 public void showLockTaskEscapeMessage() {
8070 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008071 ActivityTaskManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07008072 } catch (RemoteException e) {
8073 }
8074 }
8075
8076 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07008077 * Check whether the caption on freeform windows is displayed directly on the content.
8078 *
8079 * @return True if caption is displayed on content, false if it pushes the content down.
8080 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08008081 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07008082 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07008083 */
8084 public boolean isOverlayWithDecorCaptionEnabled() {
8085 return mWindow.isOverlayWithDecorCaptionEnabled();
8086 }
8087
8088 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08008089 * Set whether the caption should displayed directly on the content rather than push it down.
8090 *
8091 * This affects only freeform windows since they display the caption and only the main
8092 * window of the activity. The caption is used to drag the window around and also shows
8093 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07008094 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08008095 */
Andrii Kulian933076d2016-03-29 17:04:42 -07008096 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
8097 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08008098 }
8099
8100 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07008101 * Interface for informing a translucent {@link Activity} once all visible activities below it
8102 * have completed drawing. This is necessary only after an {@link Activity} has been made
8103 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
8104 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07008105 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
8106 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07008107 *
8108 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07008109 */
Jose Lima14914852014-08-14 09:14:12 -07008110 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07008111 public interface TranslucentConversionListener {
8112 /**
8113 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
8114 * below the top one have been redrawn. Following this callback it is safe to make the top
8115 * Activity translucent because the underlying Activity has been drawn.
8116 *
8117 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
8118 * occurred waiting for the Activity to complete drawing.
8119 *
8120 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07008121 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07008122 */
8123 public void onTranslucentConversionComplete(boolean drawComplete);
8124 }
Svetoslavc6d1c342015-02-26 14:44:43 -08008125
8126 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07008127 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07008128 // If the package installer crashed we may have not data - best effort.
8129 String[] permissions = (data != null) ? data.getStringArrayExtra(
8130 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8131 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8132 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08008133 onRequestPermissionsResult(requestCode, permissions, grantResults);
8134 }
8135
8136 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07008137 Fragment fragment) {
8138 // If the package installer crashed we may have not data - best effort.
8139 String[] permissions = (data != null) ? data.getStringArrayExtra(
8140 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
8141 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
8142 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
8143 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08008144 }
8145
Svet Ganov782043c2017-02-11 00:52:02 +00008146 /** @hide */
8147 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008148 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008149 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00008150 try {
8151 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07008152 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00008153 } catch (IntentSender.SendIntentException e) {
8154 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
8155 }
8156 }
8157
Svet Ganov17db9dc2017-02-21 19:54:31 -08008158 /** @hide */
8159 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008160 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08008161 mAutoFillResetNeeded = true;
8162 }
8163
Felipe Leme4753bb02017-03-22 20:24:00 -07008164 /** @hide */
8165 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008166 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07008167 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07008168 final boolean wasShowing;
8169
8170 if (mAutofillPopupWindow == null) {
8171 wasShowing = false;
8172 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
8173 } else {
8174 wasShowing = mAutofillPopupWindow.isShowing();
8175 }
Svet Ganov374cae12017-05-10 13:42:33 -07008176 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07008177
8178 return !wasShowing && mAutofillPopupWindow.isShowing();
8179 }
8180
8181 /** @hide */
8182 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08008183 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
8184 @NonNull KeyEvent keyEvent) {
8185 ViewRootImpl rootImpl = anchor.getViewRootImpl();
8186 if (rootImpl != null) {
8187 // dont care if anchorView is current focus, for example a custom view may only receive
8188 // touchEvent, not focusable but can still trigger autofill window. The Key handling
8189 // might be inside parent of the custom view.
8190 rootImpl.dispatchKeyFromAutofill(keyEvent);
8191 }
8192 }
8193
8194 /** @hide */
8195 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008196 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07008197 if (mAutofillPopupWindow == null) {
8198 return false;
8199 }
8200 mAutofillPopupWindow.dismiss();
8201 mAutofillPopupWindow = null;
8202 return true;
8203 }
8204
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008205 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07008206 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008207 public final boolean autofillClientIsFillUiShowing() {
8208 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
8209 }
8210
8211 /** @hide */
8212 @Override
8213 @NonNull
8214 public final View[] autofillClientFindViewsByAutofillIdTraversal(
8215 @NonNull AutofillId[] autofillId) {
8216 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008217 final ArrayList<ViewRootImpl> roots =
8218 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8219
8220 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8221 final View rootView = roots.get(rootNum).getView();
8222
8223 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008224 final int viewCount = autofillId.length;
8225 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008226 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07008227 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008228 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008229 }
8230 }
8231 }
8232 }
8233
8234 return views;
8235 }
8236
8237 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008238 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008239 @Nullable
8240 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07008241 final ArrayList<ViewRootImpl> roots =
8242 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
8243 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8244 final View rootView = roots.get(rootNum).getView();
8245
8246 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008247 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07008248 if (view != null) {
8249 return view;
8250 }
8251 }
8252 }
8253
8254 return null;
8255 }
8256
8257 /** @hide */
8258 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008259 public final @NonNull boolean[] autofillClientGetViewVisibility(
8260 @NonNull AutofillId[] autofillIds) {
8261 final int autofillIdCount = autofillIds.length;
8262 final boolean[] visible = new boolean[autofillIdCount];
8263 for (int i = 0; i < autofillIdCount; i++) {
8264 final AutofillId autofillId = autofillIds[i];
8265 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
8266 if (view != null) {
Felipe Lemea75333c2019-01-22 15:45:32 -08008267 if (!autofillId.isVirtualInt()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008268 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008269 } else {
Felipe Lemea75333c2019-01-22 15:45:32 -08008270 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildIntId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07008271 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008272 }
8273 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08008274 if (android.view.autofill.Helper.sVerbose) {
8275 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
8276 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008277 return visible;
8278 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008279
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008280 /** @hide */
8281 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
8282 int windowId) {
8283 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
8284 .getRootViews(getActivityToken());
8285 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
8286 final View rootView = roots.get(rootNum).getView();
8287 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
8288 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
8289 if (view != null) {
8290 return view;
8291 }
8292 }
8293 }
8294 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008295 }
8296
8297 /** @hide */
8298 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008299 public final @Nullable IBinder autofillClientGetActivityToken() {
8300 return getActivityToken();
8301 }
8302
8303 /** @hide */
8304 @Override
8305 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07008306 return !mStopped;
8307 }
8308
Dake Gu67decfa2017-12-27 11:48:08 -08008309 /** @hide */
8310 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08008311 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08008312 return isAutofillCompatibilityEnabled();
8313 }
8314
8315 /** @hide */
8316 @Override
8317 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08008318 return mAutoFillIgnoreFirstResumePause || !mResumed;
8319 }
8320
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008321 /**
8322 * If set to true, this indicates to the system that it should never take a
8323 * screenshot of the activity to be used as a representation while it is not in a started state.
8324 * <p>
8325 * Note that the system may use the window background of the theme instead to represent
8326 * the window when it is not running.
8327 * <p>
8328 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
8329 * this only affects the behavior when the activity's screenshot would be used as a
8330 * representation when the activity is not in a started state, i.e. in Overview. The system may
8331 * still take screenshots of the activity in other contexts; for example, when the user takes a
8332 * screenshot of the entire screen, or when the active
8333 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
8334 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
8335 *
8336 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
8337 * @hide
8338 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008339 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008340 public void setDisablePreviewScreenshots(boolean disable) {
8341 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008342 ActivityTaskManager.getService().setDisablePreviewScreenshots(mToken, disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01008343 } catch (RemoteException e) {
8344 Log.e(TAG, "Failed to call setDisablePreviewScreenshots", e);
8345 }
8346 }
8347
chaviw59b98852017-06-13 12:05:44 -07008348 /**
koprivaa1a78482018-10-09 10:09:23 -07008349 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07008350 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
8351 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
8352 * activity will remain in the resumed state visible on-top of the lock screen. This value can
8353 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
8354 *
8355 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
8356 * {@code false} otherwise.
8357 * @see #setTurnScreenOn(boolean)
8358 * @see android.R.attr#turnScreenOn
8359 * @see android.R.attr#showWhenLocked
8360 */
8361 public void setShowWhenLocked(boolean showWhenLocked) {
8362 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008363 ActivityTaskManager.getService().setShowWhenLocked(mToken, showWhenLocked);
chaviw59b98852017-06-13 12:05:44 -07008364 } catch (RemoteException e) {
8365 Log.e(TAG, "Failed to call setShowWhenLocked", e);
8366 }
8367 }
8368
8369 /**
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008370 * Specifies whether this {@link Activity} should be shown on top of the lock screen whenever
8371 * the lockscreen is up and this activity has another activity behind it with the showWhenLock
8372 * attribute set. That is, this activity is only visible on the lock screen if there is another
8373 * activity with the showWhenLock attribute visible at the same time on the lock screen. A use
8374 * case for this is permission dialogs, that should only be visible on the lock screen if their
8375 * requesting activity is also visible. This value can be set as a manifest attribute using
8376 * android.R.attr#inheritShowWhenLocked.
8377 *
8378 * @param inheritShowWhenLocked {@code true} to show the {@link Activity} on top of the lock
8379 * screen when this activity has another activity behind it with
8380 * the showWhenLock attribute set; {@code false} otherwise.
8381 * @see #setShowWhenLocked(boolean)
Issei Suzuki7c09f0b2019-02-28 14:45:50 +01008382 * @see android.R.attr#inheritShowWhenLocked
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008383 */
Issei Suzuki74e1eb22018-12-20 17:42:52 +01008384 public void setInheritShowWhenLocked(boolean inheritShowWhenLocked) {
8385 try {
8386 ActivityTaskManager.getService().setInheritShowWhenLocked(
8387 mToken, inheritShowWhenLocked);
8388 } catch (RemoteException e) {
8389 Log.e(TAG, "Failed to call setInheritShowWhenLocked", e);
8390 }
8391 }
8392
8393 /**
chaviw59b98852017-06-13 12:05:44 -07008394 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
8395 * Normally an activity will be transitioned to the stopped state if it is started while the
8396 * screen if off, but with this flag set the activity will cause the screen to turn on if the
8397 * activity will be visible and resumed due to the screen coming on. The screen will not be
8398 * turned on if the activity won't be visible after the screen is turned on. This flag is
8399 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
8400 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
8401 * if this flag is set and the activity calls {@link
8402 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
8403 * the screen will turn on.
8404 *
8405 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
8406 *
8407 * @see #setShowWhenLocked(boolean)
8408 * @see android.R.attr#turnScreenOn
8409 * @see android.R.attr#showWhenLocked
8410 */
8411 public void setTurnScreenOn(boolean turnScreenOn) {
8412 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008413 ActivityTaskManager.getService().setTurnScreenOn(mToken, turnScreenOn);
chaviw59b98852017-06-13 12:05:44 -07008414 } catch (RemoteException e) {
8415 Log.e(TAG, "Failed to call setTurnScreenOn", e);
8416 }
8417 }
8418
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008419 /**
8420 * Registers remote animations per transition type for this activity.
8421 *
8422 * @param definition The remote animation definition that defines which transition whould run
8423 * which remote animation.
8424 * @hide
8425 */
8426 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008427 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008428 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
8429 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07008430 ActivityTaskManager.getService().registerRemoteAnimations(mToken, definition);
Jorim Jaggif84e2f62018-01-16 14:17:59 +01008431 } catch (RemoteException e) {
8432 Log.e(TAG, "Failed to call registerRemoteAnimations", e);
8433 }
8434 }
8435
Andrii Kuliand25680c2018-02-21 15:16:58 -08008436 /** Log a lifecycle event for current user id and component class. */
8437 private void writeEventLog(int event, String reason) {
8438 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
8439 reason);
8440 }
8441
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008442 class HostCallbacks extends FragmentHostCallback<Activity> {
8443 public HostCallbacks() {
8444 super(Activity.this /*activity*/);
8445 }
8446
8447 @Override
8448 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
8449 Activity.this.dump(prefix, fd, writer, args);
8450 }
8451
8452 @Override
8453 public boolean onShouldSaveFragmentState(Fragment fragment) {
8454 return !isFinishing();
8455 }
8456
8457 @Override
8458 public LayoutInflater onGetLayoutInflater() {
8459 final LayoutInflater result = Activity.this.getLayoutInflater();
8460 if (onUseFragmentManagerInflaterFactory()) {
8461 return result.cloneInContext(Activity.this);
8462 }
8463 return result;
8464 }
8465
8466 @Override
8467 public boolean onUseFragmentManagerInflaterFactory() {
8468 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
8469 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
8470 }
8471
8472 @Override
8473 public Activity onGetHost() {
8474 return Activity.this;
8475 }
8476
8477 @Override
8478 public void onInvalidateOptionsMenu() {
8479 Activity.this.invalidateOptionsMenu();
8480 }
8481
8482 @Override
8483 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
8484 Bundle options) {
8485 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
8486 }
8487
8488 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01008489 public void onStartActivityAsUserFromFragment(
8490 Fragment fragment, Intent intent, int requestCode, Bundle options,
8491 UserHandle user) {
8492 Activity.this.startActivityAsUserFromFragment(
8493 fragment, intent, requestCode, options, user);
8494 }
8495
8496 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01008497 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
8498 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
8499 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
8500 if (mParent == null) {
8501 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
8502 flagsMask, flagsValues, options);
8503 } else if (options != null) {
8504 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
8505 fillInIntent, flagsMask, flagsValues, extraFlags, options);
8506 }
8507 }
8508
8509 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07008510 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
8511 int requestCode) {
8512 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
8513 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
8514 startActivityForResult(who, intent, requestCode, null);
8515 }
8516
8517 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008518 public boolean onHasWindowAnimations() {
8519 return getWindow() != null;
8520 }
8521
8522 @Override
8523 public int onGetWindowAnimations() {
8524 final Window w = getWindow();
8525 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8526 }
8527
Todd Kennedy434bd652015-05-04 12:29:50 -07008528 @Override
8529 public void onAttachFragment(Fragment fragment) {
8530 Activity.this.onAttachFragment(fragment);
8531 }
8532
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008533 @Nullable
8534 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008535 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008536 return Activity.this.findViewById(id);
8537 }
8538
8539 @Override
8540 public boolean onHasView() {
8541 final Window w = getWindow();
8542 return (w != null && w.peekDecorView() != null);
8543 }
8544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545}