blob: aeeaa519e2f5608c9a1712a198cde5344da636e3 [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
Dianne Hackborn625ac272010-09-17 18:29:22 -0700134import java.io.FileDescriptor;
135import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700136import java.lang.annotation.Retention;
137import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700138import java.util.ArrayList;
Felipe Leme5b32ebe2018-02-15 12:52:19 -0800139import java.util.Arrays;
Adam Powell6e346362010-07-23 10:18:23 -0700140import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700141import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Bryce Leee83f34cd2017-10-31 19:50:54 -0700143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144/**
145 * An activity is a single, focused thing that the user can do. Almost all
146 * activities interact with the user, so the Activity class takes care of
147 * creating a window for you in which you can place your UI with
148 * {@link #setContentView}. While activities are often presented to the user
149 * as full-screen windows, they can also be used in other ways: as floating
150 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
151 * or embedded inside of another activity (using {@link ActivityGroup}).
152 *
153 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * <ul>
156 * <li> {@link #onCreate} is where you initialize your activity. Most
157 * importantly, here you will usually call {@link #setContentView(int)}
158 * with a layout resource defining your UI, and using {@link #findViewById}
159 * to retrieve the widgets in that UI that you need to interact with
160 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * <li> {@link #onPause} is where you deal with the user leaving your
163 * activity. Most importantly, any changes made by the user should at this
164 * point be committed (usually to the
165 * {@link android.content.ContentProvider} holding the data).
166 * </ul>
167 *
168 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
169 * activity classes must have a corresponding
170 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
171 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <p>Topics covered here:
174 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700175 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
177 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
178 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
179 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
180 * <li><a href="#Permissions">Permissions</a>
181 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
182 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700183 *
184 * <div class="special reference">
185 * <h3>Developer Guides</h3>
186 * <p>The Activity class is an important part of an application's overall lifecycle,
187 * and the way activities are launched and put together is a fundamental
188 * part of the platform's application model. For a detailed perspective on the structure of an
189 * Android application and how activities behave, please read the
190 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
Mark Lufa434852016-08-11 17:40:33 -0700191 * <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700192 * developer guides.</p>
193 *
194 * <p>You can also find a detailed discussion about how to create activities in the
Mark Lufa434852016-08-11 17:40:33 -0700195 * <a href="{@docRoot}guide/components/activities.html">Activities</a>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700196 * developer guide.</p>
197 * </div>
198 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700199 * <a name="Fragments"></a>
200 * <h3>Fragments</h3>
201 *
Ian Lake0a1feb82017-11-13 10:26:46 -0800202 * <p>The {@link android.support.v4.app.FragmentActivity} subclass
203 * can make use of the {@link android.support.v4.app.Fragment} class to better
Dianne Hackborn291905e2010-08-17 15:17:15 -0700204 * modularize their code, build more sophisticated user interfaces for larger
Ian Lake0a1feb82017-11-13 10:26:46 -0800205 * screens, and help scale their application between small and large screens.</p>
206 *
207 * <p>For more information about using fragments, read the
208 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700209 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <a name="ActivityLifecycle"></a>
211 * <h3>Activity Lifecycle</h3>
212 *
213 * <p>Activities in the system are managed as an <em>activity stack</em>.
214 * When a new activity is started, it is placed on the top of the stack
215 * and becomes the running activity -- the previous activity always remains
216 * below it in the stack, and will not come to the foreground again until
217 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700218 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <p>An activity has essentially four states:</p>
220 * <ul>
Trevor Johns682c24e2016-04-12 10:13:47 -0700221 * <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 -0800222 * the stack),
223 * it is <em>active</em> or <em>running</em>. </li>
224 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700225 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * is <em>paused</em>. A paused activity is completely alive (it
227 * maintains all state and member information and remains attached to
228 * the window manager), but can be killed by the system in extreme
229 * low memory situations.
230 * <li>If an activity is completely obscured by another activity,
231 * it is <em>stopped</em>. It still retains all state and member information,
232 * however, it is no longer visible to the user so its window is hidden
233 * and it will often be killed by the system when memory is needed
234 * elsewhere.</li>
235 * <li>If an activity is paused or stopped, the system can drop the activity
236 * from memory by either asking it to finish, or simply killing its
237 * process. When it is displayed again to the user, it must be
238 * completely restarted and restored to its previous state.</li>
239 * </ul>
240 *
241 * <p>The following diagram shows the important state paths of an Activity.
242 * The square rectangles represent callback methods you can implement to
243 * perform operations when the Activity moves between states. The colored
244 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <p><img src="../../../images/activity_lifecycle.png"
247 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * <p>There are three key loops you may be interested in monitoring within your
250 * activity:
RoboErik55011652014-07-09 15:05:53 -0700251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * <ul>
253 * <li>The <b>entire lifetime</b> of an activity happens between the first call
254 * to {@link android.app.Activity#onCreate} through to a single final call
255 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
256 * of "global" state in onCreate(), and release all remaining resources in
257 * onDestroy(). For example, if it has a thread running in the background
258 * to download data from the network, it may create that thread in onCreate()
259 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700260 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * <li>The <b>visible lifetime</b> of an activity happens between a call to
262 * {@link android.app.Activity#onStart} until a corresponding call to
263 * {@link android.app.Activity#onStop}. During this time the user can see the
264 * activity on-screen, though it may not be in the foreground and interacting
265 * with the user. Between these two methods you can maintain resources that
266 * are needed to show the activity to the user. For example, you can register
267 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900268 * that impact your UI, and unregister it in onStop() when the user no
269 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * can be called multiple times, as the activity becomes visible and hidden
271 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700272 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
274 * {@link android.app.Activity#onResume} until a corresponding call to
275 * {@link android.app.Activity#onPause}. During this time the activity is
276 * in front of all other activities and interacting with the user. An activity
277 * can frequently go between the resumed and paused states -- for example when
278 * the device goes to sleep, when an activity result is delivered, when a new
279 * intent is delivered -- so the code in these methods should be fairly
280 * lightweight.
281 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 * <p>The entire lifecycle of an activity is defined by the following
284 * Activity methods. All of these are hooks that you can override
285 * to do appropriate work when the activity changes state. All
286 * activities will implement {@link android.app.Activity#onCreate}
287 * to do their initial setup; many will also implement
288 * {@link android.app.Activity#onPause} to commit changes to data and
289 * otherwise prepare to stop interacting with the user. You should always
290 * call up to your superclass when implementing these methods.</p>
291 *
292 * </p>
293 * <pre class="prettyprint">
294 * public class Activity extends ApplicationContext {
295 * protected void onCreate(Bundle savedInstanceState);
296 *
297 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 * protected void onRestart();
300 *
301 * protected void onResume();
302 *
303 * protected void onPause();
304 *
305 * protected void onStop();
306 *
307 * protected void onDestroy();
308 * }
309 * </pre>
310 *
311 * <p>In general the movement through an activity's lifecycle looks like
312 * this:</p>
313 *
314 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
315 * <colgroup align="left" span="3" />
316 * <colgroup align="left" />
317 * <colgroup align="center" />
318 * <colgroup align="center" />
319 *
320 * <thead>
321 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
322 * </thead>
323 *
324 * <tbody>
smain@google.com10e29b82016-06-23 12:09:30 -0700325 * <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 -0800326 * <td>Called when the activity is first created.
327 * This is where you should do all of your normal static set up:
328 * create views, bind data to lists, etc. This method also
329 * provides you with a Bundle containing the activity's previously
330 * frozen state, if there was one.
331 * <p>Always followed by <code>onStart()</code>.</td>
332 * <td align="center">No</td>
333 * <td align="center"><code>onStart()</code></td>
334 * </tr>
335 *
336 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700337 * <td colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 * <td>Called after your activity has been stopped, prior to it being
339 * started again.
340 * <p>Always followed by <code>onStart()</code></td>
341 * <td align="center">No</td>
342 * <td align="center"><code>onStart()</code></td>
343 * </tr>
344 *
smain@google.com10e29b82016-06-23 12:09:30 -0700345 * <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 -0800346 * <td>Called when the activity is becoming visible to the user.
347 * <p>Followed by <code>onResume()</code> if the activity comes
348 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
349 * <td align="center">No</td>
350 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
351 * </tr>
352 *
353 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
smain@google.com10e29b82016-06-23 12:09:30 -0700354 * <td align="left" border="0">{@link android.app.Activity#onResume onResume()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * <td>Called when the activity will start
356 * interacting with the user. At this point your activity is at
357 * the top of the activity stack, with user input going to it.
358 * <p>Always followed by <code>onPause()</code>.</td>
359 * <td align="center">No</td>
360 * <td align="center"><code>onPause()</code></td>
361 * </tr>
362 *
smain@google.com10e29b82016-06-23 12:09:30 -0700363 * <tr><td align="left" border="0">{@link android.app.Activity#onPause onPause()}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * <td>Called when the system is about to start resuming a previous
365 * activity. This is typically used to commit unsaved changes to
366 * persistent data, stop animations and other things that may be consuming
367 * CPU, etc. Implementations of this method must be very quick because
368 * the next activity will not be resumed until this method returns.
369 * <p>Followed by either <code>onResume()</code> if the activity
370 * returns back to the front, or <code>onStop()</code> if it becomes
371 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800372 * <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 -0800373 * <td align="center"><code>onResume()</code> or<br>
374 * <code>onStop()</code></td>
375 * </tr>
376 *
smain@google.com10e29b82016-06-23 12:09:30 -0700377 * <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 -0800378 * <td>Called when the activity is no longer visible to the user, because
379 * another activity has been resumed and is covering this one. This
380 * may happen either because a new activity is being started, an existing
381 * one is being brought in front of this one, or this one is being
382 * destroyed.
383 * <p>Followed by either <code>onRestart()</code> if
384 * this activity is coming back to interact with the user, or
385 * <code>onDestroy()</code> if this activity is going away.</td>
386 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
387 * <td align="center"><code>onRestart()</code> or<br>
388 * <code>onDestroy()</code></td>
389 * </tr>
390 *
smain@google.com10e29b82016-06-23 12:09:30 -0700391 * <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 -0800392 * <td>The final call you receive before your
393 * activity is destroyed. This can happen either because the
394 * activity is finishing (someone called {@link Activity#finish} on
395 * it, or because the system is temporarily destroying this
396 * instance of the activity to save space. You can distinguish
397 * between these two scenarios with the {@link
398 * Activity#isFinishing} method.</td>
399 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
400 * <td align="center"><em>nothing</em></td>
401 * </tr>
402 * </tbody>
403 * </table>
404 *
405 * <p>Note the "Killable" column in the above table -- for those methods that
406 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800407 * 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 -0800408 * of its code being executed. Because of this, you should use the
409 * {@link #onPause} method to write any persistent data (such as user edits)
410 * to storage. In addition, the method
411 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
412 * in such a background state, allowing you to save away any dynamic instance
413 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700414 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
416 * section for more information on how the lifecycle of a process is tied
417 * to the activities it is hosting. Note that it is important to save
418 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800419 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 * be called in every situation as described in its documentation.</p>
421 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800422 * <p class="note">Be aware that these semantics will change slightly between
423 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
424 * vs. those targeting prior platforms. Starting with Honeycomb, an application
425 * is not in the killable state until its {@link #onStop} has returned. This
426 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
Joshua Baxter9a841a62018-03-27 14:42:03 -0700427 * safely called after {@link #onPause()}) and allows an application to safely
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800428 * wait until {@link #onStop()} to save persistent state.</p>
429 *
Andrii Kulian391161f2018-01-29 10:50:02 -0800430 * <p class="note">For applications targeting platforms starting with
431 * {@link android.os.Build.VERSION_CODES#P} {@link #onSaveInstanceState(Bundle)}
432 * will always be called after {@link #onStop}, so an application may safely
433 * perform fragment transactions in {@link #onStop} and will be able to save
434 * persistent state later.</p>
435 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * <p>For those methods that are not marked as being killable, the activity's
437 * process will not be killed by the system starting from the time the method
438 * is called and continuing after it returns. Thus an activity is in the killable
439 * state, for example, between after <code>onPause()</code> to the start of
440 * <code>onResume()</code>.</p>
441 *
442 * <a name="ConfigurationChanges"></a>
443 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 * <p>If the configuration of the device (as defined by the
446 * {@link Configuration Resources.Configuration} class) changes,
447 * then anything displaying a user interface will need to update to match that
448 * configuration. Because Activity is the primary mechanism for interacting
449 * with the user, it includes special support for handling configuration
450 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700451 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * <p>Unless you specify otherwise, a configuration change (such as a change
453 * in screen orientation, language, input devices, etc) will cause your
454 * current activity to be <em>destroyed</em>, going through the normal activity
455 * lifecycle process of {@link #onPause},
456 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
457 * had been in the foreground or visible to the user, once {@link #onDestroy} is
458 * called in that instance then a new instance of the activity will be
459 * created, with whatever savedInstanceState the previous instance had generated
460 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 * <p>This is done because any application resource,
463 * including layout files, can change based on any configuration value. Thus
464 * the only safe way to handle a configuration change is to re-retrieve all
465 * resources, including layouts, drawables, and strings. Because activities
466 * must already know how to save their state and re-create themselves from
467 * that state, this is a convenient way to have an activity restart itself
468 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 * <p>In some special cases, you may want to bypass restarting of your
471 * activity based on one or more types of configuration changes. This is
472 * done with the {@link android.R.attr#configChanges android:configChanges}
473 * attribute in its manifest. For any types of configuration changes you say
474 * that you handle there, you will receive a call to your current activity's
475 * {@link #onConfigurationChanged} method instead of being restarted. If
476 * a configuration change involves any that you do not handle, however, the
477 * activity will still be restarted and {@link #onConfigurationChanged}
478 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 * <a name="StartingActivities"></a>
481 * <h3>Starting Activities and Getting Results</h3>
482 *
483 * <p>The {@link android.app.Activity#startActivity}
484 * method is used to start a
485 * new activity, which will be placed at the top of the activity stack. It
486 * takes a single argument, an {@link android.content.Intent Intent},
487 * which describes the activity
488 * to be executed.</p>
489 *
490 * <p>Sometimes you want to get a result back from an activity when it
491 * ends. For example, you may start an activity that lets the user pick
492 * a person in a list of contacts; when it ends, it returns the person
493 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700494 * {@link android.app.Activity#startActivityForResult(Intent, int)}
495 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700497 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 *
499 * <p>When an activity exits, it can call
500 * {@link android.app.Activity#setResult(int)}
501 * to return data back to its parent. It must always supply a result code,
502 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
503 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
504 * return back an Intent containing any additional data it wants. All of this
505 * information appears back on the
506 * parent's <code>Activity.onActivityResult()</code>, along with the integer
507 * identifier it originally supplied.</p>
508 *
509 * <p>If a child activity fails for any reason (such as crashing), the parent
510 * activity will receive a result with the code RESULT_CANCELED.</p>
511 *
512 * <pre class="prettyprint">
513 * public class MyActivity extends Activity {
514 * ...
515 *
516 * static final int PICK_CONTACT_REQUEST = 0;
517 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700518 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
520 * // When the user center presses, let them pick a contact.
521 * startActivityForResult(
522 * new Intent(Intent.ACTION_PICK,
523 * new Uri("content://contacts")),
524 * PICK_CONTACT_REQUEST);
525 * return true;
526 * }
527 * return false;
528 * }
529 *
530 * protected void onActivityResult(int requestCode, int resultCode,
531 * Intent data) {
532 * if (requestCode == PICK_CONTACT_REQUEST) {
533 * if (resultCode == RESULT_OK) {
534 * // A contact was picked. Here we will just display it
535 * // to the user.
536 * startActivity(new Intent(Intent.ACTION_VIEW, data));
537 * }
538 * }
539 * }
540 * }
541 * </pre>
542 *
543 * <a name="SavingPersistentState"></a>
544 * <h3>Saving Persistent State</h3>
545 *
546 * <p>There are generally two kinds of persistent state than an activity
547 * will deal with: shared document-like data (typically stored in a SQLite
548 * database using a {@linkplain android.content.ContentProvider content provider})
549 * and internal state such as user preferences.</p>
550 *
551 * <p>For content provider data, we suggest that activities use a
552 * "edit in place" user model. That is, any edits a user makes are effectively
553 * made immediately without requiring an additional confirmation step.
554 * Supporting this model is generally a simple matter of following two rules:</p>
555 *
556 * <ul>
557 * <li> <p>When creating a new document, the backing database entry or file for
558 * it is created immediately. For example, if the user chooses to write
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700559 * 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 -0800560 * start entering data, so that if they go to any other activity after
Andrew Solovayab8ea9f2017-10-06 19:02:28 -0700561 * that point this email will now appear in the list of drafts.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
563 * commit to the backing content provider or file any changes the user
564 * has made. This ensures that those changes will be seen by any other
565 * activity that is about to run. You will probably want to commit
566 * your data even more aggressively at key times during your
567 * activity's lifecycle: for example before starting a new
568 * activity, before finishing your own activity, when the user
569 * switches between input fields, etc.</p>
570 * </ul>
571 *
572 * <p>This model is designed to prevent data loss when a user is navigating
573 * between activities, and allows the system to safely kill an activity (because
574 * system resources are needed somewhere else) at any time after it has been
575 * paused. Note this implies
576 * that the user pressing BACK from your activity does <em>not</em>
577 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800578 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
580 *
581 * <p>See the {@linkplain android.content.ContentProvider content package} for
582 * more information about content providers. These are a key aspect of how
583 * different activities invoke and propagate data between themselves.</p>
584 *
585 * <p>The Activity class also provides an API for managing internal persistent state
586 * associated with an activity. This can be used, for example, to remember
587 * the user's preferred initial display in a calendar (day view or week view)
588 * or the user's default home page in a web browser.</p>
589 *
590 * <p>Activity persistent state is managed
591 * with the method {@link #getPreferences},
592 * allowing you to retrieve and
593 * modify a set of name/value pairs associated with the activity. To use
594 * preferences that are shared across multiple application components
595 * (activities, receivers, services, providers), you can use the underlying
596 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
597 * to retrieve a preferences
598 * object stored under a specific name.
599 * (Note that it is not possible to share settings data across application
600 * packages -- for that you will need a content provider.)</p>
601 *
602 * <p>Here is an excerpt from a calendar activity that stores the user's
603 * preferred view mode in its persistent settings:</p>
604 *
605 * <pre class="prettyprint">
606 * public class CalendarActivity extends Activity {
607 * ...
608 *
609 * static final int DAY_VIEW_MODE = 0;
610 * static final int WEEK_VIEW_MODE = 1;
611 *
612 * private SharedPreferences mPrefs;
613 * private int mCurViewMode;
614 *
615 * protected void onCreate(Bundle savedInstanceState) {
616 * super.onCreate(savedInstanceState);
617 *
618 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700619 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * }
621 *
622 * protected void onPause() {
623 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * SharedPreferences.Editor ed = mPrefs.edit();
626 * ed.putInt("view_mode", mCurViewMode);
627 * ed.commit();
628 * }
629 * }
630 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * <a name="Permissions"></a>
633 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * <p>The ability to start a particular Activity can be enforced when it is
636 * declared in its
637 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
638 * tag. By doing so, other applications will need to declare a corresponding
639 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
640 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800641 *
642 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
643 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
644 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
645 * Activity access to the specific URIs in the Intent. Access will remain
646 * until the Activity has finished (it will remain across the hosting
647 * process being killed and other temporary destruction). As of
648 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
649 * was already created and a new Intent is being delivered to
650 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
651 * to the existing ones it holds.
652 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
654 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * <a name="ProcessLifecycle"></a>
657 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700658 *
kopriva4dddc632018-03-19 14:13:08 -0700659 * <p>The Android system attempts to keep an application process around for as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 * long as possible, but eventually will need to remove old processes when
kopriva4dddc632018-03-19 14:13:08 -0700661 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 * Lifecycle</a>, the decision about which process to remove is intimately
kopriva4dddc632018-03-19 14:13:08 -0700663 * tied to the state of the user's interaction with it. In general, there
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 * are four states a process can be in based on the activities running in it,
kopriva4dddc632018-03-19 14:13:08 -0700665 * listed here in order of importance. The system will kill less important
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 * processes (the last ones) before it resorts to killing more important
667 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700668 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 * <ol>
670 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
671 * that the user is currently interacting with) is considered the most important.
672 * Its process will only be killed as a last resort, if it uses more memory
673 * than is available on the device. Generally at this point the device has
674 * reached a memory paging state, so this is required in order to keep the user
675 * interface responsive.
676 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
677 * but not in the foreground, such as one sitting behind a foreground dialog)
678 * is considered extremely important and will not be killed unless that is
679 * required to keep the foreground activity running.
680 * <li> <p>A <b>background activity</b> (an activity that is not visible to
681 * the user and has been paused) is no longer critical, so the system may
682 * safely kill its process to reclaim memory for other foreground or
683 * visible processes. If its process needs to be killed, when the user navigates
684 * back to the activity (making it visible on the screen again), its
685 * {@link #onCreate} method will be called with the savedInstanceState it had previously
686 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
687 * state as the user last left it.
688 * <li> <p>An <b>empty process</b> is one hosting no activities or other
689 * application components (such as {@link Service} or
690 * {@link android.content.BroadcastReceiver} classes). These are killed very
691 * quickly by the system as memory becomes low. For this reason, any
692 * background operation you do outside of an activity must be executed in the
693 * context of an activity BroadcastReceiver or Service to ensure that the system
694 * knows it needs to keep your process around.
695 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 * <p>Sometimes an Activity may need to do a long-running operation that exists
698 * independently of the activity lifecycle itself. An example may be a camera
699 * application that allows you to upload a picture to a web site. The upload
700 * may take a long time, and the application should allow the user to leave
Trevor Johns682c24e2016-04-12 10:13:47 -0700701 * the application while it is executing. To accomplish this, your Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 * should start a {@link Service} in which the upload takes place. This allows
703 * the system to properly prioritize your process (considering it to be more
704 * important than other non-visible applications) for the duration of the
705 * upload, independent of whether the original activity is paused, stopped,
706 * or finished.
707 */
708public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700709 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700711 OnCreateContextMenuListener, ComponentCallbacks2,
Svet Ganov782043c2017-02-11 00:52:02 +0000712 Window.OnWindowDismissedCallback, WindowControllerCallback,
Felipe Leme640f30a2017-03-06 15:44:06 -0800713 AutofillManager.AutofillClient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700715 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
717 /** Standard activity result: operation canceled. */
718 public static final int RESULT_CANCELED = 0;
719 /** Standard activity result: operation succeeded. */
720 public static final int RESULT_OK = -1;
721 /** Start of user-defined activity results. */
722 public static final int RESULT_FIRST_USER = 1;
723
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700724 /** @hide Task isn't finished when activity is finished */
725 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700726 /**
727 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
728 * past behavior the task is also removed from recents.
729 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700730 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700731 /**
732 * @hide Task is finished along with the finishing activity, but it is not removed from
733 * recents.
734 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700735 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
736
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100737 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700738 static final String FRAGMENTS_TAG = "android:fragments";
Phil Weaver846cda932017-06-15 10:10:06 -0700739 private static final String LAST_AUTOFILL_ID = "android:lastAutofillId";
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700740
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700741 private static final String AUTOFILL_RESET_NEEDED = "@android:autofillResetNeeded";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
743 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
744 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
745 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800746 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700747 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
748 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749
Svetoslav970b59c2015-06-09 16:05:21 -0700750 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
Svet Ganov782043c2017-02-11 00:52:02 +0000751 private static final String AUTO_FILL_AUTH_WHO_PREFIX = "@android:autoFillAuth:";
Svetoslav970b59c2015-06-09 16:05:21 -0700752
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100753 private static final String KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME = "com.android.systemui";
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +0100754
Andrii Kuliand25680c2018-02-21 15:16:58 -0800755 private static final int LOG_AM_ON_CREATE_CALLED = 30057;
756 private static final int LOG_AM_ON_START_CALLED = 30059;
757 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
758 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
759 private static final int LOG_AM_ON_STOP_CALLED = 30049;
760 private static final int LOG_AM_ON_RESTART_CALLED = 30058;
761 private static final int LOG_AM_ON_DESTROY_CALLED = 30060;
Andrii Kuliane55b0092018-04-19 15:29:22 -0700762 private static final int LOG_AM_ON_ACTIVITY_RESULT_CALLED = 30062;
Andrii Kuliand25680c2018-02-21 15:16:58 -0800763
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800764 private static class ManagedDialog {
765 Dialog mDialog;
766 Bundle mArgs;
767 }
768 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
770 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100771 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 private Instrumentation mInstrumentation;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100773 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 private IBinder mToken;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100775 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700776 private int mIdent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100777 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 /*package*/ String mEmbeddedID;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100779 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 private Application mApplication;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100781 @UnsupportedAppUsage
Christopher Tateb70f3df2009-04-07 16:07:59 -0700782 /*package*/ Intent mIntent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100783 @UnsupportedAppUsage
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800784 /*package*/ String mReferrer;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100785 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 private ComponentName mComponent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100787 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 /*package*/ ActivityInfo mActivityInfo;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100789 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 /*package*/ ActivityThread mMainThread;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100791 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 Activity mParent;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100793 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 boolean mCalled;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100795 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -0600796 /*package*/ boolean mResumed;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100797 @UnsupportedAppUsage
Andrii Kulian58178f22016-03-16 13:44:56 -0700798 /*package*/ boolean mStopped;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100799 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 boolean mFinished;
801 boolean mStartedActivity;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100802 @UnsupportedAppUsage
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700803 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700804 private boolean mDoReportFullyDrawn = true;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200805 private boolean mRestoredFromBundle;
Winson Chung298f95b2017-08-10 15:57:18 -0700806
807 /** {@code true} if the activity lifecycle is in a state which supports picture-in-picture.
808 * This only affects the client-side exception, the actual state check still happens in AMS. */
809 private boolean mCanEnterPictureInPicture = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700810 /** true if the activity is going through a transient pause */
811 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500812 /** true if the activity is being destroyed in order to recreate it with a new configuration */
813 /*package*/ boolean mChangingConfigurations = false;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100814 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 /*package*/ int mConfigChangeFlags;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100816 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100818 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700819 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700821 /** The autofill manager. Always access via {@link #getAutofillManager()}. */
822 @Nullable private AutofillManager mAutofillManager;
823
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700824 static final class NonConfigurationInstances {
825 Object activity;
826 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800827 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700828 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700829 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700830 }
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100831 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700832 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700833
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100834 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 private Window mWindow;
836
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100837 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 private WindowManager mWindowManager;
839 /*package*/ View mDecor = null;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100840 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 /*package*/ boolean mWindowAdded = false;
842 /*package*/ boolean mVisibleFromServer = false;
Mathew Inwood55418ea2018-12-20 15:30:45 +0000843 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700845 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700846 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
Mathew Inwood45d2c252018-09-14 12:35:36 +0100848 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn91097de2014-04-04 18:02:06 -0700849 private VoiceInteractor mVoiceInteractor;
850
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100851 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 private CharSequence mTitle;
853 private int mTitleColor = 0;
854
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700855 // we must have a handler before the FragmentController is constructed
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100856 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700857 final Handler mHandler = new Handler();
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100858 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700859 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 private static final class ManagedCursor {
862 ManagedCursor(Cursor cursor) {
863 mCursor = cursor;
864 mReleased = false;
865 mUpdated = false;
866 }
867
868 private final Cursor mCursor;
869 private boolean mReleased;
870 private boolean mUpdated;
871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700873 @GuardedBy("mManagedCursors")
874 private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<>();
875
876 @GuardedBy("this")
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100877 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 int mResultCode = RESULT_CANCELED;
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700879 @GuardedBy("this")
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100880 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700882
Craig Mautner5eda9b32013-07-02 11:58:16 -0700883 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700884 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700886 private SearchEvent mSearchEvent;
887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000889 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890
891 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
892 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700893
Winsonb6403152016-02-23 13:32:09 -0800894 private ActivityManager.TaskDescription mTaskDescription =
895 new ActivityManager.TaskDescription();
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
898
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700899 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700900 private final Object mInstanceTracker = StrictMode.trackActivity(this);
901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700903
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100904 @UnsupportedAppUsage
George Mount1fecfb22014-06-18 14:55:55 -0700905 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700906 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
907 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908
Svetoslavffb32b12015-10-15 16:54:00 -0700909 private boolean mHasCurrentPermissionsRequest;
910
Svet Ganov17db9dc2017-02-21 19:54:31 -0800911 private boolean mAutoFillResetNeeded;
Dake Gu67decfa2017-12-27 11:48:08 -0800912 private boolean mAutoFillIgnoreFirstResumePause;
Svet Ganov17db9dc2017-02-21 19:54:31 -0800913
Phil Weaver846cda932017-06-15 10:10:06 -0700914 /** The last autofill id that was returned from {@link #getNextAutofillId()} */
915 private int mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700916
Felipe Leme4753bb02017-03-22 20:24:00 -0700917 private AutofillPopupWindow mAutofillPopupWindow;
918
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800919 private static native String getDlWarning();
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 /** Return the intent that started this activity. */
922 public Intent getIntent() {
923 return mIntent;
924 }
925
RoboErik55011652014-07-09 15:05:53 -0700926 /**
927 * Change the intent returned by {@link #getIntent}. This holds a
928 * reference to the given intent; it does not copy it. Often used in
929 * conjunction with {@link #onNewIntent}.
930 *
931 * @param newIntent The new Intent object to return from getIntent
932 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 * @see #getIntent
934 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700935 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 public void setIntent(Intent newIntent) {
937 mIntent = newIntent;
938 }
939
940 /** Return the application that owns this activity. */
941 public final Application getApplication() {
942 return mApplication;
943 }
944
945 /** Is this activity embedded inside of another activity? */
946 public final boolean isChild() {
947 return mParent != null;
948 }
RoboErik55011652014-07-09 15:05:53 -0700949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 /** Return the parent activity if this view is an embedded child. */
951 public final Activity getParent() {
952 return mParent;
953 }
954
955 /** Retrieve the window manager for showing custom windows. */
956 public WindowManager getWindowManager() {
957 return mWindowManager;
958 }
959
960 /**
961 * Retrieve the current {@link android.view.Window} for the activity.
962 * This can be used to directly access parts of the Window API that
963 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700964 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 * @return Window The current window, or null if the activity is not
966 * visual.
967 */
968 public Window getWindow() {
969 return mWindow;
970 }
971
972 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800973 * Return the LoaderManager for this activity, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -0800974 *
975 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -0700976 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800977 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -0700978 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700979 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700980 }
RoboErik55011652014-07-09 15:05:53 -0700981
Dianne Hackbornc8017682010-07-06 13:34:38 -0700982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 * Calls {@link android.view.Window#getCurrentFocus} on the
984 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700985 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700987 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 * @see #getWindow
989 * @see android.view.Window#getCurrentFocus
990 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700991 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 public View getCurrentFocus() {
993 return mWindow != null ? mWindow.getCurrentFocus() : null;
994 }
995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 /**
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700997 * (Create and) return the autofill manager
998 *
999 * @return The autofill manager
1000 */
1001 @NonNull private AutofillManager getAutofillManager() {
1002 if (mAutofillManager == null) {
1003 mAutofillManager = getSystemService(AutofillManager.class);
1004 }
1005
1006 return mAutofillManager;
1007 }
1008
Felipe Lemebb567ae2017-10-04 09:56:21 -07001009 @Override
1010 protected void attachBaseContext(Context newBase) {
1011 super.attachBaseContext(newBase);
Felipe Lemed247de82018-05-14 17:51:58 -07001012 if (newBase != null) {
1013 newBase.setAutofillClient(this);
1014 }
Felipe Lemebb567ae2017-10-04 09:56:21 -07001015 }
1016
1017 /** @hide */
1018 @Override
1019 public final AutofillClient getAutofillClient() {
1020 return this;
1021 }
1022
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * Called when the activity is starting. This is where most initialization
1025 * should go: calling {@link #setContentView(int)} to inflate the
1026 * activity's UI, using {@link #findViewById} to programmatically interact
1027 * with widgets in the UI, calling
1028 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
1029 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -07001030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 * <p>You can call {@link #finish} from within this function, in
Bryce Lee476edc82018-03-12 10:06:23 -07001032 * which case onDestroy() will be immediately called after {@link #onCreate} without any of the
1033 * rest of the activity lifecycle ({@link #onStart}, {@link #onResume}, {@link #onPause}, etc)
1034 * executing.
RoboErik55011652014-07-09 15:05:53 -07001035 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 * <p><em>Derived classes must call through to the super class's
1037 * implementation of this method. If they do not, an exception will be
1038 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001039 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 * @param savedInstanceState If the activity is being re-initialized after
1041 * previously being shut down then this Bundle contains the data it most
1042 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -07001043 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 * @see #onStart
1045 * @see #onSaveInstanceState
1046 * @see #onRestoreInstanceState
1047 * @see #onPostCreate
1048 */
Tor Norbye83c68962015-03-10 20:55:31 -07001049 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -08001050 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001051 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001052 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Bryce Lee39791592017-04-26 09:29:12 -07001053
Dianne Hackborn2707d602010-07-09 18:01:20 -07001054 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001055 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001056 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001057 if (mActivityInfo.parentActivityName != null) {
1058 if (mActionBar == null) {
1059 mEnableDefaultActionBarUp = true;
1060 } else {
1061 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
1062 }
1063 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001064 if (savedInstanceState != null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001065 mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);
Philip P. Moltmanne78c7712017-06-19 12:57:13 -07001066 mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,
1067 View.LAST_APP_AUTOFILL_ID);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001068
1069 if (mAutoFillResetNeeded) {
1070 getAutofillManager().onCreate(savedInstanceState);
1071 }
1072
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001073 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
1074 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
1075 ? mLastNonConfigurationInstances.fragments : null);
1076 }
1077 mFragments.dispatchCreate();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001078 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -07001079 if (mVoiceInteractor != null) {
1080 mVoiceInteractor.attachActivity(this);
1081 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001082 mRestoredFromBundle = savedInstanceState != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 mCalled = true;
1084 }
1085
1086 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001087 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -07001088 * the attribute {@link android.R.attr#persistableMode} set to
1089 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001090 *
1091 * @param savedInstanceState if the activity is being re-initialized after
1092 * previously being shut down then this Bundle contains the data it most
1093 * recently supplied in {@link #onSaveInstanceState}.
1094 * <b><i>Note: Otherwise it is null.</i></b>
1095 * @param persistentState if the activity is being re-initialized after
1096 * previously being shut down or powered off then this Bundle contains the data it most
1097 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
1098 * <b><i>Note: Otherwise it is null.</i></b>
1099 *
1100 * @see #onCreate(android.os.Bundle)
1101 * @see #onStart
1102 * @see #onSaveInstanceState
1103 * @see #onRestoreInstanceState
1104 * @see #onPostCreate
1105 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001106 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001107 @Nullable PersistableBundle persistentState) {
1108 onCreate(savedInstanceState);
1109 }
1110
1111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 * The hook for {@link ActivityThread} to restore the state of this activity.
1113 *
1114 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1115 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1116 *
1117 * @param savedInstanceState contains the saved state
1118 */
1119 final void performRestoreInstanceState(Bundle savedInstanceState) {
1120 onRestoreInstanceState(savedInstanceState);
1121 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123
1124 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001125 * The hook for {@link ActivityThread} to restore the state of this activity.
1126 *
1127 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1128 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1129 *
1130 * @param savedInstanceState contains the saved state
1131 * @param persistentState contains the persistable saved state
1132 */
1133 final void performRestoreInstanceState(Bundle savedInstanceState,
1134 PersistableBundle persistentState) {
1135 onRestoreInstanceState(savedInstanceState, persistentState);
1136 if (savedInstanceState != null) {
1137 restoreManagedDialogs(savedInstanceState);
1138 }
1139 }
1140
1141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 * This method is called after {@link #onStart} when the activity is
1143 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001144 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 * to restore their state, but it is sometimes convenient to do it here
1146 * after all of the initialization has been done or to allow subclasses to
1147 * decide whether to use your default implementation. The default
1148 * implementation of this method performs a restore of any view state that
1149 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 * <p>This method is called between {@link #onStart} and
1152 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 * @see #onCreate
1157 * @see #onPostCreate
1158 * @see #onResume
1159 * @see #onSaveInstanceState
1160 */
1161 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1162 if (mWindow != null) {
1163 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1164 if (windowState != null) {
1165 mWindow.restoreHierarchyState(windowState);
1166 }
1167 }
1168 }
Craig Mautnera0026042014-04-23 11:45:37 -07001169
1170 /**
1171 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001172 * created with the attribute {@link android.R.attr#persistableMode} set to
1173 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1174 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001175 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1176 *
1177 * <p>This method is called between {@link #onStart} and
1178 * {@link #onPostCreate}.
1179 *
1180 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1181 *
1182 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1183 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1184 *
1185 * @see #onRestoreInstanceState(Bundle)
1186 * @see #onCreate
1187 * @see #onPostCreate
1188 * @see #onResume
1189 * @see #onSaveInstanceState
1190 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001191 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001192 PersistableBundle persistentState) {
1193 if (savedInstanceState != null) {
1194 onRestoreInstanceState(savedInstanceState);
1195 }
1196 }
1197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Restore the state of any saved managed dialogs.
1200 *
1201 * @param savedInstanceState The bundle to restore from.
1202 */
1203 private void restoreManagedDialogs(Bundle savedInstanceState) {
1204 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1205 if (b == null) {
1206 return;
1207 }
1208
1209 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1210 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001211 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 for (int i = 0; i < numDialogs; i++) {
1213 final Integer dialogId = ids[i];
1214 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1215 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001216 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1217 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001218 final ManagedDialog md = new ManagedDialog();
1219 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1220 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1221 if (md.mDialog != null) {
1222 mManagedDialogs.put(dialogId, md);
1223 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1224 md.mDialog.onRestoreInstanceState(dialogState);
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
1228 }
1229
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001230 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1231 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001232 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001233 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001234 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001235 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001236 return dialog;
1237 }
1238
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001239 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 return SAVED_DIALOG_KEY_PREFIX + key;
1241 }
1242
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001243 private static String savedDialogArgsKeyFor(int key) {
1244 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246
1247 /**
1248 * Called when activity start-up is complete (after {@link #onStart}
1249 * and {@link #onRestoreInstanceState} have been called). Applications will
1250 * generally not implement this method; it is intended for system
1251 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 * <p><em>Derived classes must call through to the super class's
1254 * implementation of this method. If they do not, an exception will be
1255 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 * @param savedInstanceState If the activity is being re-initialized after
1258 * previously being shut down then this Bundle contains the data it most
1259 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1260 * @see #onCreate
1261 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001262 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001263 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 if (!isChild()) {
1265 mTitleReady = true;
1266 onTitleChanged(getTitle(), getTitleColor());
1267 }
Winsonb6403152016-02-23 13:32:09 -08001268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 mCalled = true;
1270 }
1271
1272 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001273 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001274 * created with the attribute {@link android.R.attr#persistableMode} set to
1275 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001276 *
1277 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1278 * @param persistentState The data caming from the PersistableBundle first
1279 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1280 *
1281 * @see #onCreate
1282 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001283 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001284 @Nullable PersistableBundle persistentState) {
1285 onPostCreate(savedInstanceState);
1286 }
1287
1288 /**
RoboErik55011652014-07-09 15:05:53 -07001289 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1290 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001291 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 *
1293 * <p><em>Derived classes must call through to the super class's
1294 * implementation of this method. If they do not, an exception will be
1295 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 * @see #onCreate
1298 * @see #onStop
1299 * @see #onResume
1300 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001301 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001303 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001305
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001306 mFragments.doLoaderStart();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001307
1308 getApplication().dispatchActivityStarted(this);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001309
1310 if (mAutoFillResetNeeded) {
Dake Gu67decfa2017-12-27 11:48:08 -08001311 getAutofillManager().onVisibleForAutofill();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
1314
1315 /**
1316 * Called after {@link #onStop} when the current activity is being
1317 * re-displayed to the user (the user has navigated back to it). It will
1318 * be followed by {@link #onStart} and then {@link #onResume}.
1319 *
1320 * <p>For activities that are using raw {@link Cursor} objects (instead of
1321 * creating them through
1322 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1323 * this is usually the place
1324 * where the cursor should be requeried (because you had deactivated it in
1325 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 * <p><em>Derived classes must call through to the super class's
1328 * implementation of this method. If they do not, an exception will be
1329 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 * @see #onStop
1332 * @see #onStart
1333 * @see #onResume
1334 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001335 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 protected void onRestart() {
1337 mCalled = true;
1338 }
1339
1340 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001341 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1342 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1343 * to give the activity a hint that its state is no longer saved -- it will generally
1344 * be called after {@link #onSaveInstanceState} and prior to the activity being
1345 * resumed/started again.
1346 */
1347 public void onStateNotSaved() {
1348 }
1349
1350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1352 * {@link #onPause}, for your activity to start interacting with the user.
1353 * This is a good place to begin animations, open exclusive-access devices
1354 * (such as the camera), etc.
1355 *
1356 * <p>Keep in mind that onResume is not the best indicator that your activity
1357 * is visible to the user; a system window such as the keyguard may be in
1358 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1359 * activity is visible to the user (for example, to resume a game).
1360 *
1361 * <p><em>Derived classes must call through to the super class's
1362 * implementation of this method. If they do not, an exception will be
1363 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 * @see #onRestoreInstanceState
1366 * @see #onRestart
1367 * @see #onPostResume
1368 * @see #onPause
1369 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001370 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001372 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001373 getApplication().dispatchActivityResumed(this);
George Mountbdc4d8d2016-03-23 13:10:13 -07001374 mActivityTransitionState.onResume(this, isTopOfTask());
Dake Gu67decfa2017-12-27 11:48:08 -08001375 if (mAutoFillResetNeeded) {
1376 if (!mAutoFillIgnoreFirstResumePause) {
1377 View focus = getCurrentFocus();
1378 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
1379 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1380 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1381 // window visibility after recreation is INVISIBLE in onResume() and next frame
1382 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1383 // So we cannot call View.notifyEnterOrExited() which will do nothing
1384 // when View.isVisibleToUser() is false.
1385 getAutofillManager().notifyViewEntered(focus);
1386 }
1387 }
1388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 mCalled = true;
1390 }
1391
1392 /**
1393 * Called when activity resume is complete (after {@link #onResume} has
1394 * been called). Applications will generally not implement this method;
1395 * it is intended for system classes to do final setup after application
1396 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001397 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 * <p><em>Derived classes must call through to the super class's
1399 * implementation of this method. If they do not, an exception will be
1400 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 * @see #onResume
1403 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001404 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 protected void onPostResume() {
1406 final Window win = getWindow();
1407 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001408 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 mCalled = true;
1410 }
1411
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001412 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001413 if (mVoiceInteractor != null) {
1414 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1415 activeRequest.cancel();
1416 activeRequest.clear();
1417 }
1418 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001419 if (voiceInteractor == null) {
1420 mVoiceInteractor = null;
1421 } else {
1422 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1423 Looper.myLooper());
1424 }
1425 }
1426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001428 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001429 *
Phil Weaver846cda932017-06-15 10:10:06 -07001430 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001431 * will be unique.
1432 *
1433 * @return A ID that is unique in the activity
1434 *
1435 * {@hide}
1436 */
Felipe Leme42b97932018-02-20 13:04:31 -08001437 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001438 public int getNextAutofillId() {
1439 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1440 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001441 }
1442
Phil Weaver846cda932017-06-15 10:10:06 -07001443 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001444
Phil Weaver846cda932017-06-15 10:10:06 -07001445 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001446 }
1447
1448 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001449 * @hide
1450 */
1451 @Override
1452 public AutofillId autofillClientGetNextAutofillId() {
1453 return new AutofillId(getNextAutofillId());
1454 }
1455
1456 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001457 * Check whether this activity is running as part of a voice interaction with the user.
1458 * If true, it should perform its interaction with the user through the
1459 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1460 */
1461 public boolean isVoiceInteraction() {
1462 return mVoiceInteractor != null;
1463 }
1464
1465 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001466 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1467 * of a voice interaction. That is, returns true if this activity was directly
1468 * started by the voice interaction service as the initiation of a voice interaction.
1469 * Otherwise, for example if it was started by another activity while under voice
1470 * interaction, returns false.
1471 */
1472 public boolean isVoiceInteractionRoot() {
1473 try {
1474 return mVoiceInteractor != null
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001475 && ActivityManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001476 } catch (RemoteException e) {
1477 }
1478 return false;
1479 }
1480
1481 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001482 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1483 * interact with this activity.
1484 */
1485 public VoiceInteractor getVoiceInteractor() {
1486 return mVoiceInteractor;
1487 }
1488
1489 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001490 * Queries whether the currently enabled voice interaction service supports returning
1491 * a voice interactor for use by the activity. This is valid only for the duration of the
1492 * activity.
1493 *
1494 * @return whether the current voice interaction service supports local voice interaction
1495 */
1496 public boolean isLocalVoiceInteractionSupported() {
1497 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001498 return ActivityManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001499 } catch (RemoteException re) {
1500 }
1501 return false;
1502 }
1503
1504 /**
1505 * Starts a local voice interaction session. When ready,
1506 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1507 * to the registered voice interaction service.
1508 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1509 */
1510 public void startLocalVoiceInteraction(Bundle privateOptions) {
1511 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001512 ActivityManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001513 } catch (RemoteException re) {
1514 }
1515 }
1516
1517 /**
1518 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1519 * voice interaction session being started. You can now retrieve a voice interactor using
1520 * {@link #getVoiceInteractor()}.
1521 */
1522 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001523 }
1524
1525 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001526 * Callback to indicate that the local voice interaction has stopped either
1527 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1528 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1529 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001530 */
1531 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001532 }
1533
1534 /**
1535 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001536 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1537 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001538 */
1539 public void stopLocalVoiceInteraction() {
1540 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001541 ActivityManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001542 } catch (RemoteException re) {
1543 }
1544 }
1545
1546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 * This is called for activities that set launchMode to "singleTop" in
1548 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1549 * flag when calling {@link #startActivity}. In either case, when the
1550 * activity is re-launched while at the top of the activity stack instead
1551 * of a new instance of the activity being started, onNewIntent() will be
1552 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001553 * re-launch it.
1554 *
1555 * <p>An activity will always be paused before receiving a new intent, so
1556 * you can count on {@link #onResume} being called after this method.
1557 *
1558 * <p>Note that {@link #getIntent} still returns the original Intent. You
1559 * can use {@link #setIntent} to update it to this new Intent.
1560 *
1561 * @param intent The new intent that was started for the activity.
1562 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001564 * @see #setIntent
1565 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 */
1567 protected void onNewIntent(Intent intent) {
1568 }
1569
1570 /**
1571 * The hook for {@link ActivityThread} to save the state of this activity.
1572 *
1573 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1574 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1575 *
1576 * @param outState The bundle to save the state to.
1577 */
1578 final void performSaveInstanceState(Bundle outState) {
1579 onSaveInstanceState(outState);
1580 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001581 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001582 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001583 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 }
1585
1586 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001587 * The hook for {@link ActivityThread} to save the state of this activity.
1588 *
1589 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1590 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1591 *
1592 * @param outState The bundle to save the state to.
1593 * @param outPersistentState The bundle to save persistent state to.
1594 */
1595 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1596 onSaveInstanceState(outState, outPersistentState);
1597 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001598 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001599 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1600 ", " + outPersistentState);
1601 }
1602
1603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 * Called to retrieve per-instance state from an activity before being killed
1605 * so that the state can be restored in {@link #onCreate} or
1606 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1607 * will be passed to both).
1608 *
1609 * <p>This method is called before an activity may be killed so that when it
1610 * comes back some time in the future it can restore its state. For example,
1611 * if activity B is launched in front of activity A, and at some point activity
1612 * A is killed to reclaim resources, activity A will have a chance to save the
1613 * current state of its user interface via this method so that when the user
1614 * returns to activity A, the state of the user interface can be restored
1615 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1616 *
1617 * <p>Do not confuse this method with activity lifecycle callbacks such as
1618 * {@link #onPause}, which is always called when an activity is being placed
1619 * in the background or on its way to destruction, or {@link #onStop} which
1620 * is called before destruction. One example of when {@link #onPause} and
1621 * {@link #onStop} is called and not this method is when a user navigates back
1622 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1623 * on B because that particular instance will never be restored, so the
1624 * system avoids calling it. An example when {@link #onPause} is called and
1625 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1626 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1627 * killed during the lifetime of B since the state of the user interface of
1628 * A will stay intact.
1629 *
1630 * <p>The default implementation takes care of most of the UI per-instance
1631 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1632 * view in the hierarchy that has an id, and by saving the id of the currently
1633 * focused view (all of which is restored by the default implementation of
1634 * {@link #onRestoreInstanceState}). If you override this method to save additional
1635 * information not captured by each individual view, you will likely want to
1636 * call through to the default implementation, otherwise be prepared to save
1637 * all of the state of each view yourself.
1638 *
Andrii Kulian391161f2018-01-29 10:50:02 -08001639 * <p>If called, this method will occur after {@link #onStop} for applications
1640 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
1641 * For applications targeting earlier platform versions this method will occur
1642 * before {@link #onStop} and there are no guarantees about whether it will
1643 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 * @see #onCreate
1648 * @see #onRestoreInstanceState
1649 * @see #onPause
1650 */
1651 protected void onSaveInstanceState(Bundle outState) {
1652 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001653
Phil Weaver846cda932017-06-15 10:10:06 -07001654 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001655 Parcelable p = mFragments.saveAllState();
1656 if (p != null) {
1657 outState.putParcelable(FRAGMENTS_TAG, p);
1658 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001659 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001660 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
1661 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001662 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00001663 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
1665
1666 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001667 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001668 * created with the attribute {@link android.R.attr#persistableMode} set to
1669 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1670 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1671 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001672 *
1673 * @param outState Bundle in which to place your saved state.
1674 * @param outPersistentState State which will be saved across reboots.
1675 *
1676 * @see #onSaveInstanceState(Bundle)
1677 * @see #onCreate
1678 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1679 * @see #onPause
1680 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001681 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001682 onSaveInstanceState(outState);
1683 }
1684
1685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 * Save the state of any managed dialogs.
1687 *
1688 * @param outState place to store the saved state.
1689 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001690 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 private void saveManagedDialogs(Bundle outState) {
1692 if (mManagedDialogs == null) {
1693 return;
1694 }
1695
1696 final int numDialogs = mManagedDialogs.size();
1697 if (numDialogs == 0) {
1698 return;
1699 }
1700
1701 Bundle dialogState = new Bundle();
1702
1703 int[] ids = new int[mManagedDialogs.size()];
1704
1705 // save each dialog's bundle, gather the ids
1706 for (int i = 0; i < numDialogs; i++) {
1707 final int key = mManagedDialogs.keyAt(i);
1708 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001709 final ManagedDialog md = mManagedDialogs.valueAt(i);
1710 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1711 if (md.mArgs != null) {
1712 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
1715
1716 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1717 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1718 }
1719
1720
1721 /**
1722 * Called as part of the activity lifecycle when an activity is going into
1723 * the background, but has not (yet) been killed. The counterpart to
1724 * {@link #onResume}.
1725 *
1726 * <p>When activity B is launched in front of activity A, this callback will
1727 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1728 * so be sure to not do anything lengthy here.
1729 *
1730 * <p>This callback is mostly used for saving any persistent state the
1731 * activity is editing, to present a "edit in place" model to the user and
1732 * making sure nothing is lost if there are not enough resources to start
1733 * the new activity without first killing this one. This is also a good
1734 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001735 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 * as fast as possible, or to close resources that are exclusive access
1737 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001738 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 * <p>In situations where the system needs more memory it may kill paused
1740 * processes to reclaim resources. Because of this, you should be sure
1741 * that all of your state is saved by the time you return from
1742 * this function. In general {@link #onSaveInstanceState} is used to save
1743 * per-instance state in the activity and this method is used to store
1744 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001745 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 * <p>After receiving this call you will usually receive a following call
1747 * to {@link #onStop} (after the next activity has been resumed and
1748 * displayed), however in some cases there will be a direct call back to
1749 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 * <p><em>Derived classes must call through to the super class's
1752 * implementation of this method. If they do not, an exception will be
1753 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 * @see #onResume
1756 * @see #onSaveInstanceState
1757 * @see #onStop
1758 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001759 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001761 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001762 getApplication().dispatchActivityPaused(this);
Dake Gu67decfa2017-12-27 11:48:08 -08001763 if (mAutoFillResetNeeded) {
1764 if (!mAutoFillIgnoreFirstResumePause) {
1765 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
1766 View focus = getCurrentFocus();
1767 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
1768 getAutofillManager().notifyViewExited(focus);
1769 }
1770 } else {
1771 // reset after first pause()
1772 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
1773 mAutoFillIgnoreFirstResumePause = false;
1774 }
1775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 mCalled = true;
1777 }
1778
1779 /**
1780 * Called as part of the activity lifecycle when an activity is about to go
1781 * into the background as the result of user choice. For example, when the
1782 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1783 * when an incoming phone call causes the in-call Activity to be automatically
1784 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1785 * the activity being interrupted. In cases when it is invoked, this method
1786 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 * <p>This callback and {@link #onUserInteraction} are intended to help
1789 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001790 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07001791 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 * @see #onUserInteraction()
1793 */
1794 protected void onUserLeaveHint() {
1795 }
RoboErik55011652014-07-09 15:05:53 -07001796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001798 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001800 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001802 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804
1805 /**
1806 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001807 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001809 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 * <p>The default implementation returns null, which will cause you to
1811 * inherit the description from the previous activity. If all activities
1812 * return null, generally the label of the top activity will be used as the
1813 * description.
RoboErik55011652014-07-09 15:05:53 -07001814 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 * @return A description of what the user is doing. It should be short and
1816 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001819 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001821 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 public CharSequence onCreateDescription() {
1823 return null;
1824 }
1825
1826 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001827 * This is called when the user is requesting an assist, to build a full
1828 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1829 * application. You can override this method to place into the bundle anything
1830 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001831 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001832 *
1833 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001834 * been registered with {@link Application#registerOnProvideAssistDataListener
1835 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001836 */
1837 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001838 }
1839
1840 /**
1841 * This is called when the user is requesting an assist, to provide references
1842 * to content related to the current activity. Before being called, the
1843 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1844 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1845 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1846 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1847 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1848 *
1849 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1850 * context of the activity, and fill in its ClipData with additional content of
1851 * interest that the user is currently viewing. For example, an image gallery application
1852 * that has launched in to an activity allowing the user to swipe through pictures should
1853 * modify the intent to reference the current image they are looking it; such an
1854 * application when showing a list of pictures should add a ClipData that has
1855 * references to all of the pictures currently visible on screen.</p>
1856 *
1857 * @param outContent The assist content to return.
1858 */
1859 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001860 }
1861
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001862 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01001863 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001864 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
1865 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01001866 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001867 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001868 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1869 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01001870 }
1871
1872 /**
1873 * Dismiss the Keyboard Shortcuts screen.
1874 */
1875 public final void dismissKeyboardShortcutsHelper() {
1876 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001877 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1878 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001879 }
1880
Clara Bayarri75e09792015-07-29 16:20:40 +01001881 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001882 public void onProvideKeyboardShortcuts(
1883 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01001884 if (menu == null) {
1885 return;
1886 }
1887 KeyboardShortcutGroup group = null;
1888 int menuSize = menu.size();
1889 for (int i = 0; i < menuSize; ++i) {
1890 final MenuItem item = menu.getItem(i);
1891 final CharSequence title = item.getTitle();
1892 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01001893 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01001894 if (title != null && alphaShortcut != MIN_VALUE) {
1895 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001896 final int resource = mApplication.getApplicationInfo().labelRes;
1897 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001898 }
1899 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01001900 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01001901 }
1902 }
1903 if (group != null) {
1904 data.add(group);
1905 }
1906 }
1907
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001908 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001909 * Ask to have the current assistant shown to the user. This only works if the calling
1910 * activity is the current foreground activity. It is the same as calling
1911 * {@link android.service.voice.VoiceInteractionService#showSession
1912 * VoiceInteractionService.showSession} and requesting all of the possible context.
1913 * The receiver will always see
1914 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1915 * @return Returns true if the assistant was successfully invoked, else false. For example
1916 * false will be returned if the caller is not the current top activity.
1917 */
1918 public boolean showAssist(Bundle args) {
1919 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001920 return ActivityManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07001921 } catch (RemoteException e) {
1922 }
1923 return false;
1924 }
1925
1926 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 * Called when you are no longer visible to the user. You will next
1928 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1929 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001930 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 * <p><em>Derived classes must call through to the super class's
1932 * implementation of this method. If they do not, an exception will be
1933 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001934 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 * @see #onRestart
1936 * @see #onResume
1937 * @see #onSaveInstanceState
1938 * @see #onDestroy
1939 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001940 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001942 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001943 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001944 mActivityTransitionState.onStop();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001945 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001946 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07001948
Dake Gu67decfa2017-12-27 11:48:08 -08001949 if (mAutoFillResetNeeded) {
1950 getAutofillManager().onInvisibleForAutofill();
1951 }
1952
Felipe Lemec24a56a2017-08-03 14:27:57 -07001953 if (isFinishing()) {
1954 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08001955 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07001956 } else if (mIntent != null
1957 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
1958 // Activity was launched when user tapped a link in the Autofill Save UI - since
1959 // user launched another activity, the Save UI should not be restored when this
1960 // activity is finished.
1961 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
1962 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
1963 }
Felipe Lemebab851c2017-02-03 18:45:08 -08001964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966
1967 /**
1968 * Perform any final cleanup before an activity is destroyed. This can
1969 * happen either because the activity is finishing (someone called
1970 * {@link #finish} on it, or because the system is temporarily destroying
1971 * this instance of the activity to save space. You can distinguish
1972 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 * <p><em>Note: do not count on this method being called as a place for
1975 * saving data! For example, if an activity is editing data in a content
1976 * provider, those edits should be committed in either {@link #onPause} or
1977 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1978 * free resources like threads that are associated with an activity, so
1979 * that a destroyed activity does not leave such things around while the
1980 * rest of its application is still running. There are situations where
1981 * the system will simply kill the activity's hosting process without
1982 * calling this method (or any others) in it, so it should not be used to
1983 * do things that are intended to remain around after the process goes
1984 * away.
RoboErik55011652014-07-09 15:05:53 -07001985 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 * <p><em>Derived classes must call through to the super class's
1987 * implementation of this method. If they do not, an exception will be
1988 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001989 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 * @see #onPause
1991 * @see #onStop
1992 * @see #finish
1993 * @see #isFinishing
1994 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001995 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001997 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 mCalled = true;
1999
2000 // dismiss any dialogs we are managing.
2001 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 final int numDialogs = mManagedDialogs.size();
2003 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002004 final ManagedDialog md = mManagedDialogs.valueAt(i);
2005 if (md.mDialog.isShowing()) {
2006 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002009 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011
2012 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002013 synchronized (mManagedCursors) {
2014 int numCursors = mManagedCursors.size();
2015 for (int i = 0; i < numCursors; i++) {
2016 ManagedCursor c = mManagedCursors.get(i);
2017 if (c != null) {
2018 c.mCursor.close();
2019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002021 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Amith Yamasani49860442010-03-17 20:54:10 -07002023
2024 // Close any open search dialog
2025 if (mSearchManager != null) {
2026 mSearchManager.stopSearch();
2027 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002028
Chris Banes21b25772016-01-04 20:41:59 +00002029 if (mActionBar != null) {
2030 mActionBar.onDestroy();
2031 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002032
2033 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
2035
2036 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002037 * Report to the system that your app is now fully drawn, purely for diagnostic
2038 * purposes (calling it does not impact the visible behavior of the activity).
2039 * This is only used to help instrument application launch times, so that the
2040 * app can report when it is fully in a usable state; without this, the only thing
2041 * the system itself can determine is the point at which the activity's window
2042 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002043 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002044 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002045 * entirely drawn your UI and populated with all of the significant data. You
2046 * can safely call this method any time after first launch as well, in which case
2047 * it will simply be ignored.
2048 */
2049 public void reportFullyDrawn() {
2050 if (mDoReportFullyDrawn) {
2051 mDoReportFullyDrawn = false;
2052 try {
Jorim Jaggi4d27b842017-08-17 17:22:26 +02002053 ActivityManager.getService().reportActivityFullyDrawn(mToken, mRestoredFromBundle);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002054 } catch (RemoteException e) {
2055 }
2056 }
2057 }
2058
2059 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002060 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002061 * visa-versa. This method provides the same configuration that will be sent in the following
2062 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2063 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002064 * @see android.R.attr#resizeableActivity
2065 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002066 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002067 * @param newConfig The new configuration of the activity with the state
2068 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002069 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002070 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2071 // Left deliberately empty. There should be no side effects if a direct
2072 // subclass of Activity does not call super.
2073 onMultiWindowModeChanged(isInMultiWindowMode);
2074 }
2075
2076 /**
2077 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2078 * visa-versa.
2079 *
2080 * @see android.R.attr#resizeableActivity
2081 *
2082 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2083 *
2084 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2085 */
2086 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002087 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002088 // Left deliberately empty. There should be no side effects if a direct
2089 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002090 }
2091
2092 /**
2093 * Returns true if the activity is currently in multi-window mode.
2094 * @see android.R.attr#resizeableActivity
2095 *
2096 * @return True if the activity is in multi-window mode.
2097 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002098 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002099 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002100 return ActivityManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002101 } catch (RemoteException e) {
2102 }
2103 return false;
2104 }
2105
2106 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002107 * Called by the system when the activity changes to and from picture-in-picture mode. This
2108 * method provides the same configuration that will be sent in the following
2109 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2110 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002111 * @see android.R.attr#supportsPictureInPicture
2112 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002113 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002114 * @param newConfig The new configuration of the activity with the state
2115 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002116 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002117 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2118 Configuration newConfig) {
2119 // Left deliberately empty. There should be no side effects if a direct
2120 // subclass of Activity does not call super.
2121 onPictureInPictureModeChanged(isInPictureInPictureMode);
2122 }
2123
2124 /**
2125 * Called by the system when the activity changes to and from picture-in-picture mode.
2126 *
2127 * @see android.R.attr#supportsPictureInPicture
2128 *
2129 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2130 *
2131 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2132 */
2133 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002134 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002135 // Left deliberately empty. There should be no side effects if a direct
2136 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002137 }
2138
2139 /**
2140 * Returns true if the activity is currently in picture-in-picture mode.
2141 * @see android.R.attr#supportsPictureInPicture
2142 *
2143 * @return True if the activity is in picture-in-picture mode.
2144 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002145 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002146 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002147 return ActivityManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002148 } catch (RemoteException e) {
2149 }
2150 return false;
2151 }
2152
2153 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002154 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002155 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2156 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002157 *
Winson Chung709904f2017-04-25 11:00:48 -07002158 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002159 * @see android.R.attr#supportsPictureInPicture
2160 */
Winson Chung709904f2017-04-25 11:00:48 -07002161 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002162 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002163 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002164 }
2165
Jeff Sharkey000ce802017-04-29 13:13:27 -06002166 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002167 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002168 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002169 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002170 }
2171
2172 /**
Winson Chung709904f2017-04-25 11:00:48 -07002173 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2174 * set parameters in {@param params} will be combined with the parameters from prior calls to
2175 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2176 *
2177 * The system may disallow entering picture-in-picture in various cases, including when the
2178 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2179 *
2180 * @see android.R.attr#supportsPictureInPicture
2181 * @see PictureInPictureParams
2182 *
2183 * @param params non-null parameters to be combined with previously set parameters when entering
2184 * picture-in-picture.
2185 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002186 * @return true if the system successfully put this activity into picture-in-picture mode or was
2187 * already in picture-in-picture mode (@see {@link #isInPictureInPictureMode()). If the device
2188 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002189 */
2190 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2191 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002192 if (!deviceSupportsPictureInPictureMode()) {
2193 return false;
2194 }
Winson Chung709904f2017-04-25 11:00:48 -07002195 if (params == null) {
2196 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2197 }
Winson Chung298f95b2017-08-10 15:57:18 -07002198 if (!mCanEnterPictureInPicture) {
2199 throw new IllegalStateException("Activity must be resumed to enter"
2200 + " picture-in-picture");
2201 }
Winson Chung709904f2017-04-25 11:00:48 -07002202 return ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken, params);
2203 } catch (RemoteException e) {
2204 return false;
2205 }
2206 }
2207
Jeff Sharkey000ce802017-04-29 13:13:27 -06002208 /** @removed */
2209 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002210 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002211 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002212 }
2213
2214 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002215 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2216 * {@link #enterPictureInPictureMode()} is called.
2217 *
Winson Chung709904f2017-04-25 11:00:48 -07002218 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002219 */
Winson Chung709904f2017-04-25 11:00:48 -07002220 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002221 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002222 if (!deviceSupportsPictureInPictureMode()) {
2223 return;
2224 }
Winson Chung709904f2017-04-25 11:00:48 -07002225 if (params == null) {
2226 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002227 }
Winson Chung709904f2017-04-25 11:00:48 -07002228 ActivityManagerNative.getDefault().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002229 } catch (RemoteException e) {
2230 }
2231 }
2232
Winson Chung8802eac2017-04-17 14:21:44 -07002233 /**
Winson Chung709904f2017-04-25 11:00:48 -07002234 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2235 * user interacts with the activity currently in picture-in-picture mode. This number may change
2236 * if the global configuration changes (ie. if the device is plugged into an external display),
2237 * but will always be larger than three.
2238 */
2239 public int getMaxNumPictureInPictureActions() {
2240 try {
2241 return ActivityManagerNative.getDefault().getMaxNumPictureInPictureActions(mToken);
2242 } catch (RemoteException e) {
2243 return 0;
2244 }
2245 }
2246
Winson Chung80fa2d82018-01-25 13:58:20 -08002247 /**
2248 * @return Whether this device supports picture-in-picture.
2249 */
2250 private boolean deviceSupportsPictureInPictureMode() {
2251 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2252 }
2253
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002254 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002255 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002256 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002257 }
2258
2259 /**
2260 * Called by the system when the activity is moved from one display to another without
2261 * recreation. This means that this activity is declared to handle all changes to configuration
2262 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002263 * again.
2264 *
2265 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2266 * applied configuration actually changed. It is up to app developer to choose whether to handle
2267 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2268 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002269 *
2270 * <p>Use this callback to track changes to the displays if some activity functionality relies
2271 * on an association with some display properties.
2272 *
2273 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002274 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002275 *
2276 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002277 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002278 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002279 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002280 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002281 }
2282
Winson Chungb5c41b72016-12-07 15:00:47 -08002283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 * Called by the system when the device configuration changes while your
2285 * activity is running. Note that this will <em>only</em> be called if
2286 * you have selected configurations you would like to handle with the
2287 * {@link android.R.attr#configChanges} attribute in your manifest. If
2288 * any configuration change occurs that is not selected to be reported
2289 * by that attribute, then instead of reporting it the system will stop
2290 * and restart the activity (to have it launched with the new
2291 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002292 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 * <p>At the time that this function has been called, your Resources
2294 * object will have been updated to return resource values matching the
2295 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 * @param newConfig The new device configuration.
2298 */
2299 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002300 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002302
Dianne Hackborn9d071802010-12-08 14:49:15 -08002303 mFragments.dispatchConfigurationChanged(newConfig);
2304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 if (mWindow != null) {
2306 // Pass the configuration changed event to the window
2307 mWindow.onConfigurationChanged(newConfig);
2308 }
Adam Powell45c0b192011-07-28 15:11:57 -07002309
2310 if (mActionBar != null) {
2311 // Do this last; the action bar will need to access
2312 // view changes from above.
2313 mActionBar.onConfigurationChanged(newConfig);
2314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
RoboErik55011652014-07-09 15:05:53 -07002316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 /**
2318 * If this activity is being destroyed because it can not handle a
2319 * configuration parameter being changed (and thus its
2320 * {@link #onConfigurationChanged(Configuration)} method is
2321 * <em>not</em> being called), then you can use this method to discover
2322 * the set of changes that have occurred while in the process of being
2323 * destroyed. Note that there is no guarantee that these will be
2324 * accurate (other changes could have happened at any time), so you should
2325 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 * @return Returns a bit field of the configuration parameters that are
2328 * changing, as defined by the {@link android.content.res.Configuration}
2329 * class.
2330 */
2331 public int getChangingConfigurations() {
2332 return mConfigChangeFlags;
2333 }
RoboErik55011652014-07-09 15:05:53 -07002334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 /**
2336 * Retrieve the non-configuration instance data that was previously
2337 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2338 * be available from the initial {@link #onCreate} and
2339 * {@link #onStart} calls to the new instance, allowing you to extract
2340 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002341 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 * <p>Note that the data you retrieve here should <em>only</em> be used
2343 * as an optimization for handling configuration changes. You should always
2344 * be able to handle getting a null pointer back, and an activity must
2345 * still be able to restore itself to its previous state (through the
2346 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2347 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002348 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002349 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002350 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002351 * available on older platforms through the Android support libraries.
2352 *
2353 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002355 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002357 return mLastNonConfigurationInstances != null
2358 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
RoboErik55011652014-07-09 15:05:53 -07002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 /**
2362 * Called by the system, as part of destroying an
2363 * activity due to a configuration change, when it is known that a new
2364 * instance will immediately be created for the new configuration. You
2365 * can return any object you like here, including the activity instance
2366 * itself, which can later be retrieved by calling
2367 * {@link #getLastNonConfigurationInstance()} in the new activity
2368 * instance.
RoboErik55011652014-07-09 15:05:53 -07002369 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002370 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2371 * or later, consider instead using a {@link Fragment} with
2372 * {@link Fragment#setRetainInstance(boolean)
2373 * Fragment.setRetainInstance(boolean}.</em>
2374 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 * <p>This function is called purely as an optimization, and you must
2376 * not rely on it being called. When it is called, a number of guarantees
2377 * will be made to help optimize configuration switching:
2378 * <ul>
2379 * <li> The function will be called between {@link #onStop} and
2380 * {@link #onDestroy}.
2381 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002382 * created after this one's {@link #onDestroy()} is called. In particular,
2383 * <em>no</em> messages will be dispatched during this time (when the returned
2384 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 * <li> The object you return here will <em>always</em> be available from
2386 * the {@link #getLastNonConfigurationInstance()} method of the following
2387 * activity instance as described there.
2388 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002389 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 * <p>These guarantees are designed so that an activity can use this API
2391 * to propagate extensive state from the old to new activity instance, from
2392 * loaded bitmaps, to network connections, to evenly actively running
2393 * threads. Note that you should <em>not</em> propagate any data that
2394 * may change based on the configuration, including any data loaded from
2395 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002396 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002397 * <p>The guarantee of no message handling during the switch to the next
2398 * activity simplifies use with active objects. For example if your retained
2399 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2400 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2401 * not be called from the call here until you execute the next instance's
2402 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2403 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2404 * running in a separate thread.)
2405 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002406 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002407 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002408 * available on older platforms through the Android support libraries.
2409 *
2410 * @return any Object holding the desired state to propagate to the
2411 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 */
2413 public Object onRetainNonConfigurationInstance() {
2414 return null;
2415 }
RoboErik55011652014-07-09 15:05:53 -07002416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 /**
2418 * Retrieve the non-configuration instance data that was previously
2419 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2420 * be available from the initial {@link #onCreate} and
2421 * {@link #onStart} calls to the new instance, allowing you to extract
2422 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 * <p>Note that the data you retrieve here should <em>only</em> be used
2425 * as an optimization for handling configuration changes. You should always
2426 * be able to handle getting a null pointer back, and an activity must
2427 * still be able to restore itself to its previous state (through the
2428 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2429 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 * @return Returns the object previously returned by
2432 * {@link #onRetainNonConfigurationChildInstances()}
2433 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002434 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002435 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2436 return mLastNonConfigurationInstances != null
2437 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 }
RoboErik55011652014-07-09 15:05:53 -07002439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 /**
2441 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2442 * it should return either a mapping from child activity id strings to arbitrary objects,
2443 * or null. This method is intended to be used by Activity framework subclasses that control a
2444 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2445 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2446 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002447 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2449 return null;
2450 }
RoboErik55011652014-07-09 15:05:53 -07002451
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002452 NonConfigurationInstances retainNonConfigurationInstances() {
2453 Object activity = onRetainNonConfigurationInstance();
2454 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002455 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002456
2457 // We're already stopped but we've been asked to retain.
2458 // Our fragments are taken care of but we need to mark the loaders for retention.
2459 // In order to do this correctly we need to restart the loaders first before
2460 // handing them off to the next activity.
2461 mFragments.doLoaderStart();
2462 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002463 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002464
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002465 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002466 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002467 return null;
2468 }
RoboErik55011652014-07-09 15:05:53 -07002469
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002470 NonConfigurationInstances nci = new NonConfigurationInstances();
2471 nci.activity = activity;
2472 nci.children = children;
2473 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002474 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002475 if (mVoiceInteractor != null) {
2476 mVoiceInteractor.retainInstance();
2477 nci.voiceInteractor = mVoiceInteractor;
2478 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002479 return nci;
2480 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002483 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002485 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002487
2488 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002489 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002490 mCalled = true;
2491 mFragments.dispatchTrimMemory(level);
2492 }
2493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002495 * Return the FragmentManager for interacting with fragments associated
2496 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08002497 *
2498 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002499 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002500 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002501 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002502 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002503 }
RoboErik55011652014-07-09 15:05:53 -07002504
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002505 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002506 * Called when a Fragment is being attached to this activity, immediately
2507 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2508 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08002509 *
2510 * @deprecated Use {@link
2511 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07002512 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002513 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07002514 public void onAttachFragment(Fragment fragment) {
2515 }
RoboErik55011652014-07-09 15:05:53 -07002516
Dianne Hackbornc8017682010-07-06 13:34:38 -07002517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 * Wrapper around
2519 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2520 * that gives the resulting {@link Cursor} to call
2521 * {@link #startManagingCursor} so that the activity will manage its
2522 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002523 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002524 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2525 * or later, consider instead using {@link LoaderManager} instead, available
2526 * via {@link #getLoaderManager()}.</em>
2527 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002528 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2529 * this method, because the activity will do that for you at the appropriate time. However, if
2530 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2531 * not</em> automatically close the cursor and, in that case, you must call
2532 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 * @param uri The URI of the content provider to query.
2535 * @param projection List of columns to return.
2536 * @param selection SQL WHERE clause.
2537 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002538 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2542 * @see #startManagingCursor
2543 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002544 *
2545 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 */
Jason parks6ed50de2010-08-25 10:18:50 -05002547 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002548 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07002549 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2550 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2552 if (c != null) {
2553 startManagingCursor(c);
2554 }
2555 return c;
2556 }
2557
2558 /**
2559 * Wrapper around
2560 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2561 * that gives the resulting {@link Cursor} to call
2562 * {@link #startManagingCursor} so that the activity will manage its
2563 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002564 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002565 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2566 * or later, consider instead using {@link LoaderManager} instead, available
2567 * via {@link #getLoaderManager()}.</em>
2568 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002569 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2570 * this method, because the activity will do that for you at the appropriate time. However, if
2571 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2572 * not</em> automatically close the cursor and, in that case, you must call
2573 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002574 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 * @param uri The URI of the content provider to query.
2576 * @param projection List of columns to return.
2577 * @param selection SQL WHERE clause.
2578 * @param selectionArgs The arguments to selection, if any ?s are pesent
2579 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2584 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002585 *
2586 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 */
Jason parks6ed50de2010-08-25 10:18:50 -05002588 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002589 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2590 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2592 if (c != null) {
2593 startManagingCursor(c);
2594 }
2595 return c;
2596 }
2597
2598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 * This method allows the activity to take care of managing the given
2600 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2601 * That is, when the activity is stopped it will automatically call
2602 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2603 * it will call {@link Cursor#requery} for you. When the activity is
2604 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002605 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002606 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2607 * or later, consider instead using {@link LoaderManager} instead, available
2608 * via {@link #getLoaderManager()}.</em>
2609 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002610 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2611 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2612 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2613 * <em>will not</em> automatically close the cursor and, in that case, you must call
2614 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002615 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2619 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002620 *
2621 * @deprecated Use the new {@link android.content.CursorLoader} class with
2622 * {@link LoaderManager} instead; this is also
2623 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 */
Jason parks6ed50de2010-08-25 10:18:50 -05002625 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 public void startManagingCursor(Cursor c) {
2627 synchronized (mManagedCursors) {
2628 mManagedCursors.add(new ManagedCursor(c));
2629 }
2630 }
2631
2632 /**
2633 * Given a Cursor that was previously given to
2634 * {@link #startManagingCursor}, stop the activity's management of that
2635 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002636 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002637 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002638 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002639 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002644 *
2645 * @deprecated Use the new {@link android.content.CursorLoader} class with
2646 * {@link LoaderManager} instead; this is also
2647 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 */
Jason parks6ed50de2010-08-25 10:18:50 -05002649 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 public void stopManagingCursor(Cursor c) {
2651 synchronized (mManagedCursors) {
2652 final int N = mManagedCursors.size();
2653 for (int i=0; i<N; i++) {
2654 ManagedCursor mc = mManagedCursors.get(i);
2655 if (mc.mCursor == c) {
2656 mManagedCursors.remove(i);
2657 break;
2658 }
2659 }
2660 }
2661 }
2662
2663 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002664 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2665 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002666 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002668 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002669 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 }
2672
2673 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04002674 * Finds a view that was identified by the {@code android:id} XML attribute
2675 * that was processed in {@link #onCreate}.
2676 * <p>
2677 * <strong>Note:</strong> In most cases -- depending on compiler support --
2678 * the resulting view is automatically cast to the target class type. If
2679 * the target class type is unconstrained, an explicit cast may be
2680 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04002682 * @param id the ID to search for
2683 * @return a view with given ID if found, or {@code null} otherwise
2684 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08002685 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002687 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04002688 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 return getWindow().findViewById(id);
2690 }
RoboErik55011652014-07-09 15:05:53 -07002691
Adam Powell33b97432010-04-20 10:01:14 -07002692 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08002693 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
2694 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
2695 * no matching view in the hierarchy.
2696 * <p>
2697 * <strong>Note:</strong> In most cases -- depending on compiler support --
2698 * the resulting view is automatically cast to the target class type. If
2699 * the target class type is unconstrained, an explicit cast may be
2700 * necessary.
2701 *
2702 * @param id the ID to search for
2703 * @return a view with given ID
2704 * @see View#requireViewById(int)
2705 * @see Activity#findViewById(int)
2706 */
2707 @NonNull
2708 public final <T extends View> T requireViewById(@IdRes int id) {
2709 T view = findViewById(id);
2710 if (view == null) {
2711 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
2712 }
2713 return view;
2714 }
2715
2716 /**
Adam Powell33b97432010-04-20 10:01:14 -07002717 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002718 *
Adam Powell33b97432010-04-20 10:01:14 -07002719 * @return The Activity's ActionBar, or null if it does not have one.
2720 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002721 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002722 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002723 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002724 return mActionBar;
2725 }
Adam Powelle43340c2014-03-17 19:10:43 -07002726
2727 /**
2728 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2729 * Activity window.
2730 *
2731 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2732 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2733 * a traditional window decor action bar. The toolbar's menu will be populated with the
2734 * Activity's options menu and the navigation button will be wired through the standard
2735 * {@link android.R.id#home home} menu select action.</p>
2736 *
2737 * <p>In order to use a Toolbar within the Activity's window content the application
2738 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2739 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002740 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002741 */
Adam Powell37780142014-06-01 13:31:00 -07002742 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002743 final ActionBar ab = getActionBar();
2744 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002745 throw new IllegalStateException("This Activity already has an action bar supplied " +
2746 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2747 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2748 }
Chris Banes21b25772016-01-04 20:41:59 +00002749
2750 // If we reach here then we're setting a new action bar
2751 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002752 mMenuInflater = null;
2753
Chris Banes21b25772016-01-04 20:41:59 +00002754 // If we have an action bar currently, destroy it
2755 if (ab != null) {
2756 ab.onDestroy();
2757 }
2758
Chris Banesc7d6c322016-01-27 14:09:16 +00002759 if (toolbar != null) {
2760 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2761 mActionBar = tbab;
2762 mWindow.setCallback(tbab.getWrappedWindowCallback());
2763 } else {
2764 mActionBar = null;
2765 // Re-set the original window callback since we may have already set a Toolbar wrapper
2766 mWindow.setCallback(this);
2767 }
2768
2769 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002770 }
RoboErik55011652014-07-09 15:05:53 -07002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 /**
Adam Powell33b97432010-04-20 10:01:14 -07002773 * Creates a new ActionBar, locates the inflated ActionBarView,
2774 * initializes the ActionBar with the view, and sets mActionBar.
2775 */
Adam Powelle43340c2014-03-17 19:10:43 -07002776 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002777 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002778
2779 // Initializing the window decor can change window feature flags.
2780 // Make sure that we have the correct set before performing the test below.
2781 window.getDecorView();
2782
Adam Powell9b4c8042010-08-10 15:36:44 -07002783 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002784 return;
2785 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002786
Adam Powelle43340c2014-03-17 19:10:43 -07002787 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002788 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002789
2790 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2791 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002792 }
RoboErik55011652014-07-09 15:05:53 -07002793
Adam Powell33b97432010-04-20 10:01:14 -07002794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 * Set the activity content from a layout resource. The resource will be
2796 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002797 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002799 *
Romain Guy482b34a62011-01-20 10:59:28 -08002800 * @see #setContentView(android.view.View)
2801 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002803 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002805 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 }
2807
2808 /**
2809 * Set the activity content to an explicit view. This view is placed
2810 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002811 * view hierarchy. When calling this method, the layout parameters of the
2812 * specified view are ignored. Both the width and the height of the view are
2813 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2814 * your own layout parameters, invoke
2815 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2816 * instead.
RoboErik55011652014-07-09 15:05:53 -07002817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002819 *
2820 * @see #setContentView(int)
2821 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 */
2823 public void setContentView(View view) {
2824 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002825 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
2827
2828 /**
2829 * Set the activity content to an explicit view. This view is placed
2830 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002831 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002832 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 * @param view The desired content to display.
2834 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002835 *
2836 * @see #setContentView(android.view.View)
2837 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 */
2839 public void setContentView(View view, ViewGroup.LayoutParams params) {
2840 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002841 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843
2844 /**
2845 * Add an additional content view to the activity. Added after any existing
2846 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 * @param view The desired content to display.
2849 * @param params Layout parameters for the view.
2850 */
2851 public void addContentView(View view, ViewGroup.LayoutParams params) {
2852 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002853 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 }
2855
2856 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002857 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2858 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2859 *
2860 * <p>This method will return non-null after content has been initialized (e.g. by using
2861 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2862 *
2863 * @return This window's content TransitionManager or null if none is set.
2864 */
2865 public TransitionManager getContentTransitionManager() {
2866 return getWindow().getTransitionManager();
2867 }
2868
2869 /**
2870 * Set the {@link TransitionManager} to use for default transitions in this window.
2871 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2872 *
2873 * @param tm The TransitionManager to use for scene changes.
2874 */
2875 public void setContentTransitionManager(TransitionManager tm) {
2876 getWindow().setTransitionManager(tm);
2877 }
2878
2879 /**
2880 * Retrieve the {@link Scene} representing this window's current content.
2881 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2882 *
2883 * <p>This method will return null if the current content is not represented by a Scene.</p>
2884 *
2885 * @return Current Scene being shown or null
2886 */
2887 public Scene getContentScene() {
2888 return getWindow().getContentScene();
2889 }
2890
2891 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002892 * Sets whether this activity is finished when touched outside its window's
2893 * bounds.
2894 */
2895 public void setFinishOnTouchOutside(boolean finish) {
2896 mWindow.setCloseOnTouchOutside(finish);
2897 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002898
2899 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002900 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07002901 DEFAULT_KEYS_DISABLE,
2902 DEFAULT_KEYS_DIALER,
2903 DEFAULT_KEYS_SHORTCUT,
2904 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002905 DEFAULT_KEYS_SEARCH_GLOBAL
2906 })
Tor Norbyed9273d62013-05-30 15:59:53 -07002907 @Retention(RetentionPolicy.SOURCE)
2908 @interface DefaultKeyMode {}
2909
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2912 * keys.
RoboErik55011652014-07-09 15:05:53 -07002913 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 * @see #setDefaultKeyMode
2915 */
2916 static public final int DEFAULT_KEYS_DISABLE = 0;
2917 /**
2918 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2919 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002920 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 * @see #setDefaultKeyMode
2922 */
2923 static public final int DEFAULT_KEYS_DIALER = 1;
2924 /**
2925 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2926 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002927 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 * @see #setDefaultKeyMode
2931 */
2932 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2933 /**
2934 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2935 * will start an application-defined search. (If the application or activity does not
2936 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 * @see #setDefaultKeyMode
2941 */
2942 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2943
2944 /**
2945 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2946 * will start a global search (typically web search, but some platforms may define alternate
2947 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002948 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002950 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 * @see #setDefaultKeyMode
2952 */
2953 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2954
2955 /**
2956 * Select the default key handling for this activity. This controls what
2957 * will happen to key events that are not otherwise handled. The default
2958 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2959 * floor. Other modes allow you to launch the dialer
2960 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2961 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002962 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002964 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 * <p>Note that the mode selected here does not impact the default
2966 * handling of system keys, such as the "back" and "menu" keys, and your
2967 * activity and its views always get a first chance to receive and handle
2968 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002971 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 * @see #onKeyDown
2973 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002974 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 // Some modes use a SpannableStringBuilder to track & dispatch input events
2978 // This list must remain in sync with the switch in onKeyDown()
2979 switch (mode) {
2980 case DEFAULT_KEYS_DISABLE:
2981 case DEFAULT_KEYS_SHORTCUT:
2982 mDefaultKeySsb = null; // not used in these modes
2983 break;
2984 case DEFAULT_KEYS_DIALER:
2985 case DEFAULT_KEYS_SEARCH_LOCAL:
2986 case DEFAULT_KEYS_SEARCH_GLOBAL:
2987 mDefaultKeySsb = new SpannableStringBuilder();
2988 Selection.setSelection(mDefaultKeySsb,0);
2989 break;
2990 default:
2991 throw new IllegalArgumentException();
2992 }
2993 }
2994
2995 /**
2996 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002997 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 * is inside a TextView will not trigger the event (unless it is a navigation
2999 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003000 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 * <p>If the focused view didn't want this event, this method is called.
3002 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003003 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3004 * by calling {@link #onBackPressed()}, though the behavior varies based
3005 * on the application compatibility mode: for
3006 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3007 * it will set up the dispatch to call {@link #onKeyUp} where the action
3008 * will be performed; for earlier applications, it will perform the
3009 * action immediately in on-down, as those versions of the platform
3010 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003011 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003012 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003013 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003014 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003016 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 * this event and it should continue to be propagated.
3018 * @see #onKeyUp
3019 * @see android.view.KeyEvent
3020 */
3021 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003022 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003023 if (getApplicationInfo().targetSdkVersion
3024 >= Build.VERSION_CODES.ECLAIR) {
3025 event.startTracking();
3026 } else {
3027 onBackPressed();
3028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 return true;
3030 }
RoboErik55011652014-07-09 15:05:53 -07003031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3033 return false;
3034 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003035 Window w = getWindow();
3036 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3037 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3038 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003039 return true;
3040 }
3041 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003042 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3043 // Don't consume TAB here since it's used for navigation. Arrow keys
3044 // aren't considered "typing keys" so they already won't get consumed.
3045 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 } else {
3047 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3048 boolean clearSpannable = false;
3049 boolean handled;
3050 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3051 clearSpannable = true;
3052 handled = false;
3053 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003054 handled = TextKeyListener.getInstance().onKeyDown(
3055 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 if (handled && mDefaultKeySsb.length() > 0) {
3057 // something useable has been typed - dispatch it now.
3058
3059 final String str = mDefaultKeySsb.toString();
3060 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 switch (mDefaultKeyMode) {
3063 case DEFAULT_KEYS_DIALER:
3064 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3065 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003066 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 break;
3068 case DEFAULT_KEYS_SEARCH_LOCAL:
3069 startSearch(str, false, null, false);
3070 break;
3071 case DEFAULT_KEYS_SEARCH_GLOBAL:
3072 startSearch(str, false, null, true);
3073 break;
3074 }
3075 }
3076 }
3077 if (clearSpannable) {
3078 mDefaultKeySsb.clear();
3079 mDefaultKeySsb.clearSpans();
3080 Selection.setSelection(mDefaultKeySsb,0);
3081 }
3082 return handled;
3083 }
3084 }
3085
3086 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003087 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3088 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3089 * the event).
3090 */
3091 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3092 return false;
3093 }
3094
3095 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003097 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 * is inside a TextView will not trigger the event (unless it is a navigation
3099 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003100 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003101 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3102 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003105 * further, or <code>false</code> to indicate that you have not handled
3106 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 * @see #onKeyDown
3108 * @see KeyEvent
3109 */
3110 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003111 if (getApplicationInfo().targetSdkVersion
3112 >= Build.VERSION_CODES.ECLAIR) {
3113 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3114 && !event.isCanceled()) {
3115 onBackPressed();
3116 return true;
3117 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 return false;
3120 }
3121
3122 /**
3123 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3124 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3125 * the event).
3126 */
3127 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3128 return false;
3129 }
RoboErik55011652014-07-09 15:05:53 -07003130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003132 * Called when the activity has detected the user's press of the back
3133 * key. The default implementation simply finishes the current activity,
3134 * but you can override this to do whatever you want.
3135 */
3136 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003137 if (mActionBar != null && mActionBar.collapseActionView()) {
3138 return;
3139 }
3140
George Mount27b0dc02017-02-21 10:24:09 -08003141 FragmentManager fragmentManager = mFragments.getFragmentManager();
3142
3143 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003144 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003145 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003146 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003147
3148 /**
3149 * Called when a key shortcut event is not handled by any of the views in the Activity.
3150 * Override this method to implement global key shortcuts for the Activity.
3151 * Key shortcuts can also be implemented by setting the
3152 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3153 *
3154 * @param keyCode The value in event.getKeyCode().
3155 * @param event Description of the key event.
3156 * @return True if the key shortcut was handled.
3157 */
3158 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003159 // Let the Action Bar have a chance at handling the shortcut.
3160 ActionBar actionBar = getActionBar();
3161 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003162 }
3163
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 * Called when a touch screen event was not handled by any of the views
3166 * under it. This is most useful to process touch events that happen
3167 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 * @return Return true if you have consumed the event, false if you haven't.
3172 * The default implementation always returns false.
3173 */
3174 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003175 if (mWindow.shouldCloseOnTouch(this, event)) {
3176 finish();
3177 return true;
3178 }
RoboErik55011652014-07-09 15:05:53 -07003179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 return false;
3181 }
RoboErik55011652014-07-09 15:05:53 -07003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 /**
3184 * Called when the trackball was moved and not handled by any of the
3185 * views inside of the activity. So, for example, if the trackball moves
3186 * while focus is on a button, you will receive a call here because
3187 * buttons do not normally do anything with trackball events. The call
3188 * here happens <em>before</em> trackball movements are converted to
3189 * DPAD key events, which then get sent back to the view hierarchy, and
3190 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 * @return Return true if you have consumed the event, false if you haven't.
3195 * The default implementation always returns false.
3196 */
3197 public boolean onTrackballEvent(MotionEvent event) {
3198 return false;
3199 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003200
3201 /**
3202 * Called when a generic motion event was not handled by any of the
3203 * views inside of the activity.
3204 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003205 * Generic motion events describe joystick movements, mouse hovers, track pad
3206 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003207 * {@link MotionEvent#getSource() source} of the motion event specifies
3208 * the class of input that was received. Implementations of this method
3209 * must examine the bits in the source before processing the event.
3210 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003211 * </p><p>
3212 * Generic motion events with source class
3213 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3214 * are delivered to the view under the pointer. All other generic motion events are
3215 * delivered to the focused view.
3216 * </p><p>
3217 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3218 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003219 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003220 *
3221 * @param event The generic motion event being processed.
3222 *
3223 * @return Return true if you have consumed the event, false if you haven't.
3224 * The default implementation always returns false.
3225 */
3226 public boolean onGenericMotionEvent(MotionEvent event) {
3227 return false;
3228 }
3229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 /**
3231 * Called whenever a key, touch, or trackball event is dispatched to the
3232 * activity. Implement this method if you wish to know that the user has
3233 * interacted with the device in some way while your activity is running.
3234 * This callback and {@link #onUserLeaveHint} are intended to help
3235 * activities manage status bar notifications intelligently; specifically,
3236 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07003237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3239 * be accompanied by calls to {@link #onUserInteraction}. This
3240 * ensures that your activity will be told of relevant user activity such
3241 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 * <p>Note that this callback will be invoked for the touch down action
3244 * that begins a touch gesture, but may not be invoked for the touch-moved
3245 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 * @see #onUserLeaveHint()
3248 */
3249 public void onUserInteraction() {
3250 }
RoboErik55011652014-07-09 15:05:53 -07003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3253 // Update window manager if: we have a view, that view is
3254 // attached to its parent (which will be a RootView), and
3255 // this activity is not embedded.
3256 if (mParent == null) {
3257 View decor = mDecor;
3258 if (decor != null && decor.getParent() != null) {
3259 getWindowManager().updateViewLayout(decor, params);
3260 }
3261 }
3262 }
3263
3264 public void onContentChanged() {
3265 }
3266
3267 /**
3268 * Called when the current {@link Window} of the activity gains or loses
3269 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003270 * to the user. The default implementation clears the key tracking
3271 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003272 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003273 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 * is managed independently of activity lifecycles. As such, while focus
3275 * changes will generally have some relation to lifecycle changes (an
3276 * activity that is stopped will not generally get window focus), you
3277 * should not rely on any particular order between the callbacks here and
3278 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 * <p>As a general rule, however, a resumed activity will have window
3281 * focus... unless it has displayed other dialogs or popups that take
3282 * input focus, in which case the activity itself will not have focus
3283 * when the other windows have it. Likewise, the system may display
3284 * system-level windows (such as the status bar notification panel or
3285 * a system alert) which will temporarily take window input focus without
3286 * pausing the foreground activity.
3287 *
3288 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 * @see #hasWindowFocus()
3291 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003292 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 */
3294 public void onWindowFocusChanged(boolean hasFocus) {
3295 }
RoboErik55011652014-07-09 15:05:53 -07003296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003298 * Called when the main window associated with the activity has been
3299 * attached to the window manager.
3300 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3301 * for more information.
3302 * @see View#onAttachedToWindow
3303 */
3304 public void onAttachedToWindow() {
3305 }
RoboErik55011652014-07-09 15:05:53 -07003306
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003307 /**
3308 * Called when the main window associated with the activity has been
3309 * detached from the window manager.
3310 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3311 * for more information.
3312 * @see View#onDetachedFromWindow
3313 */
3314 public void onDetachedFromWindow() {
3315 }
RoboErik55011652014-07-09 15:05:53 -07003316
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 * Returns true if this activity's <em>main</em> window currently has window focus.
3319 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3324 */
3325 public boolean hasWindowFocus() {
3326 Window w = getWindow();
3327 if (w != null) {
3328 View d = w.getDecorView();
3329 if (d != null) {
3330 return d.hasWindowFocus();
3331 }
3332 }
3333 return false;
3334 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003335
3336 /**
3337 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003338 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003339 */
Adam Powell117b6952014-05-05 18:14:56 -07003340 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003341 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003342 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003343 if (suppressWindowTransition) {
3344 overridePendingTransition(0, 0);
3345 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003346 }
RoboErik55011652014-07-09 15:05:53 -07003347
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003348
Skuhnece2faa52015-08-11 10:36:38 -07003349 /**
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003350 * Moves the activity from {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode to
3351 * {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003352 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003353 * @hide
3354 */
3355 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08003356 public void exitFreeformMode() throws RemoteException {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003357 ActivityManager.getService().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003358 }
3359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003361 * Puts the activity in picture-in-picture mode if the activity supports.
3362 * @see android.R.attr#supportsPictureInPicture
3363 * @hide
3364 */
3365 @Override
3366 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003367 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003368 enterPictureInPictureMode();
3369 }
3370 }
3371
3372 /**
RoboErik55011652014-07-09 15:05:53 -07003373 * Called to process key events. You can override this to intercept all
3374 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003378 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 * @return boolean Return true if this event was consumed.
3380 */
3381 public boolean dispatchKeyEvent(KeyEvent event) {
3382 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003383
3384 // Let action bars open menus in response to the menu key prioritized over
3385 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003386 final int keyCode = event.getKeyCode();
3387 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003388 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3389 return true;
3390 }
3391
Dianne Hackborn8d374262009-09-14 21:21:52 -07003392 Window win = getWindow();
3393 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 return true;
3395 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003396 View decor = mDecor;
3397 if (decor == null) decor = win.getDecorView();
3398 return event.dispatch(this, decor != null
3399 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 }
3401
3402 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003403 * Called to process a key shortcut event.
3404 * You can override this to intercept all key shortcut events before they are
3405 * dispatched to the window. Be sure to call this implementation for key shortcut
3406 * events that should be handled normally.
3407 *
3408 * @param event The key shortcut event.
3409 * @return True if this event was consumed.
3410 */
3411 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3412 onUserInteraction();
3413 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3414 return true;
3415 }
3416 return onKeyShortcut(event.getKeyCode(), event);
3417 }
3418
3419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 * Called to process touch screen events. You can override this to
3421 * intercept all touch screen events before they are dispatched to the
3422 * window. Be sure to call this implementation for touch screen events
3423 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 * @return boolean Return true if this event was consumed.
3428 */
3429 public boolean dispatchTouchEvent(MotionEvent ev) {
3430 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3431 onUserInteraction();
3432 }
3433 if (getWindow().superDispatchTouchEvent(ev)) {
3434 return true;
3435 }
3436 return onTouchEvent(ev);
3437 }
RoboErik55011652014-07-09 15:05:53 -07003438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 /**
3440 * Called to process trackball events. You can override this to
3441 * intercept all trackball events before they are dispatched to the
3442 * window. Be sure to call this implementation for trackball events
3443 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003446 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 * @return boolean Return true if this event was consumed.
3448 */
3449 public boolean dispatchTrackballEvent(MotionEvent ev) {
3450 onUserInteraction();
3451 if (getWindow().superDispatchTrackballEvent(ev)) {
3452 return true;
3453 }
3454 return onTrackballEvent(ev);
3455 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003456
Jeff Browncb1404e2011-01-15 18:14:15 -08003457 /**
3458 * Called to process generic motion events. You can override this to
3459 * intercept all generic motion events before they are dispatched to the
3460 * window. Be sure to call this implementation for generic motion events
3461 * that should be handled normally.
3462 *
3463 * @param ev The generic motion event.
3464 *
3465 * @return boolean Return true if this event was consumed.
3466 */
3467 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3468 onUserInteraction();
3469 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3470 return true;
3471 }
3472 return onGenericMotionEvent(ev);
3473 }
3474
svetoslavganov75986cf2009-05-14 22:28:01 -07003475 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3476 event.setClassName(getClass().getName());
3477 event.setPackageName(getPackageName());
3478
3479 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003480 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3481 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003482 event.setFullScreen(isFullScreen);
3483
3484 CharSequence title = getTitle();
3485 if (!TextUtils.isEmpty(title)) {
3486 event.getText().add(title);
3487 }
3488
3489 return true;
3490 }
3491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 /**
3493 * Default implementation of
3494 * {@link android.view.Window.Callback#onCreatePanelView}
3495 * for activities. This
3496 * simply returns null so that all panel sub-windows will have the default
3497 * menu behavior.
3498 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003499 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 public View onCreatePanelView(int featureId) {
3501 return null;
3502 }
3503
3504 /**
3505 * Default implementation of
3506 * {@link android.view.Window.Callback#onCreatePanelMenu}
3507 * for activities. This calls through to the new
3508 * {@link #onCreateOptionsMenu} method for the
3509 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3510 * so that subclasses of Activity don't need to deal with feature codes.
3511 */
3512 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3513 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003514 boolean show = onCreateOptionsMenu(menu);
3515 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3516 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518 return false;
3519 }
3520
3521 /**
3522 * Default implementation of
3523 * {@link android.view.Window.Callback#onPreparePanel}
3524 * for activities. This
3525 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3526 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3527 * panel, so that subclasses of
3528 * Activity don't need to deal with feature codes.
3529 */
3530 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3531 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3532 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003533 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003534 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536 return true;
3537 }
3538
3539 /**
3540 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 * @return The default implementation returns true.
3543 */
3544 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003545 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003546 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003547 if (mActionBar != null) {
3548 mActionBar.dispatchMenuVisibilityChanged(true);
3549 } else {
3550 Log.e(TAG, "Tried to open action bar menu with no action bar");
3551 }
Adam Powell8515ee82010-11-30 14:09:55 -08003552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 return true;
3554 }
3555
3556 /**
3557 * Default implementation of
3558 * {@link android.view.Window.Callback#onMenuItemSelected}
3559 * for activities. This calls through to the new
3560 * {@link #onOptionsItemSelected} method for the
3561 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3562 * panel, so that subclasses of
3563 * Activity don't need to deal with feature codes.
3564 */
3565 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003566 CharSequence titleCondensed = item.getTitleCondensed();
3567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 switch (featureId) {
3569 case Window.FEATURE_OPTIONS_PANEL:
3570 // Put event logging here so it gets called even if subclass
3571 // doesn't call through to superclass's implmeentation of each
3572 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003573 if(titleCondensed != null) {
3574 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3575 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003576 if (onOptionsItemSelected(item)) {
3577 return true;
3578 }
Adam Powell04d58112012-04-09 10:22:12 -07003579 if (mFragments.dispatchOptionsItemSelected(item)) {
3580 return true;
3581 }
3582 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3583 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3584 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003585 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003586 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003587 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003588 }
Adam Powell04d58112012-04-09 10:22:12 -07003589 }
3590 return false;
RoboErik55011652014-07-09 15:05:53 -07003591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003593 if(titleCondensed != null) {
3594 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3595 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003596 if (onContextItemSelected(item)) {
3597 return true;
3598 }
3599 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 default:
3602 return false;
3603 }
3604 }
RoboErik55011652014-07-09 15:05:53 -07003605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 /**
3607 * Default implementation of
3608 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3609 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3610 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3611 * so that subclasses of Activity don't need to deal with feature codes.
3612 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3613 * {@link #onContextMenuClosed(Menu)} will be called.
3614 */
3615 public void onPanelClosed(int featureId, Menu menu) {
3616 switch (featureId) {
3617 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003618 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 onOptionsMenuClosed(menu);
3620 break;
RoboErik55011652014-07-09 15:05:53 -07003621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 case Window.FEATURE_CONTEXT_MENU:
3623 onContextMenuClosed(menu);
3624 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003625
3626 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003627 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003628 mActionBar.dispatchMenuVisibilityChanged(false);
3629 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 }
3631 }
3632
3633 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003634 * Declare that the options menu has changed, so should be recreated.
3635 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3636 * time it needs to be displayed.
3637 */
3638 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003639 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3640 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003641 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3642 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003643 }
RoboErik55011652014-07-09 15:05:53 -07003644
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003645 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 * Initialize the contents of the Activity's standard options menu. You
3647 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003648 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 * <p>This is only called once, the first time the options menu is
3650 * displayed. To update the menu every time it is displayed, see
3651 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003652 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003654 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3655 * they will be correctly ordered with application-defined menu items.
3656 * Deriving classes should always call through to the base implementation.
3657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 * <p>You can safely hold on to <var>menu</var> (and any items created
3659 * from it), making modifications to it as desired, until the next
3660 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003661 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 * <p>When you add items to the menu, you can implement the Activity's
3663 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003664 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 * @return You must return true for the menu to be displayed;
3668 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003669 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 * @see #onPrepareOptionsMenu
3671 * @see #onOptionsItemSelected
3672 */
3673 public boolean onCreateOptionsMenu(Menu menu) {
3674 if (mParent != null) {
3675 return mParent.onCreateOptionsMenu(menu);
3676 }
3677 return true;
3678 }
3679
3680 /**
3681 * Prepare the Screen's standard options menu to be displayed. This is
3682 * called right before the menu is shown, every time it is shown. You can
3683 * use this method to efficiently enable/disable items or otherwise
3684 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003685 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 * <p>The default implementation updates the system menu items based on the
3687 * activity's state. Deriving classes should always call through to the
3688 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003689 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 * @param menu The options menu as last shown or first initialized by
3691 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003692 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 * @return You must return true for the menu to be displayed;
3694 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003695 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 * @see #onCreateOptionsMenu
3697 */
3698 public boolean onPrepareOptionsMenu(Menu menu) {
3699 if (mParent != null) {
3700 return mParent.onPrepareOptionsMenu(menu);
3701 }
3702 return true;
3703 }
3704
3705 /**
3706 * This hook is called whenever an item in your options menu is selected.
3707 * The default implementation simply returns false to have the normal
3708 * processing happen (calling the item's Runnable or sending a message to
3709 * its Handler as appropriate). You can use this method for any items
3710 * for which you would like to do processing without those other
3711 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003714 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003715 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003717 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 * @return boolean Return false to allow normal menu processing to
3719 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003720 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 * @see #onCreateOptionsMenu
3722 */
3723 public boolean onOptionsItemSelected(MenuItem item) {
3724 if (mParent != null) {
3725 return mParent.onOptionsItemSelected(item);
3726 }
3727 return false;
3728 }
3729
3730 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003731 * This method is called whenever the user chooses to navigate Up within your application's
3732 * activity hierarchy from the action bar.
3733 *
3734 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3735 * was specified in the manifest for this activity or an activity-alias to it,
3736 * default Up navigation will be handled automatically. If any activity
3737 * along the parent chain requires extra Intent arguments, the Activity subclass
3738 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3739 * to supply those arguments.</p>
3740 *
Mark Lufa434852016-08-11 17:40:33 -07003741 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07003742 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3743 * from the design guide for more information about navigating within your app.</p>
3744 *
3745 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3746 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3747 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3748 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3749 *
3750 * @return true if Up navigation completed successfully and this Activity was finished,
3751 * false otherwise.
3752 */
3753 public boolean onNavigateUp() {
3754 // Automatically handle hierarchical Up navigation if the proper
3755 // metadata is available.
3756 Intent upIntent = getParentActivityIntent();
3757 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003758 if (mActivityInfo.taskAffinity == null) {
3759 // Activities with a null affinity are special; they really shouldn't
3760 // specify a parent activity intent in the first place. Just finish
3761 // the current activity and call it a day.
3762 finish();
3763 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003764 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003765 onCreateNavigateUpTaskStack(b);
3766 onPrepareNavigateUpTaskStack(b);
3767 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003768
3769 // We can't finishAffinity if we have a result.
3770 // Fall back and simply finish the current activity instead.
3771 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3772 // Tell the developer what's going on to avoid hair-pulling.
3773 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3774 finish();
3775 } else {
3776 finishAffinity();
3777 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003778 } else {
3779 navigateUpTo(upIntent);
3780 }
3781 return true;
3782 }
3783 return false;
3784 }
3785
3786 /**
3787 * This is called when a child activity of this one attempts to navigate up.
3788 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3789 *
3790 * @param child The activity making the call.
3791 */
3792 public boolean onNavigateUpFromChild(Activity child) {
3793 return onNavigateUp();
3794 }
3795
3796 /**
3797 * Define the synthetic task stack that will be generated during Up navigation from
3798 * a different task.
3799 *
3800 * <p>The default implementation of this method adds the parent chain of this activity
3801 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3802 * may choose to override this method to construct the desired task stack in a different
3803 * way.</p>
3804 *
Adam Powellf0195952012-05-02 21:38:54 -07003805 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3806 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3807 * returned by {@link #getParentActivityIntent()}.</p>
3808 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003809 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3810 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3811 *
3812 * @param builder An empty TaskStackBuilder - the application should add intents representing
3813 * the desired task stack
3814 */
3815 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3816 builder.addParentStack(this);
3817 }
3818
3819 /**
3820 * Prepare the synthetic task stack that will be generated during Up navigation
3821 * from a different task.
3822 *
3823 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3824 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3825 * If any extra data should be added to these intents before launching the new task,
3826 * the application should override this method and add that data here.</p>
3827 *
3828 * @param builder A TaskStackBuilder that has been populated with Intents by
3829 * onCreateNavigateUpTaskStack.
3830 */
3831 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3832 }
3833
3834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 * This hook is called whenever the options menu is being closed (either by the user canceling
3836 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003837 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 * @param menu The options menu as last shown or first initialized by
3839 * onCreateOptionsMenu().
3840 */
3841 public void onOptionsMenuClosed(Menu menu) {
3842 if (mParent != null) {
3843 mParent.onOptionsMenuClosed(menu);
3844 }
3845 }
RoboErik55011652014-07-09 15:05:53 -07003846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 /**
3848 * Programmatically opens the options menu. If the options menu is already
3849 * open, this method does nothing.
3850 */
3851 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003852 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3853 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003854 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 }
RoboErik55011652014-07-09 15:05:53 -07003857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 /**
3859 * Progammatically closes the options menu. If the options menu is already
3860 * closed, this method does nothing.
3861 */
3862 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07003863 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3864 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003865 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 }
3868
3869 /**
3870 * Called when a context menu for the {@code view} is about to be shown.
3871 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3872 * time the context menu is about to be shown and should be populated for
3873 * the view (or item inside the view for {@link AdapterView} subclasses,
3874 * this can be found in the {@code menuInfo})).
3875 * <p>
3876 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3877 * item has been selected.
3878 * <p>
3879 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003880 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 */
3882 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3883 }
3884
3885 /**
3886 * Registers a context menu to be shown for the given view (multiple views
3887 * can show the context menu). This method will set the
3888 * {@link OnCreateContextMenuListener} on the view to this activity, so
3889 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3890 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003891 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 * @see #unregisterForContextMenu(View)
3893 * @param view The view that should show a context menu.
3894 */
3895 public void registerForContextMenu(View view) {
3896 view.setOnCreateContextMenuListener(this);
3897 }
RoboErik55011652014-07-09 15:05:53 -07003898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 /**
3900 * Prevents a context menu to be shown for the given view. This method will remove the
3901 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003902 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 * @see #registerForContextMenu(View)
3904 * @param view The view that should stop showing a context menu.
3905 */
3906 public void unregisterForContextMenu(View view) {
3907 view.setOnCreateContextMenuListener(null);
3908 }
RoboErik55011652014-07-09 15:05:53 -07003909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 /**
3911 * Programmatically opens the context menu for a particular {@code view}.
3912 * The {@code view} should have been added via
3913 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003914 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 * @param view The view to show the context menu for.
3916 */
3917 public void openContextMenu(View view) {
3918 view.showContextMenu();
3919 }
RoboErik55011652014-07-09 15:05:53 -07003920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 /**
3922 * Programmatically closes the most recently opened context menu, if showing.
3923 */
3924 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003925 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3926 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
RoboErik55011652014-07-09 15:05:53 -07003929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 /**
3931 * This hook is called whenever an item in a context menu is selected. The
3932 * default implementation simply returns false to have the normal processing
3933 * happen (calling the item's Runnable or sending a message to its Handler
3934 * as appropriate). You can use this method for any items for which you
3935 * would like to do processing without those other facilities.
3936 * <p>
3937 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3938 * View that added this menu item.
3939 * <p>
3940 * Derived classes should call through to the base class for it to perform
3941 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003942 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 * @param item The context menu item that was selected.
3944 * @return boolean Return false to allow normal context menu processing to
3945 * proceed, true to consume it here.
3946 */
3947 public boolean onContextItemSelected(MenuItem item) {
3948 if (mParent != null) {
3949 return mParent.onContextItemSelected(item);
3950 }
3951 return false;
3952 }
3953
3954 /**
3955 * This hook is called whenever the context menu is being closed (either by
3956 * the user canceling the menu with the back/menu button, or when an item is
3957 * selected).
RoboErik55011652014-07-09 15:05:53 -07003958 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 * @param menu The context menu that is being closed.
3960 */
3961 public void onContextMenuClosed(Menu menu) {
3962 if (mParent != null) {
3963 mParent.onContextMenuClosed(menu);
3964 }
3965 }
3966
3967 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003968 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003970 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 protected Dialog onCreateDialog(int id) {
3972 return null;
3973 }
3974
3975 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003976 * Callback for creating dialogs that are managed (saved and restored) for you
3977 * by the activity. The default implementation calls through to
3978 * {@link #onCreateDialog(int)} for compatibility.
3979 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003980 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3981 * or later, consider instead using a {@link DialogFragment} instead.</em>
3982 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003983 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3984 * this method the first time, and hang onto it thereafter. Any dialog
3985 * that is created by this method will automatically be saved and restored
3986 * for you, including whether it is showing.
3987 *
3988 * <p>If you would like the activity to manage saving and restoring dialogs
3989 * for you, you should override this method and handle any ids that are
3990 * passed to {@link #showDialog}.
3991 *
3992 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3993 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3994 *
3995 * @param id The id of the dialog.
3996 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3997 * @return The dialog. If you return null, the dialog will not be created.
3998 *
3999 * @see #onPrepareDialog(int, Dialog, Bundle)
4000 * @see #showDialog(int, Bundle)
4001 * @see #dismissDialog(int)
4002 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004003 *
4004 * @deprecated Use the new {@link DialogFragment} class with
4005 * {@link FragmentManager} instead; this is also
4006 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004007 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004008 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004009 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004010 protected Dialog onCreateDialog(int id, Bundle args) {
4011 return onCreateDialog(id);
4012 }
4013
4014 /**
4015 * @deprecated Old no-arguments version of
4016 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4017 */
4018 @Deprecated
4019 protected void onPrepareDialog(int id, Dialog dialog) {
4020 dialog.setOwnerActivity(this);
4021 }
4022
4023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004025 * shown. The default implementation calls through to
4026 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004027 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 * <p>
4029 * Override this if you need to update a managed dialog based on the state
4030 * of the application each time it is shown. For example, a time picker
4031 * dialog might want to be updated with the current time. You should call
4032 * through to the superclass's implementation. The default implementation
4033 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 * @param id The id of the managed dialog.
4036 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004037 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4038 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 * @see #showDialog(int)
4040 * @see #dismissDialog(int)
4041 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004042 *
4043 * @deprecated Use the new {@link DialogFragment} class with
4044 * {@link FragmentManager} instead; this is also
4045 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004047 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004048 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4049 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 }
4051
4052 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004053 * Simple version of {@link #showDialog(int, Bundle)} that does not
4054 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4055 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004056 *
4057 * @deprecated Use the new {@link DialogFragment} class with
4058 * {@link FragmentManager} instead; this is also
4059 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004060 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004061 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004062 public final void showDialog(int id) {
4063 showDialog(id, null);
4064 }
4065
4066 /**
4067 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 * will be made with the same id the first time this is called for a given
4069 * id. From thereafter, the dialog will be automatically saved and restored.
4070 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004071 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4072 * or later, consider instead using a {@link DialogFragment} instead.</em>
4073 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004074 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 * be made to provide an opportunity to do any timely preparation.
4076 *
4077 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004078 * @param args Arguments to pass through to the dialog. These will be saved
4079 * and restored for you. Note that if the dialog is already created,
4080 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4081 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004082 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004083 * @return Returns true if the Dialog was created; false is returned if
4084 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004085 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004086 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004087 * @see #onCreateDialog(int, Bundle)
4088 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 * @see #dismissDialog(int)
4090 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004091 *
4092 * @deprecated Use the new {@link DialogFragment} class with
4093 * {@link FragmentManager} instead; this is also
4094 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004096 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004097 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004099 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004101 ManagedDialog md = mManagedDialogs.get(id);
4102 if (md == null) {
4103 md = new ManagedDialog();
4104 md.mDialog = createDialog(id, null, args);
4105 if (md.mDialog == null) {
4106 return false;
4107 }
4108 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 }
RoboErik55011652014-07-09 15:05:53 -07004110
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004111 md.mArgs = args;
4112 onPrepareDialog(id, md.mDialog, args);
4113 md.mDialog.show();
4114 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 }
4116
4117 /**
4118 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4119 *
4120 * @param id The id of the managed dialog.
4121 *
4122 * @throws IllegalArgumentException if the id was not previously shown via
4123 * {@link #showDialog(int)}.
4124 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004125 * @see #onCreateDialog(int, Bundle)
4126 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 * @see #showDialog(int)
4128 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004129 *
4130 * @deprecated Use the new {@link DialogFragment} class with
4131 * {@link FragmentManager} instead; this is also
4132 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004134 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 public final void dismissDialog(int id) {
4136 if (mManagedDialogs == null) {
4137 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 }
RoboErik55011652014-07-09 15:05:53 -07004139
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004140 final ManagedDialog md = mManagedDialogs.get(id);
4141 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 throw missingDialog(id);
4143 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004144 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 }
4146
4147 /**
4148 * Creates an exception to throw if a user passed in a dialog id that is
4149 * unexpected.
4150 */
4151 private IllegalArgumentException missingDialog(int id) {
4152 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4153 + "shown via Activity#showDialog");
4154 }
4155
4156 /**
4157 * Removes any internal references to a dialog managed by this Activity.
4158 * If the dialog is showing, it will dismiss it as part of the clean up.
4159 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004160 * <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 -08004161 * want to avoid the overhead of saving and restoring it in the future.
4162 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004163 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4164 * will not throw an exception if you try to remove an ID that does not
4165 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 * @param id The id of the managed dialog.
4168 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004169 * @see #onCreateDialog(int, Bundle)
4170 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 * @see #showDialog(int)
4172 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004173 *
4174 * @deprecated Use the new {@link DialogFragment} class with
4175 * {@link FragmentManager} instead; this is also
4176 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004178 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004180 if (mManagedDialogs != null) {
4181 final ManagedDialog md = mManagedDialogs.get(id);
4182 if (md != null) {
4183 md.mDialog.dismiss();
4184 mManagedDialogs.remove(id);
4185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188
4189 /**
4190 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004191 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004192 * <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 -07004193 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004194 * calling this function is the same as calling
4195 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4196 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004197 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 * <p>You can override this function to force global search, e.g. in response to a dedicated
4199 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004200 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004201 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4202 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4203 * return false and you must supply your own custom implementation if you want to support
4204 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004205 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004206 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004207 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4208 * not respond to search. The default implementation always returns {@code true}, except
4209 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 * @see android.app.SearchManager
4212 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004213 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4214 mSearchEvent = searchEvent;
4215 boolean result = onSearchRequested();
4216 mSearchEvent = null;
4217 return result;
4218 }
4219
4220 /**
4221 * @see #onSearchRequested(SearchEvent)
4222 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004224 final int uiMode = getResources().getConfiguration().uiMode
4225 & Configuration.UI_MODE_TYPE_MASK;
4226 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4227 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004228 startSearch(null, false, null, false);
4229 return true;
4230 } else {
4231 return false;
4232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 }
RoboErik55011652014-07-09 15:05:53 -07004234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004236 * During the onSearchRequested() callbacks, this function will return the
4237 * {@link SearchEvent} that triggered the callback, if it exists.
4238 *
4239 * @return SearchEvent The SearchEvent that triggered the {@link
4240 * #onSearchRequested} callback.
4241 */
4242 public final SearchEvent getSearchEvent() {
4243 return mSearchEvent;
4244 }
4245
4246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004248 *
4249 * <p>It is typically called from onSearchRequested(), either directly from
4250 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004252 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 * is to inject specific data such as context data, it is preferred to <i>override</i>
4254 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004255 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004256 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4257 * not supported.
4258 *
RoboErik55011652014-07-09 15:05:53 -07004259 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004261 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 * any further typing will replace it. This is useful for cases where an entire pre-formed
4263 * query is being inserted. If false, the selection point will be placed at the end of the
4264 * inserted query. This is useful when the inserted query is text that the user entered,
4265 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4266 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004267 * @param appSearchData An application can insert application-specific
4268 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 * searches. This data will be returned with SEARCH intent(s). Null if
4270 * no extra data is required.
4271 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004272 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004273 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 * @see android.app.SearchManager
4277 * @see #onSearchRequested
4278 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004279 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4280 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004281 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004282 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004283 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284 }
4285
4286 /**
krosaend2d60142009-08-17 08:56:48 -07004287 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4288 * the search dialog. Made available for testing purposes.
4289 *
4290 * @param query The query to trigger. If empty, the request will be ignored.
4291 * @param appSearchData An application can insert application-specific
4292 * context here, in order to improve quality or specificity of its own
4293 * searches. This data will be returned with SEARCH intent(s). Null if
4294 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004295 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004296 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004297 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004298 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004299 }
4300
4301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 * Request that key events come to this activity. Use this if your
4303 * activity has no views with focus, but the activity still wants
4304 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 * @see android.view.Window#takeKeyEvents
4307 */
4308 public void takeKeyEvents(boolean get) {
4309 getWindow().takeKeyEvents(get);
4310 }
4311
4312 /**
4313 * Enable extended window features. This is a convenience for calling
4314 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 * @param featureId The desired feature as defined in
4317 * {@link android.view.Window}.
4318 * @return Returns true if the requested feature is supported and now
4319 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 * @see android.view.Window#requestFeature
4322 */
4323 public final boolean requestWindowFeature(int featureId) {
4324 return getWindow().requestFeature(featureId);
4325 }
4326
4327 /**
4328 * Convenience for calling
4329 * {@link android.view.Window#setFeatureDrawableResource}.
4330 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004331 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 getWindow().setFeatureDrawableResource(featureId, resId);
4333 }
4334
4335 /**
4336 * Convenience for calling
4337 * {@link android.view.Window#setFeatureDrawableUri}.
4338 */
4339 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4340 getWindow().setFeatureDrawableUri(featureId, uri);
4341 }
4342
4343 /**
4344 * Convenience for calling
4345 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4346 */
4347 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4348 getWindow().setFeatureDrawable(featureId, drawable);
4349 }
4350
4351 /**
4352 * Convenience for calling
4353 * {@link android.view.Window#setFeatureDrawableAlpha}.
4354 */
4355 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4356 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4357 }
4358
4359 /**
4360 * Convenience for calling
4361 * {@link android.view.Window#getLayoutInflater}.
4362 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004363 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 public LayoutInflater getLayoutInflater() {
4365 return getWindow().getLayoutInflater();
4366 }
4367
4368 /**
4369 * Returns a {@link MenuInflater} with this context.
4370 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004371 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004373 // Make sure that action views can get an appropriate theme.
4374 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004375 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004376 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004377 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004378 } else {
4379 mMenuInflater = new MenuInflater(this);
4380 }
4381 }
4382 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 }
4384
4385 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004386 public void setTheme(int resid) {
4387 super.setTheme(resid);
4388 mWindow.setTheme(resid);
4389 }
4390
4391 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004392 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004393 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 if (mParent == null) {
4395 super.onApplyThemeResource(theme, resid, first);
4396 } else {
4397 try {
4398 theme.setTo(mParent.getTheme());
4399 } catch (Exception e) {
4400 // Empty
4401 }
4402 theme.applyStyle(resid, false);
4403 }
Winson073a5262016-02-29 16:04:40 -08004404
4405 // Get the primary color and update the TaskDescription for this activity
4406 TypedArray a = theme.obtainStyledAttributes(
4407 com.android.internal.R.styleable.ActivityTaskDescription);
4408 if (mTaskDescription.getPrimaryColor() == 0) {
4409 int colorPrimary = a.getColor(
4410 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4411 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4412 mTaskDescription.setPrimaryColor(colorPrimary);
4413 }
4414 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004415
4416 int colorBackground = a.getColor(
4417 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4418 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4419 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004420 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004421
4422 final int statusBarColor = a.getColor(
4423 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4424 if (statusBarColor != 0) {
4425 mTaskDescription.setStatusBarColor(statusBarColor);
4426 }
4427
4428 final int navigationBarColor = a.getColor(
4429 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4430 if (navigationBarColor != 0) {
4431 mTaskDescription.setNavigationBarColor(navigationBarColor);
4432 }
4433
Winson073a5262016-02-29 16:04:40 -08004434 a.recycle();
4435 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004436 }
4437
4438 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004439 * Requests permissions to be granted to this application. These permissions
4440 * must be requested in your manifest, they should not be granted to your app,
4441 * and they should have protection level {@link android.content.pm.PermissionInfo
4442 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4443 * the platform or a third-party app.
4444 * <p>
4445 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4446 * are granted at install time if requested in the manifest. Signature permissions
4447 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4448 * install time if requested in the manifest and the signature of your app matches
4449 * the signature of the app declaring the permissions.
4450 * </p>
4451 * <p>
4452 * If your app does not have the requested permissions the user will be presented
4453 * with UI for accepting them. After the user has accepted or rejected the
4454 * requested permissions you will receive a callback on {@link
4455 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4456 * permissions were granted or not.
4457 * </p>
4458 * <p>
4459 * Note that requesting a permission does not guarantee it will be granted and
4460 * your app should be able to run without having this permission.
4461 * </p>
4462 * <p>
4463 * This method may start an activity allowing the user to choose which permissions
4464 * to grant and which to reject. Hence, you should be prepared that your activity
4465 * may be paused and resumed. Further, granting some permissions may require
4466 * a restart of you application. In such a case, the system will recreate the
4467 * activity stack before delivering the result to {@link
4468 * #onRequestPermissionsResult(int, String[], int[])}.
4469 * </p>
4470 * <p>
4471 * When checking whether you have a permission you should use {@link
4472 * #checkSelfPermission(String)}.
4473 * </p>
4474 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004475 * Calling this API for permissions already granted to your app would show UI
4476 * to the user to decide whether the app can still hold these permissions. This
4477 * can be useful if the way your app uses data guarded by the permissions
4478 * changes significantly.
4479 * </p>
4480 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004481 * You cannot request a permission if your activity sets {@link
4482 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4483 * <code>true</code> because in this case the activity would not receive
4484 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4485 * </p>
4486 * <p>
Trevor Johns682c24e2016-04-12 10:13:47 -07004487 * The <a href="http://developer.android.com/samples/RuntimePermissions/index.html">
4488 * RuntimePermissions</a> sample app demonstrates how to use this method to
4489 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004490 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004491 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004492 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004493 * @param requestCode Application specific request code to match with a result
4494 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004495 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004496 *
Paul Miller77a0e132016-02-11 13:46:49 -08004497 * @throws IllegalArgumentException if requestCode is negative.
4498 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004499 * @see #onRequestPermissionsResult(int, String[], int[])
4500 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004501 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004502 */
4503 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004504 if (requestCode < 0) {
4505 throw new IllegalArgumentException("requestCode should be >= 0");
4506 }
Svetoslavffb32b12015-10-15 16:54:00 -07004507 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00004508 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07004509 // Dispatch the callback with empty arrays which means a cancellation.
4510 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4511 return;
4512 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004513 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004514 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004515 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004516 }
4517
4518 /**
4519 * Callback for the result from requesting permissions. This method
4520 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004521 * <p>
4522 * <strong>Note:</strong> It is possible that the permissions request interaction
4523 * with the user is interrupted. In this case you will receive empty permissions
4524 * and results arrays which should be treated as a cancellation.
4525 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004526 *
4527 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4528 * @param permissions The requested permissions. Never null.
4529 * @param grantResults The grant results for the corresponding permissions
4530 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4531 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4532 *
4533 * @see #requestPermissions(String[], int)
4534 */
4535 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4536 @NonNull int[] grantResults) {
4537 /* callback - no nothing */
4538 }
4539
4540 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004541 * Gets whether you should show UI with rationale for requesting a permission.
4542 * You should do this only if you do not have the permission and the context in
4543 * which the permission is requested does not clearly communicate to the user
4544 * what would be the benefit from granting this permission.
4545 * <p>
4546 * For example, if you write a camera app, requesting the camera permission
4547 * would be expected by the user and no rationale for why it is requested is
4548 * needed. If however, the app needs location for tagging photos then a non-tech
4549 * savvy user may wonder how location is related to taking photos. In this case
4550 * you may choose to show UI with rationale of requesting this permission.
4551 * </p>
4552 *
4553 * @param permission A permission your app wants to request.
4554 * @return Whether you can show permission rationale UI.
4555 *
4556 * @see #checkSelfPermission(String)
4557 * @see #requestPermissions(String[], int)
4558 * @see #onRequestPermissionsResult(int, String[], int[])
4559 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004560 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004561 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4562 }
4563
4564 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004565 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4566 * with no options.
4567 *
4568 * @param intent The intent to start.
4569 * @param requestCode If >= 0, this code will be returned in
4570 * onActivityResult() when the activity exits.
4571 *
4572 * @throws android.content.ActivityNotFoundException
4573 *
George Mount0a778ed2013-12-13 13:35:36 -08004574 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004575 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004576 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004577 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004578 }
4579
4580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 * Launch an activity for which you would like a result when it finished.
4582 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004583 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004584 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 * <p>Note that this method should only be used with Intent protocols
4588 * that are defined to return a result. In other protocols (such as
4589 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4590 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05004591 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
4592 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004593 *
RoboErik55011652014-07-09 15:05:53 -07004594 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004596 * activity, then your window will not be displayed until a result is
4597 * returned back from the started activity. This is to avoid visible
4598 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 * <p>This method throws {@link android.content.ActivityNotFoundException}
4601 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004602 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 * @param intent The intent to start.
4604 * @param requestCode If >= 0, this code will be returned in
4605 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004606 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004607 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004608 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004611 *
George Mount0a778ed2013-12-13 13:35:36 -08004612 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004614 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4615 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07004617 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 Instrumentation.ActivityResult ar =
4619 mInstrumentation.execStartActivity(
4620 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004621 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 if (ar != null) {
4623 mMainThread.sendActivityResult(
4624 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4625 ar.getResultData());
4626 }
4627 if (requestCode >= 0) {
4628 // If this start is requesting a result, we can avoid making
4629 // the activity visible until the result is received. Setting
4630 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4631 // activity hidden during this time, to avoid flickering.
4632 // This can only be done when a result is requested because
4633 // that guarantees we will get information back when the
4634 // activity is finished, no matter what happens to it.
4635 mStartedActivity = true;
4636 }
Adam Powell14874662013-07-18 19:42:41 -07004637
George Mount41725de2015-04-09 08:23:05 -07004638 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004639 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004641 if (options != null) {
4642 mParent.startActivityFromChild(this, intent, requestCode, options);
4643 } else {
4644 // Note we want to go through this method for compatibility with
4645 // existing applications that may have overridden it.
4646 mParent.startActivityFromChild(this, intent, requestCode);
4647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
George Mount41725de2015-04-09 08:23:05 -07004649 }
4650
4651 /**
4652 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4653 *
4654 * @param options The ActivityOptions bundle used to start an Activity.
4655 */
4656 private void cancelInputsAndStartExitTransition(Bundle options) {
4657 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4658 if (decor != null) {
4659 decor.cancelPendingInputEvents();
4660 }
George Mountff243282014-07-07 16:12:07 -07004661 if (options != null && !isTopOfTask()) {
4662 mActivityTransitionState.startExitOutTransition(this, options);
4663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 }
4665
Chet Haaseb64e777f2016-12-09 07:17:10 -08004666 /**
4667 * Returns whether there are any activity transitions currently running on this
4668 * activity. A return value of {@code true} can mean that either an enter or
4669 * exit transition is running, including whether the background of the activity
4670 * is animating as a part of that transition.
4671 *
4672 * @return true if a transition is currently running on this activity, false otherwise.
4673 */
4674 public boolean isActivityTransitionRunning() {
4675 return mActivityTransitionState.isTransitionRunning();
4676 }
4677
George Mount413739e2016-06-08 07:13:37 -07004678 private Bundle transferSpringboardActivityOptions(Bundle options) {
4679 if (options == null && (mWindow != null && !mWindow.isActive())) {
4680 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07004681 if (activityOptions != null &&
4682 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07004683 return activityOptions.toBundle();
4684 }
4685 }
4686 return options;
4687 }
4688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004690 * @hide Implement to provide correct calling token.
4691 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01004692 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01004693 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4694 startActivityForResultAsUser(intent, requestCode, null, user);
4695 }
4696
4697 /**
4698 * @hide Implement to provide correct calling token.
4699 */
4700 public void startActivityForResultAsUser(Intent intent, int requestCode,
4701 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01004702 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
4703 }
4704
4705 /**
4706 * @hide Implement to provide correct calling token.
4707 */
4708 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
4709 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004710 if (mParent != null) {
4711 throw new RuntimeException("Can't be called from a child");
4712 }
George Mount413739e2016-06-08 07:13:37 -07004713 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004714 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004715 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01004716 options, user);
4717 if (ar != null) {
4718 mMainThread.sendActivityResult(
4719 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4720 }
4721 if (requestCode >= 0) {
4722 // If this start is requesting a result, we can avoid making
4723 // the activity visible until the result is received. Setting
4724 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4725 // activity hidden during this time, to avoid flickering.
4726 // This can only be done when a result is requested because
4727 // that guarantees we will get information back when the
4728 // activity is finished, no matter what happens to it.
4729 mStartedActivity = true;
4730 }
4731
George Mount41725de2015-04-09 08:23:05 -07004732 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004733 }
4734
4735 /**
4736 * @hide Implement to provide correct calling token.
4737 */
Remi NGUYEN VANbf1ecd12019-01-20 21:18:22 +09004738 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004739 public void startActivityAsUser(Intent intent, UserHandle user) {
4740 startActivityAsUser(intent, null, user);
4741 }
4742
4743 /**
4744 * @hide Implement to provide correct calling token.
4745 */
4746 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4747 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004748 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004749 }
George Mount413739e2016-06-08 07:13:37 -07004750 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004751 Instrumentation.ActivityResult ar =
4752 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004753 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004754 intent, -1, options, user);
4755 if (ar != null) {
4756 mMainThread.sendActivityResult(
4757 mToken, mEmbeddedID, -1, ar.getResultCode(),
4758 ar.getResultData());
4759 }
George Mount41725de2015-04-09 08:23:05 -07004760 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004761 }
4762
4763 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004764 * Start a new activity as if it was started by the activity that started our
4765 * current activity. This is for the resolver and chooser activities, which operate
4766 * as intermediaries that dispatch their intent to the target the user selects -- to
4767 * do this, they must perform all security checks including permission grants as if
4768 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004769 * @param intent The Intent to start.
4770 * @param options ActivityOptions or null.
4771 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4772 * caller it is doing the start is, is actually allowed to start the target activity.
4773 * If you set this to true, you must set an explicit component in the Intent and do any
4774 * appropriate security checks yourself.
4775 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004776 * @hide
4777 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004778 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -05004779 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004780 if (mParent != null) {
4781 throw new RuntimeException("Can't be called from a child");
4782 }
George Mount413739e2016-06-08 07:13:37 -07004783 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004784 Instrumentation.ActivityResult ar =
4785 mInstrumentation.execStartActivityAsCaller(
4786 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -05004787 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004788 if (ar != null) {
4789 mMainThread.sendActivityResult(
4790 mToken, mEmbeddedID, -1, ar.getResultCode(),
4791 ar.getResultData());
4792 }
George Mount41725de2015-04-09 08:23:05 -07004793 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004794 }
4795
4796 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004797 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4798 * Intent, int, int, int, Bundle)} with no options.
4799 *
4800 * @param intent The IntentSender to launch.
4801 * @param requestCode If >= 0, this code will be returned in
4802 * onActivityResult() when the activity exits.
4803 * @param fillInIntent If non-null, this will be provided as the
4804 * intent parameter to {@link IntentSender#sendIntent}.
4805 * @param flagsMask Intent flags in the original IntentSender that you
4806 * would like to change.
4807 * @param flagsValues Desired values for any bits set in
4808 * <var>flagsMask</var>
4809 * @param extraFlags Always set to 0.
4810 */
4811 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004812 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004813 throws IntentSender.SendIntentException {
4814 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4815 flagsValues, extraFlags, null);
4816 }
4817
4818 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004819 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004820 * to use a IntentSender to describe the activity to be started. If
4821 * the IntentSender is for an activity, that activity will be started
4822 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4823 * here; otherwise, its associated action will be executed (such as
4824 * sending a broadcast) as if you had called
4825 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004826 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004827 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004828 * @param requestCode If >= 0, this code will be returned in
4829 * onActivityResult() when the activity exits.
4830 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004831 * intent parameter to {@link IntentSender#sendIntent}.
4832 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004833 * would like to change.
4834 * @param flagsValues Desired values for any bits set in
4835 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004836 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004837 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004838 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004839 * Context.startActivity(Intent, Bundle)} for more details. If options
4840 * have also been supplied by the IntentSender, options given here will
4841 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004842 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004843 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004844 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004845 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004846 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004847 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
4848 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004849 } else if (options != null) {
4850 mParent.startIntentSenderFromChild(this, intent, requestCode,
4851 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004852 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004853 // Note we want to go through this call for compatibility with
4854 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004855 mParent.startIntentSenderFromChild(this, intent, requestCode,
4856 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004857 }
4858 }
4859
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004860 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
4861 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004862 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004863 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004864 try {
4865 String resolvedType = null;
4866 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004867 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004868 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004869 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4870 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004871 int result = ActivityManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07004872 .startActivityIntentSender(mMainThread.getApplicationThread(),
4873 intent != null ? intent.getTarget() : null,
4874 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004875 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004876 requestCode, flagsMask, flagsValues, options);
4877 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004878 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004879 }
4880 Instrumentation.checkStartActivityResult(result, null);
4881 } catch (RemoteException e) {
4882 }
4883 if (requestCode >= 0) {
4884 // If this start is requesting a result, we can avoid making
4885 // the activity visible until the result is received. Setting
4886 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4887 // activity hidden during this time, to avoid flickering.
4888 // This can only be done when a result is requested because
4889 // that guarantees we will get information back when the
4890 // activity is finished, no matter what happens to it.
4891 mStartedActivity = true;
4892 }
4893 }
4894
4895 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004896 * Same as {@link #startActivity(Intent, Bundle)} with no options
4897 * specified.
4898 *
4899 * @param intent The intent to start.
4900 *
4901 * @throws android.content.ActivityNotFoundException
4902 *
Mark Lufa434852016-08-11 17:40:33 -07004903 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004904 * @see #startActivityForResult
4905 */
4906 @Override
4907 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004908 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004909 }
4910
4911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 * Launch a new activity. You will not receive any information about when
4913 * the activity exits. This implementation overrides the base version,
4914 * providing information about
4915 * the activity performing the launch. Because of this additional
4916 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4917 * required; if not specified, the new activity will be added to the
4918 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 * <p>This method throws {@link android.content.ActivityNotFoundException}
4921 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004922 *
4923 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004924 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004925 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004926 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004927 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004929 *
Mark Lufa434852016-08-11 17:40:33 -07004930 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07004931 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 */
4933 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004934 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004935 if (options != null) {
4936 startActivityForResult(intent, -1, options);
4937 } else {
4938 // Note we want to go through this call for compatibility with
4939 // applications that may have overridden the method.
4940 startActivityForResult(intent, -1);
4941 }
4942 }
4943
4944 /**
4945 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4946 * specified.
4947 *
4948 * @param intents The intents to start.
4949 *
4950 * @throws android.content.ActivityNotFoundException
4951 *
Mark Lufa434852016-08-11 17:40:33 -07004952 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004953 * @see #startActivityForResult
4954 */
4955 @Override
4956 public void startActivities(Intent[] intents) {
4957 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 }
4959
4960 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004961 * Launch a new activity. You will not receive any information about when
4962 * the activity exits. This implementation overrides the base version,
4963 * providing information about
4964 * the activity performing the launch. Because of this additional
4965 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4966 * required; if not specified, the new activity will be added to the
4967 * task of the caller.
4968 *
4969 * <p>This method throws {@link android.content.ActivityNotFoundException}
4970 * if there was no Activity found to run the given Intent.
4971 *
4972 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004973 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004974 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004975 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004976 *
4977 * @throws android.content.ActivityNotFoundException
4978 *
Mark Lufa434852016-08-11 17:40:33 -07004979 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004980 * @see #startActivityForResult
4981 */
4982 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004983 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004984 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004985 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004986 }
4987
4988 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004989 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4990 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004991 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004992 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004993 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004994 * intent parameter to {@link IntentSender#sendIntent}.
4995 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004996 * would like to change.
4997 * @param flagsValues Desired values for any bits set in
4998 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004999 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005000 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005001 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005002 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005003 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005004 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5005 extraFlags, null);
5006 }
5007
5008 /**
5009 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5010 * to start; see
5011 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5012 * for more information.
5013 *
5014 * @param intent The IntentSender to launch.
5015 * @param fillInIntent If non-null, this will be provided as the
5016 * intent parameter to {@link IntentSender#sendIntent}.
5017 * @param flagsMask Intent flags in the original IntentSender that you
5018 * would like to change.
5019 * @param flagsValues Desired values for any bits set in
5020 * <var>flagsMask</var>
5021 * @param extraFlags Always set to 0.
5022 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005023 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005024 * Context.startActivity(Intent, Bundle)} for more details. If options
5025 * have also been supplied by the IntentSender, options given here will
5026 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005027 */
5028 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005029 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005030 Bundle options) throws IntentSender.SendIntentException {
5031 if (options != null) {
5032 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5033 flagsValues, extraFlags, options);
5034 } else {
5035 // Note we want to go through this call for compatibility with
5036 // applications that may have overridden the method.
5037 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5038 flagsValues, extraFlags);
5039 }
5040 }
5041
5042 /**
5043 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5044 * with no options.
5045 *
5046 * @param intent The intent to start.
5047 * @param requestCode If >= 0, this code will be returned in
5048 * onActivityResult() when the activity exits, as described in
5049 * {@link #startActivityForResult}.
5050 *
5051 * @return If a new activity was launched then true is returned; otherwise
5052 * false is returned and you must handle the Intent yourself.
5053 *
5054 * @see #startActivity
5055 * @see #startActivityForResult
5056 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005057 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5058 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005059 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005060 }
5061
5062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 * A special variation to launch an activity only if a new activity
5064 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005065 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005067 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005069 * and the activity
5070 * that handles <var>intent</var> is the same as your currently running
5071 * activity, then a new instance is not needed. In this case, instead of
5072 * the normal behavior of calling {@link #onNewIntent} this function will
5073 * return and you can handle the Intent yourself.
5074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 * <p>This function can only be called from a top-level activity; if it is
5076 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005077 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 * @param intent The intent to start.
5079 * @param requestCode If >= 0, this code will be returned in
5080 * onActivityResult() when the activity exits, as described in
5081 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005082 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005083 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005084 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005085 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 * @return If a new activity was launched then true is returned; otherwise
5087 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005088 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 * @see #startActivity
5090 * @see #startActivityForResult
5091 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005092 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5093 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005095 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005097 Uri referrer = onProvideReferrer();
5098 if (referrer != null) {
5099 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5100 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005101 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005102 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005103 result = ActivityManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005104 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005105 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5106 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5107 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 } catch (RemoteException e) {
5109 // Empty
5110 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 if (requestCode >= 0) {
5115 // If this start is requesting a result, we can avoid making
5116 // the activity visible until the result is received. Setting
5117 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5118 // activity hidden during this time, to avoid flickering.
5119 // This can only be done when a result is requested because
5120 // that guarantees we will get information back when the
5121 // activity is finished, no matter what happens to it.
5122 mStartedActivity = true;
5123 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005124 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 }
5126
5127 throw new UnsupportedOperationException(
5128 "startActivityIfNeeded can only be called from a top-level activity");
5129 }
5130
5131 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005132 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5133 * no options.
5134 *
5135 * @param intent The intent to dispatch to the next activity. For
5136 * correct behavior, this must be the same as the Intent that started
5137 * your own activity; the only changes you can make are to the extras
5138 * inside of it.
5139 *
5140 * @return Returns a boolean indicating whether there was another Activity
5141 * to start: true if there was a next activity to start, false if there
5142 * wasn't. In general, if true is returned you will then want to call
5143 * finish() on yourself.
5144 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005145 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005146 return startNextMatchingActivity(intent, null);
5147 }
5148
5149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 * Special version of starting an activity, for use when you are replacing
5151 * other activity components. You can use this to hand the Intent off
5152 * to the next Activity that can handle it. You typically call this in
5153 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 * @param intent The intent to dispatch to the next activity. For
5156 * correct behavior, this must be the same as the Intent that started
5157 * your own activity; the only changes you can make are to the extras
5158 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005159 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005160 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005161 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 * @return Returns a boolean indicating whether there was another Activity
5164 * to start: true if there was a next activity to start, false if there
5165 * wasn't. In general, if true is returned you will then want to call
5166 * finish() on yourself.
5167 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005168 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5169 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 if (mParent == null) {
5171 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005172 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005173 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005174 return ActivityManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005175 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 } catch (RemoteException e) {
5177 // Empty
5178 }
5179 return false;
5180 }
5181
5182 throw new UnsupportedOperationException(
5183 "startNextMatchingActivity can only be called from a top-level activity");
5184 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005185
5186 /**
5187 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5188 * with no options.
5189 *
5190 * @param child The activity making the call.
5191 * @param intent The intent to start.
5192 * @param requestCode Reply request code. < 0 if reply is not requested.
5193 *
5194 * @throws android.content.ActivityNotFoundException
5195 *
5196 * @see #startActivity
5197 * @see #startActivityForResult
5198 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005199 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005200 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005201 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005202 }
5203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 /**
RoboErik55011652014-07-09 15:05:53 -07005205 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 * <p>This method throws {@link android.content.ActivityNotFoundException}
5209 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 * @param child The activity making the call.
5212 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005213 * @param requestCode Reply request code. < 0 if reply is not requested.
5214 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005215 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005216 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005217 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005219 *
5220 * @see #startActivity
5221 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005223 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005224 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005225 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 Instrumentation.ActivityResult ar =
5227 mInstrumentation.execStartActivity(
5228 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005229 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 if (ar != null) {
5231 mMainThread.sendActivityResult(
5232 mToken, child.mEmbeddedID, requestCode,
5233 ar.getResultCode(), ar.getResultData());
5234 }
George Mount41725de2015-04-09 08:23:05 -07005235 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237
5238 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005239 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5240 * with no options.
5241 *
5242 * @param fragment The fragment making the call.
5243 * @param intent The intent to start.
5244 * @param requestCode Reply request code. < 0 if reply is not requested.
5245 *
5246 * @throws android.content.ActivityNotFoundException
5247 *
5248 * @see Fragment#startActivity
5249 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005250 *
5251 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5252 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005253 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005254 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005255 public void startActivityFromFragment(@NonNull Fragment fragment,
5256 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005257 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005258 }
5259
5260 /**
RoboErik55011652014-07-09 15:05:53 -07005261 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005262 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5263 * method.
RoboErik55011652014-07-09 15:05:53 -07005264 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005265 * <p>This method throws {@link android.content.ActivityNotFoundException}
5266 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005267 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005268 * @param fragment The fragment making the call.
5269 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005270 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005271 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005272 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005273 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005274 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005275 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005276 *
5277 * @see Fragment#startActivity
5278 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005279 *
5280 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5281 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005282 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005283 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005284 public void startActivityFromFragment(@NonNull Fragment fragment,
5285 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005286 startActivityForResult(fragment.mWho, intent, requestCode, options);
5287 }
5288
5289 /**
5290 * @hide
5291 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005292 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5293 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5294 UserHandle user) {
5295 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5296 }
5297
5298 /**
5299 * @hide
5300 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005301 @Override
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005302 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005303 public void startActivityForResult(
5304 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005305 Uri referrer = onProvideReferrer();
5306 if (referrer != null) {
5307 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5308 }
George Mount413739e2016-06-08 07:13:37 -07005309 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005310 Instrumentation.ActivityResult ar =
5311 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005312 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005313 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005314 if (ar != null) {
5315 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005316 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005317 ar.getResultCode(), ar.getResultData());
5318 }
George Mount41725de2015-04-09 08:23:05 -07005319 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005320 }
5321
5322 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005323 * @hide
5324 */
5325 @Override
5326 public boolean canStartActivityForResult() {
5327 return true;
5328 }
5329
5330 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005331 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5332 * int, Intent, int, int, int, Bundle)} with no options.
5333 */
5334 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5335 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5336 int extraFlags)
5337 throws IntentSender.SendIntentException {
5338 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5339 flagsMask, flagsValues, extraFlags, null);
5340 }
5341
5342 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005343 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005344 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005345 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005346 * for more information.
5347 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005348 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5349 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005350 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005351 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005352 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5353 flagsMask, flagsValues, options);
5354 }
5355
5356 /**
5357 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5358 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5359 * for more information.
5360 *
5361 * @hide
5362 */
5363 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5364 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5365 int extraFlags, @Nullable Bundle options)
5366 throws IntentSender.SendIntentException {
5367 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5368 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005369 }
5370
5371 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005372 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5373 * or {@link #finish} to specify an explicit transition animation to
5374 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005375 *
5376 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5377 * to using this with starting activities is to supply the desired animation
5378 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005379 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005380 * you to specify a custom animation even when starting an activity from
5381 * outside the context of the current top activity.
5382 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005383 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005384 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005385 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005386 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005387 */
5388 public void overridePendingTransition(int enterAnim, int exitAnim) {
5389 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005390 ActivityManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005391 mToken, getPackageName(), enterAnim, exitAnim);
5392 } catch (RemoteException e) {
5393 }
5394 }
RoboErik55011652014-07-09 15:05:53 -07005395
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397 * Call this to set the result that your activity will return to its
5398 * caller.
RoboErik55011652014-07-09 15:05:53 -07005399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 * @param resultCode The result code to propagate back to the originating
5401 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 * @see #RESULT_CANCELED
5404 * @see #RESULT_OK
5405 * @see #RESULT_FIRST_USER
5406 * @see #setResult(int, Intent)
5407 */
5408 public final void setResult(int resultCode) {
5409 synchronized (this) {
5410 mResultCode = resultCode;
5411 mResultData = null;
5412 }
5413 }
5414
5415 /**
5416 * Call this to set the result that your activity will return to its
5417 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005418 *
5419 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5420 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5421 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5422 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5423 * Activity receiving the result access to the specific URIs in the Intent.
5424 * Access will remain until the Activity has finished (it will remain across the hosting
5425 * process being killed and other temporary destruction) and will be added
5426 * to any existing set of URI permissions it already holds.
5427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 * @param resultCode The result code to propagate back to the originating
5429 * activity, often RESULT_CANCELED or RESULT_OK
5430 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 * @see #RESULT_CANCELED
5433 * @see #RESULT_OK
5434 * @see #RESULT_FIRST_USER
5435 * @see #setResult(int)
5436 */
5437 public final void setResult(int resultCode, Intent data) {
5438 synchronized (this) {
5439 mResultCode = resultCode;
5440 mResultData = data;
5441 }
5442 }
5443
5444 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005445 * Return information about who launched this activity. If the launching Intent
5446 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5447 * that will be returned as-is; otherwise, if known, an
5448 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5449 * package name that started the Intent will be returned. This may return null if no
5450 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5451 * application package was involved.
5452 *
5453 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5454 * return the referrer that submitted that new intent to the activity. Otherwise, it
5455 * always returns the referrer of the original Intent.</p>
5456 *
5457 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5458 * referrer information, applications can spoof it.</p>
5459 */
5460 @Nullable
5461 public Uri getReferrer() {
5462 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005463 try {
5464 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5465 if (referrer != null) {
5466 return referrer;
5467 }
5468 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5469 if (referrerName != null) {
5470 return Uri.parse(referrerName);
5471 }
5472 } catch (BadParcelableException e) {
5473 Log.w(TAG, "Cannot read referrer from intent;"
5474 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005475 }
5476 if (mReferrer != null) {
5477 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5478 }
5479 return null;
5480 }
5481
5482 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005483 * Override to generate the desired referrer for the content currently being shown
5484 * by the app. The default implementation returns null, meaning the referrer will simply
5485 * be the android-app: of the package name of this activity. Return a non-null Uri to
5486 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5487 */
5488 public Uri onProvideReferrer() {
5489 return null;
5490 }
5491
5492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 * Return the name of the package that invoked this activity. This is who
5494 * the data in {@link #setResult setResult()} will be sent to. You can
5495 * use this information to validate that the recipient is allowed to
5496 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005497 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005498 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005499 * did not use the {@link #startActivityForResult}
5500 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005501 * null.</p>
5502 *
5503 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5504 * the result from this method was unstable. If the process hosting the calling
5505 * package was no longer running, it would return null instead of the proper package
5506 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5507 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005508 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 * @return The package of the activity that will receive your
5510 * reply, or null if none.
5511 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005512 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 public String getCallingPackage() {
5514 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005515 return ActivityManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 } catch (RemoteException e) {
5517 return null;
5518 }
5519 }
5520
5521 /**
5522 * Return the name of the activity that invoked this activity. This is
5523 * who the data in {@link #setResult setResult()} will be sent to. You
5524 * can use this information to validate that the recipient is allowed to
5525 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005526 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005527 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005528 * did not use the {@link #startActivityForResult}
5529 * form that includes a request code), then the calling package will be
5530 * null.
5531 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005532 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 * reply, or null if none.
5534 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005535 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 public ComponentName getCallingActivity() {
5537 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005538 return ActivityManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 } catch (RemoteException e) {
5540 return null;
5541 }
5542 }
5543
5544 /**
5545 * Control whether this activity's main window is visible. This is intended
5546 * only for the special case of an activity that is not going to show a
5547 * UI itself, but can't just finish prior to onResume() because it needs
5548 * to wait for a service binding or such. Setting this to false allows
5549 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 * <p>The default value for this is taken from the
5552 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5553 */
5554 public void setVisible(boolean visible) {
5555 if (mVisibleFromClient != visible) {
5556 mVisibleFromClient = visible;
5557 if (mVisibleFromServer) {
5558 if (visible) makeVisible();
5559 else mDecor.setVisibility(View.INVISIBLE);
5560 }
5561 }
5562 }
RoboErik55011652014-07-09 15:05:53 -07005563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 void makeVisible() {
5565 if (!mWindowAdded) {
5566 ViewManager wm = getWindowManager();
5567 wm.addView(mDecor, getWindow().getAttributes());
5568 mWindowAdded = true;
5569 }
5570 mDecor.setVisibility(View.VISIBLE);
5571 }
RoboErik55011652014-07-09 15:05:53 -07005572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 /**
5574 * Check to see whether this activity is in the process of finishing,
5575 * either because you called {@link #finish} on it or someone else
5576 * has requested that it finished. This is often used in
5577 * {@link #onPause} to determine whether the activity is simply pausing or
5578 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005579 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005581 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 * @see #finish
5583 */
5584 public boolean isFinishing() {
5585 return mFinished;
5586 }
5587
5588 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005589 * Returns true if the final {@link #onDestroy()} call has been made
5590 * on the Activity, so this instance is now dead.
5591 */
5592 public boolean isDestroyed() {
5593 return mDestroyed;
5594 }
5595
5596 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005597 * Check to see whether this activity is in the process of being destroyed in order to be
5598 * recreated with a new configuration. This is often used in
5599 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5600 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005601 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005602 * @return If the activity is being torn down in order to be recreated with a new configuration,
5603 * returns true; else returns false.
5604 */
5605 public boolean isChangingConfigurations() {
5606 return mChangingConfigurations;
5607 }
5608
5609 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005610 * Cause this Activity to be recreated with a new instance. This results
5611 * in essentially the same flow as when the Activity is created due to
5612 * a configuration change -- the current instance will go through its
5613 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5614 */
5615 public void recreate() {
5616 if (mParent != null) {
5617 throw new IllegalStateException("Can only be called on top-level activity");
5618 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07005619 if (Looper.myLooper() != mMainThread.getLooper()) {
5620 throw new IllegalStateException("Must be called from main thread");
5621 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07005622 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005623 }
5624
5625 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005626 * Finishes the current activity and specifies whether to remove the task associated with this
5627 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005629 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005630 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 if (mParent == null) {
5632 int resultCode;
5633 Intent resultData;
5634 synchronized (this) {
5635 resultCode = mResultCode;
5636 resultData = mResultData;
5637 }
Joe Onorato43a17652011-04-06 19:22:23 -07005638 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005640 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005641 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005642 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005643 if (ActivityManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07005644 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 mFinished = true;
5646 }
5647 } catch (RemoteException e) {
5648 // Empty
5649 }
5650 } else {
5651 mParent.finishFromChild(this);
5652 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07005653
5654 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
5655 // be restored now.
5656 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
5657 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
5658 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
5659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 }
5661
5662 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005663 * Call this when your activity is done and should be closed. The
5664 * ActivityResult is propagated back to whoever launched you via
5665 * onActivityResult().
5666 */
5667 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005668 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005669 }
5670
5671 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005672 * Finish this activity as well as all activities immediately below it
5673 * in the current task that have the same affinity. This is typically
5674 * used when an application can be launched on to another task (such as
5675 * from an ACTION_VIEW of a content type it understands) and the user
5676 * has used the up navigation to switch out of the current task and in
5677 * to its own task. In this case, if the user has navigated down into
5678 * any other activities of the second application, all of those should
5679 * be removed from the original task as part of the task switch.
5680 *
5681 * <p>Note that this finish does <em>not</em> allow you to deliver results
5682 * to the previous activity, and an exception will be thrown if you are trying
5683 * to do so.</p>
5684 */
5685 public void finishAffinity() {
5686 if (mParent != null) {
5687 throw new IllegalStateException("Can not be called from an embedded activity");
5688 }
5689 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5690 throw new IllegalStateException("Can not be called to deliver a result");
5691 }
5692 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005693 if (ActivityManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005694 mFinished = true;
5695 }
5696 } catch (RemoteException e) {
5697 // Empty
5698 }
5699 }
5700
5701 /**
RoboErik55011652014-07-09 15:05:53 -07005702 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 * {@link #finish} method. The default implementation simply calls
5704 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005705 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005707 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 * @see #finish
5709 */
5710 public void finishFromChild(Activity child) {
5711 finish();
5712 }
5713
5714 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005715 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5716 * to reverse its exit Transition. When the exit Transition completes,
5717 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5718 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005719 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005720 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005721 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005722 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005723 finish();
5724 }
George Mountcb4b7d92014-02-25 10:47:55 -08005725 }
5726
5727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 * Force finish another activity that you had previously started with
5729 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 * @param requestCode The request code of the activity that you had
5732 * given to startActivityForResult(). If there are multiple
5733 * activities started with this request code, they
5734 * will all be finished.
5735 */
5736 public void finishActivity(int requestCode) {
5737 if (mParent == null) {
5738 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005739 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5741 } catch (RemoteException e) {
5742 // Empty
5743 }
5744 } else {
5745 mParent.finishActivityFromChild(this, requestCode);
5746 }
5747 }
5748
5749 /**
5750 * This is called when a child activity of this one calls its
5751 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005752 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 * @param child The activity making the call.
5754 * @param requestCode Request code that had been used to start the
5755 * activity.
5756 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005757 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005759 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5761 } catch (RemoteException e) {
5762 // Empty
5763 }
5764 }
5765
5766 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005767 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005768 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005769 */
5770 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005771 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005772 }
5773
5774 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005775 * Ask that the local app instance of this activity be released to free up its memory.
5776 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5777 * a new instance of the activity will later be re-created if needed due to the user
5778 * navigating back to it.
5779 *
5780 * @return Returns true if the activity was in a state that it has started the process
5781 * of destroying its current instance; returns false if for any reason this could not
5782 * be done: it is currently visible to the user, it is already being destroyed, it is
5783 * being finished, it hasn't yet saved its state, etc.
5784 */
5785 public boolean releaseInstance() {
5786 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005787 return ActivityManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005788 } catch (RemoteException e) {
5789 // Empty
5790 }
5791 return false;
5792 }
5793
5794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 * Called when an activity you launched exits, giving you the requestCode
5796 * you started it with, the resultCode it returned, and any additional
5797 * data from it. The <var>resultCode</var> will be
5798 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5799 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 * <p>You will receive this call immediately before onResume() when your
5802 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005803 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005804 * <p>This method is never invoked if your activity sets
5805 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5806 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005807 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 * @param requestCode The integer request code originally supplied to
5809 * startActivityForResult(), allowing you to identify who this
5810 * result came from.
5811 * @param resultCode The integer result code returned by the child activity
5812 * through its setResult().
5813 * @param data An Intent, which can return result data to the caller
5814 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005815 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 * @see #startActivityForResult
5817 * @see #createPendingResult
5818 * @see #setResult(int)
5819 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005820 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 }
5822
5823 /**
George Mount62ab9b72014-05-02 13:51:17 -07005824 * Called when an activity you launched with an activity transition exposes this
5825 * Activity through a returning activity transition, giving you the resultCode
5826 * and any additional data from it. This method will only be called if the activity
5827 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005828 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005829 *
5830 * <p>The purpose of this function is to let the called Activity send a hint about
5831 * its state so that this underlying Activity can prepare to be exposed. A call to
5832 * this method does not guarantee that the called Activity has or will be exiting soon.
5833 * It only indicates that it will expose this Activity's Window and it has
5834 * some data to pass to prepare it.</p>
5835 *
5836 * @param resultCode The integer result code returned by the child activity
5837 * through its setResult().
5838 * @param data An Intent, which can return result data to the caller
5839 * (various data can be attached to Intent "extras").
5840 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005841 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005842 }
5843
5844 /**
RoboErik55011652014-07-09 15:05:53 -07005845 * Create a new PendingIntent object which you can hand to others
5846 * for them to use to send result data back to your
5847 * {@link #onActivityResult} callback. The created object will be either
5848 * one-shot (becoming invalid after a result is sent back) or multiple
5849 * (allowing any number of results to be sent through it).
5850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 * @param requestCode Private request code for the sender that will be
5852 * associated with the result data when it is returned. The sender can not
5853 * modify this value, allowing you to identify incoming results.
5854 * @param data Default data to supply in the result, which may be modified
5855 * by the sender.
5856 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5857 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5858 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5859 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5860 * or any of the flags as supported by
5861 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5862 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 * @return Returns an existing or new PendingIntent matching the given
5865 * parameters. May return null only if
5866 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5867 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005868 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 * @see PendingIntent
5870 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005871 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5872 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 String packageName = getPackageName();
5874 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005875 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005877 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005878 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005880 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07005881 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 return target != null ? new PendingIntent(target) : null;
5883 } catch (RemoteException e) {
5884 // Empty
5885 }
5886 return null;
5887 }
5888
5889 /**
5890 * Change the desired orientation of this activity. If the activity
5891 * is currently in the foreground or otherwise impacting the screen
5892 * orientation, the screen will immediately be changed (possibly causing
5893 * the activity to be restarted). Otherwise, this will be used the next
5894 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005895 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 * @param requestedOrientation An orientation constant as used in
5897 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5898 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005899 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 if (mParent == null) {
5901 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005902 ActivityManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 mToken, requestedOrientation);
5904 } catch (RemoteException e) {
5905 // Empty
5906 }
5907 } else {
5908 mParent.setRequestedOrientation(requestedOrientation);
5909 }
5910 }
RoboErik55011652014-07-09 15:05:53 -07005911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 /**
5913 * Return the current requested orientation of the activity. This will
5914 * either be the orientation requested in its component's manifest, or
5915 * the last requested orientation given to
5916 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005917 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005918 * @return Returns an orientation constant as used in
5919 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5920 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005921 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 public int getRequestedOrientation() {
5923 if (mParent == null) {
5924 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005925 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 .getRequestedOrientation(mToken);
5927 } catch (RemoteException e) {
5928 // Empty
5929 }
5930 } else {
5931 return mParent.getRequestedOrientation();
5932 }
5933 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5934 }
RoboErik55011652014-07-09 15:05:53 -07005935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 /**
5937 * Return the identifier of the task this activity is in. This identifier
5938 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 * @return Task identifier, an opaque integer.
5941 */
5942 public int getTaskId() {
5943 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005944 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005945 .getTaskForActivity(mToken, false);
5946 } catch (RemoteException e) {
5947 return -1;
5948 }
5949 }
5950
5951 /**
5952 * Return whether this activity is the root of a task. The root is the
5953 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005954 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 * @return True if this is the root activity, else false.
5956 */
Winson Chung4d8681f2017-05-23 16:22:08 -07005957 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 public boolean isTaskRoot() {
5959 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005960 return ActivityManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 } catch (RemoteException e) {
5962 return false;
5963 }
5964 }
5965
5966 /**
5967 * Move the task containing this activity to the back of the activity
5968 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 * @param nonRoot If false then this only works if the activity is the root
5971 * of a task; if true it will work for any activity in
5972 * a task.
RoboErik55011652014-07-09 15:05:53 -07005973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 * @return If the task was moved (or it was already at the
5975 * back) true is returned, else false.
5976 */
5977 public boolean moveTaskToBack(boolean nonRoot) {
5978 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005979 return ActivityManager.getService().moveActivityTaskToBack(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 mToken, nonRoot);
5981 } catch (RemoteException e) {
5982 // Empty
5983 }
5984 return false;
5985 }
5986
5987 /**
5988 * Returns class name for this activity with the package prefix removed.
5989 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 * @return The local class name.
5992 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005993 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 public String getLocalClassName() {
5995 final String pkg = getPackageName();
5996 final String cls = mComponent.getClassName();
5997 int packageLen = pkg.length();
5998 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5999 || cls.charAt(packageLen) != '.') {
6000 return cls;
6001 }
6002 return cls.substring(packageLen+1);
6003 }
RoboErik55011652014-07-09 15:05:53 -07006004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006006 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 * @return Returns the complete component name for this activity
6009 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006010 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 return mComponent;
6012 }
6013
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006014 /** @hide */
6015 @Override
6016 public final ComponentName autofillClientGetComponentName() {
6017 return getComponentName();
6018 }
6019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 /**
6021 * Retrieve a {@link SharedPreferences} object for accessing preferences
6022 * that are private to this activity. This simply calls the underlying
6023 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6024 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006025 *
6026 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006027 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 *
6029 * @return Returns the single SharedPreferences instance that can be used
6030 * to retrieve and modify the preference values.
6031 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006032 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 return getSharedPreferences(getLocalClassName(), mode);
6034 }
RoboErik55011652014-07-09 15:05:53 -07006035
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006036 private void ensureSearchManager() {
6037 if (mSearchManager != null) {
6038 return;
6039 }
RoboErik55011652014-07-09 15:05:53 -07006040
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006041 try {
6042 mSearchManager = new SearchManager(this, null);
6043 } catch (ServiceNotFoundException e) {
6044 throw new IllegalStateException(e);
6045 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006046 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006049 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 if (getBaseContext() == null) {
6051 throw new IllegalStateException(
6052 "System services not available to Activities before onCreate()");
6053 }
6054
6055 if (WINDOW_SERVICE.equals(name)) {
6056 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006057 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006058 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006059 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 }
6061 return super.getSystemService(name);
6062 }
6063
6064 /**
6065 * Change the title associated with this activity. If this is a
6066 * top-level activity, the title for its window will change. If it
6067 * is an embedded activity, the parent can do whatever it wants
6068 * with it.
6069 */
6070 public void setTitle(CharSequence title) {
6071 mTitle = title;
6072 onTitleChanged(title, mTitleColor);
6073
6074 if (mParent != null) {
6075 mParent.onChildTitleChanged(this, title);
6076 }
6077 }
6078
6079 /**
6080 * Change the title associated with this activity. If this is a
6081 * top-level activity, the title for its window will change. If it
6082 * is an embedded activity, the parent can do whatever it wants
6083 * with it.
6084 */
6085 public void setTitle(int titleId) {
6086 setTitle(getText(titleId));
6087 }
6088
Alan Viverette2525d9c2013-11-15 14:42:19 -08006089 /**
6090 * Change the color of the title associated with this activity.
6091 * <p>
6092 * This method is deprecated starting in API Level 11 and replaced by action
6093 * bar styles. For information on styling the Action Bar, read the <a
6094 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6095 * guide.
6096 *
6097 * @deprecated Use action bar styles instead.
6098 */
6099 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 public void setTitleColor(int textColor) {
6101 mTitleColor = textColor;
6102 onTitleChanged(mTitle, textColor);
6103 }
6104
6105 public final CharSequence getTitle() {
6106 return mTitle;
6107 }
6108
6109 public final int getTitleColor() {
6110 return mTitleColor;
6111 }
6112
6113 protected void onTitleChanged(CharSequence title, int color) {
6114 if (mTitleReady) {
6115 final Window win = getWindow();
6116 if (win != null) {
6117 win.setTitle(title);
6118 if (color != 0) {
6119 win.setTitleColor(color);
6120 }
6121 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006122 if (mActionBar != null) {
6123 mActionBar.setWindowTitle(title);
6124 }
Adam Powella557fdc2014-08-21 18:05:53 -07006125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 }
6127
6128 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6129 }
6130
6131 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006132 * Sets information describing the task with this activity for presentation inside the Recents
6133 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6134 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6135 * for each property until a suitable value is found. For each task the taskDescription will be
6136 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006137 *
6138 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006139 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006140 *
Winson Chunga449dc02014-05-16 11:15:04 -07006141 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006142 */
Winson Chunga449dc02014-05-16 11:15:04 -07006143 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006144 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006145 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006146 // Scale the icon down to something reasonable if it is provided
6147 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6148 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6149 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6150 true);
6151 mTaskDescription.setIcon(icon);
6152 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006153 }
6154 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006155 ActivityManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006156 } catch (RemoteException e) {
6157 }
6158 }
6159
6160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161 * Sets the visibility of the progress bar in the title.
6162 * <p>
6163 * In order for the progress bar to be shown, the feature must be requested
6164 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006165 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006167 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006169 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 public final void setProgressBarVisibility(boolean visible) {
6171 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6172 Window.PROGRESS_VISIBILITY_OFF);
6173 }
6174
6175 /**
6176 * Sets the visibility of the indeterminate progress bar in the title.
6177 * <p>
6178 * In order for the progress bar to be shown, the feature must be requested
6179 * via {@link #requestWindowFeature(int)}.
6180 *
6181 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006182 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006184 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6186 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6187 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6188 }
RoboErik55011652014-07-09 15:05:53 -07006189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 /**
6191 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6192 * is always indeterminate).
6193 * <p>
6194 * In order for the progress bar to be shown, the feature must be requested
6195 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006198 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006200 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 public final void setProgressBarIndeterminate(boolean indeterminate) {
6202 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006203 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6204 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 }
RoboErik55011652014-07-09 15:05:53 -07006206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 /**
6208 * Sets the progress for the progress bars in the title.
6209 * <p>
6210 * In order for the progress bar to be shown, the feature must be requested
6211 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006212 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 * @param progress The progress for the progress bar. Valid ranges are from
6214 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6215 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006216 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006218 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 public final void setProgress(int progress) {
6220 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6221 }
RoboErik55011652014-07-09 15:05:53 -07006222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 /**
6224 * Sets the secondary progress for the progress bar in the title. This
6225 * progress is drawn between the primary progress (set via
6226 * {@link #setProgress(int)} and the background. It can be ideal for media
6227 * scenarios such as showing the buffering progress while the default
6228 * progress shows the play progress.
6229 * <p>
6230 * In order for the progress bar to be shown, the feature must be requested
6231 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6234 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006235 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006237 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 public final void setSecondaryProgress(int secondaryProgress) {
6239 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6240 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6241 }
6242
6243 /**
6244 * Suggests an audio stream whose volume should be changed by the hardware
6245 * volume controls.
6246 * <p>
6247 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006248 * Volume requests which are received while the Activity is in the
6249 * foreground will affect this stream.
6250 * <p>
6251 * It is not guaranteed that the hardware volume controls will always change
6252 * this stream's volume (for example, if a call is in progress, its stream's
6253 * volume may be changed instead). To reset back to the default, use
6254 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006257 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 */
6259 public final void setVolumeControlStream(int streamType) {
6260 getWindow().setVolumeControlStream(streamType);
6261 }
6262
6263 /**
6264 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006265 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 * @return The suggested audio stream type whose volume should be changed by
6268 * the hardware volume controls.
6269 * @see #setVolumeControlStream(int)
6270 */
6271 public final int getVolumeControlStream() {
6272 return getWindow().getVolumeControlStream();
6273 }
RoboErik55011652014-07-09 15:05:53 -07006274
6275 /**
6276 * Sets a {@link MediaController} to send media keys and volume changes to.
6277 * <p>
6278 * The controller will be tied to the window of this Activity. Media key and
6279 * volume events which are received while the Activity is in the foreground
6280 * will be forwarded to the controller and used to invoke transport controls
6281 * or adjust the volume. This may be used instead of or in addition to
6282 * {@link #setVolumeControlStream} to affect a specific session instead of a
6283 * specific stream.
6284 * <p>
6285 * It is not guaranteed that the hardware volume controls will always change
6286 * this session's volume (for example, if a call is in progress, its
6287 * stream's volume may be changed instead). To reset back to the default use
6288 * null as the controller.
6289 *
6290 * @param controller The controller for the session which should receive
6291 * media keys and volume changes.
6292 */
6293 public final void setMediaController(MediaController controller) {
6294 getWindow().setMediaController(controller);
6295 }
6296
6297 /**
6298 * Gets the controller which should be receiving media key and volume events
6299 * while this activity is in the foreground.
6300 *
6301 * @return The controller which should receive events.
6302 * @see #setMediaController(android.media.session.MediaController)
6303 */
6304 public final MediaController getMediaController() {
6305 return getWindow().getMediaController();
6306 }
6307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 /**
6309 * Runs the specified action on the UI thread. If the current thread is the UI
6310 * thread, then the action is executed immediately. If the current thread is
6311 * not the UI thread, the action is posted to the event queue of the UI thread.
6312 *
6313 * @param action the action to run on the UI thread
6314 */
6315 public final void runOnUiThread(Runnable action) {
6316 if (Thread.currentThread() != mUiThread) {
6317 mHandler.post(action);
6318 } else {
6319 action.run();
6320 }
6321 }
6322
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006323 /** @hide */
6324 @Override
6325 public final void autofillClientRunOnUiThread(Runnable action) {
6326 runOnUiThread(action);
6327 }
6328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006330 * Standard implementation of
6331 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6332 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006333 * This implementation does nothing and is for
6334 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6335 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6336 *
6337 * @see android.view.LayoutInflater#createView
6338 * @see android.view.Window#getLayoutInflater
6339 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006340 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07006341 public View onCreateView(String name, Context context, AttributeSet attrs) {
6342 return null;
6343 }
6344
6345 /**
6346 * Standard implementation of
6347 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6348 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006349 * This implementation handles <fragment> tags to embed fragments inside
6350 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 *
6352 * @see android.view.LayoutInflater#createView
6353 * @see android.view.Window#getLayoutInflater
6354 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07006355 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006356 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006357 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006358 }
RoboErik55011652014-07-09 15:05:53 -07006359
Adam Powell371a8092014-06-20 12:51:12 -07006360 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 }
6362
Daniel Sandler69a48172010-06-23 16:29:36 -04006363 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006364 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006365 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006366 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006367 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006368 * @param fd The raw file descriptor that the dump is being sent to.
6369 * @param writer The PrintWriter to which you should dump your state. This will be
6370 * closed for you after you return.
6371 * @param args additional arguments to the dump request.
6372 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08006373 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006374 dumpInner(prefix, fd, writer, args);
6375 }
6376
6377 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Felipe Leme42260332018-08-15 08:44:12 -07006378 if (args != null && args.length > 0 && args[0].equals("--autofill")) {
6379 dumpAutofillManager(prefix, writer);
6380 return;
6381 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006382 writer.print(prefix); writer.print("Local Activity ");
6383 writer.print(Integer.toHexString(System.identityHashCode(this)));
6384 writer.println(" State:");
6385 String innerPrefix = prefix + " ";
6386 writer.print(innerPrefix); writer.print("mResumed=");
6387 writer.print(mResumed); writer.print(" mStopped=");
6388 writer.print(mStopped); writer.print(" mFinished=");
6389 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006390 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6391 writer.println(mChangingConfigurations);
6392 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6393 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006394
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006395 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6396 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006397 if (mVoiceInteractor != null) {
6398 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6399 }
Jeff Brown5182c782013-10-15 20:31:52 -07006400
Michael Wright5f48dc72013-11-01 12:42:49 -07006401 if (getWindow() != null &&
6402 getWindow().peekDecorView() != null &&
6403 getWindow().peekDecorView().getViewRootImpl() != null) {
6404 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6405 }
Jeff Brown5182c782013-10-15 20:31:52 -07006406
6407 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07006408
Felipe Leme42260332018-08-15 08:44:12 -07006409 dumpAutofillManager(prefix, writer);
6410
6411 ResourcesManager.getInstance().dump(prefix, writer);
6412 }
6413
6414 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08006415 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006416 if (afm != null) {
Felipe Leme42260332018-08-15 08:44:12 -07006417 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08006418 writer.print(prefix); writer.print("Autofill Compat Mode: ");
6419 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07006420 } else {
6421 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07006422 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07006423 }
6424
6425 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006426 * Bit indicating that this activity is "immersive" and should not be
6427 * interrupted by notifications if possible.
6428 *
6429 * This value is initially set by the manifest property
6430 * <code>android:immersive</code> but may be changed at runtime by
6431 * {@link #setImmersive}.
6432 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006433 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006434 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6435 */
6436 public boolean isImmersive() {
6437 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006438 return ActivityManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006439 } catch (RemoteException e) {
6440 return false;
6441 }
6442 }
6443
6444 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006445 * Indication of whether this is the highest level activity in this task. Can be used to
6446 * determine whether an activity launched by this activity was placed in the same task or
6447 * another task.
6448 *
6449 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006450 */
George Mountff243282014-07-07 16:12:07 -07006451 private boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006452 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006453 return false;
6454 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006455 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006456 return ActivityManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006457 } catch (RemoteException e) {
6458 return false;
6459 }
6460 }
6461
6462 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006463 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6464 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006465 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006466 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006467 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6468 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006469 * This call has no effect on non-translucent activities or on activities with the
6470 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006471 *
George Mount62ab9b72014-05-02 13:51:17 -07006472 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6473 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006474 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006475 *
6476 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006477 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006478 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006479 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006480 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006481 mTranslucentCallback = null;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006482 if (ActivityManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006483 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6484 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006485 } catch (RemoteException e) {
6486 // pass
6487 }
6488 }
6489
6490 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006491 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6492 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6493 * <p>
6494 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6495 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6496 * be called indicating that it is safe to make this activity translucent again. Until
6497 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6498 * behind the frontmost Activity will be indeterminate.
6499 * <p>
6500 * This call has no effect on non-translucent activities or on activities with the
6501 * {@link android.R.attr#windowIsFloating} attribute.
6502 *
6503 * @param callback the method to call when all visible Activities behind this one have been
6504 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006505 * @param options activity options delivered to the activity below this one. The options
6506 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006507 * @return <code>true</code> if Window was opaque and will become translucent or
6508 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006509 *
6510 * @see #convertFromTranslucent()
6511 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006512 *
6513 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006514 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006515 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006516 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006517 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006518 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006519 try {
6520 mTranslucentCallback = callback;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006521 mChangeCanvasToTranslucent = ActivityManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006522 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006523 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006524 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006525 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006526 // Make callback return as though it timed out.
6527 mChangeCanvasToTranslucent = false;
6528 drawComplete = false;
6529 }
6530 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6531 // Window is already translucent.
6532 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006533 }
George Mount3cc716c2014-06-12 16:35:35 -07006534 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006535 }
6536
6537 /** @hide */
6538 void onTranslucentConversionComplete(boolean drawComplete) {
6539 if (mTranslucentCallback != null) {
6540 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6541 mTranslucentCallback = null;
6542 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006543 if (mChangeCanvasToTranslucent) {
6544 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6545 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006546 }
6547
Craig Mautnereb8abf72014-07-02 15:04:09 -07006548 /** @hide */
6549 public void onNewActivityOptions(ActivityOptions options) {
6550 mActivityTransitionState.setEnterActivityOptions(this, options);
6551 if (!mStopped) {
6552 mActivityTransitionState.enterReady(this);
6553 }
6554 }
6555
Craig Mautner5eda9b32013-07-02 11:58:16 -07006556 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006557 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6558 * from an activity launched by this activity in its call to {@link
6559 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6560 *
6561 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6562 * @hide
6563 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01006564 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07006565 ActivityOptions getActivityOptions() {
6566 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006567 return ActivityOptions.fromBundle(
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006568 ActivityManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07006569 } catch (RemoteException e) {
6570 }
6571 return null;
6572 }
6573
6574 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006575 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006576 * this method anytime between the start of {@link #onResume()} and the return from
6577 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6578 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6579 *
6580 * <p>The actions of this call are reset each time that this activity is brought to the
6581 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6582 * to not have requested visible behind. Therefore, if you want this activity to continue to
6583 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006584 *
6585 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6586 * for dialog and translucent activities.
6587 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006588 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6589 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6590 *
6591 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006592 * the next call to onResume.
6593 *
Bryce Leed59629e2017-04-18 14:35:29 -07006594 * @deprecated This method's functionality is no longer supported as of
6595 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6596 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006597 * @param visible true to notify the system that the activity wishes to be visible behind other
6598 * translucent activities, false to indicate otherwise. Resources must be
6599 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07006600 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006601 * @return the resulting visibiity state. If true the activity will remain visible beyond
6602 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6603 * then the activity may not count on being visible behind other translucent activities,
6604 * and must stop any media playback and release resources.
6605 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6606 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006607 *
Jose Limafcf70832014-08-27 23:09:05 -07006608 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07006609 */
Bryce Leed59629e2017-04-18 14:35:29 -07006610 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006611 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07006612 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006613 }
6614
6615 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006616 * Called when a translucent activity over this activity is becoming opaque or another
6617 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006618 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006619 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006620 * <p>When this method is called the activity has 500 msec to release any resources it may be
6621 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006622 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006623 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006624 * process. Otherwise {@link #onStop()} will be called following return.
6625 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006626 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07006627 *
6628 * @deprecated This method's functionality is no longer supported as of
6629 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006630 */
Bryce Leed59629e2017-04-18 14:35:29 -07006631 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08006632 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006633 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006634 mCalled = true;
6635 }
6636
6637 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006638 * Translucent activities may call this to determine if there is an activity below them that
6639 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006640 *
Bryce Leed59629e2017-04-18 14:35:29 -07006641 * @deprecated This method's functionality is no longer supported as of
6642 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6643 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006644 * @return true if an activity below is set to visible according to the most recent call to
6645 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006646 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006647 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006648 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006649 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006650 * @hide
6651 */
Bryce Leed59629e2017-04-18 14:35:29 -07006652 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006653 @SystemApi
6654 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006655 return false;
6656 }
6657
6658 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006659 * The topmost foreground activity will receive this call when the background visibility state
6660 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006661 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006662 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006663 * due to a background activity finishing itself.
6664 *
Bryce Leed59629e2017-04-18 14:35:29 -07006665 * @deprecated This method's functionality is no longer supported as of
6666 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6667 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006668 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006669 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006670 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006671 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006672 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006673 */
Bryce Leed59629e2017-04-18 14:35:29 -07006674 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006675 @SystemApi
6676 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006677 }
6678
6679 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006680 * Activities cannot draw during the period that their windows are animating in. In order
6681 * to know when it is safe to begin drawing they can override this method which will be
6682 * called when the entering animation has completed.
6683 */
6684 public void onEnterAnimationComplete() {
6685 }
6686
6687 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006688 * @hide
6689 */
6690 public void dispatchEnterAnimationComplete() {
6691 onEnterAnimationComplete();
6692 if (getWindow() != null && getWindow().getDecorView() != null) {
6693 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6694 }
6695 }
6696
6697 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006698 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006699 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006700 * Note that changing this value will have no effect on the activity's
6701 * {@link android.content.pm.ActivityInfo} structure; that is, if
6702 * <code>android:immersive</code> is set to <code>true</code>
6703 * in the application's manifest entry for this activity, the {@link
6704 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6705 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6706 * FLAG_IMMERSIVE} bit set.
6707 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006708 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006709 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6710 */
6711 public void setImmersive(boolean i) {
6712 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006713 ActivityManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04006714 } catch (RemoteException e) {
6715 // pass
6716 }
6717 }
6718
Adam Powell6e346362010-07-23 10:18:23 -07006719 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07006720 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006721 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006722 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
6723 * while this Activity has user focus.</p>
6724 *
6725 * <p>It is recommended that applications additionally declare
6726 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
6727 * transitions when switching between VR activities.</p>
6728 *
6729 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
6730 * VR mode will not be started. Developers can handle this case as follows:</p>
6731 *
6732 * <pre>
6733 * String servicePackage = "com.whatever.app";
6734 * String serviceClass = "com.whatever.app.MyVrListenerService";
6735 *
6736 * // Name of the component of the VrListenerService to start.
6737 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
6738 *
6739 * try {
6740 * setVrModeEnabled(true, myComponentName);
6741 * } catch (PackageManager.NameNotFoundException e) {
6742 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
6743 * boolean isInstalled = false;
6744 * for (ApplicationInfo app : installed) {
6745 * if (app.packageName.equals(servicePackage)) {
6746 * isInstalled = true;
6747 * break;
6748 * }
6749 * }
6750 * if (isInstalled) {
6751 * // Package is installed, but not enabled in Settings. Let user enable it.
6752 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
6753 * } else {
6754 * // Package is not installed. Send an intent to download this.
6755 * sentIntentToLaunchAppStore(servicePackage);
6756 * }
6757 * }
6758 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006759 *
6760 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08006761 * @param requestedComponent the name of the component to use as a
6762 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
6763 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006764 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
6765 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
6766 * not been enabled in user settings.
6767 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006768 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
6769 * @see android.service.vr.VrListenerService
6770 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
6771 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006772 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08006773 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
6774 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006775 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006776 if (ActivityManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08006777 != 0) {
6778 throw new PackageManager.NameNotFoundException(
6779 requestedComponent.flattenToString());
6780 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006781 } catch (RemoteException e) {
6782 // pass
6783 }
6784 }
6785
6786 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006787 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006788 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006789 * @param callback Callback that will manage lifecycle events for this action mode
6790 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006791 *
6792 * @see ActionMode
6793 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006794 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006795 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006796 return mWindow.getDecorView().startActionMode(callback);
6797 }
6798
Adam Powelldebf3be2010-11-15 18:58:48 -08006799 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006800 * Start an action mode of the given type.
6801 *
6802 * @param callback Callback that will manage lifecycle events for this action mode
6803 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6804 * @return The ActionMode that was started, or null if it was canceled
6805 *
6806 * @see ActionMode
6807 */
6808 @Nullable
6809 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6810 return mWindow.getDecorView().startActionMode(callback, type);
6811 }
6812
6813 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006814 * Give the Activity a chance to control the UI for an action mode requested
6815 * by the system.
6816 *
6817 * <p>Note: If you are looking for a notification callback that an action mode
6818 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6819 *
6820 * @param callback The callback that should control the new action mode
6821 * @return The new action mode, or <code>null</code> if the activity does not want to
6822 * provide special handling for this action mode. (It will be handled by the system.)
6823 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006824 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006825 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006826 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006827 // Only Primary ActionModes are represented in the ActionBar.
6828 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6829 initWindowDecorActionBar();
6830 if (mActionBar != null) {
6831 return mActionBar.startActionMode(callback);
6832 }
Adam Powell6e346362010-07-23 10:18:23 -07006833 }
6834 return null;
6835 }
6836
Adam Powelldebf3be2010-11-15 18:58:48 -08006837 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006838 * {@inheritDoc}
6839 */
6840 @Nullable
6841 @Override
6842 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6843 try {
6844 mActionModeTypeStarting = type;
6845 return onWindowStartingActionMode(callback);
6846 } finally {
6847 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6848 }
6849 }
6850
6851 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006852 * Notifies the Activity that an action mode has been started.
6853 * Activity subclasses overriding this method should call the superclass implementation.
6854 *
6855 * @param mode The new action mode.
6856 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006857 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006858 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006859 public void onActionModeStarted(ActionMode mode) {
6860 }
6861
6862 /**
6863 * Notifies the activity that an action mode has finished.
6864 * Activity subclasses overriding this method should call the superclass implementation.
6865 *
6866 * @param mode The action mode that just finished.
6867 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006868 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006869 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006870 public void onActionModeFinished(ActionMode mode) {
6871 }
6872
Adam Powelldd8fab22012-03-22 17:47:27 -07006873 /**
6874 * Returns true if the app should recreate the task when navigating 'up' from this activity
6875 * by using targetIntent.
6876 *
6877 * <p>If this method returns false the app can trivially call
6878 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6879 * up navigation. If this method returns false, the app should synthesize a new task stack
6880 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6881 *
6882 * @param targetIntent An intent representing the target destination for up navigation
6883 * @return true if navigating up should recreate a new task stack, false if the same task
6884 * should be used for the destination
6885 */
6886 public boolean shouldUpRecreateTask(Intent targetIntent) {
6887 try {
6888 PackageManager pm = getPackageManager();
6889 ComponentName cn = targetIntent.getComponent();
6890 if (cn == null) {
6891 cn = targetIntent.resolveActivity(pm);
6892 }
6893 ActivityInfo info = pm.getActivityInfo(cn, 0);
6894 if (info.taskAffinity == null) {
6895 return false;
6896 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006897 return ActivityManager.getService()
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006898 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006899 } catch (RemoteException e) {
6900 return false;
6901 } catch (NameNotFoundException e) {
6902 return false;
6903 }
6904 }
6905
6906 /**
6907 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6908 * in the process. If the activity indicated by upIntent already exists in the task's history,
6909 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006910 * finished.
6911 *
6912 * <p>If the indicated activity does not appear in the history stack, this will finish
6913 * each activity in this task until the root activity of the task is reached, resulting in
6914 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6915 * when an activity may be reached by a path not passing through a canonical parent
6916 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006917 *
6918 * <p>This method should be used when performing up navigation from within the same task
6919 * as the destination. If up navigation should cross tasks in some cases, see
6920 * {@link #shouldUpRecreateTask(Intent)}.</p>
6921 *
6922 * @param upIntent An intent representing the target destination for up navigation
6923 *
6924 * @return true if up navigation successfully reached the activity indicated by upIntent and
6925 * upIntent was delivered to it. false if an instance of the indicated activity could
6926 * not be found and this activity was simply finished normally.
6927 */
6928 public boolean navigateUpTo(Intent upIntent) {
6929 if (mParent == null) {
6930 ComponentName destInfo = upIntent.getComponent();
6931 if (destInfo == null) {
6932 destInfo = upIntent.resolveActivity(getPackageManager());
6933 if (destInfo == null) {
6934 return false;
6935 }
6936 upIntent = new Intent(upIntent);
6937 upIntent.setComponent(destInfo);
6938 }
6939 int resultCode;
6940 Intent resultData;
6941 synchronized (this) {
6942 resultCode = mResultCode;
6943 resultData = mResultData;
6944 }
6945 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006946 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006947 }
6948 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006949 upIntent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006950 return ActivityManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07006951 resultCode, resultData);
6952 } catch (RemoteException e) {
6953 return false;
6954 }
6955 } else {
6956 return mParent.navigateUpToFromChild(this, upIntent);
6957 }
6958 }
6959
6960 /**
6961 * This is called when a child activity of this one calls its
6962 * {@link #navigateUpTo} method. The default implementation simply calls
6963 * navigateUpTo(upIntent) on this activity (the parent).
6964 *
6965 * @param child The activity making the call.
6966 * @param upIntent An intent representing the target destination for up navigation
6967 *
6968 * @return true if up navigation successfully reached the activity indicated by upIntent and
6969 * upIntent was delivered to it. false if an instance of the indicated activity could
6970 * not be found and this activity was simply finished normally.
6971 */
6972 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6973 return navigateUpTo(upIntent);
6974 }
6975
6976 /**
6977 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6978 * this activity's logical parent. The logical parent is named in the application's manifest
6979 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006980 * Activity subclasses may override this method to modify the Intent returned by
6981 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6982 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006983 *
Adam Powell04d58112012-04-09 10:22:12 -07006984 * @return a new Intent targeting the defined parent of this activity or null if
6985 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006986 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006987 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006988 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006989 final String parentName = mActivityInfo.parentActivityName;
6990 if (TextUtils.isEmpty(parentName)) {
6991 return null;
6992 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006993
6994 // If the parent itself has no parent, generate a main activity intent.
6995 final ComponentName target = new ComponentName(this, parentName);
6996 try {
6997 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6998 final String parentActivity = parentInfo.parentActivityName;
6999 final Intent parentIntent = parentActivity == null
7000 ? Intent.makeMainActivity(target)
7001 : new Intent().setComponent(target);
7002 return parentIntent;
7003 } catch (NameNotFoundException e) {
7004 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7005 "' in manifest");
7006 return null;
7007 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007008 }
7009
George Mount31a21722014-03-24 17:44:36 -07007010 /**
George Mount62ab9b72014-05-02 13:51:17 -07007011 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007012 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007013 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007014 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007015 *
George Mount65580562014-08-29 08:15:48 -07007016 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007017 */
George Mount65580562014-08-29 08:15:48 -07007018 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7019 if (callback == null) {
7020 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007021 }
George Mount65580562014-08-29 08:15:48 -07007022 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007023 }
7024
7025 /**
7026 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007027 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007028 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7029 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007030 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007031 *
George Mount65580562014-08-29 08:15:48 -07007032 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007033 */
George Mount65580562014-08-29 08:15:48 -07007034 public void setExitSharedElementCallback(SharedElementCallback callback) {
7035 if (callback == null) {
7036 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007037 }
George Mount65580562014-08-29 08:15:48 -07007038 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007039 }
7040
George Mount8c2614c2014-06-10 11:12:01 -07007041 /**
7042 * Postpone the entering activity transition when Activity was started with
7043 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7044 * android.util.Pair[])}.
7045 * <p>This method gives the Activity the ability to delay starting the entering and
7046 * shared element transitions until all data is loaded. Until then, the Activity won't
7047 * draw into its window, leaving the window transparent. This may also cause the
7048 * returning animation to be delayed until data is ready. This method should be
7049 * called in {@link #onCreate(android.os.Bundle)} or in
7050 * {@link #onActivityReenter(int, android.content.Intent)}.
7051 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7052 * start the transitions. If the Activity did not use
7053 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7054 * android.util.Pair[])}, then this method does nothing.</p>
7055 */
7056 public void postponeEnterTransition() {
7057 mActivityTransitionState.postponeEnterTransition();
7058 }
7059
7060 /**
7061 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7062 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7063 * to have your Activity start drawing.
7064 */
7065 public void startPostponedEnterTransition() {
7066 mActivityTransitionState.startPostponedEnterTransition();
7067 }
7068
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007069 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007070 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7071 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007072 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007073 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7074 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007075 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007076 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7077 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7078 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7079 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007080 }
7081 return null;
7082 }
7083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007085
Mathew Inwood45d2c252018-09-14 12:35:36 +01007086 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 final void setParent(Activity parent) {
7088 mParent = parent;
7089 }
7090
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007091 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007092 final void attach(Context context, ActivityThread aThread,
7093 Instrumentation instr, IBinder token, int ident,
7094 Application application, Intent intent, ActivityInfo info,
7095 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007096 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007097 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07007098 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 attachBaseContext(context);
7100
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007101 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007102
Andrii Kulian51c1b672017-04-07 18:39:32 -07007103 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007104 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007106 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007107 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7109 mWindow.setSoftInputMode(info.softInputMode);
7110 }
Adam Powell269248d2011-08-02 10:26:54 -07007111 if (info.uiOptions != 0) {
7112 mWindow.setUiOptions(info.uiOptions);
7113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 mMainThread = aThread;
7117 mInstrumentation = instr;
7118 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007119 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 mApplication = application;
7121 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007122 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 mComponent = intent.getComponent();
7124 mActivityInfo = info;
7125 mTitle = title;
7126 mParent = parent;
7127 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007128 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007129 if (voiceInteractor != null) {
7130 if (lastNonConfigurationInstances != null) {
7131 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7132 } else {
7133 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7134 Looper.myLooper());
7135 }
7136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137
Jeff Brown98365d72012-08-19 20:30:52 -07007138 mWindow.setWindowManager(
7139 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7140 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007141 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 if (mParent != null) {
7143 mWindow.setContainer(mParent.getWindow());
7144 }
7145 mWindowManager = mWindow.getWindowManager();
7146 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007147
7148 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007149
7150 setAutofillCompatibilityEnabled(application.isAutofillCompatibilityEnabled());
7151 enableAutofillCompatibilityIfNeeded();
7152 }
7153
7154 private void enableAutofillCompatibilityIfNeeded() {
7155 if (isAutofillCompatibilityEnabled()) {
7156 final AutofillManager afm = getSystemService(AutofillManager.class);
7157 if (afm != null) {
7158 afm.enableCompatibilityMode();
7159 }
7160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 }
7162
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007163 /** @hide */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007164 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07007165 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 return mParent != null ? mParent.getActivityToken() : mToken;
7167 }
7168
Andrii Kuliandfbf9712018-03-08 15:42:24 -08007169 /** @hide */
7170 @VisibleForTesting
7171 public final ActivityThread getActivityThread() {
7172 return mMainThread;
7173 }
7174
Winson Chung298f95b2017-08-10 15:57:18 -07007175 final void performCreate(Bundle icicle) {
7176 performCreate(icicle, null);
7177 }
7178
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007179 @UnsupportedAppUsage
Winson Chung298f95b2017-08-10 15:57:18 -07007180 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
7181 mCanEnterPictureInPicture = true;
7182 restoreHasCurrentPermissionRequest(icicle);
7183 if (persistentState != null) {
7184 onCreate(icicle, persistentState);
7185 } else {
7186 onCreate(icicle);
7187 }
Andrii Kuliand25680c2018-02-21 15:16:58 -08007188 writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
Winson Chung298f95b2017-08-10 15:57:18 -07007189 mActivityTransitionState.readState(icicle);
7190
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08007191 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
7192 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07007193 mFragments.dispatchActivityCreated();
Eino-Ville Talvala563df3b2016-06-06 22:04:54 +00007194 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007195 }
George Mount0a778ed2013-12-13 13:35:36 -08007196
Winson Chung298f95b2017-08-10 15:57:18 -07007197 final void performNewIntent(Intent intent) {
7198 mCanEnterPictureInPicture = true;
7199 onNewIntent(intent);
Craig Mautnera0026042014-04-23 11:45:37 -07007200 }
7201
Andrii Kuliand25680c2018-02-21 15:16:58 -08007202 final void performStart(String reason) {
George Mount62ab9b72014-05-02 13:51:17 -07007203 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007204 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07007206 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 mInstrumentation.callActivityOnStart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007208 writeEventLog(LOG_AM_ON_START_CALLED, reason);
7209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 if (!mCalled) {
7211 throw new SuperNotCalledException(
7212 "Activity " + mComponent.toShortString() +
7213 " did not call through to super.onStart()");
7214 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007215 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007216 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007217
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007218 boolean isAppDebuggable =
7219 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
7220
David Brazdil795cbfb2018-01-26 12:23:15 +00007221 // This property is set for all non-user builds except final release
David Brazdil927d6de2018-01-24 19:54:30 +00007222 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
7223
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007224 if (isAppDebuggable || isDlwarningEnabled) {
7225 String dlwarning = getDlWarning();
7226 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08007227 String appName = getApplicationInfo().loadLabel(getPackageManager())
7228 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08007229 String warning = "Detected problems with app native libraries\n" +
7230 "(please consult log for detail):\n" + dlwarning;
7231 if (isAppDebuggable) {
7232 new AlertDialog.Builder(this).
7233 setTitle(appName).
7234 setMessage(warning).
7235 setPositiveButton(android.R.string.ok, null).
7236 setCancelable(false).
7237 show();
7238 } else {
7239 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
7240 }
7241 }
7242 }
7243
George Mount62ab9b72014-05-02 13:51:17 -07007244 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 }
RoboErik55011652014-07-09 15:05:53 -07007246
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007247 /**
7248 * Restart the activity.
7249 * @param start Indicates whether the activity should also be started after restart.
7250 * The option to not start immediately is needed in case a transaction with
7251 * multiple lifecycle transitions is in progress.
7252 */
Andrii Kuliand25680c2018-02-21 15:16:58 -08007253 final void performRestart(boolean start, String reason) {
Winson Chungf7e03e12017-08-22 11:32:16 -07007254 mCanEnterPictureInPicture = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007255 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07007256
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007257 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08007258 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007259 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
7260 }
7261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 if (mStopped) {
7263 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007264
7265 synchronized (mManagedCursors) {
7266 final int N = mManagedCursors.size();
7267 for (int i=0; i<N; i++) {
7268 ManagedCursor mc = mManagedCursors.get(i);
7269 if (mc.mReleased || mc.mUpdated) {
7270 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07007271 if (getApplicationInfo().targetSdkVersion
7272 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
7273 throw new IllegalStateException(
7274 "trying to requery an already closed cursor "
7275 + mc.mCursor);
7276 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07007277 }
7278 mc.mReleased = false;
7279 mc.mUpdated = false;
7280 }
7281 }
7282 }
7283
7284 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 mInstrumentation.callActivityOnRestart(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007286 writeEventLog(LOG_AM_ON_RESTART_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 if (!mCalled) {
7288 throw new SuperNotCalledException(
7289 "Activity " + mComponent.toShortString() +
7290 " did not call through to super.onRestart()");
7291 }
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007292 if (start) {
Andrii Kuliand25680c2018-02-21 15:16:58 -08007293 performStart(reason);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08007294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 }
7296 }
RoboErik55011652014-07-09 15:05:53 -07007297
Andrii Kuliand25680c2018-02-21 15:16:58 -08007298 final void performResume(boolean followedByPause, String reason) {
7299 performRestart(true /* start */, reason);
RoboErik55011652014-07-09 15:05:53 -07007300
Dianne Hackborn445646c2010-06-25 15:52:59 -07007301 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07007302
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007303 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07007304
Dake Gu67decfa2017-12-27 11:48:08 -08007305 if (mAutoFillResetNeeded) {
7306 // When Activity is destroyed in paused state, and relaunch activity, there will be
7307 // extra onResume and onPause event, ignore the first onResume and onPause.
7308 // see ActivityThread.handleRelaunchActivity()
7309 mAutoFillIgnoreFirstResumePause = followedByPause;
7310 if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {
7311 Slog.v(TAG, "autofill will ignore first pause when relaunching " + this);
7312 }
7313 }
7314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06007316 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 mInstrumentation.callActivityOnResume(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007318 writeEventLog(LOG_AM_ON_RESUME_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 if (!mCalled) {
7320 throw new SuperNotCalledException(
7321 "Activity " + mComponent.toShortString() +
7322 " did not call through to super.onResume()");
7323 }
7324
Todd Kennedyee8c9c62014-12-10 14:22:59 -08007325 // invisible activities must be finished before onResume() completes
7326 if (!mVisibleFromClient && !mFinished) {
7327 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
7328 if (getApplicationInfo().targetSdkVersion
7329 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
7330 throw new IllegalStateException(
7331 "Activity " + mComponent.toShortString() +
7332 " did not call finish() prior to onResume() completing");
7333 }
7334 }
7335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07007338
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007339 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07007340 mFragments.execPendingActions();
Narayan Kamathff5659f2017-02-02 13:31:33 +00007341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007342 onPostResume();
7343 if (!mCalled) {
7344 throw new SuperNotCalledException(
7345 "Activity " + mComponent.toShortString() +
7346 " did not call through to super.onPostResume()");
7347 }
7348 }
7349
7350 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007351 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007352 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007353 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 onPause();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007355 writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007356 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07007357 if (!mCalled && getApplicationInfo().targetSdkVersion
7358 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
7359 throw new SuperNotCalledException(
7360 "Activity " + mComponent.toShortString() +
7361 " did not call through to super.onPause()");
7362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 }
RoboErik55011652014-07-09 15:05:53 -07007364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 final void performUserLeaving() {
7366 onUserInteraction();
7367 onUserLeaveHint();
7368 }
RoboErik55011652014-07-09 15:05:53 -07007369
Andrii Kuliand25680c2018-02-21 15:16:58 -08007370 final void performStop(boolean preserveWindow, String reason) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07007371 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007372 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
Narayan Kamathff5659f2017-02-02 13:31:33 +00007373
Winson Chung298f95b2017-08-10 15:57:18 -07007374 // Disallow entering picture-in-picture after the activity has been stopped
7375 mCanEnterPictureInPicture = false;
7376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 if (!mStopped) {
7378 if (mWindow != null) {
7379 mWindow.closeAllPanels();
7380 }
7381
Chong Zhang7687f252016-02-26 12:03:33 -08007382 // If we're preserving the window, don't setStoppedState to true, since we
7383 // need the window started immediately again. Stopping the window will
7384 // destroys hardware resources and causes flicker.
7385 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07007386 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08007387 }
RoboErik55011652014-07-09 15:05:53 -07007388
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007389 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07007390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 mCalled = false;
7392 mInstrumentation.callActivityOnStop(this);
Andrii Kuliand25680c2018-02-21 15:16:58 -08007393 writeEventLog(LOG_AM_ON_STOP_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007394 if (!mCalled) {
7395 throw new SuperNotCalledException(
7396 "Activity " + mComponent.toShortString() +
7397 " did not call through to super.onStop()");
7398 }
RoboErik55011652014-07-09 15:05:53 -07007399
Makoto Onuki2f6a0182010-02-22 13:26:59 -08007400 synchronized (mManagedCursors) {
7401 final int N = mManagedCursors.size();
7402 for (int i=0; i<N; i++) {
7403 ManagedCursor mc = mManagedCursors.get(i);
7404 if (!mc.mReleased) {
7405 mc.mCursor.deactivate();
7406 mc.mReleased = true;
7407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 }
7409 }
George Mount0a778ed2013-12-13 13:35:36 -08007410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 mStopped = true;
7412 }
7413 mResumed = false;
7414 }
7415
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007416 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07007417 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07007418 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007419 mFragments.dispatchDestroy();
7420 onDestroy();
Andrii Kuliand25680c2018-02-21 15:16:58 -08007421 writeEventLog(LOG_AM_ON_DESTROY_CALLED, "performDestroy");
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007422 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07007423 if (mVoiceInteractor != null) {
7424 mVoiceInteractor.detachActivity();
7425 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07007426 }
George Mount0a778ed2013-12-13 13:35:36 -08007427
Winson Chung5af42fc2017-03-24 17:11:33 -07007428 final void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
7429 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007430 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007431 "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode
7432 + " " + newConfig);
7433 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007434 if (mWindow != null) {
7435 mWindow.onMultiWindowModeChanged();
7436 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007437 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007438 }
7439
Winson Chung5af42fc2017-03-24 17:11:33 -07007440 final void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
7441 Configuration newConfig) {
Adam Powell858cf032016-05-09 15:45:37 -07007442 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Winson Chung5af42fc2017-03-24 17:11:33 -07007443 "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode
7444 + " " + newConfig);
7445 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Winson Chung4d8681f2017-05-23 16:22:08 -07007446 if (mWindow != null) {
7447 mWindow.onPictureInPictureModeChanged(isInPictureInPictureMode);
7448 }
Winson Chung5af42fc2017-03-24 17:11:33 -07007449 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Adam Powell858cf032016-05-09 15:45:37 -07007450 }
7451
George Mount0a778ed2013-12-13 13:35:36 -08007452 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06007453 * @hide
7454 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007455 @UnsupportedAppUsage
Jeff Hamilton52d32032011-01-08 15:31:26 -06007456 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 return mResumed;
7458 }
7459
Svetoslavffb32b12015-10-15 16:54:00 -07007460 private void storeHasCurrentPermissionRequest(Bundle bundle) {
7461 if (bundle != null && mHasCurrentPermissionsRequest) {
7462 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
7463 }
7464 }
7465
7466 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
7467 if (bundle != null) {
7468 mHasCurrentPermissionsRequest = bundle.getBoolean(
7469 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
7470 }
7471 }
7472
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007473 @UnsupportedAppUsage
Andrii Kuliane55b0092018-04-19 15:29:22 -07007474 void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
7475 String reason) {
Joe Onorato43a17652011-04-06 19:22:23 -07007476 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
7478 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07007479 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007481 onActivityResult(requestCode, resultCode, data);
7482 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
7483 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
7484 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08007485 dispatchRequestPermissionsResult(requestCode, data);
7486 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00007487 Fragment frag = mFragments.findFragmentByWho(who);
7488 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07007489 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08007490 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07007491 }
Svetoslav970b59c2015-06-09 16:05:21 -07007492 } else if (who.startsWith("@android:view:")) {
7493 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
7494 getActivityToken());
7495 for (ViewRootImpl viewRoot : views) {
7496 if (viewRoot.getView() != null
7497 && viewRoot.getView().dispatchActivityResult(
7498 who, requestCode, resultCode, data)) {
7499 return;
7500 }
7501 }
Svet Ganov782043c2017-02-11 00:52:02 +00007502 } else if (who.startsWith(AUTO_FILL_AUTH_WHO_PREFIX)) {
Svet Ganov00c771dc2017-02-19 00:06:22 -08007503 Intent resultData = (resultCode == Activity.RESULT_OK) ? data : null;
Dake Gu67decfa2017-12-27 11:48:08 -08007504 getAutofillManager().onAuthenticationResult(requestCode, resultData, getCurrentFocus());
Svetoslav970b59c2015-06-09 16:05:21 -07007505 } else {
7506 Fragment frag = mFragments.findFragmentByWho(who);
7507 if (frag != null) {
7508 frag.onActivityResult(requestCode, resultCode, data);
7509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 }
Andrii Kuliane55b0092018-04-19 15:29:22 -07007511 writeEventLog(LOG_AM_ON_ACTIVITY_RESULT_CALLED, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07007513
Jason Monka57e5e02014-05-07 10:06:48 -04007514 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01007515 * Request to put this activity in a mode where the user is locked to a restricted set of
7516 * applications.
Jason Monka57e5e02014-05-07 10:06:48 -04007517 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007518 * <p>If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns {@code true}
7519 * for this component, the current task will be launched directly into LockTask mode. Only apps
7520 * whitelisted by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])} can
7521 * be launched while LockTask mode is active. The user will not be able to leave this mode
7522 * until this activity calls {@link #stopLockTask()}. Calling this method while the device is
7523 * already in LockTask mode has no effect.
Jason Monka57e5e02014-05-07 10:06:48 -04007524 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007525 * <p>Otherwise, the current task will be launched into screen pinning mode. In this case, the
7526 * system will prompt the user with a dialog requesting permission to use this mode.
7527 * The user can exit at any time through instructions shown on the request dialog. Calling
7528 * {@link #stopLockTask()} will also terminate this mode.
Jason Monk62515be2014-05-21 16:06:19 -04007529 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007530 * <p><strong>Note:</strong> this method can only be called when the activity is foreground.
7531 * That is, between {@link #onResume()} and {@link #onPause()}.
Craig Mautner15df08a2015-04-01 12:17:18 -07007532 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007533 * @see #stopLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07007534 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04007535 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007536 public void startLockTask() {
7537 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007538 ActivityManager.getService().startLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007539 } catch (RemoteException e) {
7540 }
7541 }
7542
Jason Monka57e5e02014-05-07 10:06:48 -04007543 /**
Charles Heff9b4dff2017-09-22 10:18:37 +01007544 * Stop the current task from being locked.
Jason Monka57e5e02014-05-07 10:06:48 -04007545 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007546 * <p>Called to end the LockTask or screen pinning mode started by {@link #startLockTask()}.
7547 * This can only be called by activities that have called {@link #startLockTask()} previously.
Jason Monka57e5e02014-05-07 10:06:48 -04007548 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007549 * <p><strong>Note:</strong> If the device is in LockTask mode that is not initially started
7550 * by this activity, then calling this method will not terminate the LockTask mode, but only
7551 * finish its own task. The device will remain in LockTask mode, until the activity which
7552 * started the LockTask mode calls this method, or until its whitelist authorization is revoked
7553 * by {@link DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
Craig Mautner15df08a2015-04-01 12:17:18 -07007554 *
Charles Heff9b4dff2017-09-22 10:18:37 +01007555 * @see #startLockTask()
Craig Mautner15df08a2015-04-01 12:17:18 -07007556 * @see android.R.attr#lockTaskMode
7557 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04007558 */
Craig Mautneraea74a52014-03-08 14:23:10 -08007559 public void stopLockTask() {
7560 try {
Charles Heff9b4dff2017-09-22 10:18:37 +01007561 ActivityManager.getService().stopLockTaskModeByToken(mToken);
Craig Mautneraea74a52014-03-08 14:23:10 -08007562 } catch (RemoteException e) {
7563 }
7564 }
7565
Craig Mautner5eda9b32013-07-02 11:58:16 -07007566 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007567 * Shows the user the system defined message for telling the user how to exit
7568 * lock task mode. The task containing this activity must be in lock task mode at the time
7569 * of this call for the message to be displayed.
7570 */
7571 public void showLockTaskEscapeMessage() {
7572 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08007573 ActivityManager.getService().showLockTaskEscapeMessage(mToken);
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07007574 } catch (RemoteException e) {
7575 }
7576 }
7577
7578 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07007579 * Check whether the caption on freeform windows is displayed directly on the content.
7580 *
7581 * @return True if caption is displayed on content, false if it pushes the content down.
7582 *
Elliot Waited5ed3bc2017-01-25 15:22:55 -08007583 * @see #setOverlayWithDecorCaptionEnabled(boolean)
Wale Ogunwale724ca142017-04-24 13:40:34 -07007584 * @hide
Andrii Kulian933076d2016-03-29 17:04:42 -07007585 */
7586 public boolean isOverlayWithDecorCaptionEnabled() {
7587 return mWindow.isOverlayWithDecorCaptionEnabled();
7588 }
7589
7590 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08007591 * Set whether the caption should displayed directly on the content rather than push it down.
7592 *
7593 * This affects only freeform windows since they display the caption and only the main
7594 * window of the activity. The caption is used to drag the window around and also shows
7595 * maximize and close action buttons.
Wale Ogunwale724ca142017-04-24 13:40:34 -07007596 * @hide
Filip Gruszczynski63250652015-11-18 14:43:01 -08007597 */
Andrii Kulian933076d2016-03-29 17:04:42 -07007598 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
7599 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08007600 }
7601
7602 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07007603 * Interface for informing a translucent {@link Activity} once all visible activities below it
7604 * have completed drawing. This is necessary only after an {@link Activity} has been made
7605 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
7606 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07007607 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
7608 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07007609 *
7610 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07007611 */
Jose Lima14914852014-08-14 09:14:12 -07007612 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07007613 public interface TranslucentConversionListener {
7614 /**
7615 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
7616 * below the top one have been redrawn. Following this callback it is safe to make the top
7617 * Activity translucent because the underlying Activity has been drawn.
7618 *
7619 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
7620 * occurred waiting for the Activity to complete drawing.
7621 *
7622 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07007623 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07007624 */
7625 public void onTranslucentConversionComplete(boolean drawComplete);
7626 }
Svetoslavc6d1c342015-02-26 14:44:43 -08007627
7628 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07007629 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07007630 // If the package installer crashed we may have not data - best effort.
7631 String[] permissions = (data != null) ? data.getStringArrayExtra(
7632 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7633 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7634 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08007635 onRequestPermissionsResult(requestCode, permissions, grantResults);
7636 }
7637
7638 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07007639 Fragment fragment) {
7640 // If the package installer crashed we may have not data - best effort.
7641 String[] permissions = (data != null) ? data.getStringArrayExtra(
7642 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
7643 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
7644 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
7645 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08007646 }
7647
Svet Ganov782043c2017-02-11 00:52:02 +00007648 /** @hide */
7649 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007650 public final void autofillClientAuthenticate(int authenticationId, IntentSender intent,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007651 Intent fillInIntent) {
Svet Ganov782043c2017-02-11 00:52:02 +00007652 try {
7653 startIntentSenderForResultInner(intent, AUTO_FILL_AUTH_WHO_PREFIX,
Svetoslav Ganova9379d02017-05-09 17:40:24 -07007654 authenticationId, fillInIntent, 0, 0, null);
Svet Ganov782043c2017-02-11 00:52:02 +00007655 } catch (IntentSender.SendIntentException e) {
7656 Log.e(TAG, "authenticate() failed for intent:" + intent, e);
7657 }
7658 }
7659
Svet Ganov17db9dc2017-02-21 19:54:31 -08007660 /** @hide */
7661 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007662 public final void autofillClientResetableStateAvailable() {
Svet Ganov17db9dc2017-02-21 19:54:31 -08007663 mAutoFillResetNeeded = true;
7664 }
7665
Felipe Leme4753bb02017-03-22 20:24:00 -07007666 /** @hide */
7667 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007668 public final boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width,
Felipe Leme4753bb02017-03-22 20:24:00 -07007669 int height, @Nullable Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme4753bb02017-03-22 20:24:00 -07007670 final boolean wasShowing;
7671
7672 if (mAutofillPopupWindow == null) {
7673 wasShowing = false;
7674 mAutofillPopupWindow = new AutofillPopupWindow(presenter);
7675 } else {
7676 wasShowing = mAutofillPopupWindow.isShowing();
7677 }
Svet Ganov374cae12017-05-10 13:42:33 -07007678 mAutofillPopupWindow.update(anchor, 0, 0, width, height, anchorBounds);
Felipe Leme4753bb02017-03-22 20:24:00 -07007679
7680 return !wasShowing && mAutofillPopupWindow.isShowing();
7681 }
7682
7683 /** @hide */
7684 @Override
Dake Gu6a20a192018-02-08 12:09:30 -08007685 public final void autofillClientDispatchUnhandledKey(@NonNull View anchor,
7686 @NonNull KeyEvent keyEvent) {
7687 ViewRootImpl rootImpl = anchor.getViewRootImpl();
7688 if (rootImpl != null) {
7689 // dont care if anchorView is current focus, for example a custom view may only receive
7690 // touchEvent, not focusable but can still trigger autofill window. The Key handling
7691 // might be inside parent of the custom view.
7692 rootImpl.dispatchKeyFromAutofill(keyEvent);
7693 }
7694 }
7695
7696 /** @hide */
7697 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007698 public final boolean autofillClientRequestHideFillUi() {
Felipe Leme4753bb02017-03-22 20:24:00 -07007699 if (mAutofillPopupWindow == null) {
7700 return false;
7701 }
7702 mAutofillPopupWindow.dismiss();
7703 mAutofillPopupWindow = null;
7704 return true;
7705 }
7706
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007707 /** @hide */
Felipe Leme27e20222017-05-18 15:24:11 -07007708 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007709 public final boolean autofillClientIsFillUiShowing() {
7710 return mAutofillPopupWindow != null && mAutofillPopupWindow.isShowing();
7711 }
7712
7713 /** @hide */
7714 @Override
7715 @NonNull
7716 public final View[] autofillClientFindViewsByAutofillIdTraversal(
7717 @NonNull AutofillId[] autofillId) {
7718 final View[] views = new View[autofillId.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007719 final ArrayList<ViewRootImpl> roots =
7720 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
7721
7722 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7723 final View rootView = roots.get(rootNum).getView();
7724
7725 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007726 final int viewCount = autofillId.length;
7727 for (int viewNum = 0; viewNum < viewCount; viewNum++) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007728 if (views[viewNum] == null) {
Phil Weaver846cda932017-06-15 10:10:06 -07007729 views[viewNum] = rootView.findViewByAutofillIdTraversal(
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007730 autofillId[viewNum].getViewId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007731 }
7732 }
7733 }
7734 }
7735
7736 return views;
7737 }
7738
7739 /** @hide */
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007740 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007741 @Nullable
7742 public final View autofillClientFindViewByAutofillIdTraversal(AutofillId autofillId) {
Felipe Leme27e20222017-05-18 15:24:11 -07007743 final ArrayList<ViewRootImpl> roots =
7744 WindowManagerGlobal.getInstance().getRootViews(getActivityToken());
7745 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7746 final View rootView = roots.get(rootNum).getView();
7747
7748 if (rootView != null) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007749 final View view = rootView.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Leme27e20222017-05-18 15:24:11 -07007750 if (view != null) {
7751 return view;
7752 }
7753 }
7754 }
7755
7756 return null;
7757 }
7758
7759 /** @hide */
7760 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007761 public final @NonNull boolean[] autofillClientGetViewVisibility(
7762 @NonNull AutofillId[] autofillIds) {
7763 final int autofillIdCount = autofillIds.length;
7764 final boolean[] visible = new boolean[autofillIdCount];
7765 for (int i = 0; i < autofillIdCount; i++) {
7766 final AutofillId autofillId = autofillIds[i];
7767 final View view = autofillClientFindViewByAutofillIdTraversal(autofillId);
7768 if (view != null) {
7769 if (!autofillId.isVirtual()) {
7770 visible[i] = view.isVisibleToUser();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007771 } else {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007772 visible[i] = view.isVisibleToUserForAutofill(autofillId.getVirtualChildId());
Philip P. Moltmann134cee22017-05-06 11:28:38 -07007773 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007774 }
7775 }
Felipe Leme5b32ebe2018-02-15 12:52:19 -08007776 if (android.view.autofill.Helper.sVerbose) {
7777 Log.v(TAG, "autofillClientGetViewVisibility(): " + Arrays.toString(visible));
7778 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007779 return visible;
7780 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007781
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007782 /** @hide */
7783 public final @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId,
7784 int windowId) {
7785 final ArrayList<ViewRootImpl> roots = WindowManagerGlobal.getInstance()
7786 .getRootViews(getActivityToken());
7787 for (int rootNum = 0; rootNum < roots.size(); rootNum++) {
7788 final View rootView = roots.get(rootNum).getView();
7789 if (rootView != null && rootView.getAccessibilityWindowId() == windowId) {
7790 final View view = rootView.findViewByAccessibilityIdTraversal(viewId);
7791 if (view != null) {
7792 return view;
7793 }
7794 }
7795 }
7796 return null;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007797 }
7798
7799 /** @hide */
7800 @Override
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007801 public final @Nullable IBinder autofillClientGetActivityToken() {
7802 return getActivityToken();
7803 }
7804
7805 /** @hide */
7806 @Override
7807 public final boolean autofillClientIsVisibleForAutofill() {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07007808 return !mStopped;
7809 }
7810
Dake Gu67decfa2017-12-27 11:48:08 -08007811 /** @hide */
7812 @Override
Felipe Leme42b97932018-02-20 13:04:31 -08007813 public final boolean autofillClientIsCompatibilityModeEnabled() {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007814 return isAutofillCompatibilityEnabled();
7815 }
7816
7817 /** @hide */
7818 @Override
7819 public final boolean isDisablingEnterExitEventForAutofill() {
Dake Gu67decfa2017-12-27 11:48:08 -08007820 return mAutoFillIgnoreFirstResumePause || !mResumed;
7821 }
7822
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01007823 /**
7824 * If set to true, this indicates to the system that it should never take a
7825 * screenshot of the activity to be used as a representation while it is not in a started state.
7826 * <p>
7827 * Note that the system may use the window background of the theme instead to represent
7828 * the window when it is not running.
7829 * <p>
7830 * Also note that in comparison to {@link android.view.WindowManager.LayoutParams#FLAG_SECURE},
7831 * this only affects the behavior when the activity's screenshot would be used as a
7832 * representation when the activity is not in a started state, i.e. in Overview. The system may
7833 * still take screenshots of the activity in other contexts; for example, when the user takes a
7834 * screenshot of the entire screen, or when the active
7835 * {@link android.service.voice.VoiceInteractionService} requests a screenshot via
7836 * {@link android.service.voice.VoiceInteractionSession#SHOW_WITH_SCREENSHOT}.
7837 *
7838 * @param disable {@code true} to disable preview screenshots; {@code false} otherwise.
7839 * @hide
7840 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007841 @UnsupportedAppUsage
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01007842 public void setDisablePreviewScreenshots(boolean disable) {
7843 try {
7844 ActivityManager.getService().setDisablePreviewScreenshots(mToken, disable);
7845 } catch (RemoteException e) {
7846 Log.e(TAG, "Failed to call setDisablePreviewScreenshots", e);
7847 }
7848 }
7849
chaviw59b98852017-06-13 12:05:44 -07007850 /**
7851 * Specifies whether an {@link Activity} should be shown on top of the the lock screen whenever
7852 * the lockscreen is up and the activity is resumed. Normally an activity will be transitioned
7853 * to the stopped state if it is started while the lockscreen is up, but with this flag set the
7854 * activity will remain in the resumed state visible on-top of the lock screen. This value can
7855 * be set as a manifest attribute using {@link android.R.attr#showWhenLocked}.
7856 *
7857 * @param showWhenLocked {@code true} to show the {@link Activity} on top of the lock screen;
7858 * {@code false} otherwise.
7859 * @see #setTurnScreenOn(boolean)
7860 * @see android.R.attr#turnScreenOn
7861 * @see android.R.attr#showWhenLocked
7862 */
7863 public void setShowWhenLocked(boolean showWhenLocked) {
7864 try {
7865 ActivityManager.getService().setShowWhenLocked(mToken, showWhenLocked);
7866 } catch (RemoteException e) {
7867 Log.e(TAG, "Failed to call setShowWhenLocked", e);
7868 }
7869 }
7870
7871 /**
7872 * Specifies whether the screen should be turned on when the {@link Activity} is resumed.
7873 * Normally an activity will be transitioned to the stopped state if it is started while the
7874 * screen if off, but with this flag set the activity will cause the screen to turn on if the
7875 * activity will be visible and resumed due to the screen coming on. The screen will not be
7876 * turned on if the activity won't be visible after the screen is turned on. This flag is
7877 * normally used in conjunction with the {@link android.R.attr#showWhenLocked} flag to make sure
7878 * the activity is visible after the screen is turned on when the lockscreen is up. In addition,
7879 * if this flag is set and the activity calls {@link
7880 * KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
7881 * the screen will turn on.
7882 *
7883 * @param turnScreenOn {@code true} to turn on the screen; {@code false} otherwise.
7884 *
7885 * @see #setShowWhenLocked(boolean)
7886 * @see android.R.attr#turnScreenOn
7887 * @see android.R.attr#showWhenLocked
7888 */
7889 public void setTurnScreenOn(boolean turnScreenOn) {
7890 try {
7891 ActivityManager.getService().setTurnScreenOn(mToken, turnScreenOn);
7892 } catch (RemoteException e) {
7893 Log.e(TAG, "Failed to call setTurnScreenOn", e);
7894 }
7895 }
7896
Jorim Jaggif84e2f62018-01-16 14:17:59 +01007897 /**
7898 * Registers remote animations per transition type for this activity.
7899 *
7900 * @param definition The remote animation definition that defines which transition whould run
7901 * which remote animation.
7902 * @hide
7903 */
7904 @RequiresPermission(CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS)
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007905 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +01007906 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
7907 try {
7908 ActivityManager.getService().registerRemoteAnimations(mToken, definition);
7909 } catch (RemoteException e) {
7910 Log.e(TAG, "Failed to call registerRemoteAnimations", e);
7911 }
7912 }
7913
Andrii Kuliand25680c2018-02-21 15:16:58 -08007914 /** Log a lifecycle event for current user id and component class. */
7915 private void writeEventLog(int event, String reason) {
7916 EventLog.writeEvent(event, UserHandle.myUserId(), getComponentName().getClassName(),
7917 reason);
7918 }
7919
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007920 class HostCallbacks extends FragmentHostCallback<Activity> {
7921 public HostCallbacks() {
7922 super(Activity.this /*activity*/);
7923 }
7924
7925 @Override
7926 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
7927 Activity.this.dump(prefix, fd, writer, args);
7928 }
7929
7930 @Override
7931 public boolean onShouldSaveFragmentState(Fragment fragment) {
7932 return !isFinishing();
7933 }
7934
7935 @Override
7936 public LayoutInflater onGetLayoutInflater() {
7937 final LayoutInflater result = Activity.this.getLayoutInflater();
7938 if (onUseFragmentManagerInflaterFactory()) {
7939 return result.cloneInContext(Activity.this);
7940 }
7941 return result;
7942 }
7943
7944 @Override
7945 public boolean onUseFragmentManagerInflaterFactory() {
7946 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
7947 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
7948 }
7949
7950 @Override
7951 public Activity onGetHost() {
7952 return Activity.this;
7953 }
7954
7955 @Override
7956 public void onInvalidateOptionsMenu() {
7957 Activity.this.invalidateOptionsMenu();
7958 }
7959
7960 @Override
7961 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
7962 Bundle options) {
7963 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
7964 }
7965
7966 @Override
Tony Mak96d26fe2017-04-11 20:05:39 +01007967 public void onStartActivityAsUserFromFragment(
7968 Fragment fragment, Intent intent, int requestCode, Bundle options,
7969 UserHandle user) {
7970 Activity.this.startActivityAsUserFromFragment(
7971 fragment, intent, requestCode, options, user);
7972 }
7973
7974 @Override
Clara Bayarria0c2dc32016-04-12 12:00:15 +01007975 public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent,
7976 int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
7977 int extraFlags, Bundle options) throws IntentSender.SendIntentException {
7978 if (mParent == null) {
7979 startIntentSenderForResultInner(intent, fragment.mWho, requestCode, fillInIntent,
7980 flagsMask, flagsValues, options);
7981 } else if (options != null) {
7982 mParent.startIntentSenderFromChildFragment(fragment, intent, requestCode,
7983 fillInIntent, flagsMask, flagsValues, extraFlags, options);
7984 }
7985 }
7986
7987 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007988 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7989 int requestCode) {
7990 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7991 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7992 startActivityForResult(who, intent, requestCode, null);
7993 }
7994
7995 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007996 public boolean onHasWindowAnimations() {
7997 return getWindow() != null;
7998 }
7999
8000 @Override
8001 public int onGetWindowAnimations() {
8002 final Window w = getWindow();
8003 return (w == null) ? 0 : w.getAttributes().windowAnimations;
8004 }
8005
Todd Kennedy434bd652015-05-04 12:29:50 -07008006 @Override
8007 public void onAttachFragment(Fragment fragment) {
8008 Activity.this.onAttachFragment(fragment);
8009 }
8010
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008011 @Nullable
8012 @Override
Alan Viverette04fd4702017-04-13 16:37:06 -04008013 public <T extends View> T onFindViewById(int id) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07008014 return Activity.this.findViewById(id);
8015 }
8016
8017 @Override
8018 public boolean onHasView() {
8019 final Window w = getWindow();
8020 return (w != null && w.peekDecorView() != null);
8021 }
8022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023}