blob: 2db7979a2a12713f84bef492a4cbcb36f591ffdb [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 Leme5b32ebe2018-02-15 12:52:19 -080020
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070021import static java.lang.Character.MIN_VALUE;
Winson Chung1af8eda2016-02-05 17:55:56 +000022
Tor Norbyec615c6f2015-03-02 10:11:44 -080023import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.DrawableRes;
25import android.annotation.IdRes;
26import android.annotation.IntDef;
27import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070028import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070029import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070030import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070031import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070032import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070033import android.annotation.SystemApi;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010034import android.annotation.UnsupportedAppUsage;
Amith Yamasanieeed06c2016-05-03 15:07:03 -070035import android.app.VoiceInteractor.Request;
Jason Monk62515be2014-05-21 16:06:19 -040036import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070037import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070038import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.ComponentName;
40import android.content.ContentResolver;
41import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050042import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070043import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070044import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070045import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.SharedPreferences;
47import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080048import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070049import android.content.pm.PackageManager;
50import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.res.Configuration;
52import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070053import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.database.Cursor;
55import android.graphics.Bitmap;
56import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080057import android.graphics.Color;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -070058import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.graphics.drawable.Drawable;
60import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070061import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.net.Uri;
Adam Powelld3c63a62016-06-09 12:36:16 -070063import android.os.BadParcelableException;
Dianne Hackborn8d374262009-09-14 21:21:52 -070064import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Handler;
67import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080068import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070069import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000070import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070071import android.os.RemoteException;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060072import android.os.ServiceManager.ServiceNotFoundException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080073import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080074import android.os.SystemProperties;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070075import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070078import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000080import android.transition.Scene;
81import android.transition.TransitionManager;
82import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
85import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070086import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070087import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000089import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070090import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070092import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.view.ContextThemeWrapper;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070094import android.view.DragAndDropPermissions;
Winson Chung1af8eda2016-02-05 17:55:56 +000095import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +010097import android.view.KeyboardShortcutGroup;
98import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.LayoutInflater;
100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
103import android.view.MotionEvent;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100104import android.view.RemoteAnimationDefinition;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700105import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700107import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700109import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000111import android.view.ViewRootImpl;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700112import android.view.ViewRootImpl.ActivityConfigCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000114import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700116import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700117import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov24c90452017-12-27 15:17:14 -0800118import android.view.autofill.AutofillId;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700119import android.view.autofill.AutofillManager;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700120import android.view.autofill.AutofillManager.AutofillClient;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700121import android.view.autofill.AutofillPopupWindow;
122import android.view.autofill.IAutofillWindowPresenter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800124import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000125import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100126
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700127import com.android.internal.annotations.GuardedBy;
Andrii Kuliandfbf9712018-03-08 15:42:24 -0800128import com.android.internal.annotations.VisibleForTesting;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700129import com.android.internal.app.IVoiceInteractor;
130import com.android.internal.app.ToolbarActionBar;
131import com.android.internal.app.WindowDecorActionBar;
Philip P. Moltmanne78c7712017-06-19 12:57:13 -0700132import com.android.internal.policy.PhoneWindow;
133
Mathieu Chartier67f02a12019-03-07 09:05:44 -0800134import dalvik.system.VMRuntime;
135
Dianne Hackborn625ac272010-09-17 18:29:22 -0700136import java.io.FileDescriptor;
137import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700138import java.lang.annotation.Retention;
139import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700140import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800141import java.util.Arrays;
Adam Powell6e346362010-07-23 10:18:23 -0700142import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700143import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Bryce Leee83f34cd2017-10-31 19:50:54 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146/**
147 * An activity is a single, focused thing that the user can do. Almost all
148 * activities interact with the user, so the Activity class takes care of
149 * creating a window for you in which you can place your UI with
150 * {@link #setContentView}. While activities are often presented to the user
151 * as full-screen windows, they can also be used in other ways: as floating
152 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
153 * or embedded inside of another activity (using {@link ActivityGroup}).
154 *
155 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * <ul>
158 * <li> {@link #onCreate} is where you initialize your activity. Most
159 * importantly, here you will usually call {@link #setContentView(int)}
160 * with a layout resource defining your UI, and using {@link #findViewById}
161 * to retrieve the widgets in that UI that you need to interact with
162 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 * <li> {@link #onPause} is where you deal with the user leaving your
165 * activity. Most importantly, any changes made by the user should at this
166 * point be committed (usually to the
167 * {@link android.content.ContentProvider} holding the data).
168 * </ul>
169 *
170 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
171 * activity classes must have a corresponding
172 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
173 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <p>Topics covered here:
176 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700177 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
179 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
180 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
181 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
182 * <li><a href="#Permissions">Permissions</a>
183 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
184 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700185 *
186 * <div class="special reference">
187 * <h3>Developer Guides</h3>
188 * <p>The Activity class is an important part of an application's overall lifecycle,
189 * and the way activities are launched and put together is a fundamental
190 * part of the platform's application model. For a detailed perspective on the structure of an
191 * Android application and how activities behave, please read the
192 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700193 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700194 * developer guides.</p>
195 *
196 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700197 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700198 * developer guide.</p>
199 * </div>
200 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700201 * <a name="Fragments"></a>
202 * <h3>Fragments</h3>
203 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800204 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
205 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700206 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800207 * screens, and help scale their application between small and large screens.</p>
208 *
209 * <p>For more information about using fragments, read the
210 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <a name="ActivityLifecycle"></a>
213 * <h3>Activity Lifecycle</h3>
214 *
215 * <p>Activities in the system are managed as an <em>activity stack</em>.
216 * When a new activity is started, it is placed on the top of the stack
217 * and becomes the running activity -- the previous activity always remains
218 * below it in the stack, and will not come to the foreground again until
219 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700220 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * <p>An activity has essentially four states:</p>
222 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700223 * <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 -0800224 * the stack),
225 * it is <em>active</em> or <em>running</em>. </li>
226 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700227 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * is <em>paused</em>. A paused activity is completely alive (it
229 * maintains all state and member information and remains attached to
230 * the window manager), but can be killed by the system in extreme
231 * low memory situations.
232 * <li>If an activity is completely obscured by another activity,
233 * it is <em>stopped</em>. It still retains all state and member information,
234 * however, it is no longer visible to the user so its window is hidden
235 * and it will often be killed by the system when memory is needed
236 * elsewhere.</li>
237 * <li>If an activity is paused or stopped, the system can drop the activity
238 * from memory by either asking it to finish, or simply killing its
239 * process. When it is displayed again to the user, it must be
240 * completely restarted and restored to its previous state.</li>
241 * </ul>
242 *
243 * <p>The following diagram shows the important state paths of an Activity.
244 * The square rectangles represent callback methods you can implement to
245 * perform operations when the Activity moves between states. The colored
246 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <p><img src="../../../images/activity_lifecycle.png"
249 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * <p>There are three key loops you may be interested in monitoring within your
252 * activity:
RoboErik55011652014-07-09 15:05:53 -0700253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <ul>
255 * <li>The <b>entire lifetime</b> of an activity happens between the first call
256 * to {@link android.app.Activity#onCreate} through to a single final call
257 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
258 * of "global" state in onCreate(), and release all remaining resources in
259 * onDestroy(). For example, if it has a thread running in the background
260 * to download data from the network, it may create that thread in onCreate()
261 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * <li>The <b>visible lifetime</b> of an activity happens between a call to
264 * {@link android.app.Activity#onStart} until a corresponding call to
265 * {@link android.app.Activity#onStop}. During this time the user can see the
266 * activity on-screen, though it may not be in the foreground and interacting
267 * with the user. Between these two methods you can maintain resources that
268 * are needed to show the activity to the user. For example, you can register
269 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900270 * that impact your UI, and unregister it in onStop() when the user no
271 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * can be called multiple times, as the activity becomes visible and hidden
273 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
276 * {@link android.app.Activity#onResume} until a corresponding call to
277 * {@link android.app.Activity#onPause}. During this time the activity is
278 * in front of all other activities and interacting with the user. An activity
279 * can frequently go between the resumed and paused states -- for example when
280 * the device goes to sleep, when an activity result is delivered, when a new
281 * intent is delivered -- so the code in these methods should be fairly
282 * lightweight.
283 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * <p>The entire lifecycle of an activity is defined by the following
286 * Activity methods. All of these are hooks that you can override
287 * to do appropriate work when the activity changes state. All
288 * activities will implement {@link android.app.Activity#onCreate}
289 * to do their initial setup; many will also implement
290 * {@link android.app.Activity#onPause} to commit changes to data and
291 * otherwise prepare to stop interacting with the user. You should always
292 * call up to your superclass when implementing these methods.</p>
293 *
294 * </p>
295 * <pre class="prettyprint">
296 * public class Activity extends ApplicationContext {
297 * protected void onCreate(Bundle savedInstanceState);
298 *
299 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700300 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 * protected void onRestart();
302 *
303 * protected void onResume();
304 *
305 * protected void onPause();
306 *
307 * protected void onStop();
308 *
309 * protected void onDestroy();
310 * }
311 * </pre>
312 *
313 * <p>In general the movement through an activity's lifecycle looks like
314 * this:</p>
315 *
316 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
317 * <colgroup align="left" span="3" />
318 * <colgroup align="left" />
319 * <colgroup align="center" />
320 * <colgroup align="center" />
321 *
322 * <thead>
323 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
324 * </thead>
325 *
326 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700327 * <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 -0800328 * <td>Called when the activity is first created.
329 * This is where you should do all of your normal static set up:
330 * create views, bind data to lists, etc. This method also
331 * provides you with a Bundle containing the activity's previously
332 * frozen state, if there was one.
333 * <p>Always followed by <code>onStart()</code>.</td>
334 * <td align="center">No</td>
335 * <td align="center"><code>onStart()</code></td>
336 * </tr>
337 *
338 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700339 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 * <td>Called after your activity has been stopped, prior to it being
341 * started again.
342 * <p>Always followed by <code>onStart()</code></td>
343 * <td align="center">No</td>
344 * <td align="center"><code>onStart()</code></td>
345 * </tr>
346 *
smain@google.com10e29b82016-06-23 12:09:30 -0700347 * <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 -0800348 * <td>Called when the activity is becoming visible to the user.
349 * <p>Followed by <code>onResume()</code> if the activity comes
350 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
351 * <td align="center">No</td>
352 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
353 * </tr>
354 *
355 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700356 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * <td>Called when the activity will start
358 * interacting with the user. At this point your activity is at
359 * the top of the activity stack, with user input going to it.
360 * <p>Always followed by <code>onPause()</code>.</td>
361 * <td align="center">No</td>
362 * <td align="center"><code>onPause()</code></td>
363 * </tr>
364 *
smain@google.com10e29b82016-06-23 12:09:30 -0700365 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * <td>Called when the system is about to start resuming a previous
367 * activity. This is typically used to commit unsaved changes to
368 * persistent data, stop animations and other things that may be consuming
369 * CPU, etc. Implementations of this method must be very quick because
370 * the next activity will not be resumed until this method returns.
371 * <p>Followed by either <code>onResume()</code> if the activity
372 * returns back to the front, or <code>onStop()</code> if it becomes
373 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800374 * <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 -0800375 * <td align="center"><code>onResume()</code> or<br>
376 * <code>onStop()</code></td>
377 * </tr>
378 *
smain@google.com10e29b82016-06-23 12:09:30 -0700379 * <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 -0800380 * <td>Called when the activity is no longer visible to the user, because
381 * another activity has been resumed and is covering this one. This
382 * may happen either because a new activity is being started, an existing
383 * one is being brought in front of this one, or this one is being
384 * destroyed.
385 * <p>Followed by either <code>onRestart()</code> if
386 * this activity is coming back to interact with the user, or
387 * <code>onDestroy()</code> if this activity is going away.</td>
388 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
389 * <td align="center"><code>onRestart()</code> or<br>
390 * <code>onDestroy()</code></td>
391 * </tr>
392 *
smain@google.com10e29b82016-06-23 12:09:30 -0700393 * <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 -0800394 * <td>The final call you receive before your
395 * activity is destroyed. This can happen either because the
396 * activity is finishing (someone called {@link Activity#finish} on
koprivafdb0bff2018-09-29 15:22:41 -0700397 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * instance of the activity to save space. You can distinguish
399 * between these two scenarios with the {@link
400 * Activity#isFinishing} method.</td>
401 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
402 * <td align="center"><em>nothing</em></td>
403 * </tr>
404 * </tbody>
405 * </table>
406 *
407 * <p>Note the "Killable" column in the above table -- for those methods that
408 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800409 * 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 -0800410 * of its code being executed. Because of this, you should use the
411 * {@link #onPause} method to write any persistent data (such as user edits)
412 * to storage. In addition, the method
413 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
414 * in such a background state, allowing you to save away any dynamic instance
415 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700416 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
418 * section for more information on how the lifecycle of a process is tied
419 * to the activities it is hosting. Note that it is important to save
420 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800421 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * be called in every situation as described in its documentation.</p>
423 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800424 * <p class="note">Be aware that these semantics will change slightly between
425 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
426 * vs. those targeting prior platforms. Starting with Honeycomb, an application
427 * is not in the killable state until its {@link #onStop} has returned. This
428 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700429 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800430 * wait until {@link #onStop()} to save persistent state.</p>
431 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800432 * <p class="note">For applications targeting platforms starting with
433 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
434 * will always be called after {@link #onStop}, so an application may safely
435 * perform fragment transactions in {@link #onStop} and will be able to save
436 * persistent state later.</p>
437 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * <p>For those methods that are not marked as being killable, the activity's
439 * process will not be killed by the system starting from the time the method
440 * is called and continuing after it returns. Thus an activity is in the killable
441 * state, for example, between after <code>onPause()</code> to the start of
442 * <code>onResume()</code>.</p>
443 *
444 * <a name="ConfigurationChanges"></a>
445 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700446 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 * <p>If the configuration of the device (as defined by the
448 * {@link Configuration Resources.Configuration} class) changes,
449 * then anything displaying a user interface will need to update to match that
450 * configuration. Because Activity is the primary mechanism for interacting
451 * with the user, it includes special support for handling configuration
452 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * <p>Unless you specify otherwise, a configuration change (such as a change
455 * in screen orientation, language, input devices, etc) will cause your
456 * current activity to be <em>destroyed</em>, going through the normal activity
457 * lifecycle process of {@link #onPause},
458 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
459 * had been in the foreground or visible to the user, once {@link #onDestroy} is
460 * called in that instance then a new instance of the activity will be
461 * created, with whatever savedInstanceState the previous instance had generated
462 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 * <p>This is done because any application resource,
465 * including layout files, can change based on any configuration value. Thus
466 * the only safe way to handle a configuration change is to re-retrieve all
467 * resources, including layouts, drawables, and strings. Because activities
468 * must already know how to save their state and re-create themselves from
469 * that state, this is a convenient way to have an activity restart itself
470 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700471 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 * <p>In some special cases, you may want to bypass restarting of your
473 * activity based on one or more types of configuration changes. This is
474 * done with the {@link android.R.attr#configChanges android:configChanges}
475 * attribute in its manifest. For any types of configuration changes you say
476 * that you handle there, you will receive a call to your current activity's
477 * {@link #onConfigurationChanged} method instead of being restarted. If
478 * a configuration change involves any that you do not handle, however, the
479 * activity will still be restarted and {@link #onConfigurationChanged}
480 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * <a name="StartingActivities"></a>
483 * <h3>Starting Activities and Getting Results</h3>
484 *
485 * <p>The {@link android.app.Activity#startActivity}
486 * method is used to start a
487 * new activity, which will be placed at the top of the activity stack. It
488 * takes a single argument, an {@link android.content.Intent Intent},
489 * which describes the activity
490 * to be executed.</p>
491 *
492 * <p>Sometimes you want to get a result back from an activity when it
493 * ends. For example, you may start an activity that lets the user pick
494 * a person in a list of contacts; when it ends, it returns the person
495 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700496 * {@link android.app.Activity#startActivityForResult(Intent, int)}
497 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700499 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 *
501 * <p>When an activity exits, it can call
502 * {@link android.app.Activity#setResult(int)}
503 * to return data back to its parent. It must always supply a result code,
504 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
505 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
506 * return back an Intent containing any additional data it wants. All of this
507 * information appears back on the
508 * parent's <code>Activity.onActivityResult()</code>, along with the integer
509 * identifier it originally supplied.</p>
510 *
511 * <p>If a child activity fails for any reason (such as crashing), the parent
512 * activity will receive a result with the code RESULT_CANCELED.</p>
513 *
514 * <pre class="prettyprint">
515 * public class MyActivity extends Activity {
516 * ...
517 *
518 * static final int PICK_CONTACT_REQUEST = 0;
519 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700520 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
522 * // When the user center presses, let them pick a contact.
523 * startActivityForResult(
524 * new Intent(Intent.ACTION_PICK,
525 * new Uri("content://contacts")),
526 * PICK_CONTACT_REQUEST);
527 * return true;
528 * }
529 * return false;
530 * }
531 *
532 * protected void onActivityResult(int requestCode, int resultCode,
533 * Intent data) {
534 * if (requestCode == PICK_CONTACT_REQUEST) {
535 * if (resultCode == RESULT_OK) {
536 * // A contact was picked. Here we will just display it
537 * // to the user.
538 * startActivity(new Intent(Intent.ACTION_VIEW, data));
539 * }
540 * }
541 * }
542 * }
543 * </pre>
544 *
545 * <a name="SavingPersistentState"></a>
546 * <h3>Saving Persistent State</h3>
547 *
koprivafc3949e2018-09-17 11:35:13 -0700548 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 * will deal with: shared document-like data (typically stored in a SQLite
550 * database using a {@linkplain android.content.ContentProvider content provider})
551 * and internal state such as user preferences.</p>
552 *
koprivafc3949e2018-09-17 11:35:13 -0700553 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 * "edit in place" user model. That is, any edits a user makes are effectively
555 * made immediately without requiring an additional confirmation step.
556 * Supporting this model is generally a simple matter of following two rules:</p>
557 *
558 * <ul>
559 * <li> <p>When creating a new document, the backing database entry or file for
560 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700561 * 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 -0800562 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700563 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
565 * commit to the backing content provider or file any changes the user
566 * has made. This ensures that those changes will be seen by any other
567 * activity that is about to run. You will probably want to commit
568 * your data even more aggressively at key times during your
569 * activity's lifecycle: for example before starting a new
570 * activity, before finishing your own activity, when the user
571 * switches between input fields, etc.</p>
572 * </ul>
573 *
574 * <p>This model is designed to prevent data loss when a user is navigating
575 * between activities, and allows the system to safely kill an activity (because
576 * system resources are needed somewhere else) at any time after it has been
577 * paused. Note this implies
578 * that the user pressing BACK from your activity does <em>not</em>
579 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800580 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
582 *
583 * <p>See the {@linkplain android.content.ContentProvider content package} for
584 * more information about content providers. These are a key aspect of how
585 * different activities invoke and propagate data between themselves.</p>
586 *
587 * <p>The Activity class also provides an API for managing internal persistent state
588 * associated with an activity. This can be used, for example, to remember
589 * the user's preferred initial display in a calendar (day view or week view)
590 * or the user's default home page in a web browser.</p>
591 *
592 * <p>Activity persistent state is managed
593 * with the method {@link #getPreferences},
594 * allowing you to retrieve and
595 * modify a set of name/value pairs associated with the activity. To use
596 * preferences that are shared across multiple application components
597 * (activities, receivers, services, providers), you can use the underlying
598 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
599 * to retrieve a preferences
600 * object stored under a specific name.
601 * (Note that it is not possible to share settings data across application
602 * packages -- for that you will need a content provider.)</p>
603 *
604 * <p>Here is an excerpt from a calendar activity that stores the user's
605 * preferred view mode in its persistent settings:</p>
606 *
607 * <pre class="prettyprint">
608 * public class CalendarActivity extends Activity {
609 * ...
610 *
611 * static final int DAY_VIEW_MODE = 0;
612 * static final int WEEK_VIEW_MODE = 1;
613 *
614 * private SharedPreferences mPrefs;
615 * private int mCurViewMode;
616 *
617 * protected void onCreate(Bundle savedInstanceState) {
618 * super.onCreate(savedInstanceState);
619 *
620 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700621 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * }
623 *
624 * protected void onPause() {
625 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700626 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * SharedPreferences.Editor ed = mPrefs.edit();
628 * ed.putInt("view_mode", mCurViewMode);
629 * ed.commit();
630 * }
631 * }
632 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 * <a name="Permissions"></a>
635 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700636 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 * <p>The ability to start a particular Activity can be enforced when it is
638 * declared in its
639 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
640 * tag. By doing so, other applications will need to declare a corresponding
641 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
642 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800643 *
644 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
645 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
646 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
647 * Activity access to the specific URIs in the Intent. Access will remain
648 * until the Activity has finished (it will remain across the hosting
649 * process being killed and other temporary destruction). As of
650 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
651 * was already created and a new Intent is being delivered to
652 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
653 * to the existing ones it holds.
654 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
656 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 * <a name="ProcessLifecycle"></a>
659 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700660 *
kopriva4dddc632018-03-19 14:13:08 -0700661 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700663 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700665 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700667 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 * processes (the last ones) before it resorts to killing more important
669 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700670 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * <ol>
672 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
673 * that the user is currently interacting with) is considered the most important.
674 * Its process will only be killed as a last resort, if it uses more memory
675 * than is available on the device. Generally at this point the device has
676 * reached a memory paging state, so this is required in order to keep the user
677 * interface responsive.
678 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
679 * but not in the foreground, such as one sitting behind a foreground dialog)
680 * is considered extremely important and will not be killed unless that is
681 * required to keep the foreground activity running.
682 * <li> <p>A <b>background activity</b> (an activity that is not visible to
683 * the user and has been paused) is no longer critical, so the system may
684 * safely kill its process to reclaim memory for other foreground or
685 * visible processes. If its process needs to be killed, when the user navigates
686 * back to the activity (making it visible on the screen again), its
687 * {@link #onCreate} method will be called with the savedInstanceState it had previously
688 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
689 * state as the user last left it.
690 * <li> <p>An <b>empty process</b> is one hosting no activities or other
691 * application components (such as {@link Service} or
692 * {@link android.content.BroadcastReceiver} classes). These are killed very
693 * quickly by the system as memory becomes low. For this reason, any
694 * background operation you do outside of an activity must be executed in the
695 * context of an activity BroadcastReceiver or Service to ensure that the system
696 * knows it needs to keep your process around.
697 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700698 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 * <p>Sometimes an Activity may need to do a long-running operation that exists
700 * independently of the activity lifecycle itself. An example may be a camera
701 * application that allows you to upload a picture to a web site. The upload
702 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700703 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * should start a {@link Service} in which the upload takes place. This allows
705 * the system to properly prioritize your process (considering it to be more
706 * important than other non-visible applications) for the duration of the
707 * upload, independent of whether the original activity is paused, stopped,
708 * or finished.
709 */
710public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700711 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700713 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000714 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Leme640f30a2017-03-06 15:44:06 -0800715 AutofillManager.AutofillClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700717 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718
719 /** Standard activity result: operation canceled. */
720 public static final int RESULT_CANCELED = 0;
721 /** Standard activity result: operation succeeded. */
722 public static final int RESULT_OK = -1;
723 /** Start of user-defined activity results. */
724 public static final int RESULT_FIRST_USER = 1;
725
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700726 /** @hide Task isn't finished when activity is finished */
727 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700728 /**
729 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
730 * past behavior the task is also removed from recents.
731 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700732 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700733 /**
734 * @hide Task is finished along with the finishing activity, but it is not removed from
735 * recents.
736 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700737 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
738
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100739 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700740 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700741 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700742
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700743 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
745 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
746 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
747 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800748 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700749 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
750 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751
Svetoslav970b59c2015-06-09 16:05:21 -0700752 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000753 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700754
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100755 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100756
Andrii Kuliand25680c2018-02-21 15:16:58 -0800757 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
758 private static final int LOG_AM_ON_START_CALLED = 30059;
759 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
760 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
761 private static final int LOG_AM_ON_STOP_CALLED = 30049;
762 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
763 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700764 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800765
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800766 private static class ManagedDialog {
767 Dialog mDialog;
768 Bundle mArgs;
769 }
770 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771
772 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100773 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 private Instrumentation mInstrumentation;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100775 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 private IBinder mToken;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100777 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700778 private int mIdent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100779 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 /*package*/ String mEmbeddedID;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100781 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 private Application mApplication;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100783 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700784 /*package*/ Intent mIntent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100785 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800786 /*package*/ String mReferrer;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100787 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 private ComponentName mComponent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100789 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100791 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 /*package*/ ActivityThread mMainThread;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100793 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 Activity mParent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100795 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 boolean mCalled;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100797 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600798 /*package*/ boolean mResumed;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100799 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700800 /*package*/ boolean mStopped;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100801 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 boolean mFinished;
803 boolean mStartedActivity;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100804 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700805 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700806 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200807 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700808
809 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
810 * This only affects the client-side exception, the actual state check still happens in AMS. */
811 private boolean mCanEnterPictureInPicture = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700812 /** true if the activity is going through a transient pause */
813 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500814 /** true if the activity is being destroyed in order to recreate it with a new configuration */
815 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100816 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 /*package*/ int mConfigChangeFlags;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100818 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100820 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700821 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700823 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
824 @Nullable private AutofillManager mAutofillManager;
825
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700826 static final class NonConfigurationInstances {
827 Object activity;
828 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800829 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700830 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700831 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700832 }
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100833 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700834 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700835
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100836 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 private Window mWindow;
838
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100839 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 private WindowManager mWindowManager;
841 /*package*/ View mDecor = null;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100842 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 /*package*/ boolean mWindowAdded = false;
844 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood55418ea2018-12-20 15:30:45 +0000845 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700847 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700848 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849
Mathew Inwood45d2c252018-09-14 12:35:36 +0100850 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn91097de2014-04-04 18:02:06 -0700851 private VoiceInteractor mVoiceInteractor;
852
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100853 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 private CharSequence mTitle;
855 private int mTitleColor = 0;
856
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700857 // we must have a handler before the FragmentController is constructed
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100858 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700859 final Handler mHandler = new Handler();
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100860 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700861 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 private static final class ManagedCursor {
864 ManagedCursor(Cursor cursor) {
865 mCursor = cursor;
866 mReleased = false;
867 mUpdated = false;
868 }
869
870 private final Cursor mCursor;
871 private boolean mReleased;
872 private boolean mUpdated;
873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700875 @GuardedBy("mManagedCursors")
876 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
877
878 @GuardedBy("this")
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100879 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700881 @GuardedBy("this")
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100882 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700884
Craig Mautner5eda9b32013-07-02 11:58:16 -0700885 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700886 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700888 private SearchEvent mSearchEvent;
889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000891 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892
893 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
894 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700895
Winsonb6403152016-02-23 13:32:09 -0800896 private ActivityManager.TaskDescription mTaskDescription =
897 new ActivityManager.TaskDescription();
898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
900
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700901 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700902 private final Object mInstanceTracker = StrictMode.trackActivity(this);
903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700905
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100906 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700907 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700908 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
909 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910
Svetoslavffb32b12015-10-15 16:54:00 -0700911 private boolean mHasCurrentPermissionsRequest;
912
Svet Ganov17db9dc2017-02-21 19:54:31 -0800913 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800914 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800915
Phil Weaver846cda932017-06-15 10:10:06 -0700916 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
917 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700918
Felipe Leme4753bb02017-03-22 20:24:00 -0700919 private AutofillPopupWindow mAutofillPopupWindow;
920
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800921 private static native String getDlWarning();
922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 /** Return the intent that started this activity. */
924 public Intent getIntent() {
925 return mIntent;
926 }
927
RoboErik55011652014-07-09 15:05:53 -0700928 /**
929 * Change the intent returned by {@link #getIntent}. This holds a
930 * reference to the given intent; it does not copy it. Often used in
931 * conjunction with {@link #onNewIntent}.
932 *
933 * @param newIntent The new Intent object to return from getIntent
934 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 * @see #getIntent
936 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700937 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public void setIntent(Intent newIntent) {
939 mIntent = newIntent;
940 }
941
942 /** Return the application that owns this activity. */
943 public final Application getApplication() {
944 return mApplication;
945 }
946
947 /** Is this activity embedded inside of another activity? */
948 public final boolean isChild() {
949 return mParent != null;
950 }
RoboErik55011652014-07-09 15:05:53 -0700951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 /** Return the parent activity if this view is an embedded child. */
953 public final Activity getParent() {
954 return mParent;
955 }
956
957 /** Retrieve the window manager for showing custom windows. */
958 public WindowManager getWindowManager() {
959 return mWindowManager;
960 }
961
962 /**
963 * Retrieve the current {@link android.view.Window} for the activity.
964 * This can be used to directly access parts of the Window API that
965 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 * @return Window The current window, or null if the activity is not
968 * visual.
969 */
970 public Window getWindow() {
971 return mWindow;
972 }
973
974 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800975 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -0800976 *
977 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -0700978 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800979 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -0700980 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700981 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700982 }
RoboErik55011652014-07-09 15:05:53 -0700983
Dianne Hackbornc8017682010-07-06 13:34:38 -0700984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 * Calls {@link android.view.Window#getCurrentFocus} on the
986 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700987 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700989 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 * @see #getWindow
991 * @see android.view.Window#getCurrentFocus
992 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700993 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 public View getCurrentFocus() {
995 return mWindow != null ? mWindow.getCurrentFocus() : null;
996 }
997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 /**
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700999 * (Create and) return the autofill manager
1000 *
1001 * @return The autofill manager
1002 */
1003 @NonNull private AutofillManager getAutofillManager() {
1004 if (mAutofillManager == null) {
1005 mAutofillManager = getSystemService(AutofillManager.class);
1006 }
1007
1008 return mAutofillManager;
1009 }
1010
Felipe Lemebb567ae2017-10-04 09:56:21 -07001011 @Override
1012 protected void attachBaseContext(Context newBase) {
1013 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001014 if (newBase != null) {
1015 newBase.setAutofillClient(this);
1016 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001017 }
1018
1019 /** @hide */
1020 @Override
1021 public final AutofillClient getAutofillClient() {
1022 return this;
1023 }
1024
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 * Called when the activity is starting. This is where most initialization
1027 * should go: calling {@link #setContentView(int)} to inflate the
1028 * activity's UI, using {@link #findViewById} to programmatically interact
1029 * with widgets in the UI, calling
1030 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1031 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001034 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1035 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1036 * executing.
RoboErik55011652014-07-09 15:05:53 -07001037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 * <p><em>Derived classes must call through to the super class's
1039 * implementation of this method. If they do not, an exception will be
1040 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 * @param savedInstanceState If the activity is being re-initialized after
1043 * previously being shut down then this Bundle contains the data it most
1044 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001045 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 * @see #onStart
1047 * @see #onSaveInstanceState
1048 * @see #onRestoreInstanceState
1049 * @see #onPostCreate
1050 */
Tor Norbye83c68962015-03-10 20:55:31 -07001051 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001052 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001053 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001054 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001055
Dianne Hackborn2707d602010-07-09 18:01:20 -07001056 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001057 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001058 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001059 if (mActivityInfo.parentActivityName != null) {
1060 if (mActionBar == null) {
1061 mEnableDefaultActionBarUp = true;
1062 } else {
1063 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1064 }
1065 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001066 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001067 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001068 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1069 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001070
1071 if (mAutoFillResetNeeded) {
1072 getAutofillManager().onCreate(savedInstanceState);
1073 }
1074
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001075 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1076 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1077 ? mLastNonConfigurationInstances.fragments : null);
1078 }
1079 mFragments.dispatchCreate();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001080 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001081 if (mVoiceInteractor != null) {
1082 mVoiceInteractor.attachActivity(this);
1083 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001084 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 mCalled = true;
1086 }
1087
1088 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001089 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001090 * the attribute {@link android.R.attr#persistableMode} set to
1091 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001092 *
1093 * @param savedInstanceState if the activity is being re-initialized after
1094 * previously being shut down then this Bundle contains the data it most
1095 * recently supplied in {@link #onSaveInstanceState}.
1096 * <b><i>Note: Otherwise it is null.</i></b>
1097 * @param persistentState if the activity is being re-initialized after
1098 * previously being shut down or powered off then this Bundle contains the data it most
1099 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1100 * <b><i>Note: Otherwise it is null.</i></b>
1101 *
1102 * @see #onCreate(android.os.Bundle)
1103 * @see #onStart
1104 * @see #onSaveInstanceState
1105 * @see #onRestoreInstanceState
1106 * @see #onPostCreate
1107 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001108 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001109 @Nullable PersistableBundle persistentState) {
1110 onCreate(savedInstanceState);
1111 }
1112
1113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * The hook for {@link ActivityThread} to restore the state of this activity.
1115 *
1116 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1117 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1118 *
1119 * @param savedInstanceState contains the saved state
1120 */
1121 final void performRestoreInstanceState(Bundle savedInstanceState) {
1122 onRestoreInstanceState(savedInstanceState);
1123 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125
1126 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001127 * The hook for {@link ActivityThread} to restore the state of this activity.
1128 *
1129 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1130 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1131 *
1132 * @param savedInstanceState contains the saved state
1133 * @param persistentState contains the persistable saved state
1134 */
1135 final void performRestoreInstanceState(Bundle savedInstanceState,
1136 PersistableBundle persistentState) {
1137 onRestoreInstanceState(savedInstanceState, persistentState);
1138 if (savedInstanceState != null) {
1139 restoreManagedDialogs(savedInstanceState);
1140 }
1141 }
1142
1143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 * This method is called after {@link #onStart} when the activity is
1145 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001146 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 * to restore their state, but it is sometimes convenient to do it here
1148 * after all of the initialization has been done or to allow subclasses to
1149 * decide whether to use your default implementation. The default
1150 * implementation of this method performs a restore of any view state that
1151 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 * <p>This method is called between {@link #onStart} and
1154 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 * @see #onCreate
1159 * @see #onPostCreate
1160 * @see #onResume
1161 * @see #onSaveInstanceState
1162 */
1163 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1164 if (mWindow != null) {
1165 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1166 if (windowState != null) {
1167 mWindow.restoreHierarchyState(windowState);
1168 }
1169 }
1170 }
Craig Mautnera0026042014-04-23 11:45:37 -07001171
1172 /**
1173 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001174 * created with the attribute {@link android.R.attr#persistableMode} set to
1175 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1176 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001177 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1178 *
1179 * <p>This method is called between {@link #onStart} and
1180 * {@link #onPostCreate}.
1181 *
1182 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1183 *
1184 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1185 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1186 *
1187 * @see #onRestoreInstanceState(Bundle)
1188 * @see #onCreate
1189 * @see #onPostCreate
1190 * @see #onResume
1191 * @see #onSaveInstanceState
1192 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001193 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001194 PersistableBundle persistentState) {
1195 if (savedInstanceState != null) {
1196 onRestoreInstanceState(savedInstanceState);
1197 }
1198 }
1199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 /**
1201 * Restore the state of any saved managed dialogs.
1202 *
1203 * @param savedInstanceState The bundle to restore from.
1204 */
1205 private void restoreManagedDialogs(Bundle savedInstanceState) {
1206 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1207 if (b == null) {
1208 return;
1209 }
1210
1211 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1212 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001213 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 for (int i = 0; i < numDialogs; i++) {
1215 final Integer dialogId = ids[i];
1216 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1217 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001218 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1219 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001220 final ManagedDialog md = new ManagedDialog();
1221 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1222 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1223 if (md.mDialog != null) {
1224 mManagedDialogs.put(dialogId, md);
1225 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1226 md.mDialog.onRestoreInstanceState(dialogState);
1227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
1230 }
1231
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001232 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1233 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001234 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001235 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001236 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001237 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001238 return dialog;
1239 }
1240
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001241 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 return SAVED_DIALOG_KEY_PREFIX + key;
1243 }
1244
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001245 private static String savedDialogArgsKeyFor(int key) {
1246 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248
1249 /**
1250 * Called when activity start-up is complete (after {@link #onStart}
1251 * and {@link #onRestoreInstanceState} have been called). Applications will
1252 * generally not implement this method; it is intended for system
1253 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 * <p><em>Derived classes must call through to the super class's
1256 * implementation of this method. If they do not, an exception will be
1257 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 * @param savedInstanceState If the activity is being re-initialized after
1260 * previously being shut down then this Bundle contains the data it most
1261 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1262 * @see #onCreate
1263 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001264 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001265 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 if (!isChild()) {
1267 mTitleReady = true;
1268 onTitleChanged(getTitle(), getTitleColor());
1269 }
Winsonb6403152016-02-23 13:32:09 -08001270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 mCalled = true;
1272 }
1273
1274 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001275 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001276 * created with the attribute {@link android.R.attr#persistableMode} set to
1277 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001278 *
1279 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1280 * @param persistentState The data caming from the PersistableBundle first
1281 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1282 *
1283 * @see #onCreate
1284 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001285 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001286 @Nullable PersistableBundle persistentState) {
1287 onPostCreate(savedInstanceState);
1288 }
1289
1290 /**
RoboErik55011652014-07-09 15:05:53 -07001291 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1292 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001293 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 *
1295 * <p><em>Derived classes must call through to the super class's
1296 * implementation of this method. If they do not, an exception will be
1297 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 * @see #onCreate
1300 * @see #onStop
1301 * @see #onResume
1302 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001303 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001305 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001307
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001308 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001309
1310 getApplication().dispatchActivityStarted(this);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001311
1312 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001313 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316
1317 /**
1318 * Called after {@link #onStop} when the current activity is being
1319 * re-displayed to the user (the user has navigated back to it). It will
1320 * be followed by {@link #onStart} and then {@link #onResume}.
1321 *
1322 * <p>For activities that are using raw {@link Cursor} objects (instead of
1323 * creating them through
1324 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1325 * this is usually the place
1326 * where the cursor should be requeried (because you had deactivated it in
1327 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 * <p><em>Derived classes must call through to the super class's
1330 * implementation of this method. If they do not, an exception will be
1331 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 * @see #onStop
1334 * @see #onStart
1335 * @see #onResume
1336 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001337 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 protected void onRestart() {
1339 mCalled = true;
1340 }
1341
1342 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001343 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1344 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1345 * to give the activity a hint that its state is no longer saved -- it will generally
1346 * be called after {@link #onSaveInstanceState} and prior to the activity being
1347 * resumed/started again.
1348 */
1349 public void onStateNotSaved() {
1350 }
1351
1352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1354 * {@link #onPause}, for your activity to start interacting with the user.
1355 * This is a good place to begin animations, open exclusive-access devices
1356 * (such as the camera), etc.
1357 *
1358 * <p>Keep in mind that onResume is not the best indicator that your activity
1359 * is visible to the user; a system window such as the keyguard may be in
1360 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1361 * activity is visible to the user (for example, to resume a game).
1362 *
1363 * <p><em>Derived classes must call through to the super class's
1364 * implementation of this method. If they do not, an exception will be
1365 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 * @see #onRestoreInstanceState
1368 * @see #onRestart
1369 * @see #onPostResume
1370 * @see #onPause
1371 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001372 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001374 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001375 getApplication().dispatchActivityResumed(this);
George Mountbdc4d8d2016-03-23 13:10:13 -07001376 mActivityTransitionState.onResume(this, isTopOfTask());
Felipe Leme5cd660d2018-09-17 09:46:13 -07001377 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001378 if (mAutoFillResetNeeded) {
1379 if (!mAutoFillIgnoreFirstResumePause) {
1380 View focus = getCurrentFocus();
1381 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
1382 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1383 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1384 // window visibility after recreation is INVISIBLE in onResume() and next frame
1385 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1386 // So we cannot call View.notifyEnterOrExited() which will do nothing
1387 // when View.isVisibleToUser() is false.
1388 getAutofillManager().notifyViewEntered(focus);
1389 }
1390 }
1391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 mCalled = true;
1393 }
1394
1395 /**
1396 * Called when activity resume is complete (after {@link #onResume} has
1397 * been called). Applications will generally not implement this method;
1398 * it is intended for system classes to do final setup after application
1399 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 * <p><em>Derived classes must call through to the super class's
1402 * implementation of this method. If they do not, an exception will be
1403 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 * @see #onResume
1406 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001407 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 protected void onPostResume() {
1409 final Window win = getWindow();
1410 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001411 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 mCalled = true;
1413 }
1414
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001415 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001416 if (mVoiceInteractor != null) {
1417 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1418 activeRequest.cancel();
1419 activeRequest.clear();
1420 }
1421 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001422 if (voiceInteractor == null) {
1423 mVoiceInteractor = null;
1424 } else {
1425 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1426 Looper.myLooper());
1427 }
1428 }
1429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001431 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001432 *
Phil Weaver846cda932017-06-15 10:10:06 -07001433 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001434 * will be unique.
1435 *
1436 * @return A ID that is unique in the activity
1437 *
1438 * {@hide}
1439 */
Felipe Leme42b97932018-02-20 13:04:31 -08001440 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001441 public int getNextAutofillId() {
1442 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1443 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001444 }
1445
Phil Weaver846cda932017-06-15 10:10:06 -07001446 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001447
Phil Weaver846cda932017-06-15 10:10:06 -07001448 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001449 }
1450
1451 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001452 * @hide
1453 */
1454 @Override
1455 public AutofillId autofillClientGetNextAutofillId() {
1456 return new AutofillId(getNextAutofillId());
1457 }
1458
1459 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001460 * Check whether this activity is running as part of a voice interaction with the user.
1461 * If true, it should perform its interaction with the user through the
1462 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1463 */
1464 public boolean isVoiceInteraction() {
1465 return mVoiceInteractor != null;
1466 }
1467
1468 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001469 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1470 * of a voice interaction. That is, returns true if this activity was directly
1471 * started by the voice interaction service as the initiation of a voice interaction.
1472 * Otherwise, for example if it was started by another activity while under voice
1473 * interaction, returns false.
1474 */
1475 public boolean isVoiceInteractionRoot() {
1476 try {
1477 return mVoiceInteractor != null
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001478 && ActivityManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001479 } catch (RemoteException e) {
1480 }
1481 return false;
1482 }
1483
1484 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001485 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1486 * interact with this activity.
1487 */
1488 public VoiceInteractor getVoiceInteractor() {
1489 return mVoiceInteractor;
1490 }
1491
1492 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001493 * Queries whether the currently enabled voice interaction service supports returning
1494 * a voice interactor for use by the activity. This is valid only for the duration of the
1495 * activity.
1496 *
1497 * @return whether the current voice interaction service supports local voice interaction
1498 */
1499 public boolean isLocalVoiceInteractionSupported() {
1500 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001501 return ActivityManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001502 } catch (RemoteException re) {
1503 }
1504 return false;
1505 }
1506
1507 /**
1508 * Starts a local voice interaction session. When ready,
1509 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1510 * to the registered voice interaction service.
1511 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1512 */
1513 public void startLocalVoiceInteraction(Bundle privateOptions) {
1514 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001515 ActivityManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001516 } catch (RemoteException re) {
1517 }
1518 }
1519
1520 /**
1521 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1522 * voice interaction session being started. You can now retrieve a voice interactor using
1523 * {@link #getVoiceInteractor()}.
1524 */
1525 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001526 }
1527
1528 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001529 * Callback to indicate that the local voice interaction has stopped either
1530 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1531 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1532 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001533 */
1534 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001535 }
1536
1537 /**
1538 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001539 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1540 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001541 */
1542 public void stopLocalVoiceInteraction() {
1543 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001544 ActivityManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001545 } catch (RemoteException re) {
1546 }
1547 }
1548
1549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * This is called for activities that set launchMode to "singleTop" in
1551 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1552 * flag when calling {@link #startActivity}. In either case, when the
1553 * activity is re-launched while at the top of the activity stack instead
1554 * of a new instance of the activity being started, onNewIntent() will be
1555 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001556 * re-launch it.
1557 *
1558 * <p>An activity will always be paused before receiving a new intent, so
1559 * you can count on {@link #onResume} being called after this method.
1560 *
1561 * <p>Note that {@link #getIntent} still returns the original Intent. You
1562 * can use {@link #setIntent} to update it to this new Intent.
1563 *
1564 * @param intent The new intent that was started for the activity.
1565 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001567 * @see #setIntent
1568 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 */
1570 protected void onNewIntent(Intent intent) {
1571 }
1572
1573 /**
1574 * The hook for {@link ActivityThread} to save the state of this activity.
1575 *
1576 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1577 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1578 *
1579 * @param outState The bundle to save the state to.
1580 */
1581 final void performSaveInstanceState(Bundle outState) {
1582 onSaveInstanceState(outState);
1583 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001584 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001585 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001586 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588
1589 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001590 * The hook for {@link ActivityThread} to save the state of this activity.
1591 *
1592 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1593 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1594 *
1595 * @param outState The bundle to save the state to.
1596 * @param outPersistentState The bundle to save persistent state to.
1597 */
1598 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1599 onSaveInstanceState(outState, outPersistentState);
1600 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001601 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001602 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1603 ", " + outPersistentState);
1604 }
1605
1606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 * Called to retrieve per-instance state from an activity before being killed
1608 * so that the state can be restored in {@link #onCreate} or
1609 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1610 * will be passed to both).
1611 *
1612 * <p>This method is called before an activity may be killed so that when it
1613 * comes back some time in the future it can restore its state. For example,
1614 * if activity B is launched in front of activity A, and at some point activity
1615 * A is killed to reclaim resources, activity A will have a chance to save the
1616 * current state of its user interface via this method so that when the user
1617 * returns to activity A, the state of the user interface can be restored
1618 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1619 *
1620 * <p>Do not confuse this method with activity lifecycle callbacks such as
1621 * {@link #onPause}, which is always called when an activity is being placed
1622 * in the background or on its way to destruction, or {@link #onStop} which
1623 * is called before destruction. One example of when {@link #onPause} and
1624 * {@link #onStop} is called and not this method is when a user navigates back
1625 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1626 * on B because that particular instance will never be restored, so the
1627 * system avoids calling it. An example when {@link #onPause} is called and
1628 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1629 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1630 * killed during the lifetime of B since the state of the user interface of
1631 * A will stay intact.
1632 *
1633 * <p>The default implementation takes care of most of the UI per-instance
1634 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1635 * view in the hierarchy that has an id, and by saving the id of the currently
1636 * focused view (all of which is restored by the default implementation of
1637 * {@link #onRestoreInstanceState}). If you override this method to save additional
1638 * information not captured by each individual view, you will likely want to
1639 * call through to the default implementation, otherwise be prepared to save
1640 * all of the state of each view yourself.
1641 *
Andrii Kulian391161f2018-01-29 10:50:02 -08001642 * <p>If called, this method will occur after {@link #onStop} for applications
1643 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
1644 * For applications targeting earlier platform versions this method will occur
1645 * before {@link #onStop} and there are no guarantees about whether it will
1646 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001647 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 * @see #onCreate
1651 * @see #onRestoreInstanceState
1652 * @see #onPause
1653 */
1654 protected void onSaveInstanceState(Bundle outState) {
1655 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001656
Phil Weaver846cda932017-06-15 10:10:06 -07001657 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001658 Parcelable p = mFragments.saveAllState();
1659 if (p != null) {
1660 outState.putParcelable(FRAGMENTS_TAG, p);
1661 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001662 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001663 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
1664 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001665 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00001666 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 }
1668
1669 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001670 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001671 * created with the attribute {@link android.R.attr#persistableMode} set to
1672 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1673 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1674 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001675 *
1676 * @param outState Bundle in which to place your saved state.
1677 * @param outPersistentState State which will be saved across reboots.
1678 *
1679 * @see #onSaveInstanceState(Bundle)
1680 * @see #onCreate
1681 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1682 * @see #onPause
1683 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001684 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001685 onSaveInstanceState(outState);
1686 }
1687
1688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 * Save the state of any managed dialogs.
1690 *
1691 * @param outState place to store the saved state.
1692 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001693 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 private void saveManagedDialogs(Bundle outState) {
1695 if (mManagedDialogs == null) {
1696 return;
1697 }
1698
1699 final int numDialogs = mManagedDialogs.size();
1700 if (numDialogs == 0) {
1701 return;
1702 }
1703
1704 Bundle dialogState = new Bundle();
1705
1706 int[] ids = new int[mManagedDialogs.size()];
1707
1708 // save each dialog's bundle, gather the ids
1709 for (int i = 0; i < numDialogs; i++) {
1710 final int key = mManagedDialogs.keyAt(i);
1711 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001712 final ManagedDialog md = mManagedDialogs.valueAt(i);
1713 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1714 if (md.mArgs != null) {
1715 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
1718
1719 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1720 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1721 }
1722
1723
1724 /**
1725 * Called as part of the activity lifecycle when an activity is going into
1726 * the background, but has not (yet) been killed. The counterpart to
1727 * {@link #onResume}.
1728 *
1729 * <p>When activity B is launched in front of activity A, this callback will
1730 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1731 * so be sure to not do anything lengthy here.
1732 *
1733 * <p>This callback is mostly used for saving any persistent state the
1734 * activity is editing, to present a "edit in place" model to the user and
1735 * making sure nothing is lost if there are not enough resources to start
1736 * the new activity without first killing this one. This is also a good
1737 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001738 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 * as fast as possible, or to close resources that are exclusive access
1740 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001741 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 * <p>In situations where the system needs more memory it may kill paused
1743 * processes to reclaim resources. Because of this, you should be sure
1744 * that all of your state is saved by the time you return from
1745 * this function. In general {@link #onSaveInstanceState} is used to save
1746 * per-instance state in the activity and this method is used to store
1747 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001748 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 * <p>After receiving this call you will usually receive a following call
1750 * to {@link #onStop} (after the next activity has been resumed and
1751 * displayed), however in some cases there will be a direct call back to
1752 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 * <p><em>Derived classes must call through to the super class's
1755 * implementation of this method. If they do not, an exception will be
1756 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 * @see #onResume
1759 * @see #onSaveInstanceState
1760 * @see #onStop
1761 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001762 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001764 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001765 getApplication().dispatchActivityPaused(this);
Dake Gu67decfa2017-12-27 11:48:08 -08001766 if (mAutoFillResetNeeded) {
1767 if (!mAutoFillIgnoreFirstResumePause) {
1768 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
1769 View focus = getCurrentFocus();
1770 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
1771 getAutofillManager().notifyViewExited(focus);
1772 }
1773 } else {
1774 // reset after first pause()
1775 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
1776 mAutoFillIgnoreFirstResumePause = false;
1777 }
1778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 mCalled = true;
1780 }
1781
1782 /**
1783 * Called as part of the activity lifecycle when an activity is about to go
1784 * into the background as the result of user choice. For example, when the
1785 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1786 * when an incoming phone call causes the in-call Activity to be automatically
1787 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1788 * the activity being interrupted. In cases when it is invoked, this method
1789 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 * <p>This callback and {@link #onUserInteraction} are intended to help
1792 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001793 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07001794 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 * @see #onUserInteraction()
1796 */
1797 protected void onUserLeaveHint() {
1798 }
RoboErik55011652014-07-09 15:05:53 -07001799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001801 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001803 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001805 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
1807
1808 /**
1809 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001810 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001812 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 * <p>The default implementation returns null, which will cause you to
1814 * inherit the description from the previous activity. If all activities
1815 * return null, generally the label of the top activity will be used as the
1816 * description.
RoboErik55011652014-07-09 15:05:53 -07001817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 * @return A description of what the user is doing. It should be short and
1819 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001820 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001822 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001824 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 public CharSequence onCreateDescription() {
1826 return null;
1827 }
1828
1829 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001830 * This is called when the user is requesting an assist, to build a full
1831 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1832 * application. You can override this method to place into the bundle anything
1833 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001834 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001835 *
1836 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001837 * been registered with {@link Application#registerOnProvideAssistDataListener
1838 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001839 */
1840 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001841 }
1842
1843 /**
1844 * This is called when the user is requesting an assist, to provide references
1845 * to content related to the current activity. Before being called, the
1846 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1847 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1848 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1849 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1850 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1851 *
1852 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1853 * context of the activity, and fill in its ClipData with additional content of
1854 * interest that the user is currently viewing. For example, an image gallery application
1855 * that has launched in to an activity allowing the user to swipe through pictures should
1856 * modify the intent to reference the current image they are looking it; such an
1857 * application when showing a list of pictures should add a ClipData that has
1858 * references to all of the pictures currently visible on screen.</p>
1859 *
1860 * @param outContent The assist content to return.
1861 */
1862 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001863 }
1864
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001865 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01001866 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001867 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
1868 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01001869 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001870 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001871 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1872 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01001873 }
1874
1875 /**
1876 * Dismiss the Keyboard Shortcuts screen.
1877 */
1878 public final void dismissKeyboardShortcutsHelper() {
1879 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001880 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1881 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001882 }
1883
Clara Bayarri75e09792015-07-29 16:20:40 +01001884 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001885 public void onProvideKeyboardShortcuts(
1886 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01001887 if (menu == null) {
1888 return;
1889 }
1890 KeyboardShortcutGroup group = null;
1891 int menuSize = menu.size();
1892 for (int i = 0; i < menuSize; ++i) {
1893 final MenuItem item = menu.getItem(i);
1894 final CharSequence title = item.getTitle();
1895 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01001896 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01001897 if (title != null && alphaShortcut != MIN_VALUE) {
1898 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001899 final int resource = mApplication.getApplicationInfo().labelRes;
1900 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001901 }
1902 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01001903 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01001904 }
1905 }
1906 if (group != null) {
1907 data.add(group);
1908 }
1909 }
1910
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001911 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001912 * Ask to have the current assistant shown to the user. This only works if the calling
1913 * activity is the current foreground activity. It is the same as calling
1914 * {@link android.service.voice.VoiceInteractionService#showSession
1915 * VoiceInteractionService.showSession} and requesting all of the possible context.
1916 * The receiver will always see
1917 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1918 * @return Returns true if the assistant was successfully invoked, else false. For example
1919 * false will be returned if the caller is not the current top activity.
1920 */
1921 public boolean showAssist(Bundle args) {
1922 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001923 return ActivityManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07001924 } catch (RemoteException e) {
1925 }
1926 return false;
1927 }
1928
1929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 * Called when you are no longer visible to the user. You will next
1931 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1932 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 * <p><em>Derived classes must call through to the super class's
1935 * implementation of this method. If they do not, an exception will be
1936 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 * @see #onRestart
1939 * @see #onResume
1940 * @see #onSaveInstanceState
1941 * @see #onDestroy
1942 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001943 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001945 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001946 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001947 mActivityTransitionState.onStop();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001948 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001949 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07001951
Dake Gu67decfa2017-12-27 11:48:08 -08001952 if (mAutoFillResetNeeded) {
1953 getAutofillManager().onInvisibleForAutofill();
1954 }
1955
Felipe Lemec24a56a2017-08-03 14:27:57 -07001956 if (isFinishing()) {
1957 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08001958 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07001959 } else if (mIntent != null
1960 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
1961 // Activity was launched when user tapped a link in the Autofill Save UI - since
1962 // user launched another activity, the Save UI should not be restored when this
1963 // activity is finished.
1964 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
1965 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
1966 }
Felipe Lemebab851c2017-02-03 18:45:08 -08001967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969
1970 /**
1971 * Perform any final cleanup before an activity is destroyed. This can
1972 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07001973 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 * this instance of the activity to save space. You can distinguish
1975 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001976 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 * <p><em>Note: do not count on this method being called as a place for
1978 * saving data! For example, if an activity is editing data in a content
1979 * provider, those edits should be committed in either {@link #onPause} or
1980 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1981 * free resources like threads that are associated with an activity, so
1982 * that a destroyed activity does not leave such things around while the
1983 * rest of its application is still running. There are situations where
1984 * the system will simply kill the activity's hosting process without
1985 * calling this method (or any others) in it, so it should not be used to
1986 * do things that are intended to remain around after the process goes
1987 * away.
RoboErik55011652014-07-09 15:05:53 -07001988 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 * <p><em>Derived classes must call through to the super class's
1990 * implementation of this method. If they do not, an exception will be
1991 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001992 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 * @see #onPause
1994 * @see #onStop
1995 * @see #finish
1996 * @see #isFinishing
1997 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001998 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002000 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 mCalled = true;
2002
2003 // dismiss any dialogs we are managing.
2004 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 final int numDialogs = mManagedDialogs.size();
2006 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002007 final ManagedDialog md = mManagedDialogs.valueAt(i);
2008 if (md.mDialog.isShowing()) {
2009 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
2011 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002012 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014
2015 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002016 synchronized (mManagedCursors) {
2017 int numCursors = mManagedCursors.size();
2018 for (int i = 0; i < numCursors; i++) {
2019 ManagedCursor c = mManagedCursors.get(i);
2020 if (c != null) {
2021 c.mCursor.close();
2022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002024 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 }
Amith Yamasani49860442010-03-17 20:54:10 -07002026
2027 // Close any open search dialog
2028 if (mSearchManager != null) {
2029 mSearchManager.stopSearch();
2030 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002031
Chris Banes21b25772016-01-04 20:41:59 +00002032 if (mActionBar != null) {
2033 mActionBar.onDestroy();
2034 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002035
2036 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038
2039 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002040 * Report to the system that your app is now fully drawn, purely for diagnostic
2041 * purposes (calling it does not impact the visible behavior of the activity).
2042 * This is only used to help instrument application launch times, so that the
2043 * app can report when it is fully in a usable state; without this, the only thing
2044 * the system itself can determine is the point at which the activity's window
2045 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002046 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002047 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002048 * entirely drawn your UI and populated with all of the significant data. You
2049 * can safely call this method any time after first launch as well, in which case
2050 * it will simply be ignored.
2051 */
2052 public void reportFullyDrawn() {
2053 if (mDoReportFullyDrawn) {
2054 mDoReportFullyDrawn = false;
2055 try {
Jorim Jaggi4d27b842017-08-17 17:22:26 +02002056 ActivityManager.getService().reportActivityFullyDrawn(mToken, mRestoredFromBundle);
Mathieu Chartier67f02a12019-03-07 09:05:44 -08002057 VMRuntime.getRuntime().notifyStartupCompleted();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002058 } catch (RemoteException e) {
2059 }
2060 }
2061 }
2062
2063 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002064 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002065 * visa-versa. This method provides the same configuration that will be sent in the following
2066 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2067 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002068 * @see android.R.attr#resizeableActivity
2069 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002070 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002071 * @param newConfig The new configuration of the activity with the state
2072 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002073 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002074 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2075 // Left deliberately empty. There should be no side effects if a direct
2076 // subclass of Activity does not call super.
2077 onMultiWindowModeChanged(isInMultiWindowMode);
2078 }
2079
2080 /**
2081 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2082 * visa-versa.
2083 *
2084 * @see android.R.attr#resizeableActivity
2085 *
2086 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2087 *
2088 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2089 */
2090 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002091 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002092 // Left deliberately empty. There should be no side effects if a direct
2093 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002094 }
2095
2096 /**
2097 * Returns true if the activity is currently in multi-window mode.
2098 * @see android.R.attr#resizeableActivity
2099 *
2100 * @return True if the activity is in multi-window mode.
2101 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002102 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002103 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002104 return ActivityManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002105 } catch (RemoteException e) {
2106 }
2107 return false;
2108 }
2109
2110 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002111 * Called by the system when the activity changes to and from picture-in-picture mode. This
2112 * method provides the same configuration that will be sent in the following
2113 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2114 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002115 * @see android.R.attr#supportsPictureInPicture
2116 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002117 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002118 * @param newConfig The new configuration of the activity with the state
2119 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002120 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002121 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2122 Configuration newConfig) {
2123 // Left deliberately empty. There should be no side effects if a direct
2124 // subclass of Activity does not call super.
2125 onPictureInPictureModeChanged(isInPictureInPictureMode);
2126 }
2127
2128 /**
2129 * Called by the system when the activity changes to and from picture-in-picture mode.
2130 *
2131 * @see android.R.attr#supportsPictureInPicture
2132 *
2133 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2134 *
2135 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2136 */
2137 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002138 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002139 // Left deliberately empty. There should be no side effects if a direct
2140 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002141 }
2142
2143 /**
2144 * Returns true if the activity is currently in picture-in-picture mode.
2145 * @see android.R.attr#supportsPictureInPicture
2146 *
2147 * @return True if the activity is in picture-in-picture mode.
2148 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002149 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002150 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002151 return ActivityManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002152 } catch (RemoteException e) {
2153 }
2154 return false;
2155 }
2156
2157 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002158 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002159 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2160 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002161 *
Winson Chung709904f2017-04-25 11:00:48 -07002162 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002163 * @see android.R.attr#supportsPictureInPicture
2164 */
Winson Chung709904f2017-04-25 11:00:48 -07002165 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002166 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002167 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002168 }
2169
Jeff Sharkey000ce802017-04-29 13:13:27 -06002170 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002171 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002172 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002173 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002174 }
2175
2176 /**
Winson Chung709904f2017-04-25 11:00:48 -07002177 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2178 * set parameters in {@param params} will be combined with the parameters from prior calls to
2179 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2180 *
2181 * The system may disallow entering picture-in-picture in various cases, including when the
2182 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2183 *
2184 * @see android.R.attr#supportsPictureInPicture
2185 * @see PictureInPictureParams
2186 *
2187 * @param params non-null parameters to be combined with previously set parameters when entering
2188 * picture-in-picture.
2189 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002190 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002191 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002192 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002193 */
2194 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2195 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002196 if (!deviceSupportsPictureInPictureMode()) {
2197 return false;
2198 }
Winson Chung709904f2017-04-25 11:00:48 -07002199 if (params == null) {
2200 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2201 }
Winson Chung298f95b2017-08-10 15:57:18 -07002202 if (!mCanEnterPictureInPicture) {
2203 throw new IllegalStateException("Activity must be resumed to enter"
2204 + " picture-in-picture");
2205 }
Winson Chung709904f2017-04-25 11:00:48 -07002206 return ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken, params);
2207 } catch (RemoteException e) {
2208 return false;
2209 }
2210 }
2211
Jeff Sharkey000ce802017-04-29 13:13:27 -06002212 /** @removed */
2213 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002214 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002215 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002216 }
2217
2218 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002219 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2220 * {@link #enterPictureInPictureMode()} is called.
2221 *
Winson Chung709904f2017-04-25 11:00:48 -07002222 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002223 */
Winson Chung709904f2017-04-25 11:00:48 -07002224 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002225 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002226 if (!deviceSupportsPictureInPictureMode()) {
2227 return;
2228 }
Winson Chung709904f2017-04-25 11:00:48 -07002229 if (params == null) {
2230 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002231 }
Winson Chung709904f2017-04-25 11:00:48 -07002232 ActivityManagerNative.getDefault().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002233 } catch (RemoteException e) {
2234 }
2235 }
2236
Winson Chung8802eac2017-04-17 14:21:44 -07002237 /**
Winson Chung709904f2017-04-25 11:00:48 -07002238 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2239 * user interacts with the activity currently in picture-in-picture mode. This number may change
2240 * if the global configuration changes (ie. if the device is plugged into an external display),
2241 * but will always be larger than three.
2242 */
2243 public int getMaxNumPictureInPictureActions() {
2244 try {
2245 return ActivityManagerNative.getDefault().getMaxNumPictureInPictureActions(mToken);
2246 } catch (RemoteException e) {
2247 return 0;
2248 }
2249 }
2250
Winson Chung80fa2d82018-01-25 13:58:20 -08002251 /**
2252 * @return Whether this device supports picture-in-picture.
2253 */
2254 private boolean deviceSupportsPictureInPictureMode() {
2255 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2256 }
2257
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002258 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002259 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002260 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002261 }
2262
2263 /**
2264 * Called by the system when the activity is moved from one display to another without
2265 * recreation. This means that this activity is declared to handle all changes to configuration
2266 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002267 * again.
2268 *
2269 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2270 * applied configuration actually changed. It is up to app developer to choose whether to handle
2271 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2272 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002273 *
2274 * <p>Use this callback to track changes to the displays if some activity functionality relies
2275 * on an association with some display properties.
2276 *
2277 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002278 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002279 *
2280 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002281 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002282 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002283 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002284 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002285 }
2286
Winson Chungb5c41b72016-12-07 15:00:47 -08002287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 * Called by the system when the device configuration changes while your
2289 * activity is running. Note that this will <em>only</em> be called if
2290 * you have selected configurations you would like to handle with the
2291 * {@link android.R.attr#configChanges} attribute in your manifest. If
2292 * any configuration change occurs that is not selected to be reported
2293 * by that attribute, then instead of reporting it the system will stop
2294 * and restart the activity (to have it launched with the new
2295 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 * <p>At the time that this function has been called, your Resources
2298 * object will have been updated to return resource values matching the
2299 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002300 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 * @param newConfig The new device configuration.
2302 */
2303 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002304 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002306
Dianne Hackborn9d071802010-12-08 14:49:15 -08002307 mFragments.dispatchConfigurationChanged(newConfig);
2308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 if (mWindow != null) {
2310 // Pass the configuration changed event to the window
2311 mWindow.onConfigurationChanged(newConfig);
2312 }
Adam Powell45c0b192011-07-28 15:11:57 -07002313
2314 if (mActionBar != null) {
2315 // Do this last; the action bar will need to access
2316 // view changes from above.
2317 mActionBar.onConfigurationChanged(newConfig);
2318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
RoboErik55011652014-07-09 15:05:53 -07002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 /**
2322 * If this activity is being destroyed because it can not handle a
2323 * configuration parameter being changed (and thus its
2324 * {@link #onConfigurationChanged(Configuration)} method is
2325 * <em>not</em> being called), then you can use this method to discover
2326 * the set of changes that have occurred while in the process of being
2327 * destroyed. Note that there is no guarantee that these will be
2328 * accurate (other changes could have happened at any time), so you should
2329 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 * @return Returns a bit field of the configuration parameters that are
2332 * changing, as defined by the {@link android.content.res.Configuration}
2333 * class.
2334 */
2335 public int getChangingConfigurations() {
2336 return mConfigChangeFlags;
2337 }
RoboErik55011652014-07-09 15:05:53 -07002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 /**
2340 * Retrieve the non-configuration instance data that was previously
2341 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2342 * be available from the initial {@link #onCreate} and
2343 * {@link #onStart} calls to the new instance, allowing you to extract
2344 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002345 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 * <p>Note that the data you retrieve here should <em>only</em> be used
2347 * as an optimization for handling configuration changes. You should always
2348 * be able to handle getting a null pointer back, and an activity must
2349 * still be able to restore itself to its previous state (through the
2350 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2351 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002352 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002353 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002354 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002355 * available on older platforms through the Android support libraries.
2356 *
2357 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002359 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002361 return mLastNonConfigurationInstances != null
2362 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
RoboErik55011652014-07-09 15:05:53 -07002364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 /**
2366 * Called by the system, as part of destroying an
2367 * activity due to a configuration change, when it is known that a new
2368 * instance will immediately be created for the new configuration. You
2369 * can return any object you like here, including the activity instance
2370 * itself, which can later be retrieved by calling
2371 * {@link #getLastNonConfigurationInstance()} in the new activity
2372 * instance.
RoboErik55011652014-07-09 15:05:53 -07002373 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002374 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2375 * or later, consider instead using a {@link Fragment} with
2376 * {@link Fragment#setRetainInstance(boolean)
2377 * Fragment.setRetainInstance(boolean}.</em>
2378 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 * <p>This function is called purely as an optimization, and you must
2380 * not rely on it being called. When it is called, a number of guarantees
2381 * will be made to help optimize configuration switching:
2382 * <ul>
2383 * <li> The function will be called between {@link #onStop} and
2384 * {@link #onDestroy}.
2385 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002386 * created after this one's {@link #onDestroy()} is called. In particular,
2387 * <em>no</em> messages will be dispatched during this time (when the returned
2388 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 * <li> The object you return here will <em>always</em> be available from
2390 * the {@link #getLastNonConfigurationInstance()} method of the following
2391 * activity instance as described there.
2392 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002393 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 * <p>These guarantees are designed so that an activity can use this API
2395 * to propagate extensive state from the old to new activity instance, from
2396 * loaded bitmaps, to network connections, to evenly actively running
2397 * threads. Note that you should <em>not</em> propagate any data that
2398 * may change based on the configuration, including any data loaded from
2399 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002400 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002401 * <p>The guarantee of no message handling during the switch to the next
2402 * activity simplifies use with active objects. For example if your retained
2403 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2404 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2405 * not be called from the call here until you execute the next instance's
2406 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2407 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2408 * running in a separate thread.)
2409 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002410 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002411 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002412 * available on older platforms through the Android support libraries.
2413 *
2414 * @return any Object holding the desired state to propagate to the
2415 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 */
2417 public Object onRetainNonConfigurationInstance() {
2418 return null;
2419 }
RoboErik55011652014-07-09 15:05:53 -07002420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 /**
2422 * Retrieve the non-configuration instance data that was previously
2423 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2424 * be available from the initial {@link #onCreate} and
2425 * {@link #onStart} calls to the new instance, allowing you to extract
2426 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 * <p>Note that the data you retrieve here should <em>only</em> be used
2429 * as an optimization for handling configuration changes. You should always
2430 * be able to handle getting a null pointer back, and an activity must
2431 * still be able to restore itself to its previous state (through the
2432 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2433 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002434 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 * @return Returns the object previously returned by
2436 * {@link #onRetainNonConfigurationChildInstances()}
2437 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002438 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002439 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2440 return mLastNonConfigurationInstances != null
2441 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
RoboErik55011652014-07-09 15:05:53 -07002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 /**
2445 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2446 * it should return either a mapping from child activity id strings to arbitrary objects,
2447 * or null. This method is intended to be used by Activity framework subclasses that control a
2448 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2449 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2450 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002451 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2453 return null;
2454 }
RoboErik55011652014-07-09 15:05:53 -07002455
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002456 NonConfigurationInstances retainNonConfigurationInstances() {
2457 Object activity = onRetainNonConfigurationInstance();
2458 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002459 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002460
2461 // We're already stopped but we've been asked to retain.
2462 // Our fragments are taken care of but we need to mark the loaders for retention.
2463 // In order to do this correctly we need to restart the loaders first before
2464 // handing them off to the next activity.
2465 mFragments.doLoaderStart();
2466 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002467 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002468
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002469 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002470 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002471 return null;
2472 }
RoboErik55011652014-07-09 15:05:53 -07002473
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002474 NonConfigurationInstances nci = new NonConfigurationInstances();
2475 nci.activity = activity;
2476 nci.children = children;
2477 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002478 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002479 if (mVoiceInteractor != null) {
2480 mVoiceInteractor.retainInstance();
2481 nci.voiceInteractor = mVoiceInteractor;
2482 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002483 return nci;
2484 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002487 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002489 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002491
2492 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002493 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002494 mCalled = true;
2495 mFragments.dispatchTrimMemory(level);
2496 }
2497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002499 * Return the FragmentManager for interacting with fragments associated
2500 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08002501 *
2502 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002503 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002504 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002505 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002506 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002507 }
RoboErik55011652014-07-09 15:05:53 -07002508
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002509 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002510 * Called when a Fragment is being attached to this activity, immediately
2511 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2512 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08002513 *
2514 * @deprecated Use {@link
2515 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07002516 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002517 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07002518 public void onAttachFragment(Fragment fragment) {
2519 }
RoboErik55011652014-07-09 15:05:53 -07002520
Dianne Hackbornc8017682010-07-06 13:34:38 -07002521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 * Wrapper around
2523 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2524 * that gives the resulting {@link Cursor} to call
2525 * {@link #startManagingCursor} so that the activity will manage its
2526 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002527 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002528 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2529 * or later, consider instead using {@link LoaderManager} instead, available
2530 * via {@link #getLoaderManager()}.</em>
2531 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002532 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2533 * this method, because the activity will do that for you at the appropriate time. However, if
2534 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2535 * not</em> automatically close the cursor and, in that case, you must call
2536 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002537 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 * @param uri The URI of the content provider to query.
2539 * @param projection List of columns to return.
2540 * @param selection SQL WHERE clause.
2541 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2546 * @see #startManagingCursor
2547 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002548 *
2549 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 */
Jason parks6ed50de2010-08-25 10:18:50 -05002551 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002552 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07002553 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2554 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2556 if (c != null) {
2557 startManagingCursor(c);
2558 }
2559 return c;
2560 }
2561
2562 /**
2563 * Wrapper around
2564 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2565 * that gives the resulting {@link Cursor} to call
2566 * {@link #startManagingCursor} so that the activity will manage its
2567 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002568 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002569 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2570 * or later, consider instead using {@link LoaderManager} instead, available
2571 * via {@link #getLoaderManager()}.</em>
2572 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002573 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2574 * this method, because the activity will do that for you at the appropriate time. However, if
2575 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2576 * not</em> automatically close the cursor and, in that case, you must call
2577 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002578 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 * @param uri The URI of the content provider to query.
2580 * @param projection List of columns to return.
2581 * @param selection SQL WHERE clause.
2582 * @param selectionArgs The arguments to selection, if any ?s are pesent
2583 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002584 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2588 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002589 *
2590 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 */
Jason parks6ed50de2010-08-25 10:18:50 -05002592 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002593 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2594 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2596 if (c != null) {
2597 startManagingCursor(c);
2598 }
2599 return c;
2600 }
2601
2602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 * This method allows the activity to take care of managing the given
2604 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2605 * That is, when the activity is stopped it will automatically call
2606 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2607 * it will call {@link Cursor#requery} for you. When the activity is
2608 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002609 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002610 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2611 * or later, consider instead using {@link LoaderManager} instead, available
2612 * via {@link #getLoaderManager()}.</em>
2613 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002614 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2615 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2616 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2617 * <em>will not</em> automatically close the cursor and, in that case, you must call
2618 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002619 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002621 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2623 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002624 *
2625 * @deprecated Use the new {@link android.content.CursorLoader} class with
2626 * {@link LoaderManager} instead; this is also
2627 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 */
Jason parks6ed50de2010-08-25 10:18:50 -05002629 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 public void startManagingCursor(Cursor c) {
2631 synchronized (mManagedCursors) {
2632 mManagedCursors.add(new ManagedCursor(c));
2633 }
2634 }
2635
2636 /**
2637 * Given a Cursor that was previously given to
2638 * {@link #startManagingCursor}, stop the activity's management of that
2639 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002640 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002641 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002642 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002643 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002648 *
2649 * @deprecated Use the new {@link android.content.CursorLoader} class with
2650 * {@link LoaderManager} instead; this is also
2651 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 */
Jason parks6ed50de2010-08-25 10:18:50 -05002653 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 public void stopManagingCursor(Cursor c) {
2655 synchronized (mManagedCursors) {
2656 final int N = mManagedCursors.size();
2657 for (int i=0; i<N; i++) {
2658 ManagedCursor mc = mManagedCursors.get(i);
2659 if (mc.mCursor == c) {
2660 mManagedCursors.remove(i);
2661 break;
2662 }
2663 }
2664 }
2665 }
2666
2667 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002668 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2669 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002670 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002672 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002673 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
2676
2677 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04002678 * Finds a view that was identified by the {@code android:id} XML attribute
2679 * that was processed in {@link #onCreate}.
2680 * <p>
2681 * <strong>Note:</strong> In most cases -- depending on compiler support --
2682 * the resulting view is automatically cast to the target class type. If
2683 * the target class type is unconstrained, an explicit cast may be
2684 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04002686 * @param id the ID to search for
2687 * @return a view with given ID if found, or {@code null} otherwise
2688 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08002689 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002691 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04002692 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 return getWindow().findViewById(id);
2694 }
RoboErik55011652014-07-09 15:05:53 -07002695
Adam Powell33b97432010-04-20 10:01:14 -07002696 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08002697 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
2698 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
2699 * no matching view in the hierarchy.
2700 * <p>
2701 * <strong>Note:</strong> In most cases -- depending on compiler support --
2702 * the resulting view is automatically cast to the target class type. If
2703 * the target class type is unconstrained, an explicit cast may be
2704 * necessary.
2705 *
2706 * @param id the ID to search for
2707 * @return a view with given ID
2708 * @see View#requireViewById(int)
2709 * @see Activity#findViewById(int)
2710 */
2711 @NonNull
2712 public final <T extends View> T requireViewById(@IdRes int id) {
2713 T view = findViewById(id);
2714 if (view == null) {
2715 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
2716 }
2717 return view;
2718 }
2719
2720 /**
Adam Powell33b97432010-04-20 10:01:14 -07002721 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002722 *
Adam Powell33b97432010-04-20 10:01:14 -07002723 * @return The Activity's ActionBar, or null if it does not have one.
2724 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002725 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002726 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002727 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002728 return mActionBar;
2729 }
Adam Powelle43340c2014-03-17 19:10:43 -07002730
2731 /**
2732 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2733 * Activity window.
2734 *
2735 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2736 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2737 * a traditional window decor action bar. The toolbar's menu will be populated with the
2738 * Activity's options menu and the navigation button will be wired through the standard
2739 * {@link android.R.id#home home} menu select action.</p>
2740 *
2741 * <p>In order to use a Toolbar within the Activity's window content the application
2742 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2743 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002744 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002745 */
Adam Powell37780142014-06-01 13:31:00 -07002746 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002747 final ActionBar ab = getActionBar();
2748 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002749 throw new IllegalStateException("This Activity already has an action bar supplied " +
2750 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2751 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2752 }
Chris Banes21b25772016-01-04 20:41:59 +00002753
2754 // If we reach here then we're setting a new action bar
2755 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002756 mMenuInflater = null;
2757
Chris Banes21b25772016-01-04 20:41:59 +00002758 // If we have an action bar currently, destroy it
2759 if (ab != null) {
2760 ab.onDestroy();
2761 }
2762
Chris Banesc7d6c322016-01-27 14:09:16 +00002763 if (toolbar != null) {
2764 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2765 mActionBar = tbab;
2766 mWindow.setCallback(tbab.getWrappedWindowCallback());
2767 } else {
2768 mActionBar = null;
2769 // Re-set the original window callback since we may have already set a Toolbar wrapper
2770 mWindow.setCallback(this);
2771 }
2772
2773 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002774 }
RoboErik55011652014-07-09 15:05:53 -07002775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 /**
Adam Powell33b97432010-04-20 10:01:14 -07002777 * Creates a new ActionBar, locates the inflated ActionBarView,
2778 * initializes the ActionBar with the view, and sets mActionBar.
2779 */
Adam Powelle43340c2014-03-17 19:10:43 -07002780 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002781 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002782
2783 // Initializing the window decor can change window feature flags.
2784 // Make sure that we have the correct set before performing the test below.
2785 window.getDecorView();
2786
Adam Powell9b4c8042010-08-10 15:36:44 -07002787 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002788 return;
2789 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002790
Adam Powelle43340c2014-03-17 19:10:43 -07002791 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002792 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002793
2794 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2795 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002796 }
RoboErik55011652014-07-09 15:05:53 -07002797
Adam Powell33b97432010-04-20 10:01:14 -07002798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 * Set the activity content from a layout resource. The resource will be
2800 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002803 *
Romain Guy482b34a62011-01-20 10:59:28 -08002804 * @see #setContentView(android.view.View)
2805 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002807 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002809 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
2811
2812 /**
2813 * Set the activity content to an explicit view. This view is placed
2814 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002815 * view hierarchy. When calling this method, the layout parameters of the
2816 * specified view are ignored. Both the width and the height of the view are
2817 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2818 * your own layout parameters, invoke
2819 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2820 * instead.
RoboErik55011652014-07-09 15:05:53 -07002821 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002823 *
2824 * @see #setContentView(int)
2825 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 */
2827 public void setContentView(View view) {
2828 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002829 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831
2832 /**
2833 * Set the activity content to an explicit view. This view is placed
2834 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002835 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002836 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 * @param view The desired content to display.
2838 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002839 *
2840 * @see #setContentView(android.view.View)
2841 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 */
2843 public void setContentView(View view, ViewGroup.LayoutParams params) {
2844 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002845 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 }
2847
2848 /**
2849 * Add an additional content view to the activity. Added after any existing
2850 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 * @param view The desired content to display.
2853 * @param params Layout parameters for the view.
2854 */
2855 public void addContentView(View view, ViewGroup.LayoutParams params) {
2856 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002857 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 }
2859
2860 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002861 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2862 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2863 *
2864 * <p>This method will return non-null after content has been initialized (e.g. by using
2865 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2866 *
2867 * @return This window's content TransitionManager or null if none is set.
2868 */
2869 public TransitionManager getContentTransitionManager() {
2870 return getWindow().getTransitionManager();
2871 }
2872
2873 /**
2874 * Set the {@link TransitionManager} to use for default transitions in this window.
2875 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2876 *
2877 * @param tm The TransitionManager to use for scene changes.
2878 */
2879 public void setContentTransitionManager(TransitionManager tm) {
2880 getWindow().setTransitionManager(tm);
2881 }
2882
2883 /**
2884 * Retrieve the {@link Scene} representing this window's current content.
2885 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2886 *
2887 * <p>This method will return null if the current content is not represented by a Scene.</p>
2888 *
2889 * @return Current Scene being shown or null
2890 */
2891 public Scene getContentScene() {
2892 return getWindow().getContentScene();
2893 }
2894
2895 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002896 * Sets whether this activity is finished when touched outside its window's
2897 * bounds.
2898 */
2899 public void setFinishOnTouchOutside(boolean finish) {
2900 mWindow.setCloseOnTouchOutside(finish);
2901 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002902
2903 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002904 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07002905 DEFAULT_KEYS_DISABLE,
2906 DEFAULT_KEYS_DIALER,
2907 DEFAULT_KEYS_SHORTCUT,
2908 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002909 DEFAULT_KEYS_SEARCH_GLOBAL
2910 })
Tor Norbyed9273d62013-05-30 15:59:53 -07002911 @Retention(RetentionPolicy.SOURCE)
2912 @interface DefaultKeyMode {}
2913
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002914 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2916 * keys.
RoboErik55011652014-07-09 15:05:53 -07002917 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 * @see #setDefaultKeyMode
2919 */
2920 static public final int DEFAULT_KEYS_DISABLE = 0;
2921 /**
2922 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2923 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002924 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 * @see #setDefaultKeyMode
2926 */
2927 static public final int DEFAULT_KEYS_DIALER = 1;
2928 /**
2929 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2930 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002931 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 * @see #setDefaultKeyMode
2935 */
2936 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2937 /**
2938 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2939 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07002940 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002941 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002943 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 * @see #setDefaultKeyMode
2945 */
2946 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2947
2948 /**
2949 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2950 * will start a global search (typically web search, but some platforms may define alternate
2951 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002954 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 * @see #setDefaultKeyMode
2956 */
2957 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2958
2959 /**
2960 * Select the default key handling for this activity. This controls what
2961 * will happen to key events that are not otherwise handled. The default
2962 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2963 * floor. Other modes allow you to launch the dialer
2964 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2965 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002966 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002968 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 * <p>Note that the mode selected here does not impact the default
2970 * handling of system keys, such as the "back" and "menu" keys, and your
2971 * activity and its views always get a first chance to receive and handle
2972 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002975 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 * @see #onKeyDown
2977 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002978 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 // Some modes use a SpannableStringBuilder to track & dispatch input events
2982 // This list must remain in sync with the switch in onKeyDown()
2983 switch (mode) {
2984 case DEFAULT_KEYS_DISABLE:
2985 case DEFAULT_KEYS_SHORTCUT:
2986 mDefaultKeySsb = null; // not used in these modes
2987 break;
2988 case DEFAULT_KEYS_DIALER:
2989 case DEFAULT_KEYS_SEARCH_LOCAL:
2990 case DEFAULT_KEYS_SEARCH_GLOBAL:
2991 mDefaultKeySsb = new SpannableStringBuilder();
2992 Selection.setSelection(mDefaultKeySsb,0);
2993 break;
2994 default:
2995 throw new IllegalArgumentException();
2996 }
2997 }
2998
2999 /**
3000 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003001 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 * is inside a TextView will not trigger the event (unless it is a navigation
3003 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 * <p>If the focused view didn't want this event, this method is called.
3006 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003007 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3008 * by calling {@link #onBackPressed()}, though the behavior varies based
3009 * on the application compatibility mode: for
3010 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3011 * it will set up the dispatch to call {@link #onKeyUp} where the action
3012 * will be performed; for earlier applications, it will perform the
3013 * action immediately in on-down, as those versions of the platform
3014 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003015 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003016 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003017 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003018 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003020 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 * this event and it should continue to be propagated.
3022 * @see #onKeyUp
3023 * @see android.view.KeyEvent
3024 */
3025 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003026 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003027 if (getApplicationInfo().targetSdkVersion
3028 >= Build.VERSION_CODES.ECLAIR) {
3029 event.startTracking();
3030 } else {
3031 onBackPressed();
3032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 return true;
3034 }
RoboErik55011652014-07-09 15:05:53 -07003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3037 return false;
3038 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003039 Window w = getWindow();
3040 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3041 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3042 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003043 return true;
3044 }
3045 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003046 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3047 // Don't consume TAB here since it's used for navigation. Arrow keys
3048 // aren't considered "typing keys" so they already won't get consumed.
3049 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 } else {
3051 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3052 boolean clearSpannable = false;
3053 boolean handled;
3054 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3055 clearSpannable = true;
3056 handled = false;
3057 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003058 handled = TextKeyListener.getInstance().onKeyDown(
3059 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 if (handled && mDefaultKeySsb.length() > 0) {
3061 // something useable has been typed - dispatch it now.
3062
3063 final String str = mDefaultKeySsb.toString();
3064 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 switch (mDefaultKeyMode) {
3067 case DEFAULT_KEYS_DIALER:
3068 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3069 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003070 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 break;
3072 case DEFAULT_KEYS_SEARCH_LOCAL:
3073 startSearch(str, false, null, false);
3074 break;
3075 case DEFAULT_KEYS_SEARCH_GLOBAL:
3076 startSearch(str, false, null, true);
3077 break;
3078 }
3079 }
3080 }
3081 if (clearSpannable) {
3082 mDefaultKeySsb.clear();
3083 mDefaultKeySsb.clearSpans();
3084 Selection.setSelection(mDefaultKeySsb,0);
3085 }
3086 return handled;
3087 }
3088 }
3089
3090 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003091 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3092 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3093 * the event).
3094 */
3095 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3096 return false;
3097 }
3098
3099 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003101 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 * is inside a TextView will not trigger the event (unless it is a navigation
3103 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003104 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003105 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3106 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003109 * further, or <code>false</code> to indicate that you have not handled
3110 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 * @see #onKeyDown
3112 * @see KeyEvent
3113 */
3114 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003115 if (getApplicationInfo().targetSdkVersion
3116 >= Build.VERSION_CODES.ECLAIR) {
3117 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3118 && !event.isCanceled()) {
3119 onBackPressed();
3120 return true;
3121 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 return false;
3124 }
3125
3126 /**
3127 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3128 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3129 * the event).
3130 */
3131 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3132 return false;
3133 }
RoboErik55011652014-07-09 15:05:53 -07003134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003136 * Called when the activity has detected the user's press of the back
3137 * key. The default implementation simply finishes the current activity,
3138 * but you can override this to do whatever you want.
3139 */
3140 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003141 if (mActionBar != null && mActionBar.collapseActionView()) {
3142 return;
3143 }
3144
George Mount27b0dc02017-02-21 10:24:09 -08003145 FragmentManager fragmentManager = mFragments.getFragmentManager();
3146
3147 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003148 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003149 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003150 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003151
3152 /**
3153 * Called when a key shortcut event is not handled by any of the views in the Activity.
3154 * Override this method to implement global key shortcuts for the Activity.
3155 * Key shortcuts can also be implemented by setting the
3156 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3157 *
3158 * @param keyCode The value in event.getKeyCode().
3159 * @param event Description of the key event.
3160 * @return True if the key shortcut was handled.
3161 */
3162 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003163 // Let the Action Bar have a chance at handling the shortcut.
3164 ActionBar actionBar = getActionBar();
3165 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003166 }
3167
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 * Called when a touch screen event was not handled by any of the views
3170 * under it. This is most useful to process touch events that happen
3171 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 * @return Return true if you have consumed the event, false if you haven't.
3176 * The default implementation always returns false.
3177 */
3178 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003179 if (mWindow.shouldCloseOnTouch(this, event)) {
3180 finish();
3181 return true;
3182 }
RoboErik55011652014-07-09 15:05:53 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 return false;
3185 }
RoboErik55011652014-07-09 15:05:53 -07003186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 /**
3188 * Called when the trackball was moved and not handled by any of the
3189 * views inside of the activity. So, for example, if the trackball moves
3190 * while focus is on a button, you will receive a call here because
3191 * buttons do not normally do anything with trackball events. The call
3192 * here happens <em>before</em> trackball movements are converted to
3193 * DPAD key events, which then get sent back to the view hierarchy, and
3194 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003197 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 * @return Return true if you have consumed the event, false if you haven't.
3199 * The default implementation always returns false.
3200 */
3201 public boolean onTrackballEvent(MotionEvent event) {
3202 return false;
3203 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003204
3205 /**
3206 * Called when a generic motion event was not handled by any of the
3207 * views inside of the activity.
3208 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003209 * Generic motion events describe joystick movements, mouse hovers, track pad
3210 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003211 * {@link MotionEvent#getSource() source} of the motion event specifies
3212 * the class of input that was received. Implementations of this method
3213 * must examine the bits in the source before processing the event.
3214 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003215 * </p><p>
3216 * Generic motion events with source class
3217 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3218 * are delivered to the view under the pointer. All other generic motion events are
3219 * delivered to the focused view.
3220 * </p><p>
3221 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3222 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003223 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003224 *
3225 * @param event The generic motion event being processed.
3226 *
3227 * @return Return true if you have consumed the event, false if you haven't.
3228 * The default implementation always returns false.
3229 */
3230 public boolean onGenericMotionEvent(MotionEvent event) {
3231 return false;
3232 }
3233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 /**
3235 * Called whenever a key, touch, or trackball event is dispatched to the
3236 * activity. Implement this method if you wish to know that the user has
3237 * interacted with the device in some way while your activity is running.
3238 * This callback and {@link #onUserLeaveHint} are intended to help
3239 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003240 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3243 * be accompanied by calls to {@link #onUserInteraction}. This
3244 * ensures that your activity will be told of relevant user activity such
3245 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 * <p>Note that this callback will be invoked for the touch down action
3248 * that begins a touch gesture, but may not be invoked for the touch-moved
3249 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 * @see #onUserLeaveHint()
3252 */
3253 public void onUserInteraction() {
3254 }
RoboErik55011652014-07-09 15:05:53 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3257 // Update window manager if: we have a view, that view is
3258 // attached to its parent (which will be a RootView), and
3259 // this activity is not embedded.
3260 if (mParent == null) {
3261 View decor = mDecor;
3262 if (decor != null && decor.getParent() != null) {
3263 getWindowManager().updateViewLayout(decor, params);
3264 }
3265 }
3266 }
3267
3268 public void onContentChanged() {
3269 }
3270
3271 /**
3272 * Called when the current {@link Window} of the activity gains or loses
3273 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003274 * to the user. The default implementation clears the key tracking
3275 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003276 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003277 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 * is managed independently of activity lifecycles. As such, while focus
3279 * changes will generally have some relation to lifecycle changes (an
3280 * activity that is stopped will not generally get window focus), you
3281 * should not rely on any particular order between the callbacks here and
3282 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 * <p>As a general rule, however, a resumed activity will have window
3285 * focus... unless it has displayed other dialogs or popups that take
3286 * input focus, in which case the activity itself will not have focus
3287 * when the other windows have it. Likewise, the system may display
3288 * system-level windows (such as the status bar notification panel or
3289 * a system alert) which will temporarily take window input focus without
3290 * pausing the foreground activity.
3291 *
3292 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 * @see #hasWindowFocus()
3295 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003296 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 */
3298 public void onWindowFocusChanged(boolean hasFocus) {
3299 }
RoboErik55011652014-07-09 15:05:53 -07003300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003302 * Called when the main window associated with the activity has been
3303 * attached to the window manager.
3304 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3305 * for more information.
3306 * @see View#onAttachedToWindow
3307 */
3308 public void onAttachedToWindow() {
3309 }
RoboErik55011652014-07-09 15:05:53 -07003310
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003311 /**
3312 * Called when the main window associated with the activity has been
3313 * detached from the window manager.
3314 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3315 * for more information.
3316 * @see View#onDetachedFromWindow
3317 */
3318 public void onDetachedFromWindow() {
3319 }
RoboErik55011652014-07-09 15:05:53 -07003320
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 * Returns true if this activity's <em>main</em> window currently has window focus.
3323 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003324 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3328 */
3329 public boolean hasWindowFocus() {
3330 Window w = getWindow();
3331 if (w != null) {
3332 View d = w.getDecorView();
3333 if (d != null) {
3334 return d.hasWindowFocus();
3335 }
3336 }
3337 return false;
3338 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003339
3340 /**
3341 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003342 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003343 */
Adam Powell117b6952014-05-05 18:14:56 -07003344 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003345 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003346 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003347 if (suppressWindowTransition) {
3348 overridePendingTransition(0, 0);
3349 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003350 }
RoboErik55011652014-07-09 15:05:53 -07003351
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003352
Skuhnece2faa52015-08-11 10:36:38 -07003353 /**
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003354 * Moves the activity from {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode to
3355 * {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003356 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003357 * @hide
3358 */
3359 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08003360 public void exitFreeformMode() throws RemoteException {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003361 ActivityManager.getService().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003362 }
3363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003365 * Puts the activity in picture-in-picture mode if the activity supports.
3366 * @see android.R.attr#supportsPictureInPicture
3367 * @hide
3368 */
3369 @Override
3370 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003371 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003372 enterPictureInPictureMode();
3373 }
3374 }
3375
3376 /**
RoboErik55011652014-07-09 15:05:53 -07003377 * Called to process key events. You can override this to intercept all
3378 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 * @return boolean Return true if this event was consumed.
3384 */
3385 public boolean dispatchKeyEvent(KeyEvent event) {
3386 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003387
3388 // Let action bars open menus in response to the menu key prioritized over
3389 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003390 final int keyCode = event.getKeyCode();
3391 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003392 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3393 return true;
3394 }
3395
Dianne Hackborn8d374262009-09-14 21:21:52 -07003396 Window win = getWindow();
3397 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 return true;
3399 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003400 View decor = mDecor;
3401 if (decor == null) decor = win.getDecorView();
3402 return event.dispatch(this, decor != null
3403 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 }
3405
3406 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003407 * Called to process a key shortcut event.
3408 * You can override this to intercept all key shortcut events before they are
3409 * dispatched to the window. Be sure to call this implementation for key shortcut
3410 * events that should be handled normally.
3411 *
3412 * @param event The key shortcut event.
3413 * @return True if this event was consumed.
3414 */
3415 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3416 onUserInteraction();
3417 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3418 return true;
3419 }
3420 return onKeyShortcut(event.getKeyCode(), event);
3421 }
3422
3423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 * Called to process touch screen events. You can override this to
3425 * intercept all touch screen events before they are dispatched to the
3426 * window. Be sure to call this implementation for touch screen events
3427 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 * @return boolean Return true if this event was consumed.
3432 */
3433 public boolean dispatchTouchEvent(MotionEvent ev) {
3434 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3435 onUserInteraction();
3436 }
3437 if (getWindow().superDispatchTouchEvent(ev)) {
3438 return true;
3439 }
3440 return onTouchEvent(ev);
3441 }
RoboErik55011652014-07-09 15:05:53 -07003442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 /**
3444 * Called to process trackball events. You can override this to
3445 * intercept all trackball events before they are dispatched to the
3446 * window. Be sure to call this implementation for trackball events
3447 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 * @return boolean Return true if this event was consumed.
3452 */
3453 public boolean dispatchTrackballEvent(MotionEvent ev) {
3454 onUserInteraction();
3455 if (getWindow().superDispatchTrackballEvent(ev)) {
3456 return true;
3457 }
3458 return onTrackballEvent(ev);
3459 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003460
Jeff Browncb1404e2011-01-15 18:14:15 -08003461 /**
3462 * Called to process generic motion events. You can override this to
3463 * intercept all generic motion events before they are dispatched to the
3464 * window. Be sure to call this implementation for generic motion events
3465 * that should be handled normally.
3466 *
3467 * @param ev The generic motion event.
3468 *
3469 * @return boolean Return true if this event was consumed.
3470 */
3471 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3472 onUserInteraction();
3473 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3474 return true;
3475 }
3476 return onGenericMotionEvent(ev);
3477 }
3478
svetoslavganov75986cf2009-05-14 22:28:01 -07003479 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3480 event.setClassName(getClass().getName());
3481 event.setPackageName(getPackageName());
3482
3483 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003484 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3485 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003486 event.setFullScreen(isFullScreen);
3487
3488 CharSequence title = getTitle();
3489 if (!TextUtils.isEmpty(title)) {
3490 event.getText().add(title);
3491 }
3492
3493 return true;
3494 }
3495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 /**
3497 * Default implementation of
3498 * {@link android.view.Window.Callback#onCreatePanelView}
3499 * for activities. This
3500 * simply returns null so that all panel sub-windows will have the default
3501 * menu behavior.
3502 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003503 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 public View onCreatePanelView(int featureId) {
3505 return null;
3506 }
3507
3508 /**
3509 * Default implementation of
3510 * {@link android.view.Window.Callback#onCreatePanelMenu}
3511 * for activities. This calls through to the new
3512 * {@link #onCreateOptionsMenu} method for the
3513 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3514 * so that subclasses of Activity don't need to deal with feature codes.
3515 */
3516 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3517 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003518 boolean show = onCreateOptionsMenu(menu);
3519 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3520 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
3522 return false;
3523 }
3524
3525 /**
3526 * Default implementation of
3527 * {@link android.view.Window.Callback#onPreparePanel}
3528 * for activities. This
3529 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3530 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3531 * panel, so that subclasses of
3532 * Activity don't need to deal with feature codes.
3533 */
3534 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3535 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3536 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003537 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003538 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 }
3540 return true;
3541 }
3542
3543 /**
3544 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 * @return The default implementation returns true.
3547 */
3548 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003549 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003550 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003551 if (mActionBar != null) {
3552 mActionBar.dispatchMenuVisibilityChanged(true);
3553 } else {
3554 Log.e(TAG, "Tried to open action bar menu with no action bar");
3555 }
Adam Powell8515ee82010-11-30 14:09:55 -08003556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 return true;
3558 }
3559
3560 /**
3561 * Default implementation of
3562 * {@link android.view.Window.Callback#onMenuItemSelected}
3563 * for activities. This calls through to the new
3564 * {@link #onOptionsItemSelected} method for the
3565 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3566 * panel, so that subclasses of
3567 * Activity don't need to deal with feature codes.
3568 */
3569 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003570 CharSequence titleCondensed = item.getTitleCondensed();
3571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 switch (featureId) {
3573 case Window.FEATURE_OPTIONS_PANEL:
3574 // Put event logging here so it gets called even if subclass
3575 // doesn't call through to superclass's implmeentation of each
3576 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003577 if(titleCondensed != null) {
3578 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3579 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003580 if (onOptionsItemSelected(item)) {
3581 return true;
3582 }
Adam Powell04d58112012-04-09 10:22:12 -07003583 if (mFragments.dispatchOptionsItemSelected(item)) {
3584 return true;
3585 }
3586 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3587 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3588 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003589 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003590 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003591 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003592 }
Adam Powell04d58112012-04-09 10:22:12 -07003593 }
3594 return false;
RoboErik55011652014-07-09 15:05:53 -07003595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003597 if(titleCondensed != null) {
3598 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3599 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003600 if (onContextItemSelected(item)) {
3601 return true;
3602 }
3603 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 default:
3606 return false;
3607 }
3608 }
RoboErik55011652014-07-09 15:05:53 -07003609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 /**
3611 * Default implementation of
3612 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3613 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3614 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3615 * so that subclasses of Activity don't need to deal with feature codes.
3616 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3617 * {@link #onContextMenuClosed(Menu)} will be called.
3618 */
3619 public void onPanelClosed(int featureId, Menu menu) {
3620 switch (featureId) {
3621 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003622 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 onOptionsMenuClosed(menu);
3624 break;
RoboErik55011652014-07-09 15:05:53 -07003625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 case Window.FEATURE_CONTEXT_MENU:
3627 onContextMenuClosed(menu);
3628 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003629
3630 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003631 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003632 mActionBar.dispatchMenuVisibilityChanged(false);
3633 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 }
3635 }
3636
3637 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003638 * Declare that the options menu has changed, so should be recreated.
3639 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3640 * time it needs to be displayed.
3641 */
3642 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003643 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3644 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003645 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3646 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003647 }
RoboErik55011652014-07-09 15:05:53 -07003648
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 * Initialize the contents of the Activity's standard options menu. You
3651 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003652 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 * <p>This is only called once, the first time the options menu is
3654 * displayed. To update the menu every time it is displayed, see
3655 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003658 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3659 * they will be correctly ordered with application-defined menu items.
3660 * Deriving classes should always call through to the base implementation.
3661 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 * <p>You can safely hold on to <var>menu</var> (and any items created
3663 * from it), making modifications to it as desired, until the next
3664 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 * <p>When you add items to the menu, you can implement the Activity's
3667 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003668 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003670 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 * @return You must return true for the menu to be displayed;
3672 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 * @see #onPrepareOptionsMenu
3675 * @see #onOptionsItemSelected
3676 */
3677 public boolean onCreateOptionsMenu(Menu menu) {
3678 if (mParent != null) {
3679 return mParent.onCreateOptionsMenu(menu);
3680 }
3681 return true;
3682 }
3683
3684 /**
3685 * Prepare the Screen's standard options menu to be displayed. This is
3686 * called right before the menu is shown, every time it is shown. You can
3687 * use this method to efficiently enable/disable items or otherwise
3688 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003689 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 * <p>The default implementation updates the system menu items based on the
3691 * activity's state. Deriving classes should always call through to the
3692 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 * @param menu The options menu as last shown or first initialized by
3695 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 * @return You must return true for the menu to be displayed;
3698 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003699 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 * @see #onCreateOptionsMenu
3701 */
3702 public boolean onPrepareOptionsMenu(Menu menu) {
3703 if (mParent != null) {
3704 return mParent.onPrepareOptionsMenu(menu);
3705 }
3706 return true;
3707 }
3708
3709 /**
3710 * This hook is called whenever an item in your options menu is selected.
3711 * The default implementation simply returns false to have the normal
3712 * processing happen (calling the item's Runnable or sending a message to
3713 * its Handler as appropriate). You can use this method for any items
3714 * for which you would like to do processing without those other
3715 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003718 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 * @return boolean Return false to allow normal menu processing to
3723 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003724 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 * @see #onCreateOptionsMenu
3726 */
3727 public boolean onOptionsItemSelected(MenuItem item) {
3728 if (mParent != null) {
3729 return mParent.onOptionsItemSelected(item);
3730 }
3731 return false;
3732 }
3733
3734 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003735 * This method is called whenever the user chooses to navigate Up within your application's
3736 * activity hierarchy from the action bar.
3737 *
3738 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3739 * was specified in the manifest for this activity or an activity-alias to it,
3740 * default Up navigation will be handled automatically. If any activity
3741 * along the parent chain requires extra Intent arguments, the Activity subclass
3742 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3743 * to supply those arguments.</p>
3744 *
Mark Lufa434852016-08-11 17:40:33 -07003745 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07003746 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3747 * from the design guide for more information about navigating within your app.</p>
3748 *
3749 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3750 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3751 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3752 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3753 *
3754 * @return true if Up navigation completed successfully and this Activity was finished,
3755 * false otherwise.
3756 */
3757 public boolean onNavigateUp() {
3758 // Automatically handle hierarchical Up navigation if the proper
3759 // metadata is available.
3760 Intent upIntent = getParentActivityIntent();
3761 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003762 if (mActivityInfo.taskAffinity == null) {
3763 // Activities with a null affinity are special; they really shouldn't
3764 // specify a parent activity intent in the first place. Just finish
3765 // the current activity and call it a day.
3766 finish();
3767 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003768 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003769 onCreateNavigateUpTaskStack(b);
3770 onPrepareNavigateUpTaskStack(b);
3771 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003772
3773 // We can't finishAffinity if we have a result.
3774 // Fall back and simply finish the current activity instead.
3775 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3776 // Tell the developer what's going on to avoid hair-pulling.
3777 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3778 finish();
3779 } else {
3780 finishAffinity();
3781 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003782 } else {
3783 navigateUpTo(upIntent);
3784 }
3785 return true;
3786 }
3787 return false;
3788 }
3789
3790 /**
3791 * This is called when a child activity of this one attempts to navigate up.
3792 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3793 *
3794 * @param child The activity making the call.
3795 */
3796 public boolean onNavigateUpFromChild(Activity child) {
3797 return onNavigateUp();
3798 }
3799
3800 /**
3801 * Define the synthetic task stack that will be generated during Up navigation from
3802 * a different task.
3803 *
3804 * <p>The default implementation of this method adds the parent chain of this activity
3805 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3806 * may choose to override this method to construct the desired task stack in a different
3807 * way.</p>
3808 *
Adam Powellf0195952012-05-02 21:38:54 -07003809 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3810 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3811 * returned by {@link #getParentActivityIntent()}.</p>
3812 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003813 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3814 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3815 *
3816 * @param builder An empty TaskStackBuilder - the application should add intents representing
3817 * the desired task stack
3818 */
3819 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3820 builder.addParentStack(this);
3821 }
3822
3823 /**
3824 * Prepare the synthetic task stack that will be generated during Up navigation
3825 * from a different task.
3826 *
3827 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3828 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3829 * If any extra data should be added to these intents before launching the new task,
3830 * the application should override this method and add that data here.</p>
3831 *
3832 * @param builder A TaskStackBuilder that has been populated with Intents by
3833 * onCreateNavigateUpTaskStack.
3834 */
3835 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3836 }
3837
3838 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 * This hook is called whenever the options menu is being closed (either by the user canceling
3840 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003841 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 * @param menu The options menu as last shown or first initialized by
3843 * onCreateOptionsMenu().
3844 */
3845 public void onOptionsMenuClosed(Menu menu) {
3846 if (mParent != null) {
3847 mParent.onOptionsMenuClosed(menu);
3848 }
3849 }
RoboErik55011652014-07-09 15:05:53 -07003850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 /**
3852 * Programmatically opens the options menu. If the options menu is already
3853 * open, this method does nothing.
3854 */
3855 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003856 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3857 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003858 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 }
RoboErik55011652014-07-09 15:05:53 -07003861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 /**
3863 * Progammatically closes the options menu. If the options menu is already
3864 * closed, this method does nothing.
3865 */
3866 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07003867 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3868 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003869 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
3872
3873 /**
3874 * Called when a context menu for the {@code view} is about to be shown.
3875 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3876 * time the context menu is about to be shown and should be populated for
3877 * the view (or item inside the view for {@link AdapterView} subclasses,
3878 * this can be found in the {@code menuInfo})).
3879 * <p>
3880 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3881 * item has been selected.
3882 * <p>
3883 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003884 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 */
3886 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3887 }
3888
3889 /**
3890 * Registers a context menu to be shown for the given view (multiple views
3891 * can show the context menu). This method will set the
3892 * {@link OnCreateContextMenuListener} on the view to this activity, so
3893 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3894 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003895 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 * @see #unregisterForContextMenu(View)
3897 * @param view The view that should show a context menu.
3898 */
3899 public void registerForContextMenu(View view) {
3900 view.setOnCreateContextMenuListener(this);
3901 }
RoboErik55011652014-07-09 15:05:53 -07003902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 /**
3904 * Prevents a context menu to be shown for the given view. This method will remove the
3905 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003906 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 * @see #registerForContextMenu(View)
3908 * @param view The view that should stop showing a context menu.
3909 */
3910 public void unregisterForContextMenu(View view) {
3911 view.setOnCreateContextMenuListener(null);
3912 }
RoboErik55011652014-07-09 15:05:53 -07003913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 /**
3915 * Programmatically opens the context menu for a particular {@code view}.
3916 * The {@code view} should have been added via
3917 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003918 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 * @param view The view to show the context menu for.
3920 */
3921 public void openContextMenu(View view) {
3922 view.showContextMenu();
3923 }
RoboErik55011652014-07-09 15:05:53 -07003924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 /**
3926 * Programmatically closes the most recently opened context menu, if showing.
3927 */
3928 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003929 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3930 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 }
RoboErik55011652014-07-09 15:05:53 -07003933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 /**
3935 * This hook is called whenever an item in a context menu is selected. The
3936 * default implementation simply returns false to have the normal processing
3937 * happen (calling the item's Runnable or sending a message to its Handler
3938 * as appropriate). You can use this method for any items for which you
3939 * would like to do processing without those other facilities.
3940 * <p>
3941 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3942 * View that added this menu item.
3943 * <p>
3944 * Derived classes should call through to the base class for it to perform
3945 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003946 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 * @param item The context menu item that was selected.
3948 * @return boolean Return false to allow normal context menu processing to
3949 * proceed, true to consume it here.
3950 */
3951 public boolean onContextItemSelected(MenuItem item) {
3952 if (mParent != null) {
3953 return mParent.onContextItemSelected(item);
3954 }
3955 return false;
3956 }
3957
3958 /**
3959 * This hook is called whenever the context menu is being closed (either by
3960 * the user canceling the menu with the back/menu button, or when an item is
3961 * selected).
RoboErik55011652014-07-09 15:05:53 -07003962 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 * @param menu The context menu that is being closed.
3964 */
3965 public void onContextMenuClosed(Menu menu) {
3966 if (mParent != null) {
3967 mParent.onContextMenuClosed(menu);
3968 }
3969 }
3970
3971 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003972 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003974 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 protected Dialog onCreateDialog(int id) {
3976 return null;
3977 }
3978
3979 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003980 * Callback for creating dialogs that are managed (saved and restored) for you
3981 * by the activity. The default implementation calls through to
3982 * {@link #onCreateDialog(int)} for compatibility.
3983 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003984 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3985 * or later, consider instead using a {@link DialogFragment} instead.</em>
3986 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003987 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3988 * this method the first time, and hang onto it thereafter. Any dialog
3989 * that is created by this method will automatically be saved and restored
3990 * for you, including whether it is showing.
3991 *
3992 * <p>If you would like the activity to manage saving and restoring dialogs
3993 * for you, you should override this method and handle any ids that are
3994 * passed to {@link #showDialog}.
3995 *
3996 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3997 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3998 *
3999 * @param id The id of the dialog.
4000 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4001 * @return The dialog. If you return null, the dialog will not be created.
4002 *
4003 * @see #onPrepareDialog(int, Dialog, Bundle)
4004 * @see #showDialog(int, Bundle)
4005 * @see #dismissDialog(int)
4006 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004007 *
4008 * @deprecated Use the new {@link DialogFragment} class with
4009 * {@link FragmentManager} instead; this is also
4010 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004011 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004012 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004013 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004014 protected Dialog onCreateDialog(int id, Bundle args) {
4015 return onCreateDialog(id);
4016 }
4017
4018 /**
4019 * @deprecated Old no-arguments version of
4020 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4021 */
4022 @Deprecated
4023 protected void onPrepareDialog(int id, Dialog dialog) {
4024 dialog.setOwnerActivity(this);
4025 }
4026
4027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004029 * shown. The default implementation calls through to
4030 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004031 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 * <p>
4033 * Override this if you need to update a managed dialog based on the state
4034 * of the application each time it is shown. For example, a time picker
4035 * dialog might want to be updated with the current time. You should call
4036 * through to the superclass's implementation. The default implementation
4037 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 * @param id The id of the managed dialog.
4040 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004041 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4042 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 * @see #showDialog(int)
4044 * @see #dismissDialog(int)
4045 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004046 *
4047 * @deprecated Use the new {@link DialogFragment} class with
4048 * {@link FragmentManager} instead; this is also
4049 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004051 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004052 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4053 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
4055
4056 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004057 * Simple version of {@link #showDialog(int, Bundle)} that does not
4058 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4059 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004060 *
4061 * @deprecated Use the new {@link DialogFragment} class with
4062 * {@link FragmentManager} instead; this is also
4063 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004064 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004065 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004066 public final void showDialog(int id) {
4067 showDialog(id, null);
4068 }
4069
4070 /**
4071 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 * will be made with the same id the first time this is called for a given
4073 * id. From thereafter, the dialog will be automatically saved and restored.
4074 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004075 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4076 * or later, consider instead using a {@link DialogFragment} instead.</em>
4077 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004078 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 * be made to provide an opportunity to do any timely preparation.
4080 *
4081 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004082 * @param args Arguments to pass through to the dialog. These will be saved
4083 * and restored for you. Note that if the dialog is already created,
4084 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4085 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004086 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004087 * @return Returns true if the Dialog was created; false is returned if
4088 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004089 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004090 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004091 * @see #onCreateDialog(int, Bundle)
4092 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 * @see #dismissDialog(int)
4094 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004095 *
4096 * @deprecated Use the new {@link DialogFragment} class with
4097 * {@link FragmentManager} instead; this is also
4098 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004100 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004101 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004103 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004105 ManagedDialog md = mManagedDialogs.get(id);
4106 if (md == null) {
4107 md = new ManagedDialog();
4108 md.mDialog = createDialog(id, null, args);
4109 if (md.mDialog == null) {
4110 return false;
4111 }
4112 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 }
RoboErik55011652014-07-09 15:05:53 -07004114
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004115 md.mArgs = args;
4116 onPrepareDialog(id, md.mDialog, args);
4117 md.mDialog.show();
4118 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 }
4120
4121 /**
4122 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4123 *
4124 * @param id The id of the managed dialog.
4125 *
4126 * @throws IllegalArgumentException if the id was not previously shown via
4127 * {@link #showDialog(int)}.
4128 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004129 * @see #onCreateDialog(int, Bundle)
4130 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 * @see #showDialog(int)
4132 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004133 *
4134 * @deprecated Use the new {@link DialogFragment} class with
4135 * {@link FragmentManager} instead; this is also
4136 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004138 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 public final void dismissDialog(int id) {
4140 if (mManagedDialogs == null) {
4141 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
RoboErik55011652014-07-09 15:05:53 -07004143
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004144 final ManagedDialog md = mManagedDialogs.get(id);
4145 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 throw missingDialog(id);
4147 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004148 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 }
4150
4151 /**
4152 * Creates an exception to throw if a user passed in a dialog id that is
4153 * unexpected.
4154 */
4155 private IllegalArgumentException missingDialog(int id) {
4156 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4157 + "shown via Activity#showDialog");
4158 }
4159
4160 /**
4161 * Removes any internal references to a dialog managed by this Activity.
4162 * If the dialog is showing, it will dismiss it as part of the clean up.
4163 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004164 * <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 -08004165 * want to avoid the overhead of saving and restoring it in the future.
4166 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004167 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4168 * will not throw an exception if you try to remove an ID that does not
4169 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 * @param id The id of the managed dialog.
4172 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004173 * @see #onCreateDialog(int, Bundle)
4174 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 * @see #showDialog(int)
4176 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004177 *
4178 * @deprecated Use the new {@link DialogFragment} class with
4179 * {@link FragmentManager} instead; this is also
4180 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004182 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004184 if (mManagedDialogs != null) {
4185 final ManagedDialog md = mManagedDialogs.get(id);
4186 if (md != null) {
4187 md.mDialog.dismiss();
4188 mManagedDialogs.remove(id);
4189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 }
4192
4193 /**
4194 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004195 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004196 * <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 -07004197 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004198 * calling this function is the same as calling
4199 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4200 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 * <p>You can override this function to force global search, e.g. in response to a dedicated
4203 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004204 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004205 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4206 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4207 * return false and you must supply your own custom implementation if you want to support
4208 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004209 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004210 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004211 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4212 * not respond to search. The default implementation always returns {@code true}, except
4213 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004214 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 * @see android.app.SearchManager
4216 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004217 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4218 mSearchEvent = searchEvent;
4219 boolean result = onSearchRequested();
4220 mSearchEvent = null;
4221 return result;
4222 }
4223
4224 /**
4225 * @see #onSearchRequested(SearchEvent)
4226 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004228 final int uiMode = getResources().getConfiguration().uiMode
4229 & Configuration.UI_MODE_TYPE_MASK;
4230 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4231 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004232 startSearch(null, false, null, false);
4233 return true;
4234 } else {
4235 return false;
4236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
RoboErik55011652014-07-09 15:05:53 -07004238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004240 * During the onSearchRequested() callbacks, this function will return the
4241 * {@link SearchEvent} that triggered the callback, if it exists.
4242 *
4243 * @return SearchEvent The SearchEvent that triggered the {@link
4244 * #onSearchRequested} callback.
4245 */
4246 public final SearchEvent getSearchEvent() {
4247 return mSearchEvent;
4248 }
4249
4250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004252 *
4253 * <p>It is typically called from onSearchRequested(), either directly from
4254 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004256 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 * is to inject specific data such as context data, it is preferred to <i>override</i>
4258 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004259 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004260 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4261 * not supported.
4262 *
RoboErik55011652014-07-09 15:05:53 -07004263 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004265 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 * any further typing will replace it. This is useful for cases where an entire pre-formed
4267 * query is being inserted. If false, the selection point will be placed at the end of the
4268 * inserted query. This is useful when the inserted query is text that the user entered,
4269 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4270 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004271 * @param appSearchData An application can insert application-specific
4272 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 * searches. This data will be returned with SEARCH intent(s). Null if
4274 * no extra data is required.
4275 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004276 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004277 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 * @see android.app.SearchManager
4281 * @see #onSearchRequested
4282 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004283 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4284 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004285 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004286 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004287 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
4289
4290 /**
krosaend2d60142009-08-17 08:56:48 -07004291 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4292 * the search dialog. Made available for testing purposes.
4293 *
4294 * @param query The query to trigger. If empty, the request will be ignored.
4295 * @param appSearchData An application can insert application-specific
4296 * context here, in order to improve quality or specificity of its own
4297 * searches. This data will be returned with SEARCH intent(s). Null if
4298 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004299 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004300 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004301 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004302 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004303 }
4304
4305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 * Request that key events come to this activity. Use this if your
4307 * activity has no views with focus, but the activity still wants
4308 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 * @see android.view.Window#takeKeyEvents
4311 */
4312 public void takeKeyEvents(boolean get) {
4313 getWindow().takeKeyEvents(get);
4314 }
4315
4316 /**
4317 * Enable extended window features. This is a convenience for calling
4318 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 * @param featureId The desired feature as defined in
4321 * {@link android.view.Window}.
4322 * @return Returns true if the requested feature is supported and now
4323 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004324 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 * @see android.view.Window#requestFeature
4326 */
4327 public final boolean requestWindowFeature(int featureId) {
4328 return getWindow().requestFeature(featureId);
4329 }
4330
4331 /**
4332 * Convenience for calling
4333 * {@link android.view.Window#setFeatureDrawableResource}.
4334 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004335 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 getWindow().setFeatureDrawableResource(featureId, resId);
4337 }
4338
4339 /**
4340 * Convenience for calling
4341 * {@link android.view.Window#setFeatureDrawableUri}.
4342 */
4343 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4344 getWindow().setFeatureDrawableUri(featureId, uri);
4345 }
4346
4347 /**
4348 * Convenience for calling
4349 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4350 */
4351 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4352 getWindow().setFeatureDrawable(featureId, drawable);
4353 }
4354
4355 /**
4356 * Convenience for calling
4357 * {@link android.view.Window#setFeatureDrawableAlpha}.
4358 */
4359 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4360 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4361 }
4362
4363 /**
4364 * Convenience for calling
4365 * {@link android.view.Window#getLayoutInflater}.
4366 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004367 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 public LayoutInflater getLayoutInflater() {
4369 return getWindow().getLayoutInflater();
4370 }
4371
4372 /**
4373 * Returns a {@link MenuInflater} with this context.
4374 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004375 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004377 // Make sure that action views can get an appropriate theme.
4378 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004379 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004380 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004381 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004382 } else {
4383 mMenuInflater = new MenuInflater(this);
4384 }
4385 }
4386 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 }
4388
4389 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004390 public void setTheme(int resid) {
4391 super.setTheme(resid);
4392 mWindow.setTheme(resid);
4393 }
4394
4395 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004396 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004397 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 if (mParent == null) {
4399 super.onApplyThemeResource(theme, resid, first);
4400 } else {
4401 try {
4402 theme.setTo(mParent.getTheme());
4403 } catch (Exception e) {
4404 // Empty
4405 }
4406 theme.applyStyle(resid, false);
4407 }
Winson073a5262016-02-29 16:04:40 -08004408
4409 // Get the primary color and update the TaskDescription for this activity
4410 TypedArray a = theme.obtainStyledAttributes(
4411 com.android.internal.R.styleable.ActivityTaskDescription);
4412 if (mTaskDescription.getPrimaryColor() == 0) {
4413 int colorPrimary = a.getColor(
4414 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4415 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4416 mTaskDescription.setPrimaryColor(colorPrimary);
4417 }
4418 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004419
4420 int colorBackground = a.getColor(
4421 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4422 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4423 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004424 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004425
4426 final int statusBarColor = a.getColor(
4427 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4428 if (statusBarColor != 0) {
4429 mTaskDescription.setStatusBarColor(statusBarColor);
4430 }
4431
4432 final int navigationBarColor = a.getColor(
4433 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4434 if (navigationBarColor != 0) {
4435 mTaskDescription.setNavigationBarColor(navigationBarColor);
4436 }
4437
Winson073a5262016-02-29 16:04:40 -08004438 a.recycle();
4439 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004440 }
4441
4442 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004443 * Requests permissions to be granted to this application. These permissions
4444 * must be requested in your manifest, they should not be granted to your app,
4445 * and they should have protection level {@link android.content.pm.PermissionInfo
4446 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4447 * the platform or a third-party app.
4448 * <p>
4449 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4450 * are granted at install time if requested in the manifest. Signature permissions
4451 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4452 * install time if requested in the manifest and the signature of your app matches
4453 * the signature of the app declaring the permissions.
4454 * </p>
4455 * <p>
4456 * If your app does not have the requested permissions the user will be presented
4457 * with UI for accepting them. After the user has accepted or rejected the
4458 * requested permissions you will receive a callback on {@link
4459 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4460 * permissions were granted or not.
4461 * </p>
4462 * <p>
4463 * Note that requesting a permission does not guarantee it will be granted and
4464 * your app should be able to run without having this permission.
4465 * </p>
4466 * <p>
4467 * This method may start an activity allowing the user to choose which permissions
4468 * to grant and which to reject. Hence, you should be prepared that your activity
4469 * may be paused and resumed. Further, granting some permissions may require
4470 * a restart of you application. In such a case, the system will recreate the
4471 * activity stack before delivering the result to {@link
4472 * #onRequestPermissionsResult(int, String[], int[])}.
4473 * </p>
4474 * <p>
4475 * When checking whether you have a permission you should use {@link
4476 * #checkSelfPermission(String)}.
4477 * </p>
4478 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004479 * Calling this API for permissions already granted to your app would show UI
4480 * to the user to decide whether the app can still hold these permissions. This
4481 * can be useful if the way your app uses data guarded by the permissions
4482 * changes significantly.
4483 * </p>
4484 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004485 * You cannot request a permission if your activity sets {@link
4486 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4487 * <code>true</code> because in this case the activity would not receive
4488 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4489 * </p>
4490 * <p>
Trevor Johns682c24e2016-04-12 10:13:47 -07004491 * The <a href="http://developer.android.com/samples/RuntimePermissions/index.html">
4492 * RuntimePermissions</a> sample app demonstrates how to use this method to
4493 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004494 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004495 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004496 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004497 * @param requestCode Application specific request code to match with a result
4498 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004499 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004500 *
Paul Miller77a0e132016-02-11 13:46:49 -08004501 * @throws IllegalArgumentException if requestCode is negative.
4502 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004503 * @see #onRequestPermissionsResult(int, String[], int[])
4504 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004505 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004506 */
4507 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004508 if (requestCode < 0) {
4509 throw new IllegalArgumentException("requestCode should be >= 0");
4510 }
Svetoslavffb32b12015-10-15 16:54:00 -07004511 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00004512 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07004513 // Dispatch the callback with empty arrays which means a cancellation.
4514 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4515 return;
4516 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004517 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004518 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004519 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004520 }
4521
4522 /**
4523 * Callback for the result from requesting permissions. This method
4524 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004525 * <p>
4526 * <strong>Note:</strong> It is possible that the permissions request interaction
4527 * with the user is interrupted. In this case you will receive empty permissions
4528 * and results arrays which should be treated as a cancellation.
4529 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004530 *
4531 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4532 * @param permissions The requested permissions. Never null.
4533 * @param grantResults The grant results for the corresponding permissions
4534 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4535 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4536 *
4537 * @see #requestPermissions(String[], int)
4538 */
4539 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4540 @NonNull int[] grantResults) {
4541 /* callback - no nothing */
4542 }
4543
4544 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004545 * Gets whether you should show UI with rationale for requesting a permission.
4546 * You should do this only if you do not have the permission and the context in
4547 * which the permission is requested does not clearly communicate to the user
4548 * what would be the benefit from granting this permission.
4549 * <p>
4550 * For example, if you write a camera app, requesting the camera permission
4551 * would be expected by the user and no rationale for why it is requested is
4552 * needed. If however, the app needs location for tagging photos then a non-tech
4553 * savvy user may wonder how location is related to taking photos. In this case
4554 * you may choose to show UI with rationale of requesting this permission.
4555 * </p>
4556 *
4557 * @param permission A permission your app wants to request.
4558 * @return Whether you can show permission rationale UI.
4559 *
4560 * @see #checkSelfPermission(String)
4561 * @see #requestPermissions(String[], int)
4562 * @see #onRequestPermissionsResult(int, String[], int[])
4563 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004564 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004565 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4566 }
4567
4568 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004569 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4570 * with no options.
4571 *
4572 * @param intent The intent to start.
4573 * @param requestCode If >= 0, this code will be returned in
4574 * onActivityResult() when the activity exits.
4575 *
4576 * @throws android.content.ActivityNotFoundException
4577 *
George Mount0a778ed2013-12-13 13:35:36 -08004578 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004579 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004580 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004581 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004582 }
4583
4584 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 * Launch an activity for which you would like a result when it finished.
4586 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004587 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004588 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004590 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 * <p>Note that this method should only be used with Intent protocols
4592 * that are defined to return a result. In other protocols (such as
4593 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4594 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05004595 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
4596 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004597 *
RoboErik55011652014-07-09 15:05:53 -07004598 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004600 * activity, then your window will not be displayed until a result is
4601 * returned back from the started activity. This is to avoid visible
4602 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 * <p>This method throws {@link android.content.ActivityNotFoundException}
4605 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004606 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 * @param intent The intent to start.
4608 * @param requestCode If >= 0, this code will be returned in
4609 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004610 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004611 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004612 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004615 *
George Mount0a778ed2013-12-13 13:35:36 -08004616 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004618 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4619 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07004621 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 Instrumentation.ActivityResult ar =
4623 mInstrumentation.execStartActivity(
4624 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004625 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 if (ar != null) {
4627 mMainThread.sendActivityResult(
4628 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4629 ar.getResultData());
4630 }
4631 if (requestCode >= 0) {
4632 // If this start is requesting a result, we can avoid making
4633 // the activity visible until the result is received. Setting
4634 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4635 // activity hidden during this time, to avoid flickering.
4636 // This can only be done when a result is requested because
4637 // that guarantees we will get information back when the
4638 // activity is finished, no matter what happens to it.
4639 mStartedActivity = true;
4640 }
Adam Powell14874662013-07-18 19:42:41 -07004641
George Mount41725de2015-04-09 08:23:05 -07004642 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004643 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004645 if (options != null) {
4646 mParent.startActivityFromChild(this, intent, requestCode, options);
4647 } else {
4648 // Note we want to go through this method for compatibility with
4649 // existing applications that may have overridden it.
4650 mParent.startActivityFromChild(this, intent, requestCode);
4651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 }
George Mount41725de2015-04-09 08:23:05 -07004653 }
4654
4655 /**
4656 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4657 *
4658 * @param options The ActivityOptions bundle used to start an Activity.
4659 */
4660 private void cancelInputsAndStartExitTransition(Bundle options) {
4661 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4662 if (decor != null) {
4663 decor.cancelPendingInputEvents();
4664 }
George Mountff243282014-07-07 16:12:07 -07004665 if (options != null && !isTopOfTask()) {
4666 mActivityTransitionState.startExitOutTransition(this, options);
4667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 }
4669
Chet Haaseb64e777f2016-12-09 07:17:10 -08004670 /**
4671 * Returns whether there are any activity transitions currently running on this
4672 * activity. A return value of {@code true} can mean that either an enter or
4673 * exit transition is running, including whether the background of the activity
4674 * is animating as a part of that transition.
4675 *
4676 * @return true if a transition is currently running on this activity, false otherwise.
4677 */
4678 public boolean isActivityTransitionRunning() {
4679 return mActivityTransitionState.isTransitionRunning();
4680 }
4681
George Mount413739e2016-06-08 07:13:37 -07004682 private Bundle transferSpringboardActivityOptions(Bundle options) {
4683 if (options == null && (mWindow != null && !mWindow.isActive())) {
4684 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07004685 if (activityOptions != null &&
4686 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07004687 return activityOptions.toBundle();
4688 }
4689 }
4690 return options;
4691 }
4692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004694 * @hide Implement to provide correct calling token.
4695 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01004696 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01004697 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4698 startActivityForResultAsUser(intent, requestCode, null, user);
4699 }
4700
4701 /**
4702 * @hide Implement to provide correct calling token.
4703 */
4704 public void startActivityForResultAsUser(Intent intent, int requestCode,
4705 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01004706 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
4707 }
4708
4709 /**
4710 * @hide Implement to provide correct calling token.
4711 */
4712 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
4713 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004714 if (mParent != null) {
4715 throw new RuntimeException("Can't be called from a child");
4716 }
George Mount413739e2016-06-08 07:13:37 -07004717 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004718 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004719 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01004720 options, user);
4721 if (ar != null) {
4722 mMainThread.sendActivityResult(
4723 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4724 }
4725 if (requestCode >= 0) {
4726 // If this start is requesting a result, we can avoid making
4727 // the activity visible until the result is received. Setting
4728 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4729 // activity hidden during this time, to avoid flickering.
4730 // This can only be done when a result is requested because
4731 // that guarantees we will get information back when the
4732 // activity is finished, no matter what happens to it.
4733 mStartedActivity = true;
4734 }
4735
George Mount41725de2015-04-09 08:23:05 -07004736 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004737 }
4738
4739 /**
4740 * @hide Implement to provide correct calling token.
4741 */
Remi NGUYEN VANbf1ecd12019-01-20 21:18:22 +09004742 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004743 public void startActivityAsUser(Intent intent, UserHandle user) {
4744 startActivityAsUser(intent, null, user);
4745 }
4746
4747 /**
4748 * @hide Implement to provide correct calling token.
4749 */
4750 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4751 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004752 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004753 }
George Mount413739e2016-06-08 07:13:37 -07004754 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004755 Instrumentation.ActivityResult ar =
4756 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004757 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004758 intent, -1, options, user);
4759 if (ar != null) {
4760 mMainThread.sendActivityResult(
4761 mToken, mEmbeddedID, -1, ar.getResultCode(),
4762 ar.getResultData());
4763 }
George Mount41725de2015-04-09 08:23:05 -07004764 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004765 }
4766
4767 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004768 * Start a new activity as if it was started by the activity that started our
4769 * current activity. This is for the resolver and chooser activities, which operate
4770 * as intermediaries that dispatch their intent to the target the user selects -- to
4771 * do this, they must perform all security checks including permission grants as if
4772 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004773 * @param intent The Intent to start.
4774 * @param options ActivityOptions or null.
4775 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4776 * caller it is doing the start is, is actually allowed to start the target activity.
4777 * If you set this to true, you must set an explicit component in the Intent and do any
4778 * appropriate security checks yourself.
4779 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004780 * @hide
4781 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004782 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -05004783 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004784 if (mParent != null) {
4785 throw new RuntimeException("Can't be called from a child");
4786 }
George Mount413739e2016-06-08 07:13:37 -07004787 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004788 Instrumentation.ActivityResult ar =
4789 mInstrumentation.execStartActivityAsCaller(
4790 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -05004791 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004792 if (ar != null) {
4793 mMainThread.sendActivityResult(
4794 mToken, mEmbeddedID, -1, ar.getResultCode(),
4795 ar.getResultData());
4796 }
George Mount41725de2015-04-09 08:23:05 -07004797 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004798 }
4799
4800 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004801 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4802 * Intent, int, int, int, Bundle)} with no options.
4803 *
4804 * @param intent The IntentSender to launch.
4805 * @param requestCode If >= 0, this code will be returned in
4806 * onActivityResult() when the activity exits.
4807 * @param fillInIntent If non-null, this will be provided as the
4808 * intent parameter to {@link IntentSender#sendIntent}.
4809 * @param flagsMask Intent flags in the original IntentSender that you
4810 * would like to change.
4811 * @param flagsValues Desired values for any bits set in
4812 * <var>flagsMask</var>
4813 * @param extraFlags Always set to 0.
4814 */
4815 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004816 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004817 throws IntentSender.SendIntentException {
4818 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4819 flagsValues, extraFlags, null);
4820 }
4821
4822 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004823 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004824 * to use a IntentSender to describe the activity to be started. If
4825 * the IntentSender is for an activity, that activity will be started
4826 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4827 * here; otherwise, its associated action will be executed (such as
4828 * sending a broadcast) as if you had called
4829 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004830 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004831 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004832 * @param requestCode If >= 0, this code will be returned in
4833 * onActivityResult() when the activity exits.
4834 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004835 * intent parameter to {@link IntentSender#sendIntent}.
4836 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004837 * would like to change.
4838 * @param flagsValues Desired values for any bits set in
4839 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004840 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004841 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004842 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004843 * Context.startActivity(Intent, Bundle)} for more details. If options
4844 * have also been supplied by the IntentSender, options given here will
4845 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004846 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004847 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004848 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004849 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004850 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004851 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
4852 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004853 } else if (options != null) {
4854 mParent.startIntentSenderFromChild(this, intent, requestCode,
4855 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004856 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004857 // Note we want to go through this call for compatibility with
4858 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004859 mParent.startIntentSenderFromChild(this, intent, requestCode,
4860 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004861 }
4862 }
4863
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004864 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
4865 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004866 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004867 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004868 try {
4869 String resolvedType = null;
4870 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004871 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004872 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004873 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4874 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004875 int result = ActivityManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07004876 .startActivityIntentSender(mMainThread.getApplicationThread(),
4877 intent != null ? intent.getTarget() : null,
4878 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004879 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004880 requestCode, flagsMask, flagsValues, options);
4881 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004882 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004883 }
4884 Instrumentation.checkStartActivityResult(result, null);
4885 } catch (RemoteException e) {
4886 }
4887 if (requestCode >= 0) {
4888 // If this start is requesting a result, we can avoid making
4889 // the activity visible until the result is received. Setting
4890 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4891 // activity hidden during this time, to avoid flickering.
4892 // This can only be done when a result is requested because
4893 // that guarantees we will get information back when the
4894 // activity is finished, no matter what happens to it.
4895 mStartedActivity = true;
4896 }
4897 }
4898
4899 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004900 * Same as {@link #startActivity(Intent, Bundle)} with no options
4901 * specified.
4902 *
4903 * @param intent The intent to start.
4904 *
4905 * @throws android.content.ActivityNotFoundException
4906 *
Mark Lufa434852016-08-11 17:40:33 -07004907 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004908 * @see #startActivityForResult
4909 */
4910 @Override
4911 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004912 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004913 }
4914
4915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 * Launch a new activity. You will not receive any information about when
4917 * the activity exits. This implementation overrides the base version,
4918 * providing information about
4919 * the activity performing the launch. Because of this additional
4920 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4921 * required; if not specified, the new activity will be added to the
4922 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004923 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 * <p>This method throws {@link android.content.ActivityNotFoundException}
4925 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004926 *
4927 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004928 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004929 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004930 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004931 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004933 *
Mark Lufa434852016-08-11 17:40:33 -07004934 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07004935 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 */
4937 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004938 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004939 if (options != null) {
4940 startActivityForResult(intent, -1, options);
4941 } else {
4942 // Note we want to go through this call for compatibility with
4943 // applications that may have overridden the method.
4944 startActivityForResult(intent, -1);
4945 }
4946 }
4947
4948 /**
4949 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4950 * specified.
4951 *
4952 * @param intents The intents to start.
4953 *
4954 * @throws android.content.ActivityNotFoundException
4955 *
Mark Lufa434852016-08-11 17:40:33 -07004956 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004957 * @see #startActivityForResult
4958 */
4959 @Override
4960 public void startActivities(Intent[] intents) {
4961 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 }
4963
4964 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004965 * Launch a new activity. You will not receive any information about when
4966 * the activity exits. This implementation overrides the base version,
4967 * providing information about
4968 * the activity performing the launch. Because of this additional
4969 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4970 * required; if not specified, the new activity will be added to the
4971 * task of the caller.
4972 *
4973 * <p>This method throws {@link android.content.ActivityNotFoundException}
4974 * if there was no Activity found to run the given Intent.
4975 *
4976 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004977 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004978 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004979 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004980 *
4981 * @throws android.content.ActivityNotFoundException
4982 *
Mark Lufa434852016-08-11 17:40:33 -07004983 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004984 * @see #startActivityForResult
4985 */
4986 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004987 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004988 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004989 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004990 }
4991
4992 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004993 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4994 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004995 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004996 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004997 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004998 * intent parameter to {@link IntentSender#sendIntent}.
4999 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005000 * would like to change.
5001 * @param flagsValues Desired values for any bits set in
5002 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005003 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005004 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005005 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005006 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005007 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005008 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5009 extraFlags, null);
5010 }
5011
5012 /**
5013 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5014 * to start; see
5015 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5016 * for more information.
5017 *
5018 * @param intent The IntentSender to launch.
5019 * @param fillInIntent If non-null, this will be provided as the
5020 * intent parameter to {@link IntentSender#sendIntent}.
5021 * @param flagsMask Intent flags in the original IntentSender that you
5022 * would like to change.
5023 * @param flagsValues Desired values for any bits set in
5024 * <var>flagsMask</var>
5025 * @param extraFlags Always set to 0.
5026 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005027 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005028 * Context.startActivity(Intent, Bundle)} for more details. If options
5029 * have also been supplied by the IntentSender, options given here will
5030 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005031 */
5032 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005033 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005034 Bundle options) throws IntentSender.SendIntentException {
5035 if (options != null) {
5036 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5037 flagsValues, extraFlags, options);
5038 } else {
5039 // Note we want to go through this call for compatibility with
5040 // applications that may have overridden the method.
5041 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5042 flagsValues, extraFlags);
5043 }
5044 }
5045
5046 /**
5047 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5048 * with no options.
5049 *
5050 * @param intent The intent to start.
5051 * @param requestCode If >= 0, this code will be returned in
5052 * onActivityResult() when the activity exits, as described in
5053 * {@link #startActivityForResult}.
5054 *
5055 * @return If a new activity was launched then true is returned; otherwise
5056 * false is returned and you must handle the Intent yourself.
5057 *
5058 * @see #startActivity
5059 * @see #startActivityForResult
5060 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005061 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5062 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005063 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005064 }
5065
5066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 * A special variation to launch an activity only if a new activity
5068 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005069 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005071 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005073 * and the activity
5074 * that handles <var>intent</var> is the same as your currently running
5075 * activity, then a new instance is not needed. In this case, instead of
5076 * the normal behavior of calling {@link #onNewIntent} this function will
5077 * return and you can handle the Intent yourself.
5078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 * <p>This function can only be called from a top-level activity; if it is
5080 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005081 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 * @param intent The intent to start.
5083 * @param requestCode If >= 0, this code will be returned in
5084 * onActivityResult() when the activity exits, as described in
5085 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005086 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005087 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005088 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 * @return If a new activity was launched then true is returned; otherwise
5091 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005092 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 * @see #startActivity
5094 * @see #startActivityForResult
5095 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005096 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5097 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005099 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005101 Uri referrer = onProvideReferrer();
5102 if (referrer != null) {
5103 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5104 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005105 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005106 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005107 result = ActivityManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005108 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005109 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5110 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5111 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 } catch (RemoteException e) {
5113 // Empty
5114 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 if (requestCode >= 0) {
5119 // If this start is requesting a result, we can avoid making
5120 // the activity visible until the result is received. Setting
5121 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5122 // activity hidden during this time, to avoid flickering.
5123 // This can only be done when a result is requested because
5124 // that guarantees we will get information back when the
5125 // activity is finished, no matter what happens to it.
5126 mStartedActivity = true;
5127 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005128 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 }
5130
5131 throw new UnsupportedOperationException(
5132 "startActivityIfNeeded can only be called from a top-level activity");
5133 }
5134
5135 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005136 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5137 * no options.
5138 *
5139 * @param intent The intent to dispatch to the next activity. For
5140 * correct behavior, this must be the same as the Intent that started
5141 * your own activity; the only changes you can make are to the extras
5142 * inside of it.
5143 *
5144 * @return Returns a boolean indicating whether there was another Activity
5145 * to start: true if there was a next activity to start, false if there
5146 * wasn't. In general, if true is returned you will then want to call
5147 * finish() on yourself.
5148 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005149 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005150 return startNextMatchingActivity(intent, null);
5151 }
5152
5153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 * Special version of starting an activity, for use when you are replacing
5155 * other activity components. You can use this to hand the Intent off
5156 * to the next Activity that can handle it. You typically call this in
5157 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 * @param intent The intent to dispatch to the next activity. For
5160 * correct behavior, this must be the same as the Intent that started
5161 * your own activity; the only changes you can make are to the extras
5162 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005163 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005164 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005165 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 * @return Returns a boolean indicating whether there was another Activity
5168 * to start: true if there was a next activity to start, false if there
5169 * wasn't. In general, if true is returned you will then want to call
5170 * finish() on yourself.
5171 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005172 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5173 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 if (mParent == null) {
5175 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005176 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005177 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005178 return ActivityManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005179 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 } catch (RemoteException e) {
5181 // Empty
5182 }
5183 return false;
5184 }
5185
5186 throw new UnsupportedOperationException(
5187 "startNextMatchingActivity can only be called from a top-level activity");
5188 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005189
5190 /**
5191 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5192 * with no options.
5193 *
5194 * @param child The activity making the call.
5195 * @param intent The intent to start.
5196 * @param requestCode Reply request code. < 0 if reply is not requested.
5197 *
5198 * @throws android.content.ActivityNotFoundException
5199 *
5200 * @see #startActivity
5201 * @see #startActivityForResult
5202 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005203 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005204 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005205 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005206 }
5207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 /**
RoboErik55011652014-07-09 15:05:53 -07005209 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 * <p>This method throws {@link android.content.ActivityNotFoundException}
5213 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005214 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 * @param child The activity making the call.
5216 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005217 * @param requestCode Reply request code. < 0 if reply is not requested.
5218 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005219 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005220 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005221 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005223 *
5224 * @see #startActivity
5225 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005227 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005228 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005229 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 Instrumentation.ActivityResult ar =
5231 mInstrumentation.execStartActivity(
5232 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005233 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 if (ar != null) {
5235 mMainThread.sendActivityResult(
5236 mToken, child.mEmbeddedID, requestCode,
5237 ar.getResultCode(), ar.getResultData());
5238 }
George Mount41725de2015-04-09 08:23:05 -07005239 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
5241
5242 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005243 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5244 * with no options.
5245 *
5246 * @param fragment The fragment making the call.
5247 * @param intent The intent to start.
5248 * @param requestCode Reply request code. < 0 if reply is not requested.
5249 *
5250 * @throws android.content.ActivityNotFoundException
5251 *
5252 * @see Fragment#startActivity
5253 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005254 *
5255 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5256 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005257 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005258 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005259 public void startActivityFromFragment(@NonNull Fragment fragment,
5260 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005261 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005262 }
5263
5264 /**
RoboErik55011652014-07-09 15:05:53 -07005265 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005266 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5267 * method.
RoboErik55011652014-07-09 15:05:53 -07005268 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005269 * <p>This method throws {@link android.content.ActivityNotFoundException}
5270 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005271 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005272 * @param fragment The fragment making the call.
5273 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005274 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005275 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005276 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005277 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005278 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005279 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005280 *
5281 * @see Fragment#startActivity
5282 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005283 *
5284 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5285 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005286 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005287 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005288 public void startActivityFromFragment(@NonNull Fragment fragment,
5289 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005290 startActivityForResult(fragment.mWho, intent, requestCode, options);
5291 }
5292
5293 /**
5294 * @hide
5295 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005296 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5297 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5298 UserHandle user) {
5299 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5300 }
5301
5302 /**
5303 * @hide
5304 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005305 @Override
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005306 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005307 public void startActivityForResult(
5308 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005309 Uri referrer = onProvideReferrer();
5310 if (referrer != null) {
5311 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5312 }
George Mount413739e2016-06-08 07:13:37 -07005313 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005314 Instrumentation.ActivityResult ar =
5315 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005316 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005317 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005318 if (ar != null) {
5319 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005320 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005321 ar.getResultCode(), ar.getResultData());
5322 }
George Mount41725de2015-04-09 08:23:05 -07005323 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005324 }
5325
5326 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005327 * @hide
5328 */
5329 @Override
5330 public boolean canStartActivityForResult() {
5331 return true;
5332 }
5333
5334 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005335 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5336 * int, Intent, int, int, int, Bundle)} with no options.
5337 */
5338 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5339 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5340 int extraFlags)
5341 throws IntentSender.SendIntentException {
5342 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5343 flagsMask, flagsValues, extraFlags, null);
5344 }
5345
5346 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005347 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005348 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005349 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005350 * for more information.
5351 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005352 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5353 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005354 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005355 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005356 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5357 flagsMask, flagsValues, options);
5358 }
5359
5360 /**
5361 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5362 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5363 * for more information.
5364 *
5365 * @hide
5366 */
5367 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5368 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5369 int extraFlags, @Nullable Bundle options)
5370 throws IntentSender.SendIntentException {
5371 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5372 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005373 }
5374
5375 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005376 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5377 * or {@link #finish} to specify an explicit transition animation to
5378 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005379 *
5380 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5381 * to using this with starting activities is to supply the desired animation
5382 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005383 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005384 * you to specify a custom animation even when starting an activity from
5385 * outside the context of the current top activity.
5386 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005387 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005388 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005389 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005390 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005391 */
5392 public void overridePendingTransition(int enterAnim, int exitAnim) {
5393 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005394 ActivityManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005395 mToken, getPackageName(), enterAnim, exitAnim);
5396 } catch (RemoteException e) {
5397 }
5398 }
RoboErik55011652014-07-09 15:05:53 -07005399
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 * Call this to set the result that your activity will return to its
5402 * caller.
RoboErik55011652014-07-09 15:05:53 -07005403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 * @param resultCode The result code to propagate back to the originating
5405 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 * @see #RESULT_CANCELED
5408 * @see #RESULT_OK
5409 * @see #RESULT_FIRST_USER
5410 * @see #setResult(int, Intent)
5411 */
5412 public final void setResult(int resultCode) {
5413 synchronized (this) {
5414 mResultCode = resultCode;
5415 mResultData = null;
5416 }
5417 }
5418
5419 /**
5420 * Call this to set the result that your activity will return to its
5421 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005422 *
5423 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5424 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5425 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5426 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5427 * Activity receiving the result access to the specific URIs in the Intent.
5428 * Access will remain until the Activity has finished (it will remain across the hosting
5429 * process being killed and other temporary destruction) and will be added
5430 * to any existing set of URI permissions it already holds.
5431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 * @param resultCode The result code to propagate back to the originating
5433 * activity, often RESULT_CANCELED or RESULT_OK
5434 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005435 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 * @see #RESULT_CANCELED
5437 * @see #RESULT_OK
5438 * @see #RESULT_FIRST_USER
5439 * @see #setResult(int)
5440 */
5441 public final void setResult(int resultCode, Intent data) {
5442 synchronized (this) {
5443 mResultCode = resultCode;
5444 mResultData = data;
5445 }
5446 }
5447
5448 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005449 * Return information about who launched this activity. If the launching Intent
5450 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5451 * that will be returned as-is; otherwise, if known, an
5452 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5453 * package name that started the Intent will be returned. This may return null if no
5454 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5455 * application package was involved.
5456 *
5457 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5458 * return the referrer that submitted that new intent to the activity. Otherwise, it
5459 * always returns the referrer of the original Intent.</p>
5460 *
5461 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5462 * referrer information, applications can spoof it.</p>
5463 */
5464 @Nullable
5465 public Uri getReferrer() {
5466 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005467 try {
5468 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5469 if (referrer != null) {
5470 return referrer;
5471 }
5472 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5473 if (referrerName != null) {
5474 return Uri.parse(referrerName);
5475 }
5476 } catch (BadParcelableException e) {
5477 Log.w(TAG, "Cannot read referrer from intent;"
5478 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005479 }
5480 if (mReferrer != null) {
5481 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5482 }
5483 return null;
5484 }
5485
5486 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005487 * Override to generate the desired referrer for the content currently being shown
5488 * by the app. The default implementation returns null, meaning the referrer will simply
5489 * be the android-app: of the package name of this activity. Return a non-null Uri to
5490 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5491 */
5492 public Uri onProvideReferrer() {
5493 return null;
5494 }
5495
5496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 * Return the name of the package that invoked this activity. This is who
5498 * the data in {@link #setResult setResult()} will be sent to. You can
5499 * use this information to validate that the recipient is allowed to
5500 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005501 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005502 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005503 * did not use the {@link #startActivityForResult}
5504 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005505 * null.</p>
5506 *
5507 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5508 * the result from this method was unstable. If the process hosting the calling
5509 * package was no longer running, it would return null instead of the proper package
5510 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5511 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 * @return The package of the activity that will receive your
5514 * reply, or null if none.
5515 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005516 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 public String getCallingPackage() {
5518 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005519 return ActivityManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 } catch (RemoteException e) {
5521 return null;
5522 }
5523 }
5524
5525 /**
5526 * Return the name of the activity that invoked this activity. This is
5527 * who the data in {@link #setResult setResult()} will be sent to. You
5528 * can use this information to validate that the recipient is allowed to
5529 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005530 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005531 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005532 * did not use the {@link #startActivityForResult}
5533 * form that includes a request code), then the calling package will be
5534 * null.
5535 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005536 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 * reply, or null if none.
5538 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005539 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 public ComponentName getCallingActivity() {
5541 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005542 return ActivityManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 } catch (RemoteException e) {
5544 return null;
5545 }
5546 }
5547
5548 /**
5549 * Control whether this activity's main window is visible. This is intended
5550 * only for the special case of an activity that is not going to show a
5551 * UI itself, but can't just finish prior to onResume() because it needs
5552 * to wait for a service binding or such. Setting this to false allows
5553 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 * <p>The default value for this is taken from the
5556 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5557 */
5558 public void setVisible(boolean visible) {
5559 if (mVisibleFromClient != visible) {
5560 mVisibleFromClient = visible;
5561 if (mVisibleFromServer) {
5562 if (visible) makeVisible();
5563 else mDecor.setVisibility(View.INVISIBLE);
5564 }
5565 }
5566 }
RoboErik55011652014-07-09 15:05:53 -07005567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 void makeVisible() {
5569 if (!mWindowAdded) {
5570 ViewManager wm = getWindowManager();
5571 wm.addView(mDecor, getWindow().getAttributes());
5572 mWindowAdded = true;
5573 }
5574 mDecor.setVisibility(View.VISIBLE);
5575 }
RoboErik55011652014-07-09 15:05:53 -07005576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 /**
5578 * Check to see whether this activity is in the process of finishing,
5579 * either because you called {@link #finish} on it or someone else
5580 * has requested that it finished. This is often used in
5581 * {@link #onPause} to determine whether the activity is simply pausing or
5582 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005583 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005585 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586 * @see #finish
5587 */
5588 public boolean isFinishing() {
5589 return mFinished;
5590 }
5591
5592 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005593 * Returns true if the final {@link #onDestroy()} call has been made
5594 * on the Activity, so this instance is now dead.
5595 */
5596 public boolean isDestroyed() {
5597 return mDestroyed;
5598 }
5599
5600 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005601 * Check to see whether this activity is in the process of being destroyed in order to be
5602 * recreated with a new configuration. This is often used in
5603 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5604 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005605 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005606 * @return If the activity is being torn down in order to be recreated with a new configuration,
5607 * returns true; else returns false.
5608 */
5609 public boolean isChangingConfigurations() {
5610 return mChangingConfigurations;
5611 }
5612
5613 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005614 * Cause this Activity to be recreated with a new instance. This results
5615 * in essentially the same flow as when the Activity is created due to
5616 * a configuration change -- the current instance will go through its
5617 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5618 */
5619 public void recreate() {
5620 if (mParent != null) {
5621 throw new IllegalStateException("Can only be called on top-level activity");
5622 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07005623 if (Looper.myLooper() != mMainThread.getLooper()) {
5624 throw new IllegalStateException("Must be called from main thread");
5625 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07005626 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005627 }
5628
5629 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005630 * Finishes the current activity and specifies whether to remove the task associated with this
5631 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005633 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005634 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 if (mParent == null) {
5636 int resultCode;
5637 Intent resultData;
5638 synchronized (this) {
5639 resultCode = mResultCode;
5640 resultData = mResultData;
5641 }
Joe Onorato43a17652011-04-06 19:22:23 -07005642 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005644 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005645 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005646 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005647 if (ActivityManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07005648 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 mFinished = true;
5650 }
5651 } catch (RemoteException e) {
5652 // Empty
5653 }
5654 } else {
5655 mParent.finishFromChild(this);
5656 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07005657
5658 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
5659 // be restored now.
5660 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
5661 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
5662 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
5663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 }
5665
5666 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005667 * Call this when your activity is done and should be closed. The
5668 * ActivityResult is propagated back to whoever launched you via
5669 * onActivityResult().
5670 */
5671 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005672 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005673 }
5674
5675 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005676 * Finish this activity as well as all activities immediately below it
5677 * in the current task that have the same affinity. This is typically
5678 * used when an application can be launched on to another task (such as
5679 * from an ACTION_VIEW of a content type it understands) and the user
5680 * has used the up navigation to switch out of the current task and in
5681 * to its own task. In this case, if the user has navigated down into
5682 * any other activities of the second application, all of those should
5683 * be removed from the original task as part of the task switch.
5684 *
5685 * <p>Note that this finish does <em>not</em> allow you to deliver results
5686 * to the previous activity, and an exception will be thrown if you are trying
5687 * to do so.</p>
5688 */
5689 public void finishAffinity() {
5690 if (mParent != null) {
5691 throw new IllegalStateException("Can not be called from an embedded activity");
5692 }
5693 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5694 throw new IllegalStateException("Can not be called to deliver a result");
5695 }
5696 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005697 if (ActivityManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005698 mFinished = true;
5699 }
5700 } catch (RemoteException e) {
5701 // Empty
5702 }
5703 }
5704
5705 /**
RoboErik55011652014-07-09 15:05:53 -07005706 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 * {@link #finish} method. The default implementation simply calls
5708 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005709 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 * @see #finish
5713 */
5714 public void finishFromChild(Activity child) {
5715 finish();
5716 }
5717
5718 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005719 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5720 * to reverse its exit Transition. When the exit Transition completes,
5721 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5722 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005723 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005724 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005725 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005726 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005727 finish();
5728 }
George Mountcb4b7d92014-02-25 10:47:55 -08005729 }
5730
5731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 * Force finish another activity that you had previously started with
5733 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005734 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 * @param requestCode The request code of the activity that you had
5736 * given to startActivityForResult(). If there are multiple
5737 * activities started with this request code, they
5738 * will all be finished.
5739 */
5740 public void finishActivity(int requestCode) {
5741 if (mParent == null) {
5742 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005743 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5745 } catch (RemoteException e) {
5746 // Empty
5747 }
5748 } else {
5749 mParent.finishActivityFromChild(this, requestCode);
5750 }
5751 }
5752
5753 /**
5754 * This is called when a child activity of this one calls its
5755 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 * @param child The activity making the call.
5758 * @param requestCode Request code that had been used to start the
5759 * activity.
5760 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005761 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005763 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5765 } catch (RemoteException e) {
5766 // Empty
5767 }
5768 }
5769
5770 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005771 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005772 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005773 */
5774 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005775 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005776 }
5777
5778 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005779 * Ask that the local app instance of this activity be released to free up its memory.
5780 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5781 * a new instance of the activity will later be re-created if needed due to the user
5782 * navigating back to it.
5783 *
5784 * @return Returns true if the activity was in a state that it has started the process
5785 * of destroying its current instance; returns false if for any reason this could not
5786 * be done: it is currently visible to the user, it is already being destroyed, it is
5787 * being finished, it hasn't yet saved its state, etc.
5788 */
5789 public boolean releaseInstance() {
5790 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005791 return ActivityManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005792 } catch (RemoteException e) {
5793 // Empty
5794 }
5795 return false;
5796 }
5797
5798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 * Called when an activity you launched exits, giving you the requestCode
5800 * you started it with, the resultCode it returned, and any additional
5801 * data from it. The <var>resultCode</var> will be
5802 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5803 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005804 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 * <p>You will receive this call immediately before onResume() when your
5806 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005807 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005808 * <p>This method is never invoked if your activity sets
5809 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5810 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005811 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 * @param requestCode The integer request code originally supplied to
5813 * startActivityForResult(), allowing you to identify who this
5814 * result came from.
5815 * @param resultCode The integer result code returned by the child activity
5816 * through its setResult().
5817 * @param data An Intent, which can return result data to the caller
5818 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005819 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 * @see #startActivityForResult
5821 * @see #createPendingResult
5822 * @see #setResult(int)
5823 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005824 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 }
5826
5827 /**
George Mount62ab9b72014-05-02 13:51:17 -07005828 * Called when an activity you launched with an activity transition exposes this
5829 * Activity through a returning activity transition, giving you the resultCode
5830 * and any additional data from it. This method will only be called if the activity
5831 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005832 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005833 *
5834 * <p>The purpose of this function is to let the called Activity send a hint about
5835 * its state so that this underlying Activity can prepare to be exposed. A call to
5836 * this method does not guarantee that the called Activity has or will be exiting soon.
5837 * It only indicates that it will expose this Activity's Window and it has
5838 * some data to pass to prepare it.</p>
5839 *
5840 * @param resultCode The integer result code returned by the child activity
5841 * through its setResult().
5842 * @param data An Intent, which can return result data to the caller
5843 * (various data can be attached to Intent "extras").
5844 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005845 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005846 }
5847
5848 /**
RoboErik55011652014-07-09 15:05:53 -07005849 * Create a new PendingIntent object which you can hand to others
5850 * for them to use to send result data back to your
5851 * {@link #onActivityResult} callback. The created object will be either
5852 * one-shot (becoming invalid after a result is sent back) or multiple
5853 * (allowing any number of results to be sent through it).
5854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 * @param requestCode Private request code for the sender that will be
5856 * associated with the result data when it is returned. The sender can not
5857 * modify this value, allowing you to identify incoming results.
5858 * @param data Default data to supply in the result, which may be modified
5859 * by the sender.
5860 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5861 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5862 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5863 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5864 * or any of the flags as supported by
5865 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5866 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 * @return Returns an existing or new PendingIntent matching the given
5869 * parameters. May return null only if
5870 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5871 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005872 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 * @see PendingIntent
5874 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005875 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5876 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 String packageName = getPackageName();
5878 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005879 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005881 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005882 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005884 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07005885 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 return target != null ? new PendingIntent(target) : null;
5887 } catch (RemoteException e) {
5888 // Empty
5889 }
5890 return null;
5891 }
5892
5893 /**
5894 * Change the desired orientation of this activity. If the activity
5895 * is currently in the foreground or otherwise impacting the screen
5896 * orientation, the screen will immediately be changed (possibly causing
5897 * the activity to be restarted). Otherwise, this will be used the next
5898 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 * @param requestedOrientation An orientation constant as used in
5901 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5902 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005903 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 if (mParent == null) {
5905 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005906 ActivityManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 mToken, requestedOrientation);
5908 } catch (RemoteException e) {
5909 // Empty
5910 }
5911 } else {
5912 mParent.setRequestedOrientation(requestedOrientation);
5913 }
5914 }
RoboErik55011652014-07-09 15:05:53 -07005915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916 /**
5917 * Return the current requested orientation of the activity. This will
5918 * either be the orientation requested in its component's manifest, or
5919 * the last requested orientation given to
5920 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 * @return Returns an orientation constant as used in
5923 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5924 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005925 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 public int getRequestedOrientation() {
5927 if (mParent == null) {
5928 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005929 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 .getRequestedOrientation(mToken);
5931 } catch (RemoteException e) {
5932 // Empty
5933 }
5934 } else {
5935 return mParent.getRequestedOrientation();
5936 }
5937 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5938 }
RoboErik55011652014-07-09 15:05:53 -07005939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 /**
5941 * Return the identifier of the task this activity is in. This identifier
5942 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005943 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 * @return Task identifier, an opaque integer.
5945 */
5946 public int getTaskId() {
5947 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005948 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 .getTaskForActivity(mToken, false);
5950 } catch (RemoteException e) {
5951 return -1;
5952 }
5953 }
5954
5955 /**
5956 * Return whether this activity is the root of a task. The root is the
5957 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005958 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 * @return True if this is the root activity, else false.
5960 */
Winson Chung4d8681f2017-05-23 16:22:08 -07005961 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 public boolean isTaskRoot() {
5963 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005964 return ActivityManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 } catch (RemoteException e) {
5966 return false;
5967 }
5968 }
5969
5970 /**
5971 * Move the task containing this activity to the back of the activity
5972 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 * @param nonRoot If false then this only works if the activity is the root
5975 * of a task; if true it will work for any activity in
5976 * a task.
RoboErik55011652014-07-09 15:05:53 -07005977 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 * @return If the task was moved (or it was already at the
5979 * back) true is returned, else false.
5980 */
5981 public boolean moveTaskToBack(boolean nonRoot) {
5982 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005983 return ActivityManager.getService().moveActivityTaskToBack(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 mToken, nonRoot);
5985 } catch (RemoteException e) {
5986 // Empty
5987 }
5988 return false;
5989 }
5990
5991 /**
5992 * Returns class name for this activity with the package prefix removed.
5993 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005994 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 * @return The local class name.
5996 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005997 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 public String getLocalClassName() {
5999 final String pkg = getPackageName();
6000 final String cls = mComponent.getClassName();
6001 int packageLen = pkg.length();
6002 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6003 || cls.charAt(packageLen) != '.') {
6004 return cls;
6005 }
6006 return cls.substring(packageLen+1);
6007 }
RoboErik55011652014-07-09 15:05:53 -07006008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006010 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006011 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 * @return Returns the complete component name for this activity
6013 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006014 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 return mComponent;
6016 }
6017
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006018 /** @hide */
6019 @Override
6020 public final ComponentName autofillClientGetComponentName() {
6021 return getComponentName();
6022 }
6023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 /**
6025 * Retrieve a {@link SharedPreferences} object for accessing preferences
6026 * that are private to this activity. This simply calls the underlying
6027 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6028 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006029 *
6030 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006031 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 *
6033 * @return Returns the single SharedPreferences instance that can be used
6034 * to retrieve and modify the preference values.
6035 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006036 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 return getSharedPreferences(getLocalClassName(), mode);
6038 }
RoboErik55011652014-07-09 15:05:53 -07006039
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006040 private void ensureSearchManager() {
6041 if (mSearchManager != null) {
6042 return;
6043 }
RoboErik55011652014-07-09 15:05:53 -07006044
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006045 try {
6046 mSearchManager = new SearchManager(this, null);
6047 } catch (ServiceNotFoundException e) {
6048 throw new IllegalStateException(e);
6049 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006050 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006053 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 if (getBaseContext() == null) {
6055 throw new IllegalStateException(
6056 "System services not available to Activities before onCreate()");
6057 }
6058
6059 if (WINDOW_SERVICE.equals(name)) {
6060 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006061 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006062 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006063 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 }
6065 return super.getSystemService(name);
6066 }
6067
6068 /**
6069 * Change the title associated with this activity. If this is a
6070 * top-level activity, the title for its window will change. If it
6071 * is an embedded activity, the parent can do whatever it wants
6072 * with it.
6073 */
6074 public void setTitle(CharSequence title) {
6075 mTitle = title;
6076 onTitleChanged(title, mTitleColor);
6077
6078 if (mParent != null) {
6079 mParent.onChildTitleChanged(this, title);
6080 }
6081 }
6082
6083 /**
6084 * Change the title associated with this activity. If this is a
6085 * top-level activity, the title for its window will change. If it
6086 * is an embedded activity, the parent can do whatever it wants
6087 * with it.
6088 */
6089 public void setTitle(int titleId) {
6090 setTitle(getText(titleId));
6091 }
6092
Alan Viverette2525d9c2013-11-15 14:42:19 -08006093 /**
6094 * Change the color of the title associated with this activity.
6095 * <p>
6096 * This method is deprecated starting in API Level 11 and replaced by action
6097 * bar styles. For information on styling the Action Bar, read the <a
6098 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6099 * guide.
6100 *
6101 * @deprecated Use action bar styles instead.
6102 */
6103 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 public void setTitleColor(int textColor) {
6105 mTitleColor = textColor;
6106 onTitleChanged(mTitle, textColor);
6107 }
6108
6109 public final CharSequence getTitle() {
6110 return mTitle;
6111 }
6112
6113 public final int getTitleColor() {
6114 return mTitleColor;
6115 }
6116
6117 protected void onTitleChanged(CharSequence title, int color) {
6118 if (mTitleReady) {
6119 final Window win = getWindow();
6120 if (win != null) {
6121 win.setTitle(title);
6122 if (color != 0) {
6123 win.setTitleColor(color);
6124 }
6125 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006126 if (mActionBar != null) {
6127 mActionBar.setWindowTitle(title);
6128 }
Adam Powella557fdc2014-08-21 18:05:53 -07006129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 }
6131
6132 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6133 }
6134
6135 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006136 * Sets information describing the task with this activity for presentation inside the Recents
6137 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6138 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6139 * for each property until a suitable value is found. For each task the taskDescription will be
6140 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006141 *
6142 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006143 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006144 *
Winson Chunga449dc02014-05-16 11:15:04 -07006145 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006146 */
Winson Chunga449dc02014-05-16 11:15:04 -07006147 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006148 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006149 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006150 // Scale the icon down to something reasonable if it is provided
6151 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6152 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6153 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6154 true);
6155 mTaskDescription.setIcon(icon);
6156 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006157 }
6158 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006159 ActivityManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006160 } catch (RemoteException e) {
6161 }
6162 }
6163
6164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 * Sets the visibility of the progress bar in the title.
6166 * <p>
6167 * In order for the progress bar to be shown, the feature must be requested
6168 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006171 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006173 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006174 public final void setProgressBarVisibility(boolean visible) {
6175 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6176 Window.PROGRESS_VISIBILITY_OFF);
6177 }
6178
6179 /**
6180 * Sets the visibility of the indeterminate progress bar in the title.
6181 * <p>
6182 * In order for the progress bar to be shown, the feature must be requested
6183 * via {@link #requestWindowFeature(int)}.
6184 *
6185 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006186 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006188 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6190 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6191 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6192 }
RoboErik55011652014-07-09 15:05:53 -07006193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194 /**
6195 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6196 * is always indeterminate).
6197 * <p>
6198 * In order for the progress bar to be shown, the feature must be requested
6199 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006202 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006204 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 public final void setProgressBarIndeterminate(boolean indeterminate) {
6206 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006207 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6208 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 }
RoboErik55011652014-07-09 15:05:53 -07006210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 /**
6212 * Sets the progress for the progress bars in the title.
6213 * <p>
6214 * In order for the progress bar to be shown, the feature must be requested
6215 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006216 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 * @param progress The progress for the progress bar. Valid ranges are from
6218 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6219 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006220 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006222 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 public final void setProgress(int progress) {
6224 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6225 }
RoboErik55011652014-07-09 15:05:53 -07006226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 /**
6228 * Sets the secondary progress for the progress bar in the title. This
6229 * progress is drawn between the primary progress (set via
6230 * {@link #setProgress(int)} and the background. It can be ideal for media
6231 * scenarios such as showing the buffering progress while the default
6232 * progress shows the play progress.
6233 * <p>
6234 * In order for the progress bar to be shown, the feature must be requested
6235 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6238 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006239 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006240 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006241 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 public final void setSecondaryProgress(int secondaryProgress) {
6243 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6244 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6245 }
6246
6247 /**
6248 * Suggests an audio stream whose volume should be changed by the hardware
6249 * volume controls.
6250 * <p>
6251 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006252 * Volume requests which are received while the Activity is in the
6253 * foreground will affect this stream.
6254 * <p>
6255 * It is not guaranteed that the hardware volume controls will always change
6256 * this stream's volume (for example, if a call is in progress, its stream's
6257 * volume may be changed instead). To reset back to the default, use
6258 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006261 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 */
6263 public final void setVolumeControlStream(int streamType) {
6264 getWindow().setVolumeControlStream(streamType);
6265 }
6266
6267 /**
6268 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006269 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 * @return The suggested audio stream type whose volume should be changed by
6272 * the hardware volume controls.
6273 * @see #setVolumeControlStream(int)
6274 */
6275 public final int getVolumeControlStream() {
6276 return getWindow().getVolumeControlStream();
6277 }
RoboErik55011652014-07-09 15:05:53 -07006278
6279 /**
6280 * Sets a {@link MediaController} to send media keys and volume changes to.
6281 * <p>
6282 * The controller will be tied to the window of this Activity. Media key and
6283 * volume events which are received while the Activity is in the foreground
6284 * will be forwarded to the controller and used to invoke transport controls
6285 * or adjust the volume. This may be used instead of or in addition to
6286 * {@link #setVolumeControlStream} to affect a specific session instead of a
6287 * specific stream.
6288 * <p>
6289 * It is not guaranteed that the hardware volume controls will always change
6290 * this session's volume (for example, if a call is in progress, its
6291 * stream's volume may be changed instead). To reset back to the default use
6292 * null as the controller.
6293 *
6294 * @param controller The controller for the session which should receive
6295 * media keys and volume changes.
6296 */
6297 public final void setMediaController(MediaController controller) {
6298 getWindow().setMediaController(controller);
6299 }
6300
6301 /**
6302 * Gets the controller which should be receiving media key and volume events
6303 * while this activity is in the foreground.
6304 *
6305 * @return The controller which should receive events.
6306 * @see #setMediaController(android.media.session.MediaController)
6307 */
6308 public final MediaController getMediaController() {
6309 return getWindow().getMediaController();
6310 }
6311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 /**
6313 * Runs the specified action on the UI thread. If the current thread is the UI
6314 * thread, then the action is executed immediately. If the current thread is
6315 * not the UI thread, the action is posted to the event queue of the UI thread.
6316 *
6317 * @param action the action to run on the UI thread
6318 */
6319 public final void runOnUiThread(Runnable action) {
6320 if (Thread.currentThread() != mUiThread) {
6321 mHandler.post(action);
6322 } else {
6323 action.run();
6324 }
6325 }
6326
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006327 /** @hide */
6328 @Override
6329 public final void autofillClientRunOnUiThread(Runnable action) {
6330 runOnUiThread(action);
6331 }
6332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006334 * Standard implementation of
6335 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6336 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006337 * This implementation does nothing and is for
6338 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6339 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6340 *
6341 * @see android.view.LayoutInflater#createView
6342 * @see android.view.Window#getLayoutInflater
6343 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006344 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07006345 public View onCreateView(String name, Context context, AttributeSet attrs) {
6346 return null;
6347 }
6348
6349 /**
6350 * Standard implementation of
6351 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6352 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006353 * This implementation handles <fragment> tags to embed fragments inside
6354 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006355 *
6356 * @see android.view.LayoutInflater#createView
6357 * @see android.view.Window#getLayoutInflater
6358 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07006359 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006360 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006361 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006362 }
RoboErik55011652014-07-09 15:05:53 -07006363
Adam Powell371a8092014-06-20 12:51:12 -07006364 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 }
6366
Daniel Sandler69a48172010-06-23 16:29:36 -04006367 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006368 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006369 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006370 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006371 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006372 * @param fd The raw file descriptor that the dump is being sent to.
6373 * @param writer The PrintWriter to which you should dump your state. This will be
6374 * closed for you after you return.
6375 * @param args additional arguments to the dump request.
6376 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08006377 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006378 dumpInner(prefix, fd, writer, args);
6379 }
6380
6381 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Felipe Leme42260332018-08-15 08:44:12 -07006382 if (args != null && args.length > 0 && args[0].equals("--autofill")) {
6383 dumpAutofillManager(prefix, writer);
6384 return;
6385 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006386 writer.print(prefix); writer.print("Local Activity ");
6387 writer.print(Integer.toHexString(System.identityHashCode(this)));
6388 writer.println(" State:");
6389 String innerPrefix = prefix + " ";
6390 writer.print(innerPrefix); writer.print("mResumed=");
6391 writer.print(mResumed); writer.print(" mStopped=");
6392 writer.print(mStopped); writer.print(" mFinished=");
6393 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006394 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6395 writer.println(mChangingConfigurations);
6396 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6397 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006398
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006399 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6400 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006401 if (mVoiceInteractor != null) {
6402 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6403 }
Jeff Brown5182c782013-10-15 20:31:52 -07006404
Michael Wright5f48dc72013-11-01 12:42:49 -07006405 if (getWindow() != null &&
6406 getWindow().peekDecorView() != null &&
6407 getWindow().peekDecorView().getViewRootImpl() != null) {
6408 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6409 }
Jeff Brown5182c782013-10-15 20:31:52 -07006410
6411 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07006412
Felipe Leme42260332018-08-15 08:44:12 -07006413 dumpAutofillManager(prefix, writer);
6414
6415 ResourcesManager.getInstance().dump(prefix, writer);
6416 }
6417
6418 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08006419 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006420 if (afm != null) {
Felipe Leme42260332018-08-15 08:44:12 -07006421 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08006422 writer.print(prefix); writer.print("Autofill Compat Mode: ");
6423 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07006424 } else {
6425 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07006426 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07006427 }
6428
6429 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006430 * Bit indicating that this activity is "immersive" and should not be
6431 * interrupted by notifications if possible.
6432 *
6433 * This value is initially set by the manifest property
6434 * <code>android:immersive</code> but may be changed at runtime by
6435 * {@link #setImmersive}.
6436 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006437 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006438 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6439 */
6440 public boolean isImmersive() {
6441 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006442 return ActivityManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006443 } catch (RemoteException e) {
6444 return false;
6445 }
6446 }
6447
6448 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006449 * Indication of whether this is the highest level activity in this task. Can be used to
6450 * determine whether an activity launched by this activity was placed in the same task or
6451 * another task.
6452 *
6453 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006454 */
George Mountff243282014-07-07 16:12:07 -07006455 private boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006456 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006457 return false;
6458 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006459 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006460 return ActivityManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006461 } catch (RemoteException e) {
6462 return false;
6463 }
6464 }
6465
6466 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006467 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6468 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006469 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006470 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006471 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6472 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006473 * This call has no effect on non-translucent activities or on activities with the
6474 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006475 *
George Mount62ab9b72014-05-02 13:51:17 -07006476 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6477 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006478 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006479 *
6480 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006481 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006482 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006483 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006484 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006485 mTranslucentCallback = null;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006486 if (ActivityManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006487 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6488 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006489 } catch (RemoteException e) {
6490 // pass
6491 }
6492 }
6493
6494 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006495 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6496 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6497 * <p>
6498 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6499 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6500 * be called indicating that it is safe to make this activity translucent again. Until
6501 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6502 * behind the frontmost Activity will be indeterminate.
6503 * <p>
6504 * This call has no effect on non-translucent activities or on activities with the
6505 * {@link android.R.attr#windowIsFloating} attribute.
6506 *
6507 * @param callback the method to call when all visible Activities behind this one have been
6508 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006509 * @param options activity options delivered to the activity below this one. The options
6510 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006511 * @return <code>true</code> if Window was opaque and will become translucent or
6512 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006513 *
6514 * @see #convertFromTranslucent()
6515 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006516 *
6517 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006518 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006519 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006520 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006521 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006522 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006523 try {
6524 mTranslucentCallback = callback;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006525 mChangeCanvasToTranslucent = ActivityManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006526 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006527 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006528 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006529 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006530 // Make callback return as though it timed out.
6531 mChangeCanvasToTranslucent = false;
6532 drawComplete = false;
6533 }
6534 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6535 // Window is already translucent.
6536 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006537 }
George Mount3cc716c2014-06-12 16:35:35 -07006538 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006539 }
6540
6541 /** @hide */
6542 void onTranslucentConversionComplete(boolean drawComplete) {
6543 if (mTranslucentCallback != null) {
6544 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6545 mTranslucentCallback = null;
6546 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006547 if (mChangeCanvasToTranslucent) {
6548 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6549 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006550 }
6551
Craig Mautnereb8abf72014-07-02 15:04:09 -07006552 /** @hide */
6553 public void onNewActivityOptions(ActivityOptions options) {
6554 mActivityTransitionState.setEnterActivityOptions(this, options);
6555 if (!mStopped) {
6556 mActivityTransitionState.enterReady(this);
6557 }
6558 }
6559
Craig Mautner5eda9b32013-07-02 11:58:16 -07006560 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006561 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6562 * from an activity launched by this activity in its call to {@link
6563 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6564 *
6565 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6566 * @hide
6567 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01006568 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07006569 ActivityOptions getActivityOptions() {
6570 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006571 return ActivityOptions.fromBundle(
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006572 ActivityManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07006573 } catch (RemoteException e) {
6574 }
6575 return null;
6576 }
6577
6578 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006579 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006580 * this method anytime between the start of {@link #onResume()} and the return from
6581 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6582 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6583 *
6584 * <p>The actions of this call are reset each time that this activity is brought to the
6585 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6586 * to not have requested visible behind. Therefore, if you want this activity to continue to
6587 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006588 *
6589 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6590 * for dialog and translucent activities.
6591 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006592 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6593 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6594 *
6595 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006596 * the next call to onResume.
6597 *
Bryce Leed59629e2017-04-18 14:35:29 -07006598 * @deprecated This method's functionality is no longer supported as of
6599 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6600 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006601 * @param visible true to notify the system that the activity wishes to be visible behind other
6602 * translucent activities, false to indicate otherwise. Resources must be
6603 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07006604 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006605 * @return the resulting visibiity state. If true the activity will remain visible beyond
6606 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6607 * then the activity may not count on being visible behind other translucent activities,
6608 * and must stop any media playback and release resources.
6609 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6610 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006611 *
Jose Limafcf70832014-08-27 23:09:05 -07006612 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07006613 */
Bryce Leed59629e2017-04-18 14:35:29 -07006614 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006615 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07006616 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006617 }
6618
6619 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006620 * Called when a translucent activity over this activity is becoming opaque or another
6621 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006622 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006623 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006624 * <p>When this method is called the activity has 500 msec to release any resources it may be
6625 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006626 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006627 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006628 * process. Otherwise {@link #onStop()} will be called following return.
6629 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006630 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07006631 *
6632 * @deprecated This method's functionality is no longer supported as of
6633 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006634 */
Bryce Leed59629e2017-04-18 14:35:29 -07006635 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08006636 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006637 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006638 mCalled = true;
6639 }
6640
6641 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006642 * Translucent activities may call this to determine if there is an activity below them that
6643 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006644 *
Bryce Leed59629e2017-04-18 14:35:29 -07006645 * @deprecated This method's functionality is no longer supported as of
6646 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6647 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006648 * @return true if an activity below is set to visible according to the most recent call to
6649 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006650 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006651 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006652 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006653 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006654 * @hide
6655 */
Bryce Leed59629e2017-04-18 14:35:29 -07006656 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006657 @SystemApi
6658 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006659 return false;
6660 }
6661
6662 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006663 * The topmost foreground activity will receive this call when the background visibility state
6664 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006665 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006666 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006667 * due to a background activity finishing itself.
6668 *
Bryce Leed59629e2017-04-18 14:35:29 -07006669 * @deprecated This method's functionality is no longer supported as of
6670 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6671 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006672 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006673 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006674 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006675 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006676 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006677 */
Bryce Leed59629e2017-04-18 14:35:29 -07006678 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006679 @SystemApi
6680 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006681 }
6682
6683 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006684 * Activities cannot draw during the period that their windows are animating in. In order
6685 * to know when it is safe to begin drawing they can override this method which will be
6686 * called when the entering animation has completed.
6687 */
6688 public void onEnterAnimationComplete() {
6689 }
6690
6691 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006692 * @hide
6693 */
6694 public void dispatchEnterAnimationComplete() {
6695 onEnterAnimationComplete();
6696 if (getWindow() != null && getWindow().getDecorView() != null) {
6697 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6698 }
6699 }
6700
6701 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006702 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006703 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006704 * Note that changing this value will have no effect on the activity's
6705 * {@link android.content.pm.ActivityInfo} structure; that is, if
6706 * <code>android:immersive</code> is set to <code>true</code>
6707 * in the application's manifest entry for this activity, the {@link
6708 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6709 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6710 * FLAG_IMMERSIVE} bit set.
6711 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006712 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006713 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6714 */
6715 public void setImmersive(boolean i) {
6716 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006717 ActivityManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04006718 } catch (RemoteException e) {
6719 // pass
6720 }
6721 }
6722
Adam Powell6e346362010-07-23 10:18:23 -07006723 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07006724 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006725 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006726 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
6727 * while this Activity has user focus.</p>
6728 *
6729 * <p>It is recommended that applications additionally declare
6730 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
6731 * transitions when switching between VR activities.</p>
6732 *
6733 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
6734 * VR mode will not be started. Developers can handle this case as follows:</p>
6735 *
6736 * <pre>
6737 * String servicePackage = "com.whatever.app";
6738 * String serviceClass = "com.whatever.app.MyVrListenerService";
6739 *
6740 * // Name of the component of the VrListenerService to start.
6741 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
6742 *
6743 * try {
6744 * setVrModeEnabled(true, myComponentName);
6745 * } catch (PackageManager.NameNotFoundException e) {
6746 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
6747 * boolean isInstalled = false;
6748 * for (ApplicationInfo app : installed) {
6749 * if (app.packageName.equals(servicePackage)) {
6750 * isInstalled = true;
6751 * break;
6752 * }
6753 * }
6754 * if (isInstalled) {
6755 * // Package is installed, but not enabled in Settings. Let user enable it.
6756 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
6757 * } else {
6758 * // Package is not installed. Send an intent to download this.
6759 * sentIntentToLaunchAppStore(servicePackage);
6760 * }
6761 * }
6762 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006763 *
6764 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08006765 * @param requestedComponent the name of the component to use as a
6766 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
6767 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006768 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
6769 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
6770 * not been enabled in user settings.
6771 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006772 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
6773 * @see android.service.vr.VrListenerService
6774 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
6775 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006776 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08006777 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
6778 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006779 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006780 if (ActivityManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08006781 != 0) {
6782 throw new PackageManager.NameNotFoundException(
6783 requestedComponent.flattenToString());
6784 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006785 } catch (RemoteException e) {
6786 // pass
6787 }
6788 }
6789
6790 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006791 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006792 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006793 * @param callback Callback that will manage lifecycle events for this action mode
6794 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006795 *
6796 * @see ActionMode
6797 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006798 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006799 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006800 return mWindow.getDecorView().startActionMode(callback);
6801 }
6802
Adam Powelldebf3be2010-11-15 18:58:48 -08006803 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006804 * Start an action mode of the given type.
6805 *
6806 * @param callback Callback that will manage lifecycle events for this action mode
6807 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6808 * @return The ActionMode that was started, or null if it was canceled
6809 *
6810 * @see ActionMode
6811 */
6812 @Nullable
6813 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6814 return mWindow.getDecorView().startActionMode(callback, type);
6815 }
6816
6817 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006818 * Give the Activity a chance to control the UI for an action mode requested
6819 * by the system.
6820 *
6821 * <p>Note: If you are looking for a notification callback that an action mode
6822 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6823 *
6824 * @param callback The callback that should control the new action mode
6825 * @return The new action mode, or <code>null</code> if the activity does not want to
6826 * provide special handling for this action mode. (It will be handled by the system.)
6827 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006828 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006829 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006830 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006831 // Only Primary ActionModes are represented in the ActionBar.
6832 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6833 initWindowDecorActionBar();
6834 if (mActionBar != null) {
6835 return mActionBar.startActionMode(callback);
6836 }
Adam Powell6e346362010-07-23 10:18:23 -07006837 }
6838 return null;
6839 }
6840
Adam Powelldebf3be2010-11-15 18:58:48 -08006841 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006842 * {@inheritDoc}
6843 */
6844 @Nullable
6845 @Override
6846 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6847 try {
6848 mActionModeTypeStarting = type;
6849 return onWindowStartingActionMode(callback);
6850 } finally {
6851 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6852 }
6853 }
6854
6855 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006856 * Notifies the Activity that an action mode has been started.
6857 * Activity subclasses overriding this method should call the superclass implementation.
6858 *
6859 * @param mode The new action mode.
6860 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006861 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006862 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006863 public void onActionModeStarted(ActionMode mode) {
6864 }
6865
6866 /**
6867 * Notifies the activity that an action mode has finished.
6868 * Activity subclasses overriding this method should call the superclass implementation.
6869 *
6870 * @param mode The action mode that just finished.
6871 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006872 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006873 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006874 public void onActionModeFinished(ActionMode mode) {
6875 }
6876
Adam Powelldd8fab22012-03-22 17:47:27 -07006877 /**
6878 * Returns true if the app should recreate the task when navigating 'up' from this activity
6879 * by using targetIntent.
6880 *
6881 * <p>If this method returns false the app can trivially call
6882 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6883 * up navigation. If this method returns false, the app should synthesize a new task stack
6884 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6885 *
6886 * @param targetIntent An intent representing the target destination for up navigation
6887 * @return true if navigating up should recreate a new task stack, false if the same task
6888 * should be used for the destination
6889 */
6890 public boolean shouldUpRecreateTask(Intent targetIntent) {
6891 try {
6892 PackageManager pm = getPackageManager();
6893 ComponentName cn = targetIntent.getComponent();
6894 if (cn == null) {
6895 cn = targetIntent.resolveActivity(pm);
6896 }
6897 ActivityInfo info = pm.getActivityInfo(cn, 0);
6898 if (info.taskAffinity == null) {
6899 return false;
6900 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006901 return ActivityManager.getService()
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006902 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006903 } catch (RemoteException e) {
6904 return false;
6905 } catch (NameNotFoundException e) {
6906 return false;
6907 }
6908 }
6909
6910 /**
6911 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6912 * in the process. If the activity indicated by upIntent already exists in the task's history,
6913 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006914 * finished.
6915 *
6916 * <p>If the indicated activity does not appear in the history stack, this will finish
6917 * each activity in this task until the root activity of the task is reached, resulting in
6918 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6919 * when an activity may be reached by a path not passing through a canonical parent
6920 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006921 *
6922 * <p>This method should be used when performing up navigation from within the same task
6923 * as the destination. If up navigation should cross tasks in some cases, see
6924 * {@link #shouldUpRecreateTask(Intent)}.</p>
6925 *
6926 * @param upIntent An intent representing the target destination for up navigation
6927 *
6928 * @return true if up navigation successfully reached the activity indicated by upIntent and
6929 * upIntent was delivered to it. false if an instance of the indicated activity could
6930 * not be found and this activity was simply finished normally.
6931 */
6932 public boolean navigateUpTo(Intent upIntent) {
6933 if (mParent == null) {
6934 ComponentName destInfo = upIntent.getComponent();
6935 if (destInfo == null) {
6936 destInfo = upIntent.resolveActivity(getPackageManager());
6937 if (destInfo == null) {
6938 return false;
6939 }
6940 upIntent = new Intent(upIntent);
6941 upIntent.setComponent(destInfo);
6942 }
6943 int resultCode;
6944 Intent resultData;
6945 synchronized (this) {
6946 resultCode = mResultCode;
6947 resultData = mResultData;
6948 }
6949 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006950 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006951 }
6952 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006953 upIntent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006954 return ActivityManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07006955 resultCode, resultData);
6956 } catch (RemoteException e) {
6957 return false;
6958 }
6959 } else {
6960 return mParent.navigateUpToFromChild(this, upIntent);
6961 }
6962 }
6963
6964 /**
6965 * This is called when a child activity of this one calls its
6966 * {@link #navigateUpTo} method. The default implementation simply calls
6967 * navigateUpTo(upIntent) on this activity (the parent).
6968 *
6969 * @param child The activity making the call.
6970 * @param upIntent An intent representing the target destination for up navigation
6971 *
6972 * @return true if up navigation successfully reached the activity indicated by upIntent and
6973 * upIntent was delivered to it. false if an instance of the indicated activity could
6974 * not be found and this activity was simply finished normally.
6975 */
6976 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6977 return navigateUpTo(upIntent);
6978 }
6979
6980 /**
6981 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6982 * this activity's logical parent. The logical parent is named in the application's manifest
6983 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006984 * Activity subclasses may override this method to modify the Intent returned by
6985 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6986 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006987 *
Adam Powell04d58112012-04-09 10:22:12 -07006988 * @return a new Intent targeting the defined parent of this activity or null if
6989 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006990 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006991 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006992 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006993 final String parentName = mActivityInfo.parentActivityName;
6994 if (TextUtils.isEmpty(parentName)) {
6995 return null;
6996 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006997
6998 // If the parent itself has no parent, generate a main activity intent.
6999 final ComponentName target = new ComponentName(this, parentName);
7000 try {
7001 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
7002 final String parentActivity = parentInfo.parentActivityName;
7003 final Intent parentIntent = parentActivity == null
7004 ? Intent.makeMainActivity(target)
7005 : new Intent().setComponent(target);
7006 return parentIntent;
7007 } catch (NameNotFoundException e) {
7008 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7009 "' in manifest");
7010 return null;
7011 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007012 }
7013
George Mount31a21722014-03-24 17:44:36 -07007014 /**
George Mount62ab9b72014-05-02 13:51:17 -07007015 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007016 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007017 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007018 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007019 *
George Mount65580562014-08-29 08:15:48 -07007020 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007021 */
George Mount65580562014-08-29 08:15:48 -07007022 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7023 if (callback == null) {
7024 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007025 }
George Mount65580562014-08-29 08:15:48 -07007026 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007027 }
7028
7029 /**
7030 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007031 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007032 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7033 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007034 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007035 *
George Mount65580562014-08-29 08:15:48 -07007036 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007037 */
George Mount65580562014-08-29 08:15:48 -07007038 public void setExitSharedElementCallback(SharedElementCallback callback) {
7039 if (callback == null) {
7040 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007041 }
George Mount65580562014-08-29 08:15:48 -07007042 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007043 }
7044
George Mount8c2614c2014-06-10 11:12:01 -07007045 /**
7046 * Postpone the entering activity transition when Activity was started with
7047 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7048 * android.util.Pair[])}.
7049 * <p>This method gives the Activity the ability to delay starting the entering and
7050 * shared element transitions until all data is loaded. Until then, the Activity won't
7051 * draw into its window, leaving the window transparent. This may also cause the
7052 * returning animation to be delayed until data is ready. This method should be
7053 * called in {@link #onCreate(android.os.Bundle)} or in
7054 * {@link #onActivityReenter(int, android.content.Intent)}.
7055 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7056 * start the transitions. If the Activity did not use
7057 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7058 * android.util.Pair[])}, then this method does nothing.</p>
7059 */
7060 public void postponeEnterTransition() {
7061 mActivityTransitionState.postponeEnterTransition();
7062 }
7063
7064 /**
7065 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7066 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7067 * to have your Activity start drawing.
7068 */
7069 public void startPostponedEnterTransition() {
7070 mActivityTransitionState.startPostponedEnterTransition();
7071 }
7072
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007073 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007074 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7075 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007076 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007077 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7078 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007079 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007080 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7081 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7082 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7083 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007084 }
7085 return null;
7086 }
7087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007089
Mathew Inwood45d2c252018-09-14 12:35:36 +01007090 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 final void setParent(Activity parent) {
7092 mParent = parent;
7093 }
7094
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007095 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007096 final void attach(Context context, ActivityThread aThread,
7097 Instrumentation instr, IBinder token, int ident,
7098 Application application, Intent intent, ActivityInfo info,
7099 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007100 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007101 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07007102 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 attachBaseContext(context);
7104
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007105 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007106
Andrii Kulian51c1b672017-04-07 18:39:32 -07007107 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007108 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007110 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007111 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7113 mWindow.setSoftInputMode(info.softInputMode);
7114 }
Adam Powell269248d2011-08-02 10:26:54 -07007115 if (info.uiOptions != 0) {
7116 mWindow.setUiOptions(info.uiOptions);
7117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 mMainThread = aThread;
7121 mInstrumentation = instr;
7122 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007123 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 mApplication = application;
7125 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007126 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 mComponent = intent.getComponent();
7128 mActivityInfo = info;
7129 mTitle = title;
7130 mParent = parent;
7131 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007132 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007133 if (voiceInteractor != null) {
7134 if (lastNonConfigurationInstances != null) {
7135 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7136 } else {
7137 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7138 Looper.myLooper());
7139 }
7140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141
Jeff Brown98365d72012-08-19 20:30:52 -07007142 mWindow.setWindowManager(
7143 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7144 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007145 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 if (mParent != null) {
7147 mWindow.setContainer(mParent.getWindow());
7148 }
7149 mWindowManager = mWindow.getWindowManager();
7150 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007151
7152 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007153
7154 setAutofillCompatibilityEnabled(application.isAutofillCompatibilityEnabled());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007155 }
7156
7157 private void enableAutofillCompatibilityIfNeeded() {
7158 if (isAutofillCompatibilityEnabled()) {
7159 final AutofillManager afm = getSystemService(AutofillManager.class);
7160 if (afm != null) {
7161 afm.enableCompatibilityMode();
7162 }
7163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 }
7165
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007166 /** @hide */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007167 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007168 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 return mParent != null ? mParent.getActivityToken() : mToken;
7170 }
7171
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007172 /** @hide */
7173 @VisibleForTesting
7174 public final ActivityThread getActivityThread() {
7175 return mMainThread;
7176 }
7177
Winson Chung298f95b2017-08-10 15:57:18 -07007178 final void performCreate(Bundle icicle) {
7179 performCreate(icicle, null);
7180 }
7181
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007182 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007183 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
7184 mCanEnterPictureInPicture = true;
7185 restoreHasCurrentPermissionRequest(icicle);
7186 if (persistentState != null) {
7187 onCreate(icicle, persistentState);
7188 } else {
7189 onCreate(icicle);
7190 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007191 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007192 mActivityTransitionState.readState(icicle);
7193
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007194 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7195 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007196 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007197 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007198 }
George Mount0a778ed2013-12-13 13:35:36 -08007199
Winson Chung298f95b2017-08-10 15:57:18 -07007200 final void performNewIntent(Intent intent) {
7201 mCanEnterPictureInPicture = true;
7202 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007203 }
7204
Andrii Kuliand25680c2018-02-21 15:16:58 -08007205 final void performStart(String reason) {
George Mount62ab9b72014-05-02 13:51:17 -07007206 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007207 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007209 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007211 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 if (!mCalled) {
7214 throw new SuperNotCalledException(
7215 "Activity " + mComponent.toShortString() +
7216 " did not call through to super.onStart()");
7217 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007218 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007219 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007220
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007221 boolean isAppDebuggable =
7222 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
7223
David Brazdil795cbfb2018-01-26 12:23:15 +00007224 // This property is set for all non-user builds except final release
David Brazdil927d6de2018-01-24 19:54:30 +00007225 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
7226
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007227 if (isAppDebuggable || isDlwarningEnabled) {
7228 String dlwarning = getDlWarning();
7229 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007230 String appName = getApplicationInfo().loadLabel(getPackageManager())
7231 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007232 String warning = "Detected problems with app native libraries\n" +
7233 "(please consult log for detail):\n" + dlwarning;
7234 if (isAppDebuggable) {
7235 new AlertDialog.Builder(this).
7236 setTitle(appName).
7237 setMessage(warning).
7238 setPositiveButton(android.R.string.ok, null).
7239 setCancelable(false).
7240 show();
7241 } else {
7242 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7243 }
7244 }
7245 }
7246
George Mount62ab9b72014-05-02 13:51:17 -07007247 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 }
RoboErik55011652014-07-09 15:05:53 -07007249
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007250 /**
7251 * Restart the activity.
7252 * @param start Indicates whether the activity should also be started after restart.
7253 * The option to not start immediately is needed in case a transaction with
7254 * multiple lifecycle transitions is in progress.
7255 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007256 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007257 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007258 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007259
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007260 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007261 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007262 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7263 }
7264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 if (mStopped) {
7266 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007267
7268 synchronized (mManagedCursors) {
7269 final int N = mManagedCursors.size();
7270 for (int i=0; i<N; i++) {
7271 ManagedCursor mc = mManagedCursors.get(i);
7272 if (mc.mReleased || mc.mUpdated) {
7273 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007274 if (getApplicationInfo().targetSdkVersion
7275 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7276 throw new IllegalStateException(
7277 "trying to requery an already closed cursor "
7278 + mc.mCursor);
7279 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007280 }
7281 mc.mReleased = false;
7282 mc.mUpdated = false;
7283 }
7284 }
7285 }
7286
7287 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007289 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 if (!mCalled) {
7291 throw new SuperNotCalledException(
7292 "Activity " + mComponent.toShortString() +
7293 " did not call through to super.onRestart()");
7294 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007295 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007296 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 }
7299 }
RoboErik55011652014-07-09 15:05:53 -07007300
Andrii Kuliand25680c2018-02-21 15:16:58 -08007301 final void performResume(boolean followedByPause, String reason) {
7302 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007303
Dianne Hackborn445646c2010-06-25 15:52:59 -07007304 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007305
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007306 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007307
Dake Gu67decfa2017-12-27 11:48:08 -08007308 if (mAutoFillResetNeeded) {
7309 // When Activity is destroyed in paused state, and relaunch activity, there will be
7310 // extra onResume and onPause event, ignore the first onResume and onPause.
7311 // see ActivityThread.handleRelaunchActivity()
7312 mAutoFillIgnoreFirstResumePause = followedByPause;
7313 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7314 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7315 }
7316 }
7317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007318 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007319 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007321 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 if (!mCalled) {
7323 throw new SuperNotCalledException(
7324 "Activity " + mComponent.toShortString() +
7325 " did not call through to super.onResume()");
7326 }
7327
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007328 // invisible activities must be finished before onResume() completes
7329 if (!mVisibleFromClient && !mFinished) {
7330 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7331 if (getApplicationInfo().targetSdkVersion
7332 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7333 throw new IllegalStateException(
7334 "Activity " + mComponent.toShortString() +
7335 " did not call finish() prior to onResume() completing");
7336 }
7337 }
7338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007341
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007342 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007343 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007345 onPostResume();
7346 if (!mCalled) {
7347 throw new SuperNotCalledException(
7348 "Activity " + mComponent.toShortString() +
7349 " did not call through to super.onPostResume()");
7350 }
7351 }
7352
7353 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007354 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007355 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007356 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007358 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007359 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007360 if (!mCalled && getApplicationInfo().targetSdkVersion
7361 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7362 throw new SuperNotCalledException(
7363 "Activity " + mComponent.toShortString() +
7364 " did not call through to super.onPause()");
7365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 }
RoboErik55011652014-07-09 15:05:53 -07007367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007368 final void performUserLeaving() {
7369 onUserInteraction();
7370 onUserLeaveHint();
7371 }
RoboErik55011652014-07-09 15:05:53 -07007372
Andrii Kuliand25680c2018-02-21 15:16:58 -08007373 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007374 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007375 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007376
Winson Chung298f95b2017-08-10 15:57:18 -07007377 // Disallow entering picture-in-picture after the activity has been stopped
7378 mCanEnterPictureInPicture = false;
7379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 if (!mStopped) {
7381 if (mWindow != null) {
7382 mWindow.closeAllPanels();
7383 }
7384
Chong Zhang7687f252016-02-26 12:03:33 -08007385 // If we're preserving the window, don't setStoppedState to true, since we
7386 // need the window started immediately again. Stopping the window will
7387 // destroys hardware resources and causes flicker.
7388 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07007389 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08007390 }
RoboErik55011652014-07-09 15:05:53 -07007391
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007392 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07007393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007394 mCalled = false;
7395 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007396 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 if (!mCalled) {
7398 throw new SuperNotCalledException(
7399 "Activity " + mComponent.toShortString() +
7400 " did not call through to super.onStop()");
7401 }
RoboErik55011652014-07-09 15:05:53 -07007402
Makoto Onuki2f6a0182010-02-22 13:26:59 -08007403 synchronized (mManagedCursors) {
7404 final int N = mManagedCursors.size();
7405 for (int i=0; i<N; i++) {
7406 ManagedCursor mc = mManagedCursors.get(i);
7407 if (!mc.mReleased) {
7408 mc.mCursor.deactivate();
7409 mc.mReleased = true;
7410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 }
7412 }
George Mount0a778ed2013-12-13 13:35:36 -08007413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 mStopped = true;
7415 }
7416 mResumed = false;
7417 }
7418
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007419 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07007420 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07007421 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007422 mFragments.dispatchDestroy();
7423 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007424 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007425 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07007426 if (mVoiceInteractor != null) {
7427 mVoiceInteractor.detachActivity();
7428 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007429 }
George Mount0a778ed2013-12-13 13:35:36 -08007430
Winson Chung5af42fc2017-03-24 17:11:33 -07007431 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
7432 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007433 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007434 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
7435 + " " + newConfig);
7436 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007437 if (mWindow != null) {
7438 mWindow.onMultiWindowModeChanged();
7439 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007440 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007441 }
7442
Winson Chung5af42fc2017-03-24 17:11:33 -07007443 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
7444 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007445 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007446 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
7447 + " " + newConfig);
7448 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07007449 if (mWindow != null) {
7450 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
7451 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007452 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007453 }
7454
George Mount0a778ed2013-12-13 13:35:36 -08007455 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06007456 * @hide
7457 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007458 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06007459 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 return mResumed;
7461 }
7462
Svetoslavffb32b12015-10-15 16:54:00 -07007463 private void storeHasCurrentPermissionRequest(Bundle bundle) {
7464 if (bundle != null && mHasCurrentPermissionsRequest) {
7465 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
7466 }
7467 }
7468
7469 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
7470 if (bundle != null) {
7471 mHasCurrentPermissionsRequest = bundle.getBoolean(
7472 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
7473 }
7474 }
7475
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007476 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07007477 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
7478 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07007479 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
7481 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007482 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007483 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007484 onActivityResult(requestCode, resultCode, data);
7485 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
7486 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
7487 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08007488 dispatchRequestPermissionsResult(requestCode, data);
7489 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00007490 Fragment frag = mFragments.findFragmentByWho(who);
7491 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007492 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08007493 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07007494 }
Svetoslav970b59c2015-06-09 16:05:21 -07007495 } else if (who.startsWith("@android:view:")) {
7496 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
7497 getActivityToken());
7498 for (ViewRootImpl viewRoot : views) {
7499 if (viewRoot.getView() != null
7500 && viewRoot.getView().dispatchActivityResult(
7501 who, requestCode, resultCode, data)) {
7502 return;
7503 }
7504 }
Svet Ganov782043c2017-02-11 00:52:02 +00007505 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08007506 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08007507 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07007508 } else {
7509 Fragment frag = mFragments.findFragmentByWho(who);
7510 if (frag != null) {
7511 frag.onActivityResult(requestCode, resultCode, data);
7512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07007514 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007516
Jason Monka57e5e02014-05-07 10:06:48 -04007517 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01007518 * Request to put this activity in a mode where the user is locked to a restricted set of
7519 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04007520 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007521 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
7522 * for this component, the current task will be launched directly into LockTask mode. Only apps
7523 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
7524 * be launched while LockTask mode is active. The user will not be able to leave this mode
7525 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
7526 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04007527 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007528 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
7529 * system will prompt the user with a dialog requesting permission to use this mode.
7530 * The user can exit at any time through instructions shown on the request dialog. Calling
7531 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04007532 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007533 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
7534 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07007535 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007536 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07007537 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04007538 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007539 public void startLockTask() {
7540 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007541 ActivityManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007542 } catch (RemoteException e) {
7543 }
7544 }
7545
Jason Monka57e5e02014-05-07 10:06:48 -04007546 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01007547 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04007548 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007549 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
7550 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04007551 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007552 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
7553 * by this activity, then calling this method will not terminate the LockTask mode, but only
7554 * finish its own task. The device will remain in LockTask mode, until the activity which
7555 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
7556 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07007557 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007558 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07007559 * @see android.R.attr#lockTaskMode
7560 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04007561 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007562 public void stopLockTask() {
7563 try {
Charles Heff9b4dff2017-09-22 10:18:37 +01007564 ActivityManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007565 } catch (RemoteException e) {
7566 }
7567 }
7568
Craig Mautner5eda9b32013-07-02 11:58:16 -07007569 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007570 * Shows the user the system defined message for telling the user how to exit
7571 * lock task mode. The task containing this activity must be in lock task mode at the time
7572 * of this call for the message to be displayed.
7573 */
7574 public void showLockTaskEscapeMessage() {
7575 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007576 ActivityManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007577 } catch (RemoteException e) {
7578 }
7579 }
7580
7581 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07007582 * Check whether the caption on freeform windows is displayed directly on the content.
7583 *
7584 * @return True if caption is displayed on content, false if it pushes the content down.
7585 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08007586 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07007587 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07007588 */
7589 public boolean isOverlayWithDecorCaptionEnabled() {
7590 return mWindow.isOverlayWithDecorCaptionEnabled();
7591 }
7592
7593 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08007594 * Set whether the caption should displayed directly on the content rather than push it down.
7595 *
7596 * This affects only freeform windows since they display the caption and only the main
7597 * window of the activity. The caption is used to drag the window around and also shows
7598 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07007599 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08007600 */
Andrii Kulian933076d2016-03-29 17:04:42 -07007601 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
7602 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08007603 }
7604
7605 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007606 * Interface for informing a translucent {@link Activity} once all visible activities below it
7607 * have completed drawing. This is necessary only after an {@link Activity} has been made
7608 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
7609 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07007610 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7611 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07007612 *
7613 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007614 */
Jose Lima14914852014-08-14 09:14:12 -07007615 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007616 public interface TranslucentConversionListener {
7617 /**
7618 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
7619 * below the top one have been redrawn. Following this callback it is safe to make the top
7620 * Activity translucent because the underlying Activity has been drawn.
7621 *
7622 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
7623 * occurred waiting for the Activity to complete drawing.
7624 *
7625 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07007626 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007627 */
7628 public void onTranslucentConversionComplete(boolean drawComplete);
7629 }
Svetoslavc6d1c342015-02-26 14:44:43 -08007630
7631 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07007632 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07007633 // If the package installer crashed we may have not data - best effort.
7634 String[] permissions = (data != null) ? data.getStringArrayExtra(
7635 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7636 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7637 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08007638 onRequestPermissionsResult(requestCode, permissions, grantResults);
7639 }
7640
7641 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07007642 Fragment fragment) {
7643 // If the package installer crashed we may have not data - best effort.
7644 String[] permissions = (data != null) ? data.getStringArrayExtra(
7645 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7646 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7647 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
7648 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08007649 }
7650
Svet Ganov782043c2017-02-11 00:52:02 +00007651 /** @hide */
7652 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007653 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007654 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00007655 try {
7656 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007657 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00007658 } catch (IntentSender.SendIntentException e) {
7659 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
7660 }
7661 }
7662
Svet Ganov17db9dc2017-02-21 19:54:31 -08007663 /** @hide */
7664 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007665 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08007666 mAutoFillResetNeeded = true;
7667 }
7668
Felipe Leme4753bb02017-03-22 20:24:00 -07007669 /** @hide */
7670 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007671 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07007672 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07007673 final boolean wasShowing;
7674
7675 if (mAutofillPopupWindow == null) {
7676 wasShowing = false;
7677 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
7678 } else {
7679 wasShowing = mAutofillPopupWindow.isShowing();
7680 }
Svet Ganov374cae12017-05-10 13:42:33 -07007681 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07007682
7683 return !wasShowing && mAutofillPopupWindow.isShowing();
7684 }
7685
7686 /** @hide */
7687 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08007688 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
7689 @NonNull KeyEvent keyEvent) {
7690 ViewRootImpl rootImpl = anchor.getViewRootImpl();
7691 if (rootImpl != null) {
7692 // dont care if anchorView is current focus, for example a custom view may only receive
7693 // touchEvent, not focusable but can still trigger autofill window. The Key handling
7694 // might be inside parent of the custom view.
7695 rootImpl.dispatchKeyFromAutofill(keyEvent);
7696 }
7697 }
7698
7699 /** @hide */
7700 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007701 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07007702 if (mAutofillPopupWindow == null) {
7703 return false;
7704 }
7705 mAutofillPopupWindow.dismiss();
7706 mAutofillPopupWindow = null;
7707 return true;
7708 }
7709
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007710 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07007711 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007712 public final boolean autofillClientIsFillUiShowing() {
7713 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
7714 }
7715
7716 /** @hide */
7717 @Override
7718 @NonNull
7719 public final View[] autofillClientFindViewsByAutofillIdTraversal(
7720 @NonNull AutofillId[] autofillId) {
7721 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007722 final ArrayList<ViewRootImpl> roots =
7723 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
7724
7725 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7726 final View rootView = roots.get(rootNum).getView();
7727
7728 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007729 final int viewCount = autofillId.length;
7730 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007731 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07007732 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007733 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007734 }
7735 }
7736 }
7737 }
7738
7739 return views;
7740 }
7741
7742 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007743 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007744 @Nullable
7745 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07007746 final ArrayList<ViewRootImpl> roots =
7747 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
7748 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7749 final View rootView = roots.get(rootNum).getView();
7750
7751 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007752 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07007753 if (view != null) {
7754 return view;
7755 }
7756 }
7757 }
7758
7759 return null;
7760 }
7761
7762 /** @hide */
7763 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007764 public final @NonNull boolean[] autofillClientGetViewVisibility(
7765 @NonNull AutofillId[] autofillIds) {
7766 final int autofillIdCount = autofillIds.length;
7767 final boolean[] visible = new boolean[autofillIdCount];
7768 for (int i = 0; i < autofillIdCount; i++) {
7769 final AutofillId autofillId = autofillIds[i];
7770 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
7771 if (view != null) {
7772 if (!autofillId.isVirtual()) {
7773 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007774 } else {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007775 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007776 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007777 }
7778 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08007779 if (android.view.autofill.Helper.sVerbose) {
7780 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
7781 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007782 return visible;
7783 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007784
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007785 /** @hide */
7786 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
7787 int windowId) {
7788 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
7789 .getRootViews(getActivityToken());
7790 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7791 final View rootView = roots.get(rootNum).getView();
7792 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
7793 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
7794 if (view != null) {
7795 return view;
7796 }
7797 }
7798 }
7799 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007800 }
7801
7802 /** @hide */
7803 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007804 public final @Nullable IBinder autofillClientGetActivityToken() {
7805 return getActivityToken();
7806 }
7807
7808 /** @hide */
7809 @Override
7810 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007811 return !mStopped;
7812 }
7813
Dake Gu67decfa2017-12-27 11:48:08 -08007814 /** @hide */
7815 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08007816 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007817 return isAutofillCompatibilityEnabled();
7818 }
7819
7820 /** @hide */
7821 @Override
7822 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08007823 return mAutoFillIgnoreFirstResumePause || !mResumed;
7824 }
7825
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01007826 /**
7827 * If set to true, this indicates to the system that it should never take a
7828 * screenshot of the activity to be used as a representation while it is not in a started state.
7829 * <p>
7830 * Note that the system may use the window background of the theme instead to represent
7831 * the window when it is not running.
7832 * <p>
7833 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
7834 * this only affects the behavior when the activity's screenshot would be used as a
7835 * representation when the activity is not in a started state, i.e. in Overview. The system may
7836 * still take screenshots of the activity in other contexts; for example, when the user takes a
7837 * screenshot of the entire screen, or when the active
7838 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
7839 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
7840 *
7841 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
7842 * @hide
7843 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007844 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01007845 public void setDisablePreviewScreenshots(boolean disable) {
7846 try {
7847 ActivityManager.getService().setDisablePreviewScreenshots(mToken, disable);
7848 } catch (RemoteException e) {
7849 Log.e(TAG, "Failed to call setDisablePreviewScreenshots", e);
7850 }
7851 }
7852
chaviw59b98852017-06-13 12:05:44 -07007853 /**
koprivaa1a78482018-10-09 10:09:23 -07007854 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07007855 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
7856 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
7857 * activity will remain in the resumed state visible on-top of the lock screen. This value can
7858 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
7859 *
7860 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
7861 * {@code false} otherwise.
7862 * @see #setTurnScreenOn(boolean)
7863 * @see android.R.attr#turnScreenOn
7864 * @see android.R.attr#showWhenLocked
7865 */
7866 public void setShowWhenLocked(boolean showWhenLocked) {
7867 try {
7868 ActivityManager.getService().setShowWhenLocked(mToken, showWhenLocked);
7869 } catch (RemoteException e) {
7870 Log.e(TAG, "Failed to call setShowWhenLocked", e);
7871 }
7872 }
7873
7874 /**
7875 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
7876 * Normally an activity will be transitioned to the stopped state if it is started while the
7877 * screen if off, but with this flag set the activity will cause the screen to turn on if the
7878 * activity will be visible and resumed due to the screen coming on. The screen will not be
7879 * turned on if the activity won't be visible after the screen is turned on. This flag is
7880 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
7881 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
7882 * if this flag is set and the activity calls {@link
7883 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
7884 * the screen will turn on.
7885 *
7886 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
7887 *
7888 * @see #setShowWhenLocked(boolean)
7889 * @see android.R.attr#turnScreenOn
7890 * @see android.R.attr#showWhenLocked
7891 */
7892 public void setTurnScreenOn(boolean turnScreenOn) {
7893 try {
7894 ActivityManager.getService().setTurnScreenOn(mToken, turnScreenOn);
7895 } catch (RemoteException e) {
7896 Log.e(TAG, "Failed to call setTurnScreenOn", e);
7897 }
7898 }
7899
Jorim Jaggif84e2f62018-01-16 14:17:59 +01007900 /**
7901 * Registers remote animations per transition type for this activity.
7902 *
7903 * @param definition The remote animation definition that defines which transition whould run
7904 * which remote animation.
7905 * @hide
7906 */
7907 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007908 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01007909 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
7910 try {
7911 ActivityManager.getService().registerRemoteAnimations(mToken, definition);
7912 } catch (RemoteException e) {
7913 Log.e(TAG, "Failed to call registerRemoteAnimations", e);
7914 }
7915 }
7916
Andrii Kuliand25680c2018-02-21 15:16:58 -08007917 /** Log a lifecycle event for current user id and component class. */
7918 private void writeEventLog(int event, String reason) {
7919 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
7920 reason);
7921 }
7922
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007923 class HostCallbacks extends FragmentHostCallback<Activity> {
7924 public HostCallbacks() {
7925 super(Activity.this /*activity*/);
7926 }
7927
7928 @Override
7929 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
7930 Activity.this.dump(prefix, fd, writer, args);
7931 }
7932
7933 @Override
7934 public boolean onShouldSaveFragmentState(Fragment fragment) {
7935 return !isFinishing();
7936 }
7937
7938 @Override
7939 public LayoutInflater onGetLayoutInflater() {
7940 final LayoutInflater result = Activity.this.getLayoutInflater();
7941 if (onUseFragmentManagerInflaterFactory()) {
7942 return result.cloneInContext(Activity.this);
7943 }
7944 return result;
7945 }
7946
7947 @Override
7948 public boolean onUseFragmentManagerInflaterFactory() {
7949 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
7950 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
7951 }
7952
7953 @Override
7954 public Activity onGetHost() {
7955 return Activity.this;
7956 }
7957
7958 @Override
7959 public void onInvalidateOptionsMenu() {
7960 Activity.this.invalidateOptionsMenu();
7961 }
7962
7963 @Override
7964 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
7965 Bundle options) {
7966 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
7967 }
7968
7969 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01007970 public void onStartActivityAsUserFromFragment(
7971 Fragment fragment, Intent intent, int requestCode, Bundle options,
7972 UserHandle user) {
7973 Activity.this.startActivityAsUserFromFragment(
7974 fragment, intent, requestCode, options, user);
7975 }
7976
7977 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01007978 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
7979 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
7980 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
7981 if (mParent == null) {
7982 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
7983 flagsMask, flagsValues, options);
7984 } else if (options != null) {
7985 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
7986 fillInIntent, flagsMask, flagsValues, extraFlags, options);
7987 }
7988 }
7989
7990 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007991 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7992 int requestCode) {
7993 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7994 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7995 startActivityForResult(who, intent, requestCode, null);
7996 }
7997
7998 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007999 public boolean onHasWindowAnimations() {
8000 return getWindow() != null;
8001 }
8002
8003 @Override
8004 public int onGetWindowAnimations() {
8005 final Window w = getWindow();
8006 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8007 }
8008
Todd Kennedy434bd652015-05-04 12:29:50 -07008009 @Override
8010 public void onAttachFragment(Fragment fragment) {
8011 Activity.this.onAttachFragment(fragment);
8012 }
8013
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008014 @Nullable
8015 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008016 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008017 return Activity.this.findViewById(id);
8018 }
8019
8020 @Override
8021 public boolean onHasView() {
8022 final Window w = getWindow();
8023 return (w != null && w.peekDecorView() != null);
8024 }
8025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026}