blob: fd63cc77b28d0298a5a1d6fe9d65f07bb2d20fb8 [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
koprivafdb0bff2018-09-29 15:22:41 -0700395 * it), or because the system is temporarily destroying this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 * 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 *
koprivafc3949e2018-09-17 11:35:13 -0700546 * <p>There are generally two kinds of persistent state that an activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 * 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 *
koprivafc3949e2018-09-17 11:35:13 -0700551 * <p>For content provider data, we suggest that activities use an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 * "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());
Felipe Leme5cd660d2018-09-17 09:46:13 -07001375 enableAutofillCompatibilityIfNeeded();
Dake Gu67decfa2017-12-27 11:48:08 -08001376 if (mAutoFillResetNeeded) {
1377 if (!mAutoFillIgnoreFirstResumePause) {
1378 View focus = getCurrentFocus();
1379 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
1380 // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
1381 // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
1382 // window visibility after recreation is INVISIBLE in onResume() and next frame
1383 // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
1384 // So we cannot call View.notifyEnterOrExited() which will do nothing
1385 // when View.isVisibleToUser() is false.
1386 getAutofillManager().notifyViewEntered(focus);
1387 }
1388 }
1389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 mCalled = true;
1391 }
1392
1393 /**
1394 * Called when activity resume is complete (after {@link #onResume} has
1395 * been called). Applications will generally not implement this method;
1396 * it is intended for system classes to do final setup after application
1397 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001398 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 * <p><em>Derived classes must call through to the super class's
1400 * implementation of this method. If they do not, an exception will be
1401 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 * @see #onResume
1404 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001405 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 protected void onPostResume() {
1407 final Window win = getWindow();
1408 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001409 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mCalled = true;
1411 }
1412
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001413 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
Amith Yamasanieeed06c2016-05-03 15:07:03 -07001414 if (mVoiceInteractor != null) {
1415 for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
1416 activeRequest.cancel();
1417 activeRequest.clear();
1418 }
1419 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001420 if (voiceInteractor == null) {
1421 mVoiceInteractor = null;
1422 } else {
1423 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1424 Looper.myLooper());
1425 }
1426 }
1427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 /**
Phil Weaver846cda932017-06-15 10:10:06 -07001429 * Gets the next autofill ID.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001430 *
Phil Weaver846cda932017-06-15 10:10:06 -07001431 * <p>All IDs will be bigger than {@link View#LAST_APP_AUTOFILL_ID}. All IDs returned
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001432 * will be unique.
1433 *
1434 * @return A ID that is unique in the activity
1435 *
1436 * {@hide}
1437 */
Felipe Leme42b97932018-02-20 13:04:31 -08001438 @Override
Phil Weaver846cda932017-06-15 10:10:06 -07001439 public int getNextAutofillId() {
1440 if (mLastAutofillId == Integer.MAX_VALUE - 1) {
1441 mLastAutofillId = View.LAST_APP_AUTOFILL_ID;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001442 }
1443
Phil Weaver846cda932017-06-15 10:10:06 -07001444 mLastAutofillId++;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001445
Phil Weaver846cda932017-06-15 10:10:06 -07001446 return mLastAutofillId;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001447 }
1448
1449 /**
Felipe Leme42b97932018-02-20 13:04:31 -08001450 * @hide
1451 */
1452 @Override
1453 public AutofillId autofillClientGetNextAutofillId() {
1454 return new AutofillId(getNextAutofillId());
1455 }
1456
1457 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001458 * Check whether this activity is running as part of a voice interaction with the user.
1459 * If true, it should perform its interaction with the user through the
1460 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1461 */
1462 public boolean isVoiceInteraction() {
1463 return mVoiceInteractor != null;
1464 }
1465
1466 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001467 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1468 * of a voice interaction. That is, returns true if this activity was directly
1469 * started by the voice interaction service as the initiation of a voice interaction.
1470 * Otherwise, for example if it was started by another activity while under voice
1471 * interaction, returns false.
1472 */
1473 public boolean isVoiceInteractionRoot() {
1474 try {
1475 return mVoiceInteractor != null
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001476 && ActivityManager.getService().isRootVoiceInteraction(mToken);
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001477 } catch (RemoteException e) {
1478 }
1479 return false;
1480 }
1481
1482 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001483 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1484 * interact with this activity.
1485 */
1486 public VoiceInteractor getVoiceInteractor() {
1487 return mVoiceInteractor;
1488 }
1489
1490 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001491 * Queries whether the currently enabled voice interaction service supports returning
1492 * a voice interactor for use by the activity. This is valid only for the duration of the
1493 * activity.
1494 *
1495 * @return whether the current voice interaction service supports local voice interaction
1496 */
1497 public boolean isLocalVoiceInteractionSupported() {
1498 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001499 return ActivityManager.getService().supportsLocalVoiceInteraction();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001500 } catch (RemoteException re) {
1501 }
1502 return false;
1503 }
1504
1505 /**
1506 * Starts a local voice interaction session. When ready,
1507 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1508 * to the registered voice interaction service.
1509 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1510 */
1511 public void startLocalVoiceInteraction(Bundle privateOptions) {
1512 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001513 ActivityManager.getService().startLocalVoiceInteraction(mToken, privateOptions);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001514 } catch (RemoteException re) {
1515 }
1516 }
1517
1518 /**
1519 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1520 * voice interaction session being started. You can now retrieve a voice interactor using
1521 * {@link #getVoiceInteractor()}.
1522 */
1523 public void onLocalVoiceInteractionStarted() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001524 }
1525
1526 /**
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001527 * Callback to indicate that the local voice interaction has stopped either
1528 * because it was requested through a call to {@link #stopLocalVoiceInteraction()}
1529 * or because it was canceled by the user. The previously acquired {@link VoiceInteractor}
1530 * is no longer valid after this.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001531 */
1532 public void onLocalVoiceInteractionStopped() {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001533 }
1534
1535 /**
1536 * Request to terminate the current voice interaction that was previously started
Amith Yamasanid8d967e2016-04-08 09:57:01 -07001537 * using {@link #startLocalVoiceInteraction(Bundle)}. When the interaction is
1538 * terminated, {@link #onLocalVoiceInteractionStopped()} will be called.
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001539 */
1540 public void stopLocalVoiceInteraction() {
1541 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001542 ActivityManager.getService().stopLocalVoiceInteraction(mToken);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001543 } catch (RemoteException re) {
1544 }
1545 }
1546
1547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 * This is called for activities that set launchMode to "singleTop" in
1549 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1550 * flag when calling {@link #startActivity}. In either case, when the
1551 * activity is re-launched while at the top of the activity stack instead
1552 * of a new instance of the activity being started, onNewIntent() will be
1553 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001554 * re-launch it.
1555 *
1556 * <p>An activity will always be paused before receiving a new intent, so
1557 * you can count on {@link #onResume} being called after this method.
1558 *
1559 * <p>Note that {@link #getIntent} still returns the original Intent. You
1560 * can use {@link #setIntent} to update it to this new Intent.
1561 *
1562 * @param intent The new intent that was started for the activity.
1563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001565 * @see #setIntent
1566 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 */
1568 protected void onNewIntent(Intent intent) {
1569 }
1570
1571 /**
1572 * The hook for {@link ActivityThread} to save the state of this activity.
1573 *
1574 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1575 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1576 *
1577 * @param outState The bundle to save the state to.
1578 */
1579 final void performSaveInstanceState(Bundle outState) {
1580 onSaveInstanceState(outState);
1581 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001582 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001583 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001584 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586
1587 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001588 * The hook for {@link ActivityThread} to save the state of this activity.
1589 *
1590 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1591 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1592 *
1593 * @param outState The bundle to save the state to.
1594 * @param outPersistentState The bundle to save persistent state to.
1595 */
1596 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1597 onSaveInstanceState(outState, outPersistentState);
1598 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001599 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001600 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1601 ", " + outPersistentState);
1602 }
1603
1604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 * Called to retrieve per-instance state from an activity before being killed
1606 * so that the state can be restored in {@link #onCreate} or
1607 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1608 * will be passed to both).
1609 *
1610 * <p>This method is called before an activity may be killed so that when it
1611 * comes back some time in the future it can restore its state. For example,
1612 * if activity B is launched in front of activity A, and at some point activity
1613 * A is killed to reclaim resources, activity A will have a chance to save the
1614 * current state of its user interface via this method so that when the user
1615 * returns to activity A, the state of the user interface can be restored
1616 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1617 *
1618 * <p>Do not confuse this method with activity lifecycle callbacks such as
1619 * {@link #onPause}, which is always called when an activity is being placed
1620 * in the background or on its way to destruction, or {@link #onStop} which
1621 * is called before destruction. One example of when {@link #onPause} and
1622 * {@link #onStop} is called and not this method is when a user navigates back
1623 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1624 * on B because that particular instance will never be restored, so the
1625 * system avoids calling it. An example when {@link #onPause} is called and
1626 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1627 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1628 * killed during the lifetime of B since the state of the user interface of
1629 * A will stay intact.
1630 *
1631 * <p>The default implementation takes care of most of the UI per-instance
1632 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1633 * view in the hierarchy that has an id, and by saving the id of the currently
1634 * focused view (all of which is restored by the default implementation of
1635 * {@link #onRestoreInstanceState}). If you override this method to save additional
1636 * information not captured by each individual view, you will likely want to
1637 * call through to the default implementation, otherwise be prepared to save
1638 * all of the state of each view yourself.
1639 *
Andrii Kulian391161f2018-01-29 10:50:02 -08001640 * <p>If called, this method will occur after {@link #onStop} for applications
1641 * targeting platforms starting with {@link android.os.Build.VERSION_CODES#P}.
1642 * For applications targeting earlier platform versions this method will occur
1643 * before {@link #onStop} and there are no guarantees about whether it will
1644 * occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001647 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 * @see #onCreate
1649 * @see #onRestoreInstanceState
1650 * @see #onPause
1651 */
1652 protected void onSaveInstanceState(Bundle outState) {
1653 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001654
Phil Weaver846cda932017-06-15 10:10:06 -07001655 outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001656 Parcelable p = mFragments.saveAllState();
1657 if (p != null) {
1658 outState.putParcelable(FRAGMENTS_TAG, p);
1659 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001660 if (mAutoFillResetNeeded) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001661 outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
1662 getAutofillManager().onSaveInstanceState(outState);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001663 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00001664 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666
1667 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001668 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001669 * created with the attribute {@link android.R.attr#persistableMode} set to
1670 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1671 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1672 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001673 *
1674 * @param outState Bundle in which to place your saved state.
1675 * @param outPersistentState State which will be saved across reboots.
1676 *
1677 * @see #onSaveInstanceState(Bundle)
1678 * @see #onCreate
1679 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1680 * @see #onPause
1681 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001682 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001683 onSaveInstanceState(outState);
1684 }
1685
1686 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 * Save the state of any managed dialogs.
1688 *
1689 * @param outState place to store the saved state.
1690 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001691 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 private void saveManagedDialogs(Bundle outState) {
1693 if (mManagedDialogs == null) {
1694 return;
1695 }
1696
1697 final int numDialogs = mManagedDialogs.size();
1698 if (numDialogs == 0) {
1699 return;
1700 }
1701
1702 Bundle dialogState = new Bundle();
1703
1704 int[] ids = new int[mManagedDialogs.size()];
1705
1706 // save each dialog's bundle, gather the ids
1707 for (int i = 0; i < numDialogs; i++) {
1708 final int key = mManagedDialogs.keyAt(i);
1709 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001710 final ManagedDialog md = mManagedDialogs.valueAt(i);
1711 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1712 if (md.mArgs != null) {
1713 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
1716
1717 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1718 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1719 }
1720
1721
1722 /**
1723 * Called as part of the activity lifecycle when an activity is going into
1724 * the background, but has not (yet) been killed. The counterpart to
1725 * {@link #onResume}.
1726 *
1727 * <p>When activity B is launched in front of activity A, this callback will
1728 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1729 * so be sure to not do anything lengthy here.
1730 *
1731 * <p>This callback is mostly used for saving any persistent state the
1732 * activity is editing, to present a "edit in place" model to the user and
1733 * making sure nothing is lost if there are not enough resources to start
1734 * the new activity without first killing this one. This is also a good
1735 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001736 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 * as fast as possible, or to close resources that are exclusive access
1738 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 * <p>In situations where the system needs more memory it may kill paused
1741 * processes to reclaim resources. Because of this, you should be sure
1742 * that all of your state is saved by the time you return from
1743 * this function. In general {@link #onSaveInstanceState} is used to save
1744 * per-instance state in the activity and this method is used to store
1745 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001746 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 * <p>After receiving this call you will usually receive a following call
1748 * to {@link #onStop} (after the next activity has been resumed and
1749 * displayed), however in some cases there will be a direct call back to
1750 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001751 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 * <p><em>Derived classes must call through to the super class's
1753 * implementation of this method. If they do not, an exception will be
1754 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001755 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 * @see #onResume
1757 * @see #onSaveInstanceState
1758 * @see #onStop
1759 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001760 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001762 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Narayan Kamathff5659f2017-02-02 13:31:33 +00001763 getApplication().dispatchActivityPaused(this);
Dake Gu67decfa2017-12-27 11:48:08 -08001764 if (mAutoFillResetNeeded) {
1765 if (!mAutoFillIgnoreFirstResumePause) {
1766 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill notifyViewExited " + this);
1767 View focus = getCurrentFocus();
1768 if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
1769 getAutofillManager().notifyViewExited(focus);
1770 }
1771 } else {
1772 // reset after first pause()
1773 if (DEBUG_LIFECYCLE) Slog.v(TAG, "autofill got first pause " + this);
1774 mAutoFillIgnoreFirstResumePause = false;
1775 }
1776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 mCalled = true;
1778 }
1779
1780 /**
1781 * Called as part of the activity lifecycle when an activity is about to go
1782 * into the background as the result of user choice. For example, when the
1783 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1784 * when an incoming phone call causes the in-call Activity to be automatically
1785 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1786 * the activity being interrupted. In cases when it is invoked, this method
1787 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001788 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * <p>This callback and {@link #onUserInteraction} are intended to help
1790 * activities manage status bar notifications intelligently; specifically,
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001791 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07001792 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 * @see #onUserInteraction()
1794 */
1795 protected void onUserLeaveHint() {
1796 }
RoboErik55011652014-07-09 15:05:53 -07001797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 /**
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001799 * @deprecated Method doesn't do anything and will be removed in the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 */
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001801 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001803 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 }
1805
1806 /**
1807 * Generate a new description for this activity. This method is called
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001808 * before stopping the activity and can, if desired, return some textual
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 * <p>The default implementation returns null, which will cause you to
1812 * inherit the description from the previous activity. If all activities
1813 * return null, generally the label of the top activity will be used as the
1814 * description.
RoboErik55011652014-07-09 15:05:53 -07001815 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 * @return A description of what the user is doing. It should be short and
1817 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 * @see #onSaveInstanceState
Wale Ogunwale578b3bd2018-02-13 08:30:53 -08001820 * @see #onStop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001822 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 public CharSequence onCreateDescription() {
1824 return null;
1825 }
1826
1827 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001828 * This is called when the user is requesting an assist, to build a full
1829 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1830 * application. You can override this method to place into the bundle anything
1831 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001832 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001833 *
1834 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001835 * been registered with {@link Application#registerOnProvideAssistDataListener
1836 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001837 */
1838 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001839 }
1840
1841 /**
1842 * This is called when the user is requesting an assist, to provide references
1843 * to content related to the current activity. Before being called, the
1844 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1845 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1846 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1847 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1848 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1849 *
1850 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1851 * context of the activity, and fill in its ClipData with additional content of
1852 * interest that the user is currently viewing. For example, an image gallery application
1853 * that has launched in to an activity allowing the user to swipe through pictures should
1854 * modify the intent to reference the current image they are looking it; such an
1855 * application when showing a list of pictures should add a ClipData that has
1856 * references to all of the pictures currently visible on screen.</p>
1857 *
1858 * @param outContent The assist content to return.
1859 */
1860 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001861 }
1862
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001863 /**
Clara Bayarriab591ba2016-05-16 17:48:16 +01001864 * Request the Keyboard Shortcuts screen to show up. This will trigger
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001865 * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
1866 */
Clara Bayarriac6f0342016-05-16 14:15:14 +01001867 public final void requestShowKeyboardShortcuts() {
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001868 Intent intent = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001869 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1870 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Andrei Stingaceanu0bf096f2016-04-14 18:11:57 +01001871 }
1872
1873 /**
1874 * Dismiss the Keyboard Shortcuts screen.
1875 */
1876 public final void dismissKeyboardShortcutsHelper() {
1877 Intent intent = new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
Andrei Stingaceanua3d26932016-07-15 11:12:20 +01001878 intent.setPackage(KEYBOARD_SHORTCUTS_RECEIVER_PKG_NAME);
1879 sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Clara Bayarrieb3c2d32016-04-01 14:37:32 +01001880 }
1881
Clara Bayarri75e09792015-07-29 16:20:40 +01001882 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001883 public void onProvideKeyboardShortcuts(
1884 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01001885 if (menu == null) {
1886 return;
1887 }
1888 KeyboardShortcutGroup group = null;
1889 int menuSize = menu.size();
1890 for (int i = 0; i < menuSize; ++i) {
1891 final MenuItem item = menu.getItem(i);
1892 final CharSequence title = item.getTitle();
1893 final char alphaShortcut = item.getAlphabeticShortcut();
Peeyush Agarwale631e322016-10-19 11:41:42 +01001894 final int alphaModifiers = item.getAlphabeticModifiers();
Clara Bayarri75e09792015-07-29 16:20:40 +01001895 if (title != null && alphaShortcut != MIN_VALUE) {
1896 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001897 final int resource = mApplication.getApplicationInfo().labelRes;
1898 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001899 }
1900 group.addItem(new KeyboardShortcutInfo(
Peeyush Agarwale631e322016-10-19 11:41:42 +01001901 title, alphaShortcut, alphaModifiers));
Clara Bayarri75e09792015-07-29 16:20:40 +01001902 }
1903 }
1904 if (group != null) {
1905 data.add(group);
1906 }
1907 }
1908
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001909 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001910 * Ask to have the current assistant shown to the user. This only works if the calling
1911 * activity is the current foreground activity. It is the same as calling
1912 * {@link android.service.voice.VoiceInteractionService#showSession
1913 * VoiceInteractionService.showSession} and requesting all of the possible context.
1914 * The receiver will always see
1915 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1916 * @return Returns true if the assistant was successfully invoked, else false. For example
1917 * false will be returned if the caller is not the current top activity.
1918 */
1919 public boolean showAssist(Bundle args) {
1920 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001921 return ActivityManager.getService().showAssistFromActivity(mToken, args);
Dianne Hackborn17f69352015-07-17 18:04:14 -07001922 } catch (RemoteException e) {
1923 }
1924 return false;
1925 }
1926
1927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 * Called when you are no longer visible to the user. You will next
1929 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1930 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001931 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 * <p><em>Derived classes must call through to the super class's
1933 * implementation of this method. If they do not, an exception will be
1934 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001935 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 * @see #onRestart
1937 * @see #onResume
1938 * @see #onSaveInstanceState
1939 * @see #onDestroy
1940 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001941 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001943 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001944 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001945 mActivityTransitionState.onStop();
Narayan Kamathff5659f2017-02-02 13:31:33 +00001946 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001947 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 mCalled = true;
Felipe Lemec24a56a2017-08-03 14:27:57 -07001949
Dake Gu67decfa2017-12-27 11:48:08 -08001950 if (mAutoFillResetNeeded) {
1951 getAutofillManager().onInvisibleForAutofill();
1952 }
1953
Felipe Lemec24a56a2017-08-03 14:27:57 -07001954 if (isFinishing()) {
1955 if (mAutoFillResetNeeded) {
Felipe Leme5b32ebe2018-02-15 12:52:19 -08001956 getAutofillManager().onActivityFinishing();
Felipe Lemec24a56a2017-08-03 14:27:57 -07001957 } else if (mIntent != null
1958 && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
1959 // Activity was launched when user tapped a link in the Autofill Save UI - since
1960 // user launched another activity, the Save UI should not be restored when this
1961 // activity is finished.
1962 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL,
1963 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
1964 }
Felipe Lemebab851c2017-02-03 18:45:08 -08001965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967
1968 /**
1969 * Perform any final cleanup before an activity is destroyed. This can
1970 * happen either because the activity is finishing (someone called
koprivafdb0bff2018-09-29 15:22:41 -07001971 * {@link #finish} on it), or because the system is temporarily destroying
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 * this instance of the activity to save space. You can distinguish
1973 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 * <p><em>Note: do not count on this method being called as a place for
1976 * saving data! For example, if an activity is editing data in a content
1977 * provider, those edits should be committed in either {@link #onPause} or
1978 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1979 * free resources like threads that are associated with an activity, so
1980 * that a destroyed activity does not leave such things around while the
1981 * rest of its application is still running. There are situations where
1982 * the system will simply kill the activity's hosting process without
1983 * calling this method (or any others) in it, so it should not be used to
1984 * do things that are intended to remain around after the process goes
1985 * away.
RoboErik55011652014-07-09 15:05:53 -07001986 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 * <p><em>Derived classes must call through to the super class's
1988 * implementation of this method. If they do not, an exception will be
1989 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 * @see #onPause
1992 * @see #onStop
1993 * @see #finish
1994 * @see #isFinishing
1995 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001996 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001998 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 mCalled = true;
2000
2001 // dismiss any dialogs we are managing.
2002 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 final int numDialogs = mManagedDialogs.size();
2004 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002005 final ManagedDialog md = mManagedDialogs.valueAt(i);
2006 if (md.mDialog.isShowing()) {
2007 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
2009 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002010 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012
2013 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002014 synchronized (mManagedCursors) {
2015 int numCursors = mManagedCursors.size();
2016 for (int i = 0; i < numCursors; i++) {
2017 ManagedCursor c = mManagedCursors.get(i);
2018 if (c != null) {
2019 c.mCursor.close();
2020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08002022 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
Amith Yamasani49860442010-03-17 20:54:10 -07002024
2025 // Close any open search dialog
2026 if (mSearchManager != null) {
2027 mSearchManager.stopSearch();
2028 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002029
Chris Banes21b25772016-01-04 20:41:59 +00002030 if (mActionBar != null) {
2031 mActionBar.onDestroy();
2032 }
Narayan Kamathff5659f2017-02-02 13:31:33 +00002033
2034 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036
2037 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002038 * Report to the system that your app is now fully drawn, purely for diagnostic
2039 * purposes (calling it does not impact the visible behavior of the activity).
2040 * This is only used to help instrument application launch times, so that the
2041 * app can report when it is fully in a usable state; without this, the only thing
2042 * the system itself can determine is the point at which the activity's window
2043 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002044 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002045 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002046 * entirely drawn your UI and populated with all of the significant data. You
2047 * can safely call this method any time after first launch as well, in which case
2048 * it will simply be ignored.
2049 */
2050 public void reportFullyDrawn() {
2051 if (mDoReportFullyDrawn) {
2052 mDoReportFullyDrawn = false;
2053 try {
Jorim Jaggi4d27b842017-08-17 17:22:26 +02002054 ActivityManager.getService().reportActivityFullyDrawn(mToken, mRestoredFromBundle);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002055 } catch (RemoteException e) {
2056 }
2057 }
2058 }
2059
2060 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08002061 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
Winson Chung5af42fc2017-03-24 17:11:33 -07002062 * visa-versa. This method provides the same configuration that will be sent in the following
2063 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2064 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002065 * @see android.R.attr#resizeableActivity
2066 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002067 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002068 * @param newConfig The new configuration of the activity with the state
2069 * {@param isInMultiWindowMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002070 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002071 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2072 // Left deliberately empty. There should be no side effects if a direct
2073 // subclass of Activity does not call super.
2074 onMultiWindowModeChanged(isInMultiWindowMode);
2075 }
2076
2077 /**
2078 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
2079 * visa-versa.
2080 *
2081 * @see android.R.attr#resizeableActivity
2082 *
2083 * @param isInMultiWindowMode True if the activity is in multi-window mode.
2084 *
2085 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
2086 */
2087 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002088 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002089 // Left deliberately empty. There should be no side effects if a direct
2090 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002091 }
2092
2093 /**
2094 * Returns true if the activity is currently in multi-window mode.
2095 * @see android.R.attr#resizeableActivity
2096 *
2097 * @return True if the activity is in multi-window mode.
2098 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002099 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002100 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002101 return ActivityManager.getService().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002102 } catch (RemoteException e) {
2103 }
2104 return false;
2105 }
2106
2107 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07002108 * Called by the system when the activity changes to and from picture-in-picture mode. This
2109 * method provides the same configuration that will be sent in the following
2110 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
2111 *
Wale Ogunwale5f986092015-12-04 15:35:38 -08002112 * @see android.R.attr#supportsPictureInPicture
2113 *
Andrii Kulian933076d2016-03-29 17:04:42 -07002114 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07002115 * @param newConfig The new configuration of the activity with the state
2116 * {@param isInPictureInPictureMode}.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002117 */
Winson Chung5af42fc2017-03-24 17:11:33 -07002118 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2119 Configuration newConfig) {
2120 // Left deliberately empty. There should be no side effects if a direct
2121 // subclass of Activity does not call super.
2122 onPictureInPictureModeChanged(isInPictureInPictureMode);
2123 }
2124
2125 /**
2126 * Called by the system when the activity changes to and from picture-in-picture mode.
2127 *
2128 * @see android.R.attr#supportsPictureInPicture
2129 *
2130 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
2131 *
2132 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
2133 */
2134 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002135 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Adam Powell858cf032016-05-09 15:45:37 -07002136 // Left deliberately empty. There should be no side effects if a direct
2137 // subclass of Activity does not call super.
Wale Ogunwale5f986092015-12-04 15:35:38 -08002138 }
2139
2140 /**
2141 * Returns true if the activity is currently in picture-in-picture mode.
2142 * @see android.R.attr#supportsPictureInPicture
2143 *
2144 * @return True if the activity is in picture-in-picture mode.
2145 */
Andrii Kulian933076d2016-03-29 17:04:42 -07002146 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002147 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002148 return ActivityManager.getService().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002149 } catch (RemoteException e) {
2150 }
2151 return false;
2152 }
2153
2154 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002155 * Puts the activity in picture-in-picture mode if possible in the current system state. Any
Winson Chung709904f2017-04-25 11:00:48 -07002156 * prior calls to {@link #setPictureInPictureParams(PictureInPictureParams)} will still apply
2157 * when entering picture-in-picture through this call.
Winson Chungc2baac02017-01-11 13:34:47 -08002158 *
Winson Chung709904f2017-04-25 11:00:48 -07002159 * @see #enterPictureInPictureMode(PictureInPictureParams)
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002160 * @see android.R.attr#supportsPictureInPicture
2161 */
Winson Chung709904f2017-04-25 11:00:48 -07002162 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002163 public void enterPictureInPictureMode() {
Winson Chung709904f2017-04-25 11:00:48 -07002164 enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002165 }
2166
Jeff Sharkey000ce802017-04-29 13:13:27 -06002167 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -07002168 @Deprecated
Winson Chungc2baac02017-01-11 13:34:47 -08002169 public boolean enterPictureInPictureMode(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002170 return enterPictureInPictureMode(PictureInPictureArgs.convert(args));
Winson Chungc2baac02017-01-11 13:34:47 -08002171 }
2172
2173 /**
Winson Chung709904f2017-04-25 11:00:48 -07002174 * Puts the activity in picture-in-picture mode if possible in the current system state. The
2175 * set parameters in {@param params} will be combined with the parameters from prior calls to
2176 * {@link #setPictureInPictureParams(PictureInPictureParams)}.
2177 *
2178 * The system may disallow entering picture-in-picture in various cases, including when the
2179 * activity is not visible, if the screen is locked or if the user has an activity pinned.
2180 *
2181 * @see android.R.attr#supportsPictureInPicture
2182 * @see PictureInPictureParams
2183 *
2184 * @param params non-null parameters to be combined with previously set parameters when entering
2185 * picture-in-picture.
2186 *
Winson Chung80fa2d82018-01-25 13:58:20 -08002187 * @return true if the system successfully put this activity into picture-in-picture mode or was
Andrew Solovay8a788f42018-12-12 14:25:47 -08002188 * already in picture-in-picture mode (see {@link #isInPictureInPictureMode()}). If the device
Winson Chung80fa2d82018-01-25 13:58:20 -08002189 * does not support picture-in-picture, return false.
Winson Chung709904f2017-04-25 11:00:48 -07002190 */
2191 public boolean enterPictureInPictureMode(@NonNull PictureInPictureParams params) {
2192 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002193 if (!deviceSupportsPictureInPictureMode()) {
2194 return false;
2195 }
Winson Chung709904f2017-04-25 11:00:48 -07002196 if (params == null) {
2197 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
2198 }
Winson Chung298f95b2017-08-10 15:57:18 -07002199 if (!mCanEnterPictureInPicture) {
2200 throw new IllegalStateException("Activity must be resumed to enter"
2201 + " picture-in-picture");
2202 }
Winson Chung709904f2017-04-25 11:00:48 -07002203 return ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken, params);
2204 } catch (RemoteException e) {
2205 return false;
2206 }
2207 }
2208
Jeff Sharkey000ce802017-04-29 13:13:27 -06002209 /** @removed */
2210 @Deprecated
Winson Chung709904f2017-04-25 11:00:48 -07002211 public void setPictureInPictureArgs(@NonNull PictureInPictureArgs args) {
Jeff Sharkey000ce802017-04-29 13:13:27 -06002212 setPictureInPictureParams(PictureInPictureArgs.convert(args));
Winson Chung709904f2017-04-25 11:00:48 -07002213 }
2214
2215 /**
Winson Chungc2baac02017-01-11 13:34:47 -08002216 * Updates the properties of the picture-in-picture activity, or sets it to be used later when
2217 * {@link #enterPictureInPictureMode()} is called.
2218 *
Winson Chung709904f2017-04-25 11:00:48 -07002219 * @param params the new parameters for the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -08002220 */
Winson Chung709904f2017-04-25 11:00:48 -07002221 public void setPictureInPictureParams(@NonNull PictureInPictureParams params) {
Winson Chungc2baac02017-01-11 13:34:47 -08002222 try {
Winson Chung80fa2d82018-01-25 13:58:20 -08002223 if (!deviceSupportsPictureInPictureMode()) {
2224 return;
2225 }
Winson Chung709904f2017-04-25 11:00:48 -07002226 if (params == null) {
2227 throw new IllegalArgumentException("Expected non-null picture-in-picture params");
Winson Chungc2baac02017-01-11 13:34:47 -08002228 }
Winson Chung709904f2017-04-25 11:00:48 -07002229 ActivityManagerNative.getDefault().setPictureInPictureParams(mToken, params);
Winson Chungb5c41b72016-12-07 15:00:47 -08002230 } catch (RemoteException e) {
2231 }
2232 }
2233
Winson Chung8802eac2017-04-17 14:21:44 -07002234 /**
Winson Chung709904f2017-04-25 11:00:48 -07002235 * Return the number of actions that will be displayed in the picture-in-picture UI when the
2236 * user interacts with the activity currently in picture-in-picture mode. This number may change
2237 * if the global configuration changes (ie. if the device is plugged into an external display),
2238 * but will always be larger than three.
2239 */
2240 public int getMaxNumPictureInPictureActions() {
2241 try {
2242 return ActivityManagerNative.getDefault().getMaxNumPictureInPictureActions(mToken);
2243 } catch (RemoteException e) {
2244 return 0;
2245 }
2246 }
2247
Winson Chung80fa2d82018-01-25 13:58:20 -08002248 /**
2249 * @return Whether this device supports picture-in-picture.
2250 */
2251 private boolean deviceSupportsPictureInPictureMode() {
2252 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
2253 }
2254
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002255 void dispatchMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002256 updateDisplay(displayId);
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002257 onMovedToDisplay(displayId, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002258 }
2259
2260 /**
2261 * Called by the system when the activity is moved from one display to another without
2262 * recreation. This means that this activity is declared to handle all changes to configuration
2263 * that happened when it was switched to another display, so it wasn't destroyed and created
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002264 * again.
2265 *
2266 * <p>This call will be followed by {@link #onConfigurationChanged(Configuration)} if the
2267 * applied configuration actually changed. It is up to app developer to choose whether to handle
2268 * the change in this method or in the following {@link #onConfigurationChanged(Configuration)}
2269 * call.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002270 *
2271 * <p>Use this callback to track changes to the displays if some activity functionality relies
2272 * on an association with some display properties.
2273 *
2274 * @param displayId The id of the display to which activity was moved.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002275 * @param config Configuration of the activity resources on new display after move.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002276 *
2277 * @see #onConfigurationChanged(Configuration)
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002278 * @see View#onMovedToDisplay(int, Configuration)
Andrii Kuliancadacce2017-04-26 18:09:54 -07002279 * @hide
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002280 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07002281 public void onMovedToDisplay(int displayId, Configuration config) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002282 }
2283
Winson Chungb5c41b72016-12-07 15:00:47 -08002284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 * Called by the system when the device configuration changes while your
2286 * activity is running. Note that this will <em>only</em> be called if
2287 * you have selected configurations you would like to handle with the
2288 * {@link android.R.attr#configChanges} attribute in your manifest. If
2289 * any configuration change occurs that is not selected to be reported
2290 * by that attribute, then instead of reporting it the system will stop
2291 * and restart the activity (to have it launched with the new
2292 * configuration).
RoboErik55011652014-07-09 15:05:53 -07002293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 * <p>At the time that this function has been called, your Resources
2295 * object will have been updated to return resource values matching the
2296 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07002297 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 * @param newConfig The new device configuration.
2299 */
2300 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002301 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01002303
Dianne Hackborn9d071802010-12-08 14:49:15 -08002304 mFragments.dispatchConfigurationChanged(newConfig);
2305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 if (mWindow != null) {
2307 // Pass the configuration changed event to the window
2308 mWindow.onConfigurationChanged(newConfig);
2309 }
Adam Powell45c0b192011-07-28 15:11:57 -07002310
2311 if (mActionBar != null) {
2312 // Do this last; the action bar will need to access
2313 // view changes from above.
2314 mActionBar.onConfigurationChanged(newConfig);
2315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
RoboErik55011652014-07-09 15:05:53 -07002317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 /**
2319 * If this activity is being destroyed because it can not handle a
2320 * configuration parameter being changed (and thus its
2321 * {@link #onConfigurationChanged(Configuration)} method is
2322 * <em>not</em> being called), then you can use this method to discover
2323 * the set of changes that have occurred while in the process of being
2324 * destroyed. Note that there is no guarantee that these will be
2325 * accurate (other changes could have happened at any time), so you should
2326 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002327 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 * @return Returns a bit field of the configuration parameters that are
2329 * changing, as defined by the {@link android.content.res.Configuration}
2330 * class.
2331 */
2332 public int getChangingConfigurations() {
2333 return mConfigChangeFlags;
2334 }
RoboErik55011652014-07-09 15:05:53 -07002335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 /**
2337 * Retrieve the non-configuration instance data that was previously
2338 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2339 * be available from the initial {@link #onCreate} and
2340 * {@link #onStart} calls to the new instance, allowing you to extract
2341 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002342 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 * <p>Note that the data you retrieve here should <em>only</em> be used
2344 * as an optimization for handling configuration changes. You should always
2345 * be able to handle getting a null pointer back, and an activity must
2346 * still be able to restore itself to its previous state (through the
2347 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2348 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002349 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002350 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002351 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002352 * available on older platforms through the Android support libraries.
2353 *
2354 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002356 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002358 return mLastNonConfigurationInstances != null
2359 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
RoboErik55011652014-07-09 15:05:53 -07002361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 /**
2363 * Called by the system, as part of destroying an
2364 * activity due to a configuration change, when it is known that a new
2365 * instance will immediately be created for the new configuration. You
2366 * can return any object you like here, including the activity instance
2367 * itself, which can later be retrieved by calling
2368 * {@link #getLastNonConfigurationInstance()} in the new activity
2369 * instance.
RoboErik55011652014-07-09 15:05:53 -07002370 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002371 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2372 * or later, consider instead using a {@link Fragment} with
2373 * {@link Fragment#setRetainInstance(boolean)
2374 * Fragment.setRetainInstance(boolean}.</em>
2375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 * <p>This function is called purely as an optimization, and you must
2377 * not rely on it being called. When it is called, a number of guarantees
2378 * will be made to help optimize configuration switching:
2379 * <ul>
2380 * <li> The function will be called between {@link #onStop} and
2381 * {@link #onDestroy}.
2382 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002383 * created after this one's {@link #onDestroy()} is called. In particular,
2384 * <em>no</em> messages will be dispatched during this time (when the returned
2385 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 * <li> The object you return here will <em>always</em> be available from
2387 * the {@link #getLastNonConfigurationInstance()} method of the following
2388 * activity instance as described there.
2389 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002390 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 * <p>These guarantees are designed so that an activity can use this API
2392 * to propagate extensive state from the old to new activity instance, from
2393 * loaded bitmaps, to network connections, to evenly actively running
2394 * threads. Note that you should <em>not</em> propagate any data that
2395 * may change based on the configuration, including any data loaded from
2396 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002397 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002398 * <p>The guarantee of no message handling during the switch to the next
2399 * activity simplifies use with active objects. For example if your retained
2400 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2401 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2402 * not be called from the call here until you execute the next instance's
2403 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2404 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2405 * running in a separate thread.)
2406 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002407 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002408 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002409 * available on older platforms through the Android support libraries.
2410 *
2411 * @return any Object holding the desired state to propagate to the
2412 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 */
2414 public Object onRetainNonConfigurationInstance() {
2415 return null;
2416 }
RoboErik55011652014-07-09 15:05:53 -07002417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 /**
2419 * Retrieve the non-configuration instance data that was previously
2420 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2421 * be available from the initial {@link #onCreate} and
2422 * {@link #onStart} calls to the new instance, allowing you to extract
2423 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 * <p>Note that the data you retrieve here should <em>only</em> be used
2426 * as an optimization for handling configuration changes. You should always
2427 * be able to handle getting a null pointer back, and an activity must
2428 * still be able to restore itself to its previous state (through the
2429 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2430 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 * @return Returns the object previously returned by
2433 * {@link #onRetainNonConfigurationChildInstances()}
2434 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002435 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002436 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2437 return mLastNonConfigurationInstances != null
2438 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
RoboErik55011652014-07-09 15:05:53 -07002440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 /**
2442 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2443 * it should return either a mapping from child activity id strings to arbitrary objects,
2444 * or null. This method is intended to be used by Activity framework subclasses that control a
2445 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2446 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2447 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002448 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2450 return null;
2451 }
RoboErik55011652014-07-09 15:05:53 -07002452
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002453 NonConfigurationInstances retainNonConfigurationInstances() {
2454 Object activity = onRetainNonConfigurationInstance();
2455 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002456 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002457
2458 // We're already stopped but we've been asked to retain.
2459 // Our fragments are taken care of but we need to mark the loaders for retention.
2460 // In order to do this correctly we need to restart the loaders first before
2461 // handing them off to the next activity.
2462 mFragments.doLoaderStart();
2463 mFragments.doLoaderStop(true);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002464 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
Adam Powellcbade7f2016-04-15 11:14:37 -07002465
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002466 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002467 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002468 return null;
2469 }
RoboErik55011652014-07-09 15:05:53 -07002470
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002471 NonConfigurationInstances nci = new NonConfigurationInstances();
2472 nci.activity = activity;
2473 nci.children = children;
2474 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002475 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002476 if (mVoiceInteractor != null) {
2477 mVoiceInteractor.retainInstance();
2478 nci.voiceInteractor = mVoiceInteractor;
2479 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002480 return nci;
2481 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002484 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002486 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002488
2489 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002490 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002491 mCalled = true;
2492 mFragments.dispatchTrimMemory(level);
2493 }
2494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002496 * Return the FragmentManager for interacting with fragments associated
2497 * with this activity.
Ian Lake0a1feb82017-11-13 10:26:46 -08002498 *
2499 * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002500 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002501 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002502 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002503 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002504 }
RoboErik55011652014-07-09 15:05:53 -07002505
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002506 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002507 * Called when a Fragment is being attached to this activity, immediately
2508 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2509 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
Ian Lake0a1feb82017-11-13 10:26:46 -08002510 *
2511 * @deprecated Use {@link
2512 * android.support.v4.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)}
Dianne Hackbornc8017682010-07-06 13:34:38 -07002513 */
Ian Lake0a1feb82017-11-13 10:26:46 -08002514 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07002515 public void onAttachFragment(Fragment fragment) {
2516 }
RoboErik55011652014-07-09 15:05:53 -07002517
Dianne Hackbornc8017682010-07-06 13:34:38 -07002518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 * Wrapper around
2520 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2521 * that gives the resulting {@link Cursor} to call
2522 * {@link #startManagingCursor} so that the activity will manage its
2523 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002524 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002525 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2526 * or later, consider instead using {@link LoaderManager} instead, available
2527 * via {@link #getLoaderManager()}.</em>
2528 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002529 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2530 * this method, because the activity will do that for you at the appropriate time. However, if
2531 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2532 * not</em> automatically close the cursor and, in that case, you must call
2533 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002534 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 * @param uri The URI of the content provider to query.
2536 * @param projection List of columns to return.
2537 * @param selection SQL WHERE clause.
2538 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002539 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2543 * @see #startManagingCursor
2544 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002545 *
2546 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 */
Jason parks6ed50de2010-08-25 10:18:50 -05002548 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002549 @UnsupportedAppUsage
Dianne Hackborn291905e2010-08-17 15:17:15 -07002550 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2551 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2553 if (c != null) {
2554 startManagingCursor(c);
2555 }
2556 return c;
2557 }
2558
2559 /**
2560 * Wrapper around
2561 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2562 * that gives the resulting {@link Cursor} to call
2563 * {@link #startManagingCursor} so that the activity will manage its
2564 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002565 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002566 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2567 * or later, consider instead using {@link LoaderManager} instead, available
2568 * via {@link #getLoaderManager()}.</em>
2569 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002570 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2571 * this method, because the activity will do that for you at the appropriate time. However, if
2572 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2573 * not</em> automatically close the cursor and, in that case, you must call
2574 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002575 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 * @param uri The URI of the content provider to query.
2577 * @param projection List of columns to return.
2578 * @param selection SQL WHERE clause.
2579 * @param selectionArgs The arguments to selection, if any ?s are pesent
2580 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002581 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002583 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2585 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002586 *
2587 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 */
Jason parks6ed50de2010-08-25 10:18:50 -05002589 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002590 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2591 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2593 if (c != null) {
2594 startManagingCursor(c);
2595 }
2596 return c;
2597 }
2598
2599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 * This method allows the activity to take care of managing the given
2601 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2602 * That is, when the activity is stopped it will automatically call
2603 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2604 * it will call {@link Cursor#requery} for you. When the activity is
2605 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002606 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002607 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2608 * or later, consider instead using {@link LoaderManager} instead, available
2609 * via {@link #getLoaderManager()}.</em>
2610 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002611 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2612 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2613 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2614 * <em>will not</em> automatically close the cursor and, in that case, you must call
2615 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002618 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2620 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002621 *
2622 * @deprecated Use the new {@link android.content.CursorLoader} class with
2623 * {@link LoaderManager} instead; this is also
2624 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 */
Jason parks6ed50de2010-08-25 10:18:50 -05002626 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 public void startManagingCursor(Cursor c) {
2628 synchronized (mManagedCursors) {
2629 mManagedCursors.add(new ManagedCursor(c));
2630 }
2631 }
2632
2633 /**
2634 * Given a Cursor that was previously given to
2635 * {@link #startManagingCursor}, stop the activity's management of that
2636 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002637 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002638 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002639 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002640 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002641 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002645 *
2646 * @deprecated Use the new {@link android.content.CursorLoader} class with
2647 * {@link LoaderManager} instead; this is also
2648 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 */
Jason parks6ed50de2010-08-25 10:18:50 -05002650 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 public void stopManagingCursor(Cursor c) {
2652 synchronized (mManagedCursors) {
2653 final int N = mManagedCursors.size();
2654 for (int i=0; i<N; i++) {
2655 ManagedCursor mc = mManagedCursors.get(i);
2656 if (mc.mCursor == c) {
2657 mManagedCursors.remove(i);
2658 break;
2659 }
2660 }
2661 }
2662 }
2663
2664 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002665 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2666 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002667 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002669 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002670 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
2673
2674 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -04002675 * Finds a view that was identified by the {@code android:id} XML attribute
2676 * that was processed in {@link #onCreate}.
2677 * <p>
2678 * <strong>Note:</strong> In most cases -- depending on compiler support --
2679 * the resulting view is automatically cast to the target class type. If
2680 * the target class type is unconstrained, an explicit cast may be
2681 * necessary.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 *
Alan Viverettedb7423c2017-03-31 13:13:58 -04002683 * @param id the ID to search for
2684 * @return a view with given ID if found, or {@code null} otherwise
2685 * @see View#findViewById(int)
Chris Craik6faa9e52018-01-11 10:46:10 -08002686 * @see Activity#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002688 @Nullable
Alan Viverettedb7423c2017-03-31 13:13:58 -04002689 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 return getWindow().findViewById(id);
2691 }
RoboErik55011652014-07-09 15:05:53 -07002692
Adam Powell33b97432010-04-20 10:01:14 -07002693 /**
Chris Craik6faa9e52018-01-11 10:46:10 -08002694 * Finds a view that was identified by the {@code android:id} XML attribute that was processed
2695 * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
2696 * no matching view in the hierarchy.
2697 * <p>
2698 * <strong>Note:</strong> In most cases -- depending on compiler support --
2699 * the resulting view is automatically cast to the target class type. If
2700 * the target class type is unconstrained, an explicit cast may be
2701 * necessary.
2702 *
2703 * @param id the ID to search for
2704 * @return a view with given ID
2705 * @see View#requireViewById(int)
2706 * @see Activity#findViewById(int)
2707 */
2708 @NonNull
2709 public final <T extends View> T requireViewById(@IdRes int id) {
2710 T view = findViewById(id);
2711 if (view == null) {
2712 throw new IllegalArgumentException("ID does not reference a View inside this Activity");
2713 }
2714 return view;
2715 }
2716
2717 /**
Adam Powell33b97432010-04-20 10:01:14 -07002718 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002719 *
Adam Powell33b97432010-04-20 10:01:14 -07002720 * @return The Activity's ActionBar, or null if it does not have one.
2721 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002722 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002723 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002724 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002725 return mActionBar;
2726 }
Adam Powelle43340c2014-03-17 19:10:43 -07002727
2728 /**
2729 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2730 * Activity window.
2731 *
2732 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2733 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2734 * a traditional window decor action bar. The toolbar's menu will be populated with the
2735 * Activity's options menu and the navigation button will be wired through the standard
2736 * {@link android.R.id#home home} menu select action.</p>
2737 *
2738 * <p>In order to use a Toolbar within the Activity's window content the application
2739 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2740 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002741 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002742 */
Adam Powell37780142014-06-01 13:31:00 -07002743 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002744 final ActionBar ab = getActionBar();
2745 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002746 throw new IllegalStateException("This Activity already has an action bar supplied " +
2747 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2748 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2749 }
Chris Banes21b25772016-01-04 20:41:59 +00002750
2751 // If we reach here then we're setting a new action bar
2752 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002753 mMenuInflater = null;
2754
Chris Banes21b25772016-01-04 20:41:59 +00002755 // If we have an action bar currently, destroy it
2756 if (ab != null) {
2757 ab.onDestroy();
2758 }
2759
Chris Banesc7d6c322016-01-27 14:09:16 +00002760 if (toolbar != null) {
2761 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2762 mActionBar = tbab;
2763 mWindow.setCallback(tbab.getWrappedWindowCallback());
2764 } else {
2765 mActionBar = null;
2766 // Re-set the original window callback since we may have already set a Toolbar wrapper
2767 mWindow.setCallback(this);
2768 }
2769
2770 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002771 }
RoboErik55011652014-07-09 15:05:53 -07002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 /**
Adam Powell33b97432010-04-20 10:01:14 -07002774 * Creates a new ActionBar, locates the inflated ActionBarView,
2775 * initializes the ActionBar with the view, and sets mActionBar.
2776 */
Adam Powelle43340c2014-03-17 19:10:43 -07002777 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002778 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002779
2780 // Initializing the window decor can change window feature flags.
2781 // Make sure that we have the correct set before performing the test below.
2782 window.getDecorView();
2783
Adam Powell9b4c8042010-08-10 15:36:44 -07002784 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002785 return;
2786 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002787
Adam Powelle43340c2014-03-17 19:10:43 -07002788 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002789 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002790
2791 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2792 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002793 }
RoboErik55011652014-07-09 15:05:53 -07002794
Adam Powell33b97432010-04-20 10:01:14 -07002795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 * Set the activity content from a layout resource. The resource will be
2797 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002798 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002800 *
Romain Guy482b34a62011-01-20 10:59:28 -08002801 * @see #setContentView(android.view.View)
2802 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002804 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002806 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 }
2808
2809 /**
2810 * Set the activity content to an explicit view. This view is placed
2811 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002812 * view hierarchy. When calling this method, the layout parameters of the
2813 * specified view are ignored. Both the width and the height of the view are
2814 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2815 * your own layout parameters, invoke
2816 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2817 * instead.
RoboErik55011652014-07-09 15:05:53 -07002818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002820 *
2821 * @see #setContentView(int)
2822 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 */
2824 public void setContentView(View view) {
2825 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002826 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
2828
2829 /**
2830 * Set the activity content to an explicit view. This view is placed
2831 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002832 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002833 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 * @param view The desired content to display.
2835 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002836 *
2837 * @see #setContentView(android.view.View)
2838 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 */
2840 public void setContentView(View view, ViewGroup.LayoutParams params) {
2841 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002842 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 }
2844
2845 /**
2846 * Add an additional content view to the activity. Added after any existing
2847 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002848 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 * @param view The desired content to display.
2850 * @param params Layout parameters for the view.
2851 */
2852 public void addContentView(View view, ViewGroup.LayoutParams params) {
2853 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002854 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
2856
2857 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002858 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2859 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2860 *
2861 * <p>This method will return non-null after content has been initialized (e.g. by using
2862 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2863 *
2864 * @return This window's content TransitionManager or null if none is set.
2865 */
2866 public TransitionManager getContentTransitionManager() {
2867 return getWindow().getTransitionManager();
2868 }
2869
2870 /**
2871 * Set the {@link TransitionManager} to use for default transitions in this window.
2872 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2873 *
2874 * @param tm The TransitionManager to use for scene changes.
2875 */
2876 public void setContentTransitionManager(TransitionManager tm) {
2877 getWindow().setTransitionManager(tm);
2878 }
2879
2880 /**
2881 * Retrieve the {@link Scene} representing this window's current content.
2882 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2883 *
2884 * <p>This method will return null if the current content is not represented by a Scene.</p>
2885 *
2886 * @return Current Scene being shown or null
2887 */
2888 public Scene getContentScene() {
2889 return getWindow().getContentScene();
2890 }
2891
2892 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002893 * Sets whether this activity is finished when touched outside its window's
2894 * bounds.
2895 */
2896 public void setFinishOnTouchOutside(boolean finish) {
2897 mWindow.setCloseOnTouchOutside(finish);
2898 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002899
2900 /** @hide */
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002901 @IntDef(prefix = { "DEFAULT_KEYS_" }, value = {
Tor Norbyed9273d62013-05-30 15:59:53 -07002902 DEFAULT_KEYS_DISABLE,
2903 DEFAULT_KEYS_DIALER,
2904 DEFAULT_KEYS_SHORTCUT,
2905 DEFAULT_KEYS_SEARCH_LOCAL,
Jeff Sharkey6503bd82017-04-19 23:24:18 -06002906 DEFAULT_KEYS_SEARCH_GLOBAL
2907 })
Tor Norbyed9273d62013-05-30 15:59:53 -07002908 @Retention(RetentionPolicy.SOURCE)
2909 @interface DefaultKeyMode {}
2910
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2913 * keys.
RoboErik55011652014-07-09 15:05:53 -07002914 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 * @see #setDefaultKeyMode
2916 */
2917 static public final int DEFAULT_KEYS_DISABLE = 0;
2918 /**
2919 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2920 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 * @see #setDefaultKeyMode
2923 */
2924 static public final int DEFAULT_KEYS_DIALER = 1;
2925 /**
2926 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2927 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002928 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002930 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 * @see #setDefaultKeyMode
2932 */
2933 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2934 /**
2935 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2936 * will start an application-defined search. (If the application or activity does not
koprivaa1a78482018-10-09 10:09:23 -07002937 * actually define a search, the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002938 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 * @see #setDefaultKeyMode
2942 */
2943 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2944
2945 /**
2946 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2947 * will start a global search (typically web search, but some platforms may define alternate
2948 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002949 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002951 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 * @see #setDefaultKeyMode
2953 */
2954 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2955
2956 /**
2957 * Select the default key handling for this activity. This controls what
2958 * will happen to key events that are not otherwise handled. The default
2959 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2960 * floor. Other modes allow you to launch the dialer
2961 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2962 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002963 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002965 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 * <p>Note that the mode selected here does not impact the default
2967 * handling of system keys, such as the "back" and "menu" keys, and your
2968 * activity and its views always get a first chance to receive and handle
2969 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002970 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002972 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 * @see #onKeyDown
2974 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002975 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 // Some modes use a SpannableStringBuilder to track & dispatch input events
2979 // This list must remain in sync with the switch in onKeyDown()
2980 switch (mode) {
2981 case DEFAULT_KEYS_DISABLE:
2982 case DEFAULT_KEYS_SHORTCUT:
2983 mDefaultKeySsb = null; // not used in these modes
2984 break;
2985 case DEFAULT_KEYS_DIALER:
2986 case DEFAULT_KEYS_SEARCH_LOCAL:
2987 case DEFAULT_KEYS_SEARCH_GLOBAL:
2988 mDefaultKeySsb = new SpannableStringBuilder();
2989 Selection.setSelection(mDefaultKeySsb,0);
2990 break;
2991 default:
2992 throw new IllegalArgumentException();
2993 }
2994 }
2995
2996 /**
2997 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002998 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 * is inside a TextView will not trigger the event (unless it is a navigation
3000 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 * <p>If the focused view didn't want this event, this method is called.
3003 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003004 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
3005 * by calling {@link #onBackPressed()}, though the behavior varies based
3006 * on the application compatibility mode: for
3007 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
3008 * it will set up the dispatch to call {@link #onKeyUp} where the action
3009 * will be performed; for earlier applications, it will perform the
3010 * action immediately in on-down, as those versions of the platform
3011 * behaved.
RoboErik55011652014-07-09 15:05:53 -07003012 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07003013 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003014 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07003015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003017 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 * this event and it should continue to be propagated.
3019 * @see #onKeyUp
3020 * @see android.view.KeyEvent
3021 */
3022 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003023 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003024 if (getApplicationInfo().targetSdkVersion
3025 >= Build.VERSION_CODES.ECLAIR) {
3026 event.startTracking();
3027 } else {
3028 onBackPressed();
3029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 return true;
3031 }
RoboErik55011652014-07-09 15:05:53 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
3034 return false;
3035 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003036 Window w = getWindow();
3037 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3038 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
3039 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003040 return true;
3041 }
3042 return false;
Evan Roskyc46203782017-02-17 13:11:53 -08003043 } else if (keyCode == KeyEvent.KEYCODE_TAB) {
3044 // Don't consume TAB here since it's used for navigation. Arrow keys
3045 // aren't considered "typing keys" so they already won't get consumed.
3046 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 } else {
3048 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
3049 boolean clearSpannable = false;
3050 boolean handled;
3051 if ((event.getRepeatCount() != 0) || event.isSystem()) {
3052 clearSpannable = true;
3053 handled = false;
3054 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003055 handled = TextKeyListener.getInstance().onKeyDown(
3056 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 if (handled && mDefaultKeySsb.length() > 0) {
3058 // something useable has been typed - dispatch it now.
3059
3060 final String str = mDefaultKeySsb.toString();
3061 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07003062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 switch (mDefaultKeyMode) {
3064 case DEFAULT_KEYS_DIALER:
3065 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
3066 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07003067 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 break;
3069 case DEFAULT_KEYS_SEARCH_LOCAL:
3070 startSearch(str, false, null, false);
3071 break;
3072 case DEFAULT_KEYS_SEARCH_GLOBAL:
3073 startSearch(str, false, null, true);
3074 break;
3075 }
3076 }
3077 }
3078 if (clearSpannable) {
3079 mDefaultKeySsb.clear();
3080 mDefaultKeySsb.clearSpans();
3081 Selection.setSelection(mDefaultKeySsb,0);
3082 }
3083 return handled;
3084 }
3085 }
3086
3087 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003088 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3089 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3090 * the event).
3091 */
3092 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3093 return false;
3094 }
3095
3096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07003098 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 * is inside a TextView will not trigger the event (unless it is a navigation
3100 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07003101 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003102 * <p>The default implementation handles KEYCODE_BACK to stop the activity
3103 * and go back.
RoboErik55011652014-07-09 15:05:53 -07003104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07003106 * further, or <code>false</code> to indicate that you have not handled
3107 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 * @see #onKeyDown
3109 * @see KeyEvent
3110 */
3111 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07003112 if (getApplicationInfo().targetSdkVersion
3113 >= Build.VERSION_CODES.ECLAIR) {
3114 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
3115 && !event.isCanceled()) {
3116 onBackPressed();
3117 return true;
3118 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 return false;
3121 }
3122
3123 /**
3124 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3125 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3126 * the event).
3127 */
3128 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
3129 return false;
3130 }
RoboErik55011652014-07-09 15:05:53 -07003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003133 * Called when the activity has detected the user's press of the back
3134 * key. The default implementation simply finishes the current activity,
3135 * but you can override this to do whatever you want.
3136 */
3137 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07003138 if (mActionBar != null && mActionBar.collapseActionView()) {
3139 return;
3140 }
3141
George Mount27b0dc02017-02-21 10:24:09 -08003142 FragmentManager fragmentManager = mFragments.getFragmentManager();
3143
3144 if (fragmentManager.isStateSaved() || !fragmentManager.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07003145 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07003146 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003147 }
Jeff Brown64da12a2011-01-04 19:57:47 -08003148
3149 /**
3150 * Called when a key shortcut event is not handled by any of the views in the Activity.
3151 * Override this method to implement global key shortcuts for the Activity.
3152 * Key shortcuts can also be implemented by setting the
3153 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
3154 *
3155 * @param keyCode The value in event.getKeyCode().
3156 * @param event Description of the key event.
3157 * @return True if the key shortcut was handled.
3158 */
3159 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07003160 // Let the Action Bar have a chance at handling the shortcut.
3161 ActionBar actionBar = getActionBar();
3162 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08003163 }
3164
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 * Called when a touch screen event was not handled by any of the views
3167 * under it. This is most useful to process touch events that happen
3168 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07003169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07003171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 * @return Return true if you have consumed the event, false if you haven't.
3173 * The default implementation always returns false.
3174 */
3175 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08003176 if (mWindow.shouldCloseOnTouch(this, event)) {
3177 finish();
3178 return true;
3179 }
RoboErik55011652014-07-09 15:05:53 -07003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 return false;
3182 }
RoboErik55011652014-07-09 15:05:53 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 /**
3185 * Called when the trackball was moved and not handled by any of the
3186 * views inside of the activity. So, for example, if the trackball moves
3187 * while focus is on a button, you will receive a call here because
3188 * buttons do not normally do anything with trackball events. The call
3189 * here happens <em>before</em> trackball movements are converted to
3190 * DPAD key events, which then get sent back to the view hierarchy, and
3191 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07003192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07003194 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 * @return Return true if you have consumed the event, false if you haven't.
3196 * The default implementation always returns false.
3197 */
3198 public boolean onTrackballEvent(MotionEvent event) {
3199 return false;
3200 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003201
3202 /**
3203 * Called when a generic motion event was not handled by any of the
3204 * views inside of the activity.
3205 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08003206 * Generic motion events describe joystick movements, mouse hovers, track pad
3207 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08003208 * {@link MotionEvent#getSource() source} of the motion event specifies
3209 * the class of input that was received. Implementations of this method
3210 * must examine the bits in the source before processing the event.
3211 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08003212 * </p><p>
3213 * Generic motion events with source class
3214 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
3215 * are delivered to the view under the pointer. All other generic motion events are
3216 * delivered to the focused view.
3217 * </p><p>
3218 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
3219 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08003220 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08003221 *
3222 * @param event The generic motion event being processed.
3223 *
3224 * @return Return true if you have consumed the event, false if you haven't.
3225 * The default implementation always returns false.
3226 */
3227 public boolean onGenericMotionEvent(MotionEvent event) {
3228 return false;
3229 }
3230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 /**
3232 * Called whenever a key, touch, or trackball event is dispatched to the
3233 * activity. Implement this method if you wish to know that the user has
3234 * interacted with the device in some way while your activity is running.
3235 * This callback and {@link #onUserLeaveHint} are intended to help
3236 * activities manage status bar notifications intelligently; specifically,
koprivaa1a78482018-10-09 10:09:23 -07003237 * for helping activities determine the proper time to cancel a notification.
RoboErik55011652014-07-09 15:05:53 -07003238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
3240 * be accompanied by calls to {@link #onUserInteraction}. This
3241 * ensures that your activity will be told of relevant user activity such
3242 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07003243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 * <p>Note that this callback will be invoked for the touch down action
3245 * that begins a touch gesture, but may not be invoked for the touch-moved
3246 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07003247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 * @see #onUserLeaveHint()
3249 */
3250 public void onUserInteraction() {
3251 }
RoboErik55011652014-07-09 15:05:53 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
3254 // Update window manager if: we have a view, that view is
3255 // attached to its parent (which will be a RootView), and
3256 // this activity is not embedded.
3257 if (mParent == null) {
3258 View decor = mDecor;
3259 if (decor != null && decor.getParent() != null) {
3260 getWindowManager().updateViewLayout(decor, params);
3261 }
3262 }
3263 }
3264
3265 public void onContentChanged() {
3266 }
3267
3268 /**
3269 * Called when the current {@link Window} of the activity gains or loses
3270 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003271 * to the user. The default implementation clears the key tracking
3272 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07003273 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003274 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 * is managed independently of activity lifecycles. As such, while focus
3276 * changes will generally have some relation to lifecycle changes (an
3277 * activity that is stopped will not generally get window focus), you
3278 * should not rely on any particular order between the callbacks here and
3279 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07003280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 * <p>As a general rule, however, a resumed activity will have window
3282 * focus... unless it has displayed other dialogs or popups that take
3283 * input focus, in which case the activity itself will not have focus
3284 * when the other windows have it. Likewise, the system may display
3285 * system-level windows (such as the status bar notification panel or
3286 * a system alert) which will temporarily take window input focus without
3287 * pausing the foreground activity.
3288 *
3289 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07003290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 * @see #hasWindowFocus()
3292 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003293 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 */
3295 public void onWindowFocusChanged(boolean hasFocus) {
3296 }
RoboErik55011652014-07-09 15:05:53 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003299 * Called when the main window associated with the activity has been
3300 * attached to the window manager.
3301 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
3302 * for more information.
3303 * @see View#onAttachedToWindow
3304 */
3305 public void onAttachedToWindow() {
3306 }
RoboErik55011652014-07-09 15:05:53 -07003307
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003308 /**
3309 * Called when the main window associated with the activity has been
3310 * detached from the window manager.
3311 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
3312 * for more information.
3313 * @see View#onDetachedFromWindow
3314 */
3315 public void onDetachedFromWindow() {
3316 }
RoboErik55011652014-07-09 15:05:53 -07003317
Dianne Hackborn3be63c02009-08-20 19:31:38 -07003318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 * Returns true if this activity's <em>main</em> window currently has window focus.
3320 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07003321 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07003323 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
3325 */
3326 public boolean hasWindowFocus() {
3327 Window w = getWindow();
3328 if (w != null) {
3329 View d = w.getDecorView();
3330 if (d != null) {
3331 return d.hasWindowFocus();
3332 }
3333 }
3334 return false;
3335 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003336
3337 /**
3338 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07003339 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003340 */
Adam Powell117b6952014-05-05 18:14:56 -07003341 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -05003342 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003343 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Ned Burns7d6cb912016-12-02 17:25:33 -05003344 if (suppressWindowTransition) {
3345 overridePendingTransition(0, 0);
3346 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08003347 }
RoboErik55011652014-07-09 15:05:53 -07003348
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003349
Skuhnece2faa52015-08-11 10:36:38 -07003350 /**
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003351 * Moves the activity from {@link WindowConfiguration#WINDOWING_MODE_FREEFORM} windowing mode to
3352 * {@link WindowConfiguration#WINDOWING_MODE_FULLSCREEN}.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003353 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003354 * @hide
3355 */
3356 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08003357 public void exitFreeformMode() throws RemoteException {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003358 ActivityManager.getService().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003359 }
3360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 /**
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003362 * Puts the activity in picture-in-picture mode if the activity supports.
3363 * @see android.R.attr#supportsPictureInPicture
3364 * @hide
3365 */
3366 @Override
3367 public void enterPictureInPictureModeIfPossible() {
Winson Chungd3395382016-12-13 11:49:09 -08003368 if (mActivityInfo.supportsPictureInPicture()) {
Jaewan Kimd98dcab2016-05-03 02:52:18 +09003369 enterPictureInPictureMode();
3370 }
3371 }
3372
3373 /**
RoboErik55011652014-07-09 15:05:53 -07003374 * Called to process key events. You can override this to intercept all
3375 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003377 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003379 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 * @return boolean Return true if this event was consumed.
3381 */
3382 public boolean dispatchKeyEvent(KeyEvent event) {
3383 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003384
3385 // Let action bars open menus in response to the menu key prioritized over
3386 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003387 final int keyCode = event.getKeyCode();
3388 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003389 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3390 return true;
3391 }
3392
Dianne Hackborn8d374262009-09-14 21:21:52 -07003393 Window win = getWindow();
3394 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 return true;
3396 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003397 View decor = mDecor;
3398 if (decor == null) decor = win.getDecorView();
3399 return event.dispatch(this, decor != null
3400 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
3402
3403 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003404 * Called to process a key shortcut event.
3405 * You can override this to intercept all key shortcut events before they are
3406 * dispatched to the window. Be sure to call this implementation for key shortcut
3407 * events that should be handled normally.
3408 *
3409 * @param event The key shortcut event.
3410 * @return True if this event was consumed.
3411 */
3412 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3413 onUserInteraction();
3414 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3415 return true;
3416 }
3417 return onKeyShortcut(event.getKeyCode(), event);
3418 }
3419
3420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 * Called to process touch screen events. You can override this to
3422 * intercept all touch screen events before they are dispatched to the
3423 * window. Be sure to call this implementation for touch screen events
3424 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003425 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 * @return boolean Return true if this event was consumed.
3429 */
3430 public boolean dispatchTouchEvent(MotionEvent ev) {
3431 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3432 onUserInteraction();
3433 }
3434 if (getWindow().superDispatchTouchEvent(ev)) {
3435 return true;
3436 }
3437 return onTouchEvent(ev);
3438 }
RoboErik55011652014-07-09 15:05:53 -07003439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 /**
3441 * Called to process trackball events. You can override this to
3442 * intercept all trackball events before they are dispatched to the
3443 * window. Be sure to call this implementation for trackball events
3444 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 * @return boolean Return true if this event was consumed.
3449 */
3450 public boolean dispatchTrackballEvent(MotionEvent ev) {
3451 onUserInteraction();
3452 if (getWindow().superDispatchTrackballEvent(ev)) {
3453 return true;
3454 }
3455 return onTrackballEvent(ev);
3456 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003457
Jeff Browncb1404e2011-01-15 18:14:15 -08003458 /**
3459 * Called to process generic motion events. You can override this to
3460 * intercept all generic motion events before they are dispatched to the
3461 * window. Be sure to call this implementation for generic motion events
3462 * that should be handled normally.
3463 *
3464 * @param ev The generic motion event.
3465 *
3466 * @return boolean Return true if this event was consumed.
3467 */
3468 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3469 onUserInteraction();
3470 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3471 return true;
3472 }
3473 return onGenericMotionEvent(ev);
3474 }
3475
svetoslavganov75986cf2009-05-14 22:28:01 -07003476 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3477 event.setClassName(getClass().getName());
3478 event.setPackageName(getPackageName());
3479
3480 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003481 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3482 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003483 event.setFullScreen(isFullScreen);
3484
3485 CharSequence title = getTitle();
3486 if (!TextUtils.isEmpty(title)) {
3487 event.getText().add(title);
3488 }
3489
3490 return true;
3491 }
3492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 /**
3494 * Default implementation of
3495 * {@link android.view.Window.Callback#onCreatePanelView}
3496 * for activities. This
3497 * simply returns null so that all panel sub-windows will have the default
3498 * menu behavior.
3499 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003500 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 public View onCreatePanelView(int featureId) {
3502 return null;
3503 }
3504
3505 /**
3506 * Default implementation of
3507 * {@link android.view.Window.Callback#onCreatePanelMenu}
3508 * for activities. This calls through to the new
3509 * {@link #onCreateOptionsMenu} method for the
3510 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3511 * so that subclasses of Activity don't need to deal with feature codes.
3512 */
3513 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3514 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003515 boolean show = onCreateOptionsMenu(menu);
3516 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3517 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
3519 return false;
3520 }
3521
3522 /**
3523 * Default implementation of
3524 * {@link android.view.Window.Callback#onPreparePanel}
3525 * for activities. This
3526 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3527 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3528 * panel, so that subclasses of
3529 * Activity don't need to deal with feature codes.
3530 */
3531 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3532 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3533 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003534 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003535 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
3537 return true;
3538 }
3539
3540 /**
3541 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 * @return The default implementation returns true.
3544 */
3545 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003546 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003547 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003548 if (mActionBar != null) {
3549 mActionBar.dispatchMenuVisibilityChanged(true);
3550 } else {
3551 Log.e(TAG, "Tried to open action bar menu with no action bar");
3552 }
Adam Powell8515ee82010-11-30 14:09:55 -08003553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 return true;
3555 }
3556
3557 /**
3558 * Default implementation of
3559 * {@link android.view.Window.Callback#onMenuItemSelected}
3560 * for activities. This calls through to the new
3561 * {@link #onOptionsItemSelected} method for the
3562 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3563 * panel, so that subclasses of
3564 * Activity don't need to deal with feature codes.
3565 */
3566 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003567 CharSequence titleCondensed = item.getTitleCondensed();
3568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 switch (featureId) {
3570 case Window.FEATURE_OPTIONS_PANEL:
3571 // Put event logging here so it gets called even if subclass
3572 // doesn't call through to superclass's implmeentation of each
3573 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003574 if(titleCondensed != null) {
3575 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3576 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003577 if (onOptionsItemSelected(item)) {
3578 return true;
3579 }
Adam Powell04d58112012-04-09 10:22:12 -07003580 if (mFragments.dispatchOptionsItemSelected(item)) {
3581 return true;
3582 }
3583 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3584 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3585 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003586 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003587 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003588 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003589 }
Adam Powell04d58112012-04-09 10:22:12 -07003590 }
3591 return false;
RoboErik55011652014-07-09 15:05:53 -07003592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003594 if(titleCondensed != null) {
3595 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3596 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003597 if (onContextItemSelected(item)) {
3598 return true;
3599 }
3600 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 default:
3603 return false;
3604 }
3605 }
RoboErik55011652014-07-09 15:05:53 -07003606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 /**
3608 * Default implementation of
3609 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3610 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3611 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3612 * so that subclasses of Activity don't need to deal with feature codes.
3613 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3614 * {@link #onContextMenuClosed(Menu)} will be called.
3615 */
3616 public void onPanelClosed(int featureId, Menu menu) {
3617 switch (featureId) {
3618 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003619 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 onOptionsMenuClosed(menu);
3621 break;
RoboErik55011652014-07-09 15:05:53 -07003622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 case Window.FEATURE_CONTEXT_MENU:
3624 onContextMenuClosed(menu);
3625 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003626
3627 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003628 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003629 mActionBar.dispatchMenuVisibilityChanged(false);
3630 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 }
3632 }
3633
3634 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003635 * Declare that the options menu has changed, so should be recreated.
3636 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3637 * time it needs to be displayed.
3638 */
3639 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003640 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3641 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003642 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3643 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003644 }
RoboErik55011652014-07-09 15:05:53 -07003645
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 * Initialize the contents of the Activity's standard options menu. You
3648 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 * <p>This is only called once, the first time the options menu is
3651 * displayed. To update the menu every time it is displayed, see
3652 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003655 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3656 * they will be correctly ordered with application-defined menu items.
3657 * Deriving classes should always call through to the base implementation.
3658 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 * <p>You can safely hold on to <var>menu</var> (and any items created
3660 * from it), making modifications to it as desired, until the next
3661 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 * <p>When you add items to the menu, you can implement the Activity's
3664 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 * @return You must return true for the menu to be displayed;
3669 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003670 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 * @see #onPrepareOptionsMenu
3672 * @see #onOptionsItemSelected
3673 */
3674 public boolean onCreateOptionsMenu(Menu menu) {
3675 if (mParent != null) {
3676 return mParent.onCreateOptionsMenu(menu);
3677 }
3678 return true;
3679 }
3680
3681 /**
3682 * Prepare the Screen's standard options menu to be displayed. This is
3683 * called right before the menu is shown, every time it is shown. You can
3684 * use this method to efficiently enable/disable items or otherwise
3685 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 * <p>The default implementation updates the system menu items based on the
3688 * activity's state. Deriving classes should always call through to the
3689 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003690 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 * @param menu The options menu as last shown or first initialized by
3692 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 * @return You must return true for the menu to be displayed;
3695 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 * @see #onCreateOptionsMenu
3698 */
3699 public boolean onPrepareOptionsMenu(Menu menu) {
3700 if (mParent != null) {
3701 return mParent.onPrepareOptionsMenu(menu);
3702 }
3703 return true;
3704 }
3705
3706 /**
3707 * This hook is called whenever an item in your options menu is selected.
3708 * The default implementation simply returns false to have the normal
3709 * processing happen (calling the item's Runnable or sending a message to
3710 * its Handler as appropriate). You can use this method for any items
3711 * for which you would like to do processing without those other
3712 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003713 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003715 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003718 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 * @return boolean Return false to allow normal menu processing to
3720 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 * @see #onCreateOptionsMenu
3723 */
3724 public boolean onOptionsItemSelected(MenuItem item) {
3725 if (mParent != null) {
3726 return mParent.onOptionsItemSelected(item);
3727 }
3728 return false;
3729 }
3730
3731 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003732 * This method is called whenever the user chooses to navigate Up within your application's
3733 * activity hierarchy from the action bar.
3734 *
3735 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3736 * was specified in the manifest for this activity or an activity-alias to it,
3737 * default Up navigation will be handled automatically. If any activity
3738 * along the parent chain requires extra Intent arguments, the Activity subclass
3739 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3740 * to supply those arguments.</p>
3741 *
Mark Lufa434852016-08-11 17:40:33 -07003742 * <p>See <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back Stack</a>
Adam Powelldd8fab22012-03-22 17:47:27 -07003743 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3744 * from the design guide for more information about navigating within your app.</p>
3745 *
3746 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3747 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3748 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3749 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3750 *
3751 * @return true if Up navigation completed successfully and this Activity was finished,
3752 * false otherwise.
3753 */
3754 public boolean onNavigateUp() {
3755 // Automatically handle hierarchical Up navigation if the proper
3756 // metadata is available.
3757 Intent upIntent = getParentActivityIntent();
3758 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003759 if (mActivityInfo.taskAffinity == null) {
3760 // Activities with a null affinity are special; they really shouldn't
3761 // specify a parent activity intent in the first place. Just finish
3762 // the current activity and call it a day.
3763 finish();
3764 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003765 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003766 onCreateNavigateUpTaskStack(b);
3767 onPrepareNavigateUpTaskStack(b);
3768 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003769
3770 // We can't finishAffinity if we have a result.
3771 // Fall back and simply finish the current activity instead.
3772 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3773 // Tell the developer what's going on to avoid hair-pulling.
3774 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3775 finish();
3776 } else {
3777 finishAffinity();
3778 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003779 } else {
3780 navigateUpTo(upIntent);
3781 }
3782 return true;
3783 }
3784 return false;
3785 }
3786
3787 /**
3788 * This is called when a child activity of this one attempts to navigate up.
3789 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3790 *
3791 * @param child The activity making the call.
3792 */
3793 public boolean onNavigateUpFromChild(Activity child) {
3794 return onNavigateUp();
3795 }
3796
3797 /**
3798 * Define the synthetic task stack that will be generated during Up navigation from
3799 * a different task.
3800 *
3801 * <p>The default implementation of this method adds the parent chain of this activity
3802 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3803 * may choose to override this method to construct the desired task stack in a different
3804 * way.</p>
3805 *
Adam Powellf0195952012-05-02 21:38:54 -07003806 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3807 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3808 * returned by {@link #getParentActivityIntent()}.</p>
3809 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003810 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3811 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3812 *
3813 * @param builder An empty TaskStackBuilder - the application should add intents representing
3814 * the desired task stack
3815 */
3816 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3817 builder.addParentStack(this);
3818 }
3819
3820 /**
3821 * Prepare the synthetic task stack that will be generated during Up navigation
3822 * from a different task.
3823 *
3824 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3825 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3826 * If any extra data should be added to these intents before launching the new task,
3827 * the application should override this method and add that data here.</p>
3828 *
3829 * @param builder A TaskStackBuilder that has been populated with Intents by
3830 * onCreateNavigateUpTaskStack.
3831 */
3832 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3833 }
3834
3835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 * This hook is called whenever the options menu is being closed (either by the user canceling
3837 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003838 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 * @param menu The options menu as last shown or first initialized by
3840 * onCreateOptionsMenu().
3841 */
3842 public void onOptionsMenuClosed(Menu menu) {
3843 if (mParent != null) {
3844 mParent.onOptionsMenuClosed(menu);
3845 }
3846 }
RoboErik55011652014-07-09 15:05:53 -07003847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 /**
3849 * Programmatically opens the options menu. If the options menu is already
3850 * open, this method does nothing.
3851 */
3852 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003853 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3854 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003855 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 }
RoboErik55011652014-07-09 15:05:53 -07003858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 /**
3860 * Progammatically closes the options menu. If the options menu is already
3861 * closed, this method does nothing.
3862 */
3863 public void closeOptionsMenu() {
Evan Rosky41823d12017-03-21 14:42:35 -07003864 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3865 (mActionBar == null || !mActionBar.closeOptionsMenu())) {
Jose Lima7a22fc62015-01-23 17:24:22 -08003866 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 }
3869
3870 /**
3871 * Called when a context menu for the {@code view} is about to be shown.
3872 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3873 * time the context menu is about to be shown and should be populated for
3874 * the view (or item inside the view for {@link AdapterView} subclasses,
3875 * this can be found in the {@code menuInfo})).
3876 * <p>
3877 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3878 * item has been selected.
3879 * <p>
3880 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003881 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 */
3883 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3884 }
3885
3886 /**
3887 * Registers a context menu to be shown for the given view (multiple views
3888 * can show the context menu). This method will set the
3889 * {@link OnCreateContextMenuListener} on the view to this activity, so
3890 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3891 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 * @see #unregisterForContextMenu(View)
3894 * @param view The view that should show a context menu.
3895 */
3896 public void registerForContextMenu(View view) {
3897 view.setOnCreateContextMenuListener(this);
3898 }
RoboErik55011652014-07-09 15:05:53 -07003899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 /**
3901 * Prevents a context menu to be shown for the given view. This method will remove the
3902 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 * @see #registerForContextMenu(View)
3905 * @param view The view that should stop showing a context menu.
3906 */
3907 public void unregisterForContextMenu(View view) {
3908 view.setOnCreateContextMenuListener(null);
3909 }
RoboErik55011652014-07-09 15:05:53 -07003910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 /**
3912 * Programmatically opens the context menu for a particular {@code view}.
3913 * The {@code view} should have been added via
3914 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003915 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 * @param view The view to show the context menu for.
3917 */
3918 public void openContextMenu(View view) {
3919 view.showContextMenu();
3920 }
RoboErik55011652014-07-09 15:05:53 -07003921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 /**
3923 * Programmatically closes the most recently opened context menu, if showing.
3924 */
3925 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003926 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3927 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003929 }
RoboErik55011652014-07-09 15:05:53 -07003930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 /**
3932 * This hook is called whenever an item in a context menu is selected. The
3933 * default implementation simply returns false to have the normal processing
3934 * happen (calling the item's Runnable or sending a message to its Handler
3935 * as appropriate). You can use this method for any items for which you
3936 * would like to do processing without those other facilities.
3937 * <p>
3938 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3939 * View that added this menu item.
3940 * <p>
3941 * Derived classes should call through to the base class for it to perform
3942 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003943 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 * @param item The context menu item that was selected.
3945 * @return boolean Return false to allow normal context menu processing to
3946 * proceed, true to consume it here.
3947 */
3948 public boolean onContextItemSelected(MenuItem item) {
3949 if (mParent != null) {
3950 return mParent.onContextItemSelected(item);
3951 }
3952 return false;
3953 }
3954
3955 /**
3956 * This hook is called whenever the context menu is being closed (either by
3957 * the user canceling the menu with the back/menu button, or when an item is
3958 * selected).
RoboErik55011652014-07-09 15:05:53 -07003959 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 * @param menu The context menu that is being closed.
3961 */
3962 public void onContextMenuClosed(Menu menu) {
3963 if (mParent != null) {
3964 mParent.onContextMenuClosed(menu);
3965 }
3966 }
3967
3968 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003969 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003971 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 protected Dialog onCreateDialog(int id) {
3973 return null;
3974 }
3975
3976 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003977 * Callback for creating dialogs that are managed (saved and restored) for you
3978 * by the activity. The default implementation calls through to
3979 * {@link #onCreateDialog(int)} for compatibility.
3980 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003981 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3982 * or later, consider instead using a {@link DialogFragment} instead.</em>
3983 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003984 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3985 * this method the first time, and hang onto it thereafter. Any dialog
3986 * that is created by this method will automatically be saved and restored
3987 * for you, including whether it is showing.
3988 *
3989 * <p>If you would like the activity to manage saving and restoring dialogs
3990 * for you, you should override this method and handle any ids that are
3991 * passed to {@link #showDialog}.
3992 *
3993 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3994 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3995 *
3996 * @param id The id of the dialog.
3997 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3998 * @return The dialog. If you return null, the dialog will not be created.
3999 *
4000 * @see #onPrepareDialog(int, Dialog, Bundle)
4001 * @see #showDialog(int, Bundle)
4002 * @see #dismissDialog(int)
4003 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004004 *
4005 * @deprecated Use the new {@link DialogFragment} class with
4006 * {@link FragmentManager} instead; this is also
4007 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004008 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004009 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07004010 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004011 protected Dialog onCreateDialog(int id, Bundle args) {
4012 return onCreateDialog(id);
4013 }
4014
4015 /**
4016 * @deprecated Old no-arguments version of
4017 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
4018 */
4019 @Deprecated
4020 protected void onPrepareDialog(int id, Dialog dialog) {
4021 dialog.setOwnerActivity(this);
4022 }
4023
4024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004026 * shown. The default implementation calls through to
4027 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07004028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 * <p>
4030 * Override this if you need to update a managed dialog based on the state
4031 * of the application each time it is shown. For example, a time picker
4032 * dialog might want to be updated with the current time. You should call
4033 * through to the superclass's implementation. The default implementation
4034 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07004035 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 * @param id The id of the managed dialog.
4037 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004038 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
4039 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 * @see #showDialog(int)
4041 * @see #dismissDialog(int)
4042 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004043 *
4044 * @deprecated Use the new {@link DialogFragment} class with
4045 * {@link FragmentManager} instead; this is also
4046 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004048 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004049 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
4050 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 }
4052
4053 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004054 * Simple version of {@link #showDialog(int, Bundle)} that does not
4055 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
4056 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004057 *
4058 * @deprecated Use the new {@link DialogFragment} class with
4059 * {@link FragmentManager} instead; this is also
4060 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004061 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004062 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004063 public final void showDialog(int id) {
4064 showDialog(id, null);
4065 }
4066
4067 /**
4068 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 * will be made with the same id the first time this is called for a given
4070 * id. From thereafter, the dialog will be automatically saved and restored.
4071 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07004072 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
4073 * or later, consider instead using a {@link DialogFragment} instead.</em>
4074 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004075 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 * be made to provide an opportunity to do any timely preparation.
4077 *
4078 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004079 * @param args Arguments to pass through to the dialog. These will be saved
4080 * and restored for you. Note that if the dialog is already created,
4081 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
4082 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08004083 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004084 * @return Returns true if the Dialog was created; false is returned if
4085 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07004086 *
Joe Onorato37296dc2009-07-31 17:58:55 -07004087 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004088 * @see #onCreateDialog(int, Bundle)
4089 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 * @see #dismissDialog(int)
4091 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004092 *
4093 * @deprecated Use the new {@link DialogFragment} class with
4094 * {@link FragmentManager} instead; this is also
4095 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004097 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004098 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004100 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004102 ManagedDialog md = mManagedDialogs.get(id);
4103 if (md == null) {
4104 md = new ManagedDialog();
4105 md.mDialog = createDialog(id, null, args);
4106 if (md.mDialog == null) {
4107 return false;
4108 }
4109 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 }
RoboErik55011652014-07-09 15:05:53 -07004111
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004112 md.mArgs = args;
4113 onPrepareDialog(id, md.mDialog, args);
4114 md.mDialog.show();
4115 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 }
4117
4118 /**
4119 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
4120 *
4121 * @param id The id of the managed dialog.
4122 *
4123 * @throws IllegalArgumentException if the id was not previously shown via
4124 * {@link #showDialog(int)}.
4125 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004126 * @see #onCreateDialog(int, Bundle)
4127 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 * @see #showDialog(int)
4129 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004130 *
4131 * @deprecated Use the new {@link DialogFragment} class with
4132 * {@link FragmentManager} instead; this is also
4133 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004135 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 public final void dismissDialog(int id) {
4137 if (mManagedDialogs == null) {
4138 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 }
RoboErik55011652014-07-09 15:05:53 -07004140
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004141 final ManagedDialog md = mManagedDialogs.get(id);
4142 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 throw missingDialog(id);
4144 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004145 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
4147
4148 /**
4149 * Creates an exception to throw if a user passed in a dialog id that is
4150 * unexpected.
4151 */
4152 private IllegalArgumentException missingDialog(int id) {
4153 return new IllegalArgumentException("no dialog with id " + id + " was ever "
4154 + "shown via Activity#showDialog");
4155 }
4156
4157 /**
4158 * Removes any internal references to a dialog managed by this Activity.
4159 * If the dialog is showing, it will dismiss it as part of the clean up.
4160 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004161 * <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 -08004162 * want to avoid the overhead of saving and restoring it in the future.
4163 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004164 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
4165 * will not throw an exception if you try to remove an ID that does not
4166 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07004167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 * @param id The id of the managed dialog.
4169 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08004170 * @see #onCreateDialog(int, Bundle)
4171 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 * @see #showDialog(int)
4173 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07004174 *
4175 * @deprecated Use the new {@link DialogFragment} class with
4176 * {@link FragmentManager} instead; this is also
4177 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07004179 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07004181 if (mManagedDialogs != null) {
4182 final ManagedDialog md = mManagedDialogs.get(id);
4183 if (md != null) {
4184 md.mDialog.dismiss();
4185 mManagedDialogs.remove(id);
4186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 }
4189
4190 /**
4191 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07004192 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01004193 * <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 -07004194 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01004195 * calling this function is the same as calling
4196 * {@link #startSearch startSearch(null, false, null, false)}, which launches
4197 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07004198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 * <p>You can override this function to force global search, e.g. in response to a dedicated
4200 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07004201 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004202 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION} or
4203 * {@link Configuration#UI_MODE_TYPE_WATCH}, the default implementation changes to simply
4204 * return false and you must supply your own custom implementation if you want to support
4205 * search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004206 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004207 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004208 * @return Returns {@code true} if search launched, and {@code false} if the activity does
4209 * not respond to search. The default implementation always returns {@code true}, except
4210 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07004211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 * @see android.app.SearchManager
4213 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004214 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
4215 mSearchEvent = searchEvent;
4216 boolean result = onSearchRequested();
4217 mSearchEvent = null;
4218 return result;
4219 }
4220
4221 /**
4222 * @see #onSearchRequested(SearchEvent)
4223 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 public boolean onSearchRequested() {
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004225 final int uiMode = getResources().getConfiguration().uiMode
4226 & Configuration.UI_MODE_TYPE_MASK;
4227 if (uiMode != Configuration.UI_MODE_TYPE_TELEVISION
4228 && uiMode != Configuration.UI_MODE_TYPE_WATCH) {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07004229 startSearch(null, false, null, false);
4230 return true;
4231 } else {
4232 return false;
4233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 }
RoboErik55011652014-07-09 15:05:53 -07004235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07004237 * During the onSearchRequested() callbacks, this function will return the
4238 * {@link SearchEvent} that triggered the callback, if it exists.
4239 *
4240 * @return SearchEvent The SearchEvent that triggered the {@link
4241 * #onSearchRequested} callback.
4242 */
4243 public final SearchEvent getSearchEvent() {
4244 return mSearchEvent;
4245 }
4246
4247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07004249 *
4250 * <p>It is typically called from onSearchRequested(), either directly from
4251 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07004253 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 * is to inject specific data such as context data, it is preferred to <i>override</i>
4255 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07004256 *
Erik Wolsheimerac5cecd2017-12-05 13:11:50 -08004257 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_WATCH}, use of this API is
4258 * not supported.
4259 *
RoboErik55011652014-07-09 15:05:53 -07004260 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07004262 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 * any further typing will replace it. This is useful for cases where an entire pre-formed
4264 * query is being inserted. If false, the selection point will be placed at the end of the
4265 * inserted query. This is useful when the inserted query is text that the user entered,
4266 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
4267 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07004268 * @param appSearchData An application can insert application-specific
4269 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 * searches. This data will be returned with SEARCH intent(s). Null if
4271 * no extra data is required.
4272 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07004273 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07004274 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07004276 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 * @see android.app.SearchManager
4278 * @see #onSearchRequested
4279 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004280 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
4281 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004282 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004283 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07004284 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
4286
4287 /**
krosaend2d60142009-08-17 08:56:48 -07004288 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
4289 * the search dialog. Made available for testing purposes.
4290 *
4291 * @param query The query to trigger. If empty, the request will be ignored.
4292 * @param appSearchData An application can insert application-specific
4293 * context here, in order to improve quality or specificity of its own
4294 * searches. This data will be returned with SEARCH intent(s). Null if
4295 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07004296 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004297 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07004298 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01004299 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07004300 }
4301
4302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 * Request that key events come to this activity. Use this if your
4304 * activity has no views with focus, but the activity still wants
4305 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07004306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 * @see android.view.Window#takeKeyEvents
4308 */
4309 public void takeKeyEvents(boolean get) {
4310 getWindow().takeKeyEvents(get);
4311 }
4312
4313 /**
4314 * Enable extended window features. This is a convenience for calling
4315 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07004316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 * @param featureId The desired feature as defined in
4318 * {@link android.view.Window}.
4319 * @return Returns true if the requested feature is supported and now
4320 * enabled.
RoboErik55011652014-07-09 15:05:53 -07004321 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 * @see android.view.Window#requestFeature
4323 */
4324 public final boolean requestWindowFeature(int featureId) {
4325 return getWindow().requestFeature(featureId);
4326 }
4327
4328 /**
4329 * Convenience for calling
4330 * {@link android.view.Window#setFeatureDrawableResource}.
4331 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07004332 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 getWindow().setFeatureDrawableResource(featureId, resId);
4334 }
4335
4336 /**
4337 * Convenience for calling
4338 * {@link android.view.Window#setFeatureDrawableUri}.
4339 */
4340 public final void setFeatureDrawableUri(int featureId, Uri uri) {
4341 getWindow().setFeatureDrawableUri(featureId, uri);
4342 }
4343
4344 /**
4345 * Convenience for calling
4346 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
4347 */
4348 public final void setFeatureDrawable(int featureId, Drawable drawable) {
4349 getWindow().setFeatureDrawable(featureId, drawable);
4350 }
4351
4352 /**
4353 * Convenience for calling
4354 * {@link android.view.Window#setFeatureDrawableAlpha}.
4355 */
4356 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
4357 getWindow().setFeatureDrawableAlpha(featureId, alpha);
4358 }
4359
4360 /**
4361 * Convenience for calling
4362 * {@link android.view.Window#getLayoutInflater}.
4363 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004364 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 public LayoutInflater getLayoutInflater() {
4366 return getWindow().getLayoutInflater();
4367 }
4368
4369 /**
4370 * Returns a {@link MenuInflater} with this context.
4371 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004372 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004374 // Make sure that action views can get an appropriate theme.
4375 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004376 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004377 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004378 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004379 } else {
4380 mMenuInflater = new MenuInflater(this);
4381 }
4382 }
4383 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 }
4385
4386 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004387 public void setTheme(int resid) {
4388 super.setTheme(resid);
4389 mWindow.setTheme(resid);
4390 }
4391
4392 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004393 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004394 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 if (mParent == null) {
4396 super.onApplyThemeResource(theme, resid, first);
4397 } else {
4398 try {
4399 theme.setTo(mParent.getTheme());
4400 } catch (Exception e) {
4401 // Empty
4402 }
4403 theme.applyStyle(resid, false);
4404 }
Winson073a5262016-02-29 16:04:40 -08004405
4406 // Get the primary color and update the TaskDescription for this activity
4407 TypedArray a = theme.obtainStyledAttributes(
4408 com.android.internal.R.styleable.ActivityTaskDescription);
4409 if (mTaskDescription.getPrimaryColor() == 0) {
4410 int colorPrimary = a.getColor(
4411 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
4412 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
4413 mTaskDescription.setPrimaryColor(colorPrimary);
4414 }
4415 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004416
4417 int colorBackground = a.getColor(
4418 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4419 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4420 mTaskDescription.setBackgroundColor(colorBackground);
Winson073a5262016-02-29 16:04:40 -08004421 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02004422
4423 final int statusBarColor = a.getColor(
4424 com.android.internal.R.styleable.ActivityTaskDescription_statusBarColor, 0);
4425 if (statusBarColor != 0) {
4426 mTaskDescription.setStatusBarColor(statusBarColor);
4427 }
4428
4429 final int navigationBarColor = a.getColor(
4430 com.android.internal.R.styleable.ActivityTaskDescription_navigationBarColor, 0);
4431 if (navigationBarColor != 0) {
4432 mTaskDescription.setNavigationBarColor(navigationBarColor);
4433 }
4434
Winson073a5262016-02-29 16:04:40 -08004435 a.recycle();
4436 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004437 }
4438
4439 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004440 * Requests permissions to be granted to this application. These permissions
4441 * must be requested in your manifest, they should not be granted to your app,
4442 * and they should have protection level {@link android.content.pm.PermissionInfo
4443 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4444 * the platform or a third-party app.
4445 * <p>
4446 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4447 * are granted at install time if requested in the manifest. Signature permissions
4448 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4449 * install time if requested in the manifest and the signature of your app matches
4450 * the signature of the app declaring the permissions.
4451 * </p>
4452 * <p>
4453 * If your app does not have the requested permissions the user will be presented
4454 * with UI for accepting them. After the user has accepted or rejected the
4455 * requested permissions you will receive a callback on {@link
4456 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4457 * permissions were granted or not.
4458 * </p>
4459 * <p>
4460 * Note that requesting a permission does not guarantee it will be granted and
4461 * your app should be able to run without having this permission.
4462 * </p>
4463 * <p>
4464 * This method may start an activity allowing the user to choose which permissions
4465 * to grant and which to reject. Hence, you should be prepared that your activity
4466 * may be paused and resumed. Further, granting some permissions may require
4467 * a restart of you application. In such a case, the system will recreate the
4468 * activity stack before delivering the result to {@link
4469 * #onRequestPermissionsResult(int, String[], int[])}.
4470 * </p>
4471 * <p>
4472 * When checking whether you have a permission you should use {@link
4473 * #checkSelfPermission(String)}.
4474 * </p>
4475 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004476 * Calling this API for permissions already granted to your app would show UI
4477 * to the user to decide whether the app can still hold these permissions. This
4478 * can be useful if the way your app uses data guarded by the permissions
4479 * changes significantly.
4480 * </p>
4481 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004482 * You cannot request a permission if your activity sets {@link
4483 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4484 * <code>true</code> because in this case the activity would not receive
4485 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4486 * </p>
4487 * <p>
Trevor Johns682c24e2016-04-12 10:13:47 -07004488 * The <a href="http://developer.android.com/samples/RuntimePermissions/index.html">
4489 * RuntimePermissions</a> sample app demonstrates how to use this method to
4490 * request permissions at run time.
Svetoslavc6d1c342015-02-26 14:44:43 -08004491 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004492 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004493 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004494 * @param requestCode Application specific request code to match with a result
4495 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004496 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004497 *
Paul Miller77a0e132016-02-11 13:46:49 -08004498 * @throws IllegalArgumentException if requestCode is negative.
4499 *
Svetoslavc6d1c342015-02-26 14:44:43 -08004500 * @see #onRequestPermissionsResult(int, String[], int[])
4501 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004502 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004503 */
4504 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Paul Miller77a0e132016-02-11 13:46:49 -08004505 if (requestCode < 0) {
4506 throw new IllegalArgumentException("requestCode should be >= 0");
4507 }
Svetoslavffb32b12015-10-15 16:54:00 -07004508 if (mHasCurrentPermissionsRequest) {
Ahmad Melegye2bfbf12017-11-14 21:27:28 +00004509 Log.w(TAG, "Can request only one set of permissions at a time");
Svetoslavffb32b12015-10-15 16:54:00 -07004510 // Dispatch the callback with empty arrays which means a cancellation.
4511 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4512 return;
4513 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004514 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004515 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004516 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004517 }
4518
4519 /**
4520 * Callback for the result from requesting permissions. This method
4521 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004522 * <p>
4523 * <strong>Note:</strong> It is possible that the permissions request interaction
4524 * with the user is interrupted. In this case you will receive empty permissions
4525 * and results arrays which should be treated as a cancellation.
4526 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004527 *
4528 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4529 * @param permissions The requested permissions. Never null.
4530 * @param grantResults The grant results for the corresponding permissions
4531 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4532 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4533 *
4534 * @see #requestPermissions(String[], int)
4535 */
4536 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4537 @NonNull int[] grantResults) {
4538 /* callback - no nothing */
4539 }
4540
4541 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004542 * Gets whether you should show UI with rationale for requesting a permission.
4543 * You should do this only if you do not have the permission and the context in
4544 * which the permission is requested does not clearly communicate to the user
4545 * what would be the benefit from granting this permission.
4546 * <p>
4547 * For example, if you write a camera app, requesting the camera permission
4548 * would be expected by the user and no rationale for why it is requested is
4549 * needed. If however, the app needs location for tagging photos then a non-tech
4550 * savvy user may wonder how location is related to taking photos. In this case
4551 * you may choose to show UI with rationale of requesting this permission.
4552 * </p>
4553 *
4554 * @param permission A permission your app wants to request.
4555 * @return Whether you can show permission rationale UI.
4556 *
4557 * @see #checkSelfPermission(String)
4558 * @see #requestPermissions(String[], int)
4559 * @see #onRequestPermissionsResult(int, String[], int[])
4560 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004561 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004562 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4563 }
4564
4565 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004566 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4567 * with no options.
4568 *
4569 * @param intent The intent to start.
4570 * @param requestCode If >= 0, this code will be returned in
4571 * onActivityResult() when the activity exits.
4572 *
4573 * @throws android.content.ActivityNotFoundException
4574 *
George Mount0a778ed2013-12-13 13:35:36 -08004575 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004576 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004577 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004578 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004579 }
4580
4581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 * Launch an activity for which you would like a result when it finished.
4583 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004584 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004585 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 * <p>Note that this method should only be used with Intent protocols
4589 * that are defined to return a result. In other protocols (such as
4590 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4591 * not get the result when you expect. For example, if the activity you
Mark Renoufcca6d052016-12-02 14:11:18 -05004592 * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
4593 * run in your task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004594 *
RoboErik55011652014-07-09 15:05:53 -07004595 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004597 * activity, then your window will not be displayed until a result is
4598 * returned back from the started activity. This is to avoid visible
4599 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004600 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 * <p>This method throws {@link android.content.ActivityNotFoundException}
4602 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 * @param intent The intent to start.
4605 * @param requestCode If >= 0, this code will be returned in
4606 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004607 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004608 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004609 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004612 *
George Mount0a778ed2013-12-13 13:35:36 -08004613 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004615 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4616 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 if (mParent == null) {
George Mount413739e2016-06-08 07:13:37 -07004618 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 Instrumentation.ActivityResult ar =
4620 mInstrumentation.execStartActivity(
4621 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004622 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 if (ar != null) {
4624 mMainThread.sendActivityResult(
4625 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4626 ar.getResultData());
4627 }
4628 if (requestCode >= 0) {
4629 // If this start is requesting a result, we can avoid making
4630 // the activity visible until the result is received. Setting
4631 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4632 // activity hidden during this time, to avoid flickering.
4633 // This can only be done when a result is requested because
4634 // that guarantees we will get information back when the
4635 // activity is finished, no matter what happens to it.
4636 mStartedActivity = true;
4637 }
Adam Powell14874662013-07-18 19:42:41 -07004638
George Mount41725de2015-04-09 08:23:05 -07004639 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004640 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004642 if (options != null) {
4643 mParent.startActivityFromChild(this, intent, requestCode, options);
4644 } else {
4645 // Note we want to go through this method for compatibility with
4646 // existing applications that may have overridden it.
4647 mParent.startActivityFromChild(this, intent, requestCode);
4648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 }
George Mount41725de2015-04-09 08:23:05 -07004650 }
4651
4652 /**
4653 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4654 *
4655 * @param options The ActivityOptions bundle used to start an Activity.
4656 */
4657 private void cancelInputsAndStartExitTransition(Bundle options) {
4658 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4659 if (decor != null) {
4660 decor.cancelPendingInputEvents();
4661 }
George Mountff243282014-07-07 16:12:07 -07004662 if (options != null && !isTopOfTask()) {
4663 mActivityTransitionState.startExitOutTransition(this, options);
4664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 }
4666
Chet Haaseb64e777f2016-12-09 07:17:10 -08004667 /**
4668 * Returns whether there are any activity transitions currently running on this
4669 * activity. A return value of {@code true} can mean that either an enter or
4670 * exit transition is running, including whether the background of the activity
4671 * is animating as a part of that transition.
4672 *
4673 * @return true if a transition is currently running on this activity, false otherwise.
4674 */
4675 public boolean isActivityTransitionRunning() {
4676 return mActivityTransitionState.isTransitionRunning();
4677 }
4678
George Mount413739e2016-06-08 07:13:37 -07004679 private Bundle transferSpringboardActivityOptions(Bundle options) {
4680 if (options == null && (mWindow != null && !mWindow.isActive())) {
4681 final ActivityOptions activityOptions = getActivityOptions();
George Mount96383782016-06-08 15:13:52 -07004682 if (activityOptions != null &&
4683 activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
George Mount413739e2016-06-08 07:13:37 -07004684 return activityOptions.toBundle();
4685 }
4686 }
4687 return options;
4688 }
4689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004691 * @hide Implement to provide correct calling token.
4692 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01004693 @UnsupportedAppUsage
Alexandra Gherghina83231452014-07-04 12:05:20 +01004694 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4695 startActivityForResultAsUser(intent, requestCode, null, user);
4696 }
4697
4698 /**
4699 * @hide Implement to provide correct calling token.
4700 */
4701 public void startActivityForResultAsUser(Intent intent, int requestCode,
4702 @Nullable Bundle options, UserHandle user) {
Tony Mak96d26fe2017-04-11 20:05:39 +01004703 startActivityForResultAsUser(intent, mEmbeddedID, requestCode, options, user);
4704 }
4705
4706 /**
4707 * @hide Implement to provide correct calling token.
4708 */
4709 public void startActivityForResultAsUser(Intent intent, String resultWho, int requestCode,
4710 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004711 if (mParent != null) {
4712 throw new RuntimeException("Can't be called from a child");
4713 }
George Mount413739e2016-06-08 07:13:37 -07004714 options = transferSpringboardActivityOptions(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004715 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004716 this, mMainThread.getApplicationThread(), mToken, resultWho, intent, requestCode,
Alexandra Gherghina83231452014-07-04 12:05:20 +01004717 options, user);
4718 if (ar != null) {
4719 mMainThread.sendActivityResult(
4720 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4721 }
4722 if (requestCode >= 0) {
4723 // If this start is requesting a result, we can avoid making
4724 // the activity visible until the result is received. Setting
4725 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4726 // activity hidden during this time, to avoid flickering.
4727 // This can only be done when a result is requested because
4728 // that guarantees we will get information back when the
4729 // activity is finished, no matter what happens to it.
4730 mStartedActivity = true;
4731 }
4732
George Mount41725de2015-04-09 08:23:05 -07004733 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004734 }
4735
4736 /**
4737 * @hide Implement to provide correct calling token.
4738 */
Remi NGUYEN VANbf1ecd12019-01-20 21:18:22 +09004739 @Override
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004740 public void startActivityAsUser(Intent intent, UserHandle user) {
4741 startActivityAsUser(intent, null, user);
4742 }
4743
4744 /**
4745 * @hide Implement to provide correct calling token.
4746 */
4747 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4748 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004749 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004750 }
George Mount413739e2016-06-08 07:13:37 -07004751 options = transferSpringboardActivityOptions(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004752 Instrumentation.ActivityResult ar =
4753 mInstrumentation.execStartActivity(
Tony Mak96d26fe2017-04-11 20:05:39 +01004754 this, mMainThread.getApplicationThread(), mToken, mEmbeddedID,
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004755 intent, -1, options, user);
4756 if (ar != null) {
4757 mMainThread.sendActivityResult(
4758 mToken, mEmbeddedID, -1, ar.getResultCode(),
4759 ar.getResultData());
4760 }
George Mount41725de2015-04-09 08:23:05 -07004761 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004762 }
4763
4764 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004765 * Start a new activity as if it was started by the activity that started our
4766 * current activity. This is for the resolver and chooser activities, which operate
4767 * as intermediaries that dispatch their intent to the target the user selects -- to
4768 * do this, they must perform all security checks including permission grants as if
4769 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004770 * @param intent The Intent to start.
4771 * @param options ActivityOptions or null.
4772 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4773 * caller it is doing the start is, is actually allowed to start the target activity.
4774 * If you set this to true, you must set an explicit component in the Intent and do any
4775 * appropriate security checks yourself.
4776 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004777 * @hide
4778 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004779 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -05004780 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004781 if (mParent != null) {
4782 throw new RuntimeException("Can't be called from a child");
4783 }
George Mount413739e2016-06-08 07:13:37 -07004784 options = transferSpringboardActivityOptions(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004785 Instrumentation.ActivityResult ar =
4786 mInstrumentation.execStartActivityAsCaller(
4787 this, mMainThread.getApplicationThread(), mToken, this,
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -05004788 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004789 if (ar != null) {
4790 mMainThread.sendActivityResult(
4791 mToken, mEmbeddedID, -1, ar.getResultCode(),
4792 ar.getResultData());
4793 }
George Mount41725de2015-04-09 08:23:05 -07004794 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004795 }
4796
4797 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004798 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4799 * Intent, int, int, int, Bundle)} with no options.
4800 *
4801 * @param intent The IntentSender to launch.
4802 * @param requestCode If >= 0, this code will be returned in
4803 * onActivityResult() when the activity exits.
4804 * @param fillInIntent If non-null, this will be provided as the
4805 * intent parameter to {@link IntentSender#sendIntent}.
4806 * @param flagsMask Intent flags in the original IntentSender that you
4807 * would like to change.
4808 * @param flagsValues Desired values for any bits set in
4809 * <var>flagsMask</var>
4810 * @param extraFlags Always set to 0.
4811 */
4812 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004813 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004814 throws IntentSender.SendIntentException {
4815 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4816 flagsValues, extraFlags, null);
4817 }
4818
4819 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004820 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004821 * to use a IntentSender to describe the activity to be started. If
4822 * the IntentSender is for an activity, that activity will be started
4823 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4824 * here; otherwise, its associated action will be executed (such as
4825 * sending a broadcast) as if you had called
4826 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004827 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004828 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004829 * @param requestCode If >= 0, this code will be returned in
4830 * onActivityResult() when the activity exits.
4831 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004832 * intent parameter to {@link IntentSender#sendIntent}.
4833 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004834 * would like to change.
4835 * @param flagsValues Desired values for any bits set in
4836 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004837 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004838 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004839 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004840 * Context.startActivity(Intent, Bundle)} for more details. If options
4841 * have also been supplied by the IntentSender, options given here will
4842 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004843 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004844 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004845 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004846 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004847 if (mParent == null) {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004848 startIntentSenderForResultInner(intent, mEmbeddedID, requestCode, fillInIntent,
4849 flagsMask, flagsValues, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004850 } else if (options != null) {
4851 mParent.startIntentSenderFromChild(this, intent, requestCode,
4852 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004853 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004854 // Note we want to go through this call for compatibility with
4855 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004856 mParent.startIntentSenderFromChild(this, intent, requestCode,
4857 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004858 }
4859 }
4860
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004861 private void startIntentSenderForResultInner(IntentSender intent, String who, int requestCode,
4862 Intent fillInIntent, int flagsMask, int flagsValues,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004863 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004864 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004865 try {
4866 String resolvedType = null;
4867 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004868 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004869 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004870 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4871 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004872 int result = ActivityManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07004873 .startActivityIntentSender(mMainThread.getApplicationThread(),
4874 intent != null ? intent.getTarget() : null,
4875 intent != null ? intent.getWhitelistToken() : null,
Clara Bayarria0c2dc32016-04-12 12:00:15 +01004876 fillInIntent, resolvedType, mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004877 requestCode, flagsMask, flagsValues, options);
4878 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004879 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004880 }
4881 Instrumentation.checkStartActivityResult(result, null);
4882 } catch (RemoteException e) {
4883 }
4884 if (requestCode >= 0) {
4885 // If this start is requesting a result, we can avoid making
4886 // the activity visible until the result is received. Setting
4887 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4888 // activity hidden during this time, to avoid flickering.
4889 // This can only be done when a result is requested because
4890 // that guarantees we will get information back when the
4891 // activity is finished, no matter what happens to it.
4892 mStartedActivity = true;
4893 }
4894 }
4895
4896 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004897 * Same as {@link #startActivity(Intent, Bundle)} with no options
4898 * specified.
4899 *
4900 * @param intent The intent to start.
4901 *
4902 * @throws android.content.ActivityNotFoundException
4903 *
Mark Lufa434852016-08-11 17:40:33 -07004904 * @see #startActivity(Intent, Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004905 * @see #startActivityForResult
4906 */
4907 @Override
4908 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004909 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004910 }
4911
4912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 * Launch a new activity. You will not receive any information about when
4914 * the activity exits. This implementation overrides the base version,
4915 * providing information about
4916 * the activity performing the launch. Because of this additional
4917 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4918 * required; if not specified, the new activity will be added to the
4919 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004920 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 * <p>This method throws {@link android.content.ActivityNotFoundException}
4922 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004923 *
4924 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004925 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004926 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004927 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004928 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004930 *
Mark Lufa434852016-08-11 17:40:33 -07004931 * @see #startActivity(Intent)
RoboErik55011652014-07-09 15:05:53 -07004932 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 */
4934 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004935 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004936 if (options != null) {
4937 startActivityForResult(intent, -1, options);
4938 } else {
4939 // Note we want to go through this call for compatibility with
4940 // applications that may have overridden the method.
4941 startActivityForResult(intent, -1);
4942 }
4943 }
4944
4945 /**
4946 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4947 * specified.
4948 *
4949 * @param intents The intents to start.
4950 *
4951 * @throws android.content.ActivityNotFoundException
4952 *
Mark Lufa434852016-08-11 17:40:33 -07004953 * @see #startActivities(Intent[], Bundle)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004954 * @see #startActivityForResult
4955 */
4956 @Override
4957 public void startActivities(Intent[] intents) {
4958 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 }
4960
4961 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004962 * Launch a new activity. You will not receive any information about when
4963 * the activity exits. This implementation overrides the base version,
4964 * providing information about
4965 * the activity performing the launch. Because of this additional
4966 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4967 * required; if not specified, the new activity will be added to the
4968 * task of the caller.
4969 *
4970 * <p>This method throws {@link android.content.ActivityNotFoundException}
4971 * if there was no Activity found to run the given Intent.
4972 *
4973 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004974 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08004975 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004976 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004977 *
4978 * @throws android.content.ActivityNotFoundException
4979 *
Mark Lufa434852016-08-11 17:40:33 -07004980 * @see #startActivities(Intent[])
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004981 * @see #startActivityForResult
4982 */
4983 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004984 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004985 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004986 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004987 }
4988
4989 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004990 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4991 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004992 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004993 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004994 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004995 * intent parameter to {@link IntentSender#sendIntent}.
4996 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004997 * would like to change.
4998 * @param flagsValues Desired values for any bits set in
4999 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005000 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005001 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005002 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005003 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005004 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005005 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
5006 extraFlags, null);
5007 }
5008
5009 /**
5010 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
5011 * to start; see
5012 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
5013 * for more information.
5014 *
5015 * @param intent The IntentSender to launch.
5016 * @param fillInIntent If non-null, this will be provided as the
5017 * intent parameter to {@link IntentSender#sendIntent}.
5018 * @param flagsMask Intent flags in the original IntentSender that you
5019 * would like to change.
5020 * @param flagsValues Desired values for any bits set in
5021 * <var>flagsMask</var>
5022 * @param extraFlags Always set to 0.
5023 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005024 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005025 * Context.startActivity(Intent, Bundle)} for more details. If options
5026 * have also been supplied by the IntentSender, options given here will
5027 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005028 */
5029 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005030 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005031 Bundle options) throws IntentSender.SendIntentException {
5032 if (options != null) {
5033 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5034 flagsValues, extraFlags, options);
5035 } else {
5036 // Note we want to go through this call for compatibility with
5037 // applications that may have overridden the method.
5038 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
5039 flagsValues, extraFlags);
5040 }
5041 }
5042
5043 /**
5044 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
5045 * with no options.
5046 *
5047 * @param intent The intent to start.
5048 * @param requestCode If >= 0, this code will be returned in
5049 * onActivityResult() when the activity exits, as described in
5050 * {@link #startActivityForResult}.
5051 *
5052 * @return If a new activity was launched then true is returned; otherwise
5053 * false is returned and you must handle the Intent yourself.
5054 *
5055 * @see #startActivity
5056 * @see #startActivityForResult
5057 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005058 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5059 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005060 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005061 }
5062
5063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 * A special variation to launch an activity only if a new activity
5065 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07005066 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07005068 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07005070 * and the activity
5071 * that handles <var>intent</var> is the same as your currently running
5072 * activity, then a new instance is not needed. In this case, instead of
5073 * the normal behavior of calling {@link #onNewIntent} this function will
5074 * return and you can handle the Intent yourself.
5075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 * <p>This function can only be called from a top-level activity; if it is
5077 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07005078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 * @param intent The intent to start.
5080 * @param requestCode If >= 0, this code will be returned in
5081 * onActivityResult() when the activity exits, as described in
5082 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005083 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005084 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005085 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 * @return If a new activity was launched then true is returned; otherwise
5088 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07005089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 * @see #startActivity
5091 * @see #startActivityForResult
5092 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005093 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
5094 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005096 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005098 Uri referrer = onProvideReferrer();
5099 if (referrer != null) {
5100 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5101 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005102 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005103 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005104 result = ActivityManager.getService()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08005105 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07005106 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
5107 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
5108 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 } catch (RemoteException e) {
5110 // Empty
5111 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08005112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08005114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 if (requestCode >= 0) {
5116 // If this start is requesting a result, we can avoid making
5117 // the activity visible until the result is received. Setting
5118 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
5119 // activity hidden during this time, to avoid flickering.
5120 // This can only be done when a result is requested because
5121 // that guarantees we will get information back when the
5122 // activity is finished, no matter what happens to it.
5123 mStartedActivity = true;
5124 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005125 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 }
5127
5128 throw new UnsupportedOperationException(
5129 "startActivityIfNeeded can only be called from a top-level activity");
5130 }
5131
5132 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005133 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
5134 * no options.
5135 *
5136 * @param intent The intent to dispatch to the next activity. For
5137 * correct behavior, this must be the same as the Intent that started
5138 * your own activity; the only changes you can make are to the extras
5139 * inside of it.
5140 *
5141 * @return Returns a boolean indicating whether there was another Activity
5142 * to start: true if there was a next activity to start, false if there
5143 * wasn't. In general, if true is returned you will then want to call
5144 * finish() on yourself.
5145 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005146 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07005147 return startNextMatchingActivity(intent, null);
5148 }
5149
5150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 * Special version of starting an activity, for use when you are replacing
5152 * other activity components. You can use this to hand the Intent off
5153 * to the next Activity that can handle it. You typically call this in
5154 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07005155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 * @param intent The intent to dispatch to the next activity. For
5157 * correct behavior, this must be the same as the Intent that started
5158 * your own activity; the only changes you can make are to the extras
5159 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005160 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005161 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005162 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 * @return Returns a boolean indicating whether there was another Activity
5165 * to start: true if there was a next activity to start, false if there
5166 * wasn't. In general, if true is returned you will then want to call
5167 * finish() on yourself.
5168 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005169 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
5170 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 if (mParent == null) {
5172 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005173 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005174 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005175 return ActivityManager.getService()
Dianne Hackborna4972e92012-03-14 10:38:05 -07005176 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 } catch (RemoteException e) {
5178 // Empty
5179 }
5180 return false;
5181 }
5182
5183 throw new UnsupportedOperationException(
5184 "startNextMatchingActivity can only be called from a top-level activity");
5185 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07005186
5187 /**
5188 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
5189 * with no options.
5190 *
5191 * @param child The activity making the call.
5192 * @param intent The intent to start.
5193 * @param requestCode Reply request code. < 0 if reply is not requested.
5194 *
5195 * @throws android.content.ActivityNotFoundException
5196 *
5197 * @see #startActivity
5198 * @see #startActivityForResult
5199 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005200 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005201 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005202 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005203 }
5204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 /**
RoboErik55011652014-07-09 15:05:53 -07005206 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07005208 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 * <p>This method throws {@link android.content.ActivityNotFoundException}
5210 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 * @param child The activity making the call.
5213 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005214 * @param requestCode Reply request code. < 0 if reply is not requested.
5215 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005216 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005217 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005218 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005220 *
5221 * @see #startActivity
5222 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07005224 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07005225 int requestCode, @Nullable Bundle options) {
George Mount413739e2016-06-08 07:13:37 -07005226 options = transferSpringboardActivityOptions(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 Instrumentation.ActivityResult ar =
5228 mInstrumentation.execStartActivity(
5229 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005230 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 if (ar != null) {
5232 mMainThread.sendActivityResult(
5233 mToken, child.mEmbeddedID, requestCode,
5234 ar.getResultCode(), ar.getResultData());
5235 }
George Mount41725de2015-04-09 08:23:05 -07005236 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 }
5238
5239 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005240 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
5241 * with no options.
5242 *
5243 * @param fragment The fragment making the call.
5244 * @param intent The intent to start.
5245 * @param requestCode Reply request code. < 0 if reply is not requested.
5246 *
5247 * @throws android.content.ActivityNotFoundException
5248 *
5249 * @see Fragment#startActivity
5250 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005251 *
5252 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5253 * android.support.v4.app.Fragment,Intent,int)}
Dianne Hackborna4972e92012-03-14 10:38:05 -07005254 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005255 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005256 public void startActivityFromFragment(@NonNull Fragment fragment,
5257 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07005258 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07005259 }
5260
5261 /**
RoboErik55011652014-07-09 15:05:53 -07005262 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005263 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
5264 * method.
RoboErik55011652014-07-09 15:05:53 -07005265 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005266 * <p>This method throws {@link android.content.ActivityNotFoundException}
5267 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07005268 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005269 * @param fragment The fragment making the call.
5270 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07005271 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07005272 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08005273 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005274 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07005275 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005276 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07005277 *
5278 * @see Fragment#startActivity
5279 * @see Fragment#startActivityForResult
Ian Lake0a1feb82017-11-13 10:26:46 -08005280 *
5281 * @deprecated Use {@link android.support.v4.app.FragmentActivity#startActivityFromFragment(
5282 * android.support.v4.app.Fragment,Intent,int,Bundle)}
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005283 */
Ian Lake0a1feb82017-11-13 10:26:46 -08005284 @Deprecated
Tor Norbye788fc2b2015-07-05 16:10:42 -07005285 public void startActivityFromFragment(@NonNull Fragment fragment,
5286 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005287 startActivityForResult(fragment.mWho, intent, requestCode, options);
5288 }
5289
5290 /**
5291 * @hide
5292 */
Tony Mak96d26fe2017-04-11 20:05:39 +01005293 public void startActivityAsUserFromFragment(@NonNull Fragment fragment,
5294 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options,
5295 UserHandle user) {
5296 startActivityForResultAsUser(intent, fragment.mWho, requestCode, options, user);
5297 }
5298
5299 /**
5300 * @hide
5301 */
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005302 @Override
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005303 @UnsupportedAppUsage
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005304 public void startActivityForResult(
5305 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005306 Uri referrer = onProvideReferrer();
5307 if (referrer != null) {
5308 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
5309 }
George Mount413739e2016-06-08 07:13:37 -07005310 options = transferSpringboardActivityOptions(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005311 Instrumentation.ActivityResult ar =
5312 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005313 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07005314 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005315 if (ar != null) {
5316 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005317 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005318 ar.getResultCode(), ar.getResultData());
5319 }
George Mount41725de2015-04-09 08:23:05 -07005320 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005321 }
5322
5323 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00005324 * @hide
5325 */
5326 @Override
5327 public boolean canStartActivityForResult() {
5328 return true;
5329 }
5330
5331 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07005332 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
5333 * int, Intent, int, int, int, Bundle)} with no options.
5334 */
5335 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5336 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5337 int extraFlags)
5338 throws IntentSender.SendIntentException {
5339 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
5340 flagsMask, flagsValues, extraFlags, null);
5341 }
5342
5343 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005344 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005345 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07005346 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005347 * for more information.
5348 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005349 public void startIntentSenderFromChild(Activity child, IntentSender intent,
5350 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07005351 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07005352 throws IntentSender.SendIntentException {
Clara Bayarria0c2dc32016-04-12 12:00:15 +01005353 startIntentSenderForResultInner(intent, child.mEmbeddedID, requestCode, fillInIntent,
5354 flagsMask, flagsValues, options);
5355 }
5356
5357 /**
5358 * Like {@link #startIntentSenderFromChild}, but taking a Fragment; see
5359 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
5360 * for more information.
5361 *
5362 * @hide
5363 */
5364 public void startIntentSenderFromChildFragment(Fragment child, IntentSender intent,
5365 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
5366 int extraFlags, @Nullable Bundle options)
5367 throws IntentSender.SendIntentException {
5368 startIntentSenderForResultInner(intent, child.mWho, requestCode, fillInIntent,
5369 flagsMask, flagsValues, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07005370 }
5371
5372 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005373 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
5374 * or {@link #finish} to specify an explicit transition animation to
5375 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005376 *
5377 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
5378 * to using this with starting activities is to supply the desired animation
5379 * information through a {@link ActivityOptions} bundle to
Mark Lufa434852016-08-11 17:40:33 -07005380 * {@link #startActivity(Intent, Bundle)} or a related function. This allows
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07005381 * you to specify a custom animation even when starting an activity from
5382 * outside the context of the current top activity.
5383 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005384 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005385 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005386 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07005387 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005388 */
5389 public void overridePendingTransition(int enterAnim, int exitAnim) {
5390 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005391 ActivityManager.getService().overridePendingTransition(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005392 mToken, getPackageName(), enterAnim, exitAnim);
5393 } catch (RemoteException e) {
5394 }
5395 }
RoboErik55011652014-07-09 15:05:53 -07005396
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07005397 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 * Call this to set the result that your activity will return to its
5399 * caller.
RoboErik55011652014-07-09 15:05:53 -07005400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 * @param resultCode The result code to propagate back to the originating
5402 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07005403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 * @see #RESULT_CANCELED
5405 * @see #RESULT_OK
5406 * @see #RESULT_FIRST_USER
5407 * @see #setResult(int, Intent)
5408 */
5409 public final void setResult(int resultCode) {
5410 synchronized (this) {
5411 mResultCode = resultCode;
5412 mResultData = null;
5413 }
5414 }
5415
5416 /**
5417 * Call this to set the result that your activity will return to its
5418 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005419 *
5420 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
5421 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
5422 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
5423 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
5424 * Activity receiving the result access to the specific URIs in the Intent.
5425 * Access will remain until the Activity has finished (it will remain across the hosting
5426 * process being killed and other temporary destruction) and will be added
5427 * to any existing set of URI permissions it already holds.
5428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 * @param resultCode The result code to propagate back to the originating
5430 * activity, often RESULT_CANCELED or RESULT_OK
5431 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07005432 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 * @see #RESULT_CANCELED
5434 * @see #RESULT_OK
5435 * @see #RESULT_FIRST_USER
5436 * @see #setResult(int)
5437 */
5438 public final void setResult(int resultCode, Intent data) {
5439 synchronized (this) {
5440 mResultCode = resultCode;
5441 mResultData = data;
5442 }
5443 }
5444
5445 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005446 * Return information about who launched this activity. If the launching Intent
5447 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
5448 * that will be returned as-is; otherwise, if known, an
5449 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
5450 * package name that started the Intent will be returned. This may return null if no
5451 * referrer can be identified -- it is neither explicitly specified, nor is it known which
5452 * application package was involved.
5453 *
5454 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
5455 * return the referrer that submitted that new intent to the activity. Otherwise, it
5456 * always returns the referrer of the original Intent.</p>
5457 *
5458 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5459 * referrer information, applications can spoof it.</p>
5460 */
5461 @Nullable
5462 public Uri getReferrer() {
5463 Intent intent = getIntent();
Adam Powelld3c63a62016-06-09 12:36:16 -07005464 try {
5465 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5466 if (referrer != null) {
5467 return referrer;
5468 }
5469 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5470 if (referrerName != null) {
5471 return Uri.parse(referrerName);
5472 }
5473 } catch (BadParcelableException e) {
5474 Log.w(TAG, "Cannot read referrer from intent;"
5475 + " intent extras contain unknown custom Parcelable objects");
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005476 }
5477 if (mReferrer != null) {
5478 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5479 }
5480 return null;
5481 }
5482
5483 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005484 * Override to generate the desired referrer for the content currently being shown
5485 * by the app. The default implementation returns null, meaning the referrer will simply
5486 * be the android-app: of the package name of this activity. Return a non-null Uri to
5487 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5488 */
5489 public Uri onProvideReferrer() {
5490 return null;
5491 }
5492
5493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 * Return the name of the package that invoked this activity. This is who
5495 * the data in {@link #setResult setResult()} will be sent to. You can
5496 * use this information to validate that the recipient is allowed to
5497 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005498 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005499 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005500 * did not use the {@link #startActivityForResult}
5501 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005502 * null.</p>
5503 *
5504 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5505 * the result from this method was unstable. If the process hosting the calling
5506 * package was no longer running, it would return null instead of the proper package
5507 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5508 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005509 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 * @return The package of the activity that will receive your
5511 * reply, or null if none.
5512 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005513 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 public String getCallingPackage() {
5515 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005516 return ActivityManager.getService().getCallingPackage(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 } catch (RemoteException e) {
5518 return null;
5519 }
5520 }
5521
5522 /**
5523 * Return the name of the activity that invoked this activity. This is
5524 * who the data in {@link #setResult setResult()} will be sent to. You
5525 * can use this information to validate that the recipient is allowed to
5526 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005527 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005528 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005529 * did not use the {@link #startActivityForResult}
5530 * form that includes a request code), then the calling package will be
5531 * null.
5532 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005533 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 * reply, or null if none.
5535 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005536 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 public ComponentName getCallingActivity() {
5538 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005539 return ActivityManager.getService().getCallingActivity(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 } catch (RemoteException e) {
5541 return null;
5542 }
5543 }
5544
5545 /**
5546 * Control whether this activity's main window is visible. This is intended
5547 * only for the special case of an activity that is not going to show a
5548 * UI itself, but can't just finish prior to onResume() because it needs
5549 * to wait for a service binding or such. Setting this to false allows
5550 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 * <p>The default value for this is taken from the
5553 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5554 */
5555 public void setVisible(boolean visible) {
5556 if (mVisibleFromClient != visible) {
5557 mVisibleFromClient = visible;
5558 if (mVisibleFromServer) {
5559 if (visible) makeVisible();
5560 else mDecor.setVisibility(View.INVISIBLE);
5561 }
5562 }
5563 }
RoboErik55011652014-07-09 15:05:53 -07005564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 void makeVisible() {
5566 if (!mWindowAdded) {
5567 ViewManager wm = getWindowManager();
5568 wm.addView(mDecor, getWindow().getAttributes());
5569 mWindowAdded = true;
5570 }
5571 mDecor.setVisibility(View.VISIBLE);
5572 }
RoboErik55011652014-07-09 15:05:53 -07005573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 /**
5575 * Check to see whether this activity is in the process of finishing,
5576 * either because you called {@link #finish} on it or someone else
5577 * has requested that it finished. This is often used in
5578 * {@link #onPause} to determine whether the activity is simply pausing or
5579 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 * @see #finish
5584 */
5585 public boolean isFinishing() {
5586 return mFinished;
5587 }
5588
5589 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005590 * Returns true if the final {@link #onDestroy()} call has been made
5591 * on the Activity, so this instance is now dead.
5592 */
5593 public boolean isDestroyed() {
5594 return mDestroyed;
5595 }
5596
5597 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005598 * Check to see whether this activity is in the process of being destroyed in order to be
5599 * recreated with a new configuration. This is often used in
5600 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5601 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005602 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005603 * @return If the activity is being torn down in order to be recreated with a new configuration,
5604 * returns true; else returns false.
5605 */
5606 public boolean isChangingConfigurations() {
5607 return mChangingConfigurations;
5608 }
5609
5610 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005611 * Cause this Activity to be recreated with a new instance. This results
5612 * in essentially the same flow as when the Activity is created due to
5613 * a configuration change -- the current instance will go through its
5614 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5615 */
5616 public void recreate() {
5617 if (mParent != null) {
5618 throw new IllegalStateException("Can only be called on top-level activity");
5619 }
Wale Ogunwale0527d912018-05-16 17:23:48 -07005620 if (Looper.myLooper() != mMainThread.getLooper()) {
5621 throw new IllegalStateException("Must be called from main thread");
5622 }
Andrii Kulian320e3b52018-05-03 16:26:25 -07005623 mMainThread.scheduleRelaunchActivity(mToken);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005624 }
5625
5626 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005627 * Finishes the current activity and specifies whether to remove the task associated with this
5628 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005630 @UnsupportedAppUsage
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005631 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 if (mParent == null) {
5633 int resultCode;
5634 Intent resultData;
5635 synchronized (this) {
5636 resultCode = mResultCode;
5637 resultData = mResultData;
5638 }
Joe Onorato43a17652011-04-06 19:22:23 -07005639 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005641 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005642 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005643 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005644 if (ActivityManager.getService()
Winson Chung3b3f4642014-04-22 10:08:18 -07005645 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 mFinished = true;
5647 }
5648 } catch (RemoteException e) {
5649 // Empty
5650 }
5651 } else {
5652 mParent.finishFromChild(this);
5653 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07005654
5655 // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
5656 // be restored now.
5657 if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
5658 getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
5659 mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
5660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 }
5662
5663 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005664 * Call this when your activity is done and should be closed. The
5665 * ActivityResult is propagated back to whoever launched you via
5666 * onActivityResult().
5667 */
5668 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005669 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005670 }
5671
5672 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005673 * Finish this activity as well as all activities immediately below it
5674 * in the current task that have the same affinity. This is typically
5675 * used when an application can be launched on to another task (such as
5676 * from an ACTION_VIEW of a content type it understands) and the user
5677 * has used the up navigation to switch out of the current task and in
5678 * to its own task. In this case, if the user has navigated down into
5679 * any other activities of the second application, all of those should
5680 * be removed from the original task as part of the task switch.
5681 *
5682 * <p>Note that this finish does <em>not</em> allow you to deliver results
5683 * to the previous activity, and an exception will be thrown if you are trying
5684 * to do so.</p>
5685 */
5686 public void finishAffinity() {
5687 if (mParent != null) {
5688 throw new IllegalStateException("Can not be called from an embedded activity");
5689 }
5690 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5691 throw new IllegalStateException("Can not be called to deliver a result");
5692 }
5693 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005694 if (ActivityManager.getService().finishActivityAffinity(mToken)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005695 mFinished = true;
5696 }
5697 } catch (RemoteException e) {
5698 // Empty
5699 }
5700 }
5701
5702 /**
RoboErik55011652014-07-09 15:05:53 -07005703 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 * {@link #finish} method. The default implementation simply calls
5705 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005706 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005708 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 * @see #finish
5710 */
5711 public void finishFromChild(Activity child) {
5712 finish();
5713 }
5714
5715 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005716 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5717 * to reverse its exit Transition. When the exit Transition completes,
5718 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5719 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005720 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005721 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005722 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005723 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005724 finish();
5725 }
George Mountcb4b7d92014-02-25 10:47:55 -08005726 }
5727
5728 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 * Force finish another activity that you had previously started with
5730 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 * @param requestCode The request code of the activity that you had
5733 * given to startActivityForResult(). If there are multiple
5734 * activities started with this request code, they
5735 * will all be finished.
5736 */
5737 public void finishActivity(int requestCode) {
5738 if (mParent == null) {
5739 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005740 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5742 } catch (RemoteException e) {
5743 // Empty
5744 }
5745 } else {
5746 mParent.finishActivityFromChild(this, requestCode);
5747 }
5748 }
5749
5750 /**
5751 * This is called when a child activity of this one calls its
5752 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 * @param child The activity making the call.
5755 * @param requestCode Request code that had been used to start the
5756 * activity.
5757 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005758 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005760 ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5762 } catch (RemoteException e) {
5763 // Empty
5764 }
5765 }
5766
5767 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005768 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005769 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005770 */
5771 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005772 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005773 }
5774
5775 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005776 * Ask that the local app instance of this activity be released to free up its memory.
5777 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5778 * a new instance of the activity will later be re-created if needed due to the user
5779 * navigating back to it.
5780 *
5781 * @return Returns true if the activity was in a state that it has started the process
5782 * of destroying its current instance; returns false if for any reason this could not
5783 * be done: it is currently visible to the user, it is already being destroyed, it is
5784 * being finished, it hasn't yet saved its state, etc.
5785 */
5786 public boolean releaseInstance() {
5787 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005788 return ActivityManager.getService().releaseActivityInstance(mToken);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005789 } catch (RemoteException e) {
5790 // Empty
5791 }
5792 return false;
5793 }
5794
5795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 * Called when an activity you launched exits, giving you the requestCode
5797 * you started it with, the resultCode it returned, and any additional
5798 * data from it. The <var>resultCode</var> will be
5799 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5800 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 * <p>You will receive this call immediately before onResume() when your
5803 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005804 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005805 * <p>This method is never invoked if your activity sets
5806 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5807 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005808 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 * @param requestCode The integer request code originally supplied to
5810 * startActivityForResult(), allowing you to identify who this
5811 * result came from.
5812 * @param resultCode The integer result code returned by the child activity
5813 * through its setResult().
5814 * @param data An Intent, which can return result data to the caller
5815 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 * @see #startActivityForResult
5818 * @see #createPendingResult
5819 * @see #setResult(int)
5820 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005821 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 }
5823
5824 /**
George Mount62ab9b72014-05-02 13:51:17 -07005825 * Called when an activity you launched with an activity transition exposes this
5826 * Activity through a returning activity transition, giving you the resultCode
5827 * and any additional data from it. This method will only be called if the activity
5828 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005829 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005830 *
5831 * <p>The purpose of this function is to let the called Activity send a hint about
5832 * its state so that this underlying Activity can prepare to be exposed. A call to
5833 * this method does not guarantee that the called Activity has or will be exiting soon.
5834 * It only indicates that it will expose this Activity's Window and it has
5835 * some data to pass to prepare it.</p>
5836 *
5837 * @param resultCode The integer result code returned by the child activity
5838 * through its setResult().
5839 * @param data An Intent, which can return result data to the caller
5840 * (various data can be attached to Intent "extras").
5841 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005842 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005843 }
5844
5845 /**
RoboErik55011652014-07-09 15:05:53 -07005846 * Create a new PendingIntent object which you can hand to others
5847 * for them to use to send result data back to your
5848 * {@link #onActivityResult} callback. The created object will be either
5849 * one-shot (becoming invalid after a result is sent back) or multiple
5850 * (allowing any number of results to be sent through it).
5851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 * @param requestCode Private request code for the sender that will be
5853 * associated with the result data when it is returned. The sender can not
5854 * modify this value, allowing you to identify incoming results.
5855 * @param data Default data to supply in the result, which may be modified
5856 * by the sender.
5857 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5858 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5859 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5860 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5861 * or any of the flags as supported by
5862 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5863 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 * @return Returns an existing or new PendingIntent matching the given
5866 * parameters. May return null only if
5867 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5868 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005869 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 * @see PendingIntent
5871 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005872 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5873 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 String packageName = getPackageName();
5875 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005876 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 IIntentSender target =
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005878 ActivityManager.getService().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005879 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005881 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07005882 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 return target != null ? new PendingIntent(target) : null;
5884 } catch (RemoteException e) {
5885 // Empty
5886 }
5887 return null;
5888 }
5889
5890 /**
5891 * Change the desired orientation of this activity. If the activity
5892 * is currently in the foreground or otherwise impacting the screen
5893 * orientation, the screen will immediately be changed (possibly causing
5894 * the activity to be restarted). Otherwise, this will be used the next
5895 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005896 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 * @param requestedOrientation An orientation constant as used in
5898 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5899 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005900 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 if (mParent == null) {
5902 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005903 ActivityManager.getService().setRequestedOrientation(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 mToken, requestedOrientation);
5905 } catch (RemoteException e) {
5906 // Empty
5907 }
5908 } else {
5909 mParent.setRequestedOrientation(requestedOrientation);
5910 }
5911 }
RoboErik55011652014-07-09 15:05:53 -07005912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 /**
5914 * Return the current requested orientation of the activity. This will
5915 * either be the orientation requested in its component's manifest, or
5916 * the last requested orientation given to
5917 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005918 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 * @return Returns an orientation constant as used in
5920 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5921 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005922 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 public int getRequestedOrientation() {
5924 if (mParent == null) {
5925 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005926 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 .getRequestedOrientation(mToken);
5928 } catch (RemoteException e) {
5929 // Empty
5930 }
5931 } else {
5932 return mParent.getRequestedOrientation();
5933 }
5934 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5935 }
RoboErik55011652014-07-09 15:05:53 -07005936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 /**
5938 * Return the identifier of the task this activity is in. This identifier
5939 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 * @return Task identifier, an opaque integer.
5942 */
5943 public int getTaskId() {
5944 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005945 return ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 .getTaskForActivity(mToken, false);
5947 } catch (RemoteException e) {
5948 return -1;
5949 }
5950 }
5951
5952 /**
5953 * Return whether this activity is the root of a task. The root is the
5954 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005955 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 * @return True if this is the root activity, else false.
5957 */
Winson Chung4d8681f2017-05-23 16:22:08 -07005958 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 public boolean isTaskRoot() {
5960 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005961 return ActivityManager.getService().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 } catch (RemoteException e) {
5963 return false;
5964 }
5965 }
5966
5967 /**
5968 * Move the task containing this activity to the back of the activity
5969 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005970 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 * @param nonRoot If false then this only works if the activity is the root
5972 * of a task; if true it will work for any activity in
5973 * a task.
RoboErik55011652014-07-09 15:05:53 -07005974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 * @return If the task was moved (or it was already at the
5976 * back) true is returned, else false.
5977 */
5978 public boolean moveTaskToBack(boolean nonRoot) {
5979 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005980 return ActivityManager.getService().moveActivityTaskToBack(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 mToken, nonRoot);
5982 } catch (RemoteException e) {
5983 // Empty
5984 }
5985 return false;
5986 }
5987
5988 /**
5989 * Returns class name for this activity with the package prefix removed.
5990 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005991 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 * @return The local class name.
5993 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005994 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 public String getLocalClassName() {
5996 final String pkg = getPackageName();
5997 final String cls = mComponent.getClassName();
5998 int packageLen = pkg.length();
5999 if (!cls.startsWith(pkg) || cls.length() <= packageLen
6000 || cls.charAt(packageLen) != '.') {
6001 return cls;
6002 }
6003 return cls.substring(packageLen+1);
6004 }
RoboErik55011652014-07-09 15:05:53 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 /**
Felipe Leme17292d12017-10-24 14:03:10 -07006007 * Returns the complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07006008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 * @return Returns the complete component name for this activity
6010 */
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006011 public ComponentName getComponentName() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 return mComponent;
6013 }
6014
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006015 /** @hide */
6016 @Override
6017 public final ComponentName autofillClientGetComponentName() {
6018 return getComponentName();
6019 }
6020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 /**
6022 * Retrieve a {@link SharedPreferences} object for accessing preferences
6023 * that are private to this activity. This simply calls the underlying
6024 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
6025 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07006026 *
6027 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07006028 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 *
6030 * @return Returns the single SharedPreferences instance that can be used
6031 * to retrieve and modify the preference values.
6032 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06006033 public SharedPreferences getPreferences(@Context.PreferencesMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 return getSharedPreferences(getLocalClassName(), mode);
6035 }
RoboErik55011652014-07-09 15:05:53 -07006036
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006037 private void ensureSearchManager() {
6038 if (mSearchManager != null) {
6039 return;
6040 }
RoboErik55011652014-07-09 15:05:53 -07006041
Jeff Sharkey49ca5292016-05-10 12:54:45 -06006042 try {
6043 mSearchManager = new SearchManager(this, null);
6044 } catch (ServiceNotFoundException e) {
6045 throw new IllegalStateException(e);
6046 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006047 }
Tor Norbyed9273d62013-05-30 15:59:53 -07006048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07006050 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 if (getBaseContext() == null) {
6052 throw new IllegalStateException(
6053 "System services not available to Activities before onCreate()");
6054 }
6055
6056 if (WINDOW_SERVICE.equals(name)) {
6057 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006058 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006059 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01006060 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 }
6062 return super.getSystemService(name);
6063 }
6064
6065 /**
6066 * Change the title associated with this activity. If this is a
6067 * top-level activity, the title for its window will change. If it
6068 * is an embedded activity, the parent can do whatever it wants
6069 * with it.
6070 */
6071 public void setTitle(CharSequence title) {
6072 mTitle = title;
6073 onTitleChanged(title, mTitleColor);
6074
6075 if (mParent != null) {
6076 mParent.onChildTitleChanged(this, title);
6077 }
6078 }
6079
6080 /**
6081 * Change the title associated with this activity. If this is a
6082 * top-level activity, the title for its window will change. If it
6083 * is an embedded activity, the parent can do whatever it wants
6084 * with it.
6085 */
6086 public void setTitle(int titleId) {
6087 setTitle(getText(titleId));
6088 }
6089
Alan Viverette2525d9c2013-11-15 14:42:19 -08006090 /**
6091 * Change the color of the title associated with this activity.
6092 * <p>
6093 * This method is deprecated starting in API Level 11 and replaced by action
6094 * bar styles. For information on styling the Action Bar, read the <a
6095 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
6096 * guide.
6097 *
6098 * @deprecated Use action bar styles instead.
6099 */
6100 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 public void setTitleColor(int textColor) {
6102 mTitleColor = textColor;
6103 onTitleChanged(mTitle, textColor);
6104 }
6105
6106 public final CharSequence getTitle() {
6107 return mTitle;
6108 }
6109
6110 public final int getTitleColor() {
6111 return mTitleColor;
6112 }
6113
6114 protected void onTitleChanged(CharSequence title, int color) {
6115 if (mTitleReady) {
6116 final Window win = getWindow();
6117 if (win != null) {
6118 win.setTitle(title);
6119 if (color != 0) {
6120 win.setTitleColor(color);
6121 }
6122 }
Adam Powellaf2d8592014-08-26 18:06:40 -07006123 if (mActionBar != null) {
6124 mActionBar.setWindowTitle(title);
6125 }
Adam Powella557fdc2014-08-21 18:05:53 -07006126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 }
6128
6129 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
6130 }
6131
6132 /**
Winson Chunga449dc02014-05-16 11:15:04 -07006133 * Sets information describing the task with this activity for presentation inside the Recents
6134 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
6135 * are traversed in order from the topmost activity to the bottommost. The traversal continues
6136 * for each property until a suitable value is found. For each task the taskDescription will be
6137 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07006138 *
6139 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07006140 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07006141 *
Winson Chunga449dc02014-05-16 11:15:04 -07006142 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07006143 */
Winson Chunga449dc02014-05-16 11:15:04 -07006144 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08006145 if (mTaskDescription != taskDescription) {
Jorim Jaggif4156ea2017-04-10 18:39:04 -07006146 mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
Winsonb6403152016-02-23 13:32:09 -08006147 // Scale the icon down to something reasonable if it is provided
6148 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
6149 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
6150 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
6151 true);
6152 mTaskDescription.setIcon(icon);
6153 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07006154 }
6155 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006156 ActivityManager.getService().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07006157 } catch (RemoteException e) {
6158 }
6159 }
6160
6161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 * Sets the visibility of the progress bar in the title.
6163 * <p>
6164 * In order for the progress bar to be shown, the feature must be requested
6165 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006168 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006170 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 public final void setProgressBarVisibility(boolean visible) {
6172 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
6173 Window.PROGRESS_VISIBILITY_OFF);
6174 }
6175
6176 /**
6177 * Sets the visibility of the indeterminate progress bar in the title.
6178 * <p>
6179 * In order for the progress bar to be shown, the feature must be requested
6180 * via {@link #requestWindowFeature(int)}.
6181 *
6182 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006183 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006185 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 public final void setProgressBarIndeterminateVisibility(boolean visible) {
6187 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
6188 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
6189 }
RoboErik55011652014-07-09 15:05:53 -07006190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 /**
6192 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
6193 * is always indeterminate).
6194 * <p>
6195 * In order for the progress bar to be shown, the feature must be requested
6196 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006197 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006199 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006201 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006202 public final void setProgressBarIndeterminate(boolean indeterminate) {
6203 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00006204 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
6205 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 }
RoboErik55011652014-07-09 15:05:53 -07006207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 /**
6209 * Sets the progress for the progress bars in the title.
6210 * <p>
6211 * In order for the progress bar to be shown, the feature must be requested
6212 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006213 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 * @param progress The progress for the progress bar. Valid ranges are from
6215 * 0 to 10000 (both inclusive). If 10000 is given, the progress
6216 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04006217 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006219 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 public final void setProgress(int progress) {
6221 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
6222 }
RoboErik55011652014-07-09 15:05:53 -07006223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006224 /**
6225 * Sets the secondary progress for the progress bar in the title. This
6226 * progress is drawn between the primary progress (set via
6227 * {@link #setProgress(int)} and the background. It can be ideal for media
6228 * scenarios such as showing the buffering progress while the default
6229 * progress shows the play progress.
6230 * <p>
6231 * In order for the progress bar to be shown, the feature must be requested
6232 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07006233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
6235 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04006236 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04006238 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 public final void setSecondaryProgress(int secondaryProgress) {
6240 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
6241 secondaryProgress + Window.PROGRESS_SECONDARY_START);
6242 }
6243
6244 /**
6245 * Suggests an audio stream whose volume should be changed by the hardware
6246 * volume controls.
6247 * <p>
6248 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07006249 * Volume requests which are received while the Activity is in the
6250 * foreground will affect this stream.
6251 * <p>
6252 * It is not guaranteed that the hardware volume controls will always change
6253 * this stream's volume (for example, if a call is in progress, its stream's
6254 * volume may be changed instead). To reset back to the default, use
6255 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
6256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07006258 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 */
6260 public final void setVolumeControlStream(int streamType) {
6261 getWindow().setVolumeControlStream(streamType);
6262 }
6263
6264 /**
6265 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07006266 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07006267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 * @return The suggested audio stream type whose volume should be changed by
6269 * the hardware volume controls.
6270 * @see #setVolumeControlStream(int)
6271 */
6272 public final int getVolumeControlStream() {
6273 return getWindow().getVolumeControlStream();
6274 }
RoboErik55011652014-07-09 15:05:53 -07006275
6276 /**
6277 * Sets a {@link MediaController} to send media keys and volume changes to.
6278 * <p>
6279 * The controller will be tied to the window of this Activity. Media key and
6280 * volume events which are received while the Activity is in the foreground
6281 * will be forwarded to the controller and used to invoke transport controls
6282 * or adjust the volume. This may be used instead of or in addition to
6283 * {@link #setVolumeControlStream} to affect a specific session instead of a
6284 * specific stream.
6285 * <p>
6286 * It is not guaranteed that the hardware volume controls will always change
6287 * this session's volume (for example, if a call is in progress, its
6288 * stream's volume may be changed instead). To reset back to the default use
6289 * null as the controller.
6290 *
6291 * @param controller The controller for the session which should receive
6292 * media keys and volume changes.
6293 */
6294 public final void setMediaController(MediaController controller) {
6295 getWindow().setMediaController(controller);
6296 }
6297
6298 /**
6299 * Gets the controller which should be receiving media key and volume events
6300 * while this activity is in the foreground.
6301 *
6302 * @return The controller which should receive events.
6303 * @see #setMediaController(android.media.session.MediaController)
6304 */
6305 public final MediaController getMediaController() {
6306 return getWindow().getMediaController();
6307 }
6308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 /**
6310 * Runs the specified action on the UI thread. If the current thread is the UI
6311 * thread, then the action is executed immediately. If the current thread is
6312 * not the UI thread, the action is posted to the event queue of the UI thread.
6313 *
6314 * @param action the action to run on the UI thread
6315 */
6316 public final void runOnUiThread(Runnable action) {
6317 if (Thread.currentThread() != mUiThread) {
6318 mHandler.post(action);
6319 } else {
6320 action.run();
6321 }
6322 }
6323
Svetoslav Ganov24c90452017-12-27 15:17:14 -08006324 /** @hide */
6325 @Override
6326 public final void autofillClientRunOnUiThread(Runnable action) {
6327 runOnUiThread(action);
6328 }
6329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006331 * Standard implementation of
6332 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
6333 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006334 * This implementation does nothing and is for
6335 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
6336 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
6337 *
6338 * @see android.view.LayoutInflater#createView
6339 * @see android.view.Window#getLayoutInflater
6340 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006341 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07006342 public View onCreateView(String name, Context context, AttributeSet attrs) {
6343 return null;
6344 }
6345
6346 /**
6347 * Standard implementation of
6348 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
6349 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006350 * This implementation handles <fragment> tags to embed fragments inside
6351 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 *
6353 * @see android.view.LayoutInflater#createView
6354 * @see android.view.Window#getLayoutInflater
6355 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07006356 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006357 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07006358 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07006359 }
RoboErik55011652014-07-09 15:05:53 -07006360
Adam Powell371a8092014-06-20 12:51:12 -07006361 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 }
6363
Daniel Sandler69a48172010-06-23 16:29:36 -04006364 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07006365 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07006366 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07006367 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08006368 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07006369 * @param fd The raw file descriptor that the dump is being sent to.
6370 * @param writer The PrintWriter to which you should dump your state. This will be
6371 * closed for you after you return.
6372 * @param args additional arguments to the dump request.
6373 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08006374 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07006375 dumpInner(prefix, fd, writer, args);
6376 }
6377
6378 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Felipe Leme42260332018-08-15 08:44:12 -07006379 if (args != null && args.length > 0 && args[0].equals("--autofill")) {
6380 dumpAutofillManager(prefix, writer);
6381 return;
6382 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08006383 writer.print(prefix); writer.print("Local Activity ");
6384 writer.print(Integer.toHexString(System.identityHashCode(this)));
6385 writer.println(" State:");
6386 String innerPrefix = prefix + " ";
6387 writer.print(innerPrefix); writer.print("mResumed=");
6388 writer.print(mResumed); writer.print(" mStopped=");
6389 writer.print(mStopped); writer.print(" mFinished=");
6390 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08006391 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
6392 writer.println(mChangingConfigurations);
6393 writer.print(innerPrefix); writer.print("mCurrentConfig=");
6394 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07006395
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006396 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
6397 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07006398 if (mVoiceInteractor != null) {
6399 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
6400 }
Jeff Brown5182c782013-10-15 20:31:52 -07006401
Michael Wright5f48dc72013-11-01 12:42:49 -07006402 if (getWindow() != null &&
6403 getWindow().peekDecorView() != null &&
6404 getWindow().peekDecorView().getViewRootImpl() != null) {
6405 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
6406 }
Jeff Brown5182c782013-10-15 20:31:52 -07006407
6408 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Felipe Lemec24a56a2017-08-03 14:27:57 -07006409
Felipe Leme42260332018-08-15 08:44:12 -07006410 dumpAutofillManager(prefix, writer);
6411
6412 ResourcesManager.getInstance().dump(prefix, writer);
6413 }
6414
6415 void dumpAutofillManager(String prefix, PrintWriter writer) {
Felipe Leme9d16dff2018-01-29 18:12:43 -08006416 final AutofillManager afm = getAutofillManager();
Felipe Lemec24a56a2017-08-03 14:27:57 -07006417 if (afm != null) {
Felipe Leme42260332018-08-15 08:44:12 -07006418 afm.dump(prefix, writer);
Svet Ganov47b37aa2018-02-16 00:11:39 -08006419 writer.print(prefix); writer.print("Autofill Compat Mode: ");
6420 writer.println(isAutofillCompatibilityEnabled());
Felipe Leme686128e2017-10-17 14:02:20 -07006421 } else {
6422 writer.print(prefix); writer.println("No AutofillManager");
Felipe Lemec24a56a2017-08-03 14:27:57 -07006423 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07006424 }
6425
6426 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006427 * Bit indicating that this activity is "immersive" and should not be
6428 * interrupted by notifications if possible.
6429 *
6430 * This value is initially set by the manifest property
6431 * <code>android:immersive</code> but may be changed at runtime by
6432 * {@link #setImmersive}.
6433 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006434 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04006435 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6436 */
6437 public boolean isImmersive() {
6438 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006439 return ActivityManager.getService().isImmersive(mToken);
Daniel Sandler69a48172010-06-23 16:29:36 -04006440 } catch (RemoteException e) {
6441 return false;
6442 }
6443 }
6444
6445 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07006446 * Indication of whether this is the highest level activity in this task. Can be used to
6447 * determine whether an activity launched by this activity was placed in the same task or
6448 * another task.
6449 *
6450 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07006451 */
George Mountff243282014-07-07 16:12:07 -07006452 private boolean isTopOfTask() {
George Mountf43ea5d2016-05-26 09:52:53 -07006453 if (mToken == null || mWindow == null) {
George Mountd0ca0e02016-03-29 10:32:56 -07006454 return false;
6455 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07006456 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006457 return ActivityManager.getService().isTopOfTask(getActivityToken());
Craig Mautnerd61dc202014-07-07 11:09:11 -07006458 } catch (RemoteException e) {
6459 return false;
6460 }
6461 }
6462
6463 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07006464 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
6465 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006466 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006467 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006468 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
6469 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07006470 * This call has no effect on non-translucent activities or on activities with the
6471 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006472 *
George Mount62ab9b72014-05-02 13:51:17 -07006473 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6474 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006475 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006476 *
6477 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07006478 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006479 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006480 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07006481 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07006482 mTranslucentCallback = null;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006483 if (ActivityManager.getService().convertFromTranslucent(mToken)) {
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006484 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
6485 }
Craig Mautner4addfc52013-06-25 08:05:45 -07006486 } catch (RemoteException e) {
6487 // pass
6488 }
6489 }
6490
6491 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006492 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
6493 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
6494 * <p>
6495 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
6496 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
6497 * be called indicating that it is safe to make this activity translucent again. Until
6498 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
6499 * behind the frontmost Activity will be indeterminate.
6500 * <p>
6501 * This call has no effect on non-translucent activities or on activities with the
6502 * {@link android.R.attr#windowIsFloating} attribute.
6503 *
6504 * @param callback the method to call when all visible Activities behind this one have been
6505 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07006506 * @param options activity options delivered to the activity below this one. The options
6507 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07006508 * @return <code>true</code> if Window was opaque and will become translucent or
6509 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006510 *
6511 * @see #convertFromTranslucent()
6512 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006513 *
6514 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006515 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006516 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006517 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006518 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006519 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006520 try {
6521 mTranslucentCallback = callback;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006522 mChangeCanvasToTranslucent = ActivityManager.getService().convertToTranslucent(
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006523 mToken, options == null ? null : options.toBundle());
George Mount9e183972014-09-03 12:35:09 -07006524 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006525 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006526 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006527 // Make callback return as though it timed out.
6528 mChangeCanvasToTranslucent = false;
6529 drawComplete = false;
6530 }
6531 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6532 // Window is already translucent.
6533 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006534 }
George Mount3cc716c2014-06-12 16:35:35 -07006535 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006536 }
6537
6538 /** @hide */
6539 void onTranslucentConversionComplete(boolean drawComplete) {
6540 if (mTranslucentCallback != null) {
6541 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6542 mTranslucentCallback = null;
6543 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006544 if (mChangeCanvasToTranslucent) {
6545 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6546 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006547 }
6548
Craig Mautnereb8abf72014-07-02 15:04:09 -07006549 /** @hide */
6550 public void onNewActivityOptions(ActivityOptions options) {
6551 mActivityTransitionState.setEnterActivityOptions(this, options);
6552 if (!mStopped) {
6553 mActivityTransitionState.enterReady(this);
6554 }
6555 }
6556
Craig Mautner5eda9b32013-07-02 11:58:16 -07006557 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006558 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6559 * from an activity launched by this activity in its call to {@link
6560 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6561 *
6562 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6563 * @hide
6564 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01006565 @UnsupportedAppUsage
Craig Mautner233ceee2014-05-09 17:05:11 -07006566 ActivityOptions getActivityOptions() {
6567 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006568 return ActivityOptions.fromBundle(
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006569 ActivityManager.getService().getActivityOptions(mToken));
Craig Mautner233ceee2014-05-09 17:05:11 -07006570 } catch (RemoteException e) {
6571 }
6572 return null;
6573 }
6574
6575 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006576 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006577 * this method anytime between the start of {@link #onResume()} and the return from
6578 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6579 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6580 *
6581 * <p>The actions of this call are reset each time that this activity is brought to the
6582 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6583 * to not have requested visible behind. Therefore, if you want this activity to continue to
6584 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006585 *
6586 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6587 * for dialog and translucent activities.
6588 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006589 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6590 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6591 *
6592 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006593 * the next call to onResume.
6594 *
Bryce Leed59629e2017-04-18 14:35:29 -07006595 * @deprecated This method's functionality is no longer supported as of
6596 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6597 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006598 * @param visible true to notify the system that the activity wishes to be visible behind other
6599 * translucent activities, false to indicate otherwise. Resources must be
6600 * released when passing false to this method.
Bryce Leed59629e2017-04-18 14:35:29 -07006601 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006602 * @return the resulting visibiity state. If true the activity will remain visible beyond
6603 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6604 * then the activity may not count on being visible behind other translucent activities,
6605 * and must stop any media playback and release resources.
6606 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6607 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006608 *
Jose Limafcf70832014-08-27 23:09:05 -07006609 * @see #onVisibleBehindCanceled()
Craig Mautneree2e45a2014-06-27 12:10:03 -07006610 */
Bryce Leed59629e2017-04-18 14:35:29 -07006611 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006612 public boolean requestVisibleBehind(boolean visible) {
Robert Carr8661c5e2017-06-27 13:46:14 -07006613 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006614 }
6615
6616 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006617 * Called when a translucent activity over this activity is becoming opaque or another
6618 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006619 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006620 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006621 * <p>When this method is called the activity has 500 msec to release any resources it may be
6622 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006623 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006624 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006625 * process. Otherwise {@link #onStop()} will be called following return.
6626 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006627 * @see #requestVisibleBehind(boolean)
Bryce Leed59629e2017-04-18 14:35:29 -07006628 *
6629 * @deprecated This method's functionality is no longer supported as of
6630 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006631 */
Bryce Leed59629e2017-04-18 14:35:29 -07006632 @Deprecated
Tor Norbyec615c6f2015-03-02 10:11:44 -08006633 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006634 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006635 mCalled = true;
6636 }
6637
6638 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006639 * Translucent activities may call this to determine if there is an activity below them that
6640 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006641 *
Bryce Leed59629e2017-04-18 14:35:29 -07006642 * @deprecated This method's functionality is no longer supported as of
6643 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6644 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006645 * @return true if an activity below is set to visible according to the most recent call to
6646 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006647 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006648 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006649 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006650 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006651 * @hide
6652 */
Bryce Leed59629e2017-04-18 14:35:29 -07006653 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006654 @SystemApi
6655 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006656 return false;
6657 }
6658
6659 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006660 * The topmost foreground activity will receive this call when the background visibility state
6661 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006662 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006663 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006664 * due to a background activity finishing itself.
6665 *
Bryce Leed59629e2017-04-18 14:35:29 -07006666 * @deprecated This method's functionality is no longer supported as of
6667 * {@link android.os.Build.VERSION_CODES#O} and will be removed in a future release.
6668 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006669 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006670 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006671 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006672 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006673 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006674 */
Bryce Leed59629e2017-04-18 14:35:29 -07006675 @Deprecated
Jose Lima4b6c6692014-08-12 17:41:12 -07006676 @SystemApi
6677 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006678 }
6679
6680 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006681 * Activities cannot draw during the period that their windows are animating in. In order
6682 * to know when it is safe to begin drawing they can override this method which will be
6683 * called when the entering animation has completed.
6684 */
6685 public void onEnterAnimationComplete() {
6686 }
6687
6688 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006689 * @hide
6690 */
6691 public void dispatchEnterAnimationComplete() {
6692 onEnterAnimationComplete();
6693 if (getWindow() != null && getWindow().getDecorView() != null) {
6694 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6695 }
6696 }
6697
6698 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006699 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006700 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006701 * Note that changing this value will have no effect on the activity's
6702 * {@link android.content.pm.ActivityInfo} structure; that is, if
6703 * <code>android:immersive</code> is set to <code>true</code>
6704 * in the application's manifest entry for this activity, the {@link
6705 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6706 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6707 * FLAG_IMMERSIVE} bit set.
6708 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006709 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006710 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6711 */
6712 public void setImmersive(boolean i) {
6713 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006714 ActivityManager.getService().setImmersive(mToken, i);
Daniel Sandler69a48172010-06-23 16:29:36 -04006715 } catch (RemoteException e) {
6716 // pass
6717 }
6718 }
6719
Adam Powell6e346362010-07-23 10:18:23 -07006720 /**
Ruben Brunk927d3452016-05-02 19:30:51 -07006721 * Enable or disable virtual reality (VR) mode for this Activity.
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006722 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006723 * <p>VR mode is a hint to Android system to switch to a mode optimized for VR applications
6724 * while this Activity has user focus.</p>
6725 *
6726 * <p>It is recommended that applications additionally declare
6727 * {@link android.R.attr#enableVrMode} in their manifest to allow for smooth activity
6728 * transitions when switching between VR activities.</p>
6729 *
6730 * <p>If the requested {@link android.service.vr.VrListenerService} component is not available,
6731 * VR mode will not be started. Developers can handle this case as follows:</p>
6732 *
6733 * <pre>
6734 * String servicePackage = "com.whatever.app";
6735 * String serviceClass = "com.whatever.app.MyVrListenerService";
6736 *
6737 * // Name of the component of the VrListenerService to start.
6738 * ComponentName serviceComponent = new ComponentName(servicePackage, serviceClass);
6739 *
6740 * try {
6741 * setVrModeEnabled(true, myComponentName);
6742 * } catch (PackageManager.NameNotFoundException e) {
6743 * List&lt;ApplicationInfo> installed = getPackageManager().getInstalledApplications(0);
6744 * boolean isInstalled = false;
6745 * for (ApplicationInfo app : installed) {
6746 * if (app.packageName.equals(servicePackage)) {
6747 * isInstalled = true;
6748 * break;
6749 * }
6750 * }
6751 * if (isInstalled) {
6752 * // Package is installed, but not enabled in Settings. Let user enable it.
6753 * startActivity(new Intent(Settings.ACTION_VR_LISTENER_SETTINGS));
6754 * } else {
6755 * // Package is not installed. Send an intent to download this.
6756 * sentIntentToLaunchAppStore(servicePackage);
6757 * }
6758 * }
6759 * </pre>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006760 *
6761 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08006762 * @param requestedComponent the name of the component to use as a
6763 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
6764 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006765 * @throws android.content.pm.PackageManager.NameNotFoundException if the given component
6766 * to run as a {@link android.service.vr.VrListenerService} is not installed, or has
6767 * not been enabled in user settings.
6768 *
Ruben Brunk927d3452016-05-02 19:30:51 -07006769 * @see android.content.pm.PackageManager#FEATURE_VR_MODE_HIGH_PERFORMANCE
6770 * @see android.service.vr.VrListenerService
6771 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS
6772 * @see android.R.attr#enableVrMode
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006773 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08006774 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
6775 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006776 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006777 if (ActivityManager.getService().setVrMode(mToken, enabled, requestedComponent)
Ruben Brunke24b9a62016-02-16 21:38:24 -08006778 != 0) {
6779 throw new PackageManager.NameNotFoundException(
6780 requestedComponent.flattenToString());
6781 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006782 } catch (RemoteException e) {
6783 // pass
6784 }
6785 }
6786
6787 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006788 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006789 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006790 * @param callback Callback that will manage lifecycle events for this action mode
6791 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006792 *
6793 * @see ActionMode
6794 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006795 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006796 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006797 return mWindow.getDecorView().startActionMode(callback);
6798 }
6799
Adam Powelldebf3be2010-11-15 18:58:48 -08006800 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006801 * Start an action mode of the given type.
6802 *
6803 * @param callback Callback that will manage lifecycle events for this action mode
6804 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6805 * @return The ActionMode that was started, or null if it was canceled
6806 *
6807 * @see ActionMode
6808 */
6809 @Nullable
6810 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6811 return mWindow.getDecorView().startActionMode(callback, type);
6812 }
6813
6814 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006815 * Give the Activity a chance to control the UI for an action mode requested
6816 * by the system.
6817 *
6818 * <p>Note: If you are looking for a notification callback that an action mode
6819 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6820 *
6821 * @param callback The callback that should control the new action mode
6822 * @return The new action mode, or <code>null</code> if the activity does not want to
6823 * provide special handling for this action mode. (It will be handled by the system.)
6824 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006825 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006826 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006827 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006828 // Only Primary ActionModes are represented in the ActionBar.
6829 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6830 initWindowDecorActionBar();
6831 if (mActionBar != null) {
6832 return mActionBar.startActionMode(callback);
6833 }
Adam Powell6e346362010-07-23 10:18:23 -07006834 }
6835 return null;
6836 }
6837
Adam Powelldebf3be2010-11-15 18:58:48 -08006838 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006839 * {@inheritDoc}
6840 */
6841 @Nullable
6842 @Override
6843 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6844 try {
6845 mActionModeTypeStarting = type;
6846 return onWindowStartingActionMode(callback);
6847 } finally {
6848 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6849 }
6850 }
6851
6852 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006853 * Notifies the Activity that an action mode has been started.
6854 * Activity subclasses overriding this method should call the superclass implementation.
6855 *
6856 * @param mode The new action mode.
6857 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006858 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006859 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006860 public void onActionModeStarted(ActionMode mode) {
6861 }
6862
6863 /**
6864 * Notifies the activity that an action mode has finished.
6865 * Activity subclasses overriding this method should call the superclass implementation.
6866 *
6867 * @param mode The action mode that just finished.
6868 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006869 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006870 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006871 public void onActionModeFinished(ActionMode mode) {
6872 }
6873
Adam Powelldd8fab22012-03-22 17:47:27 -07006874 /**
6875 * Returns true if the app should recreate the task when navigating 'up' from this activity
6876 * by using targetIntent.
6877 *
6878 * <p>If this method returns false the app can trivially call
6879 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6880 * up navigation. If this method returns false, the app should synthesize a new task stack
6881 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6882 *
6883 * @param targetIntent An intent representing the target destination for up navigation
6884 * @return true if navigating up should recreate a new task stack, false if the same task
6885 * should be used for the destination
6886 */
6887 public boolean shouldUpRecreateTask(Intent targetIntent) {
6888 try {
6889 PackageManager pm = getPackageManager();
6890 ComponentName cn = targetIntent.getComponent();
6891 if (cn == null) {
6892 cn = targetIntent.resolveActivity(pm);
6893 }
6894 ActivityInfo info = pm.getActivityInfo(cn, 0);
6895 if (info.taskAffinity == null) {
6896 return false;
6897 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006898 return ActivityManager.getService()
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006899 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006900 } catch (RemoteException e) {
6901 return false;
6902 } catch (NameNotFoundException e) {
6903 return false;
6904 }
6905 }
6906
6907 /**
6908 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6909 * in the process. If the activity indicated by upIntent already exists in the task's history,
6910 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006911 * finished.
6912 *
6913 * <p>If the indicated activity does not appear in the history stack, this will finish
6914 * each activity in this task until the root activity of the task is reached, resulting in
6915 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6916 * when an activity may be reached by a path not passing through a canonical parent
6917 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006918 *
6919 * <p>This method should be used when performing up navigation from within the same task
6920 * as the destination. If up navigation should cross tasks in some cases, see
6921 * {@link #shouldUpRecreateTask(Intent)}.</p>
6922 *
6923 * @param upIntent An intent representing the target destination for up navigation
6924 *
6925 * @return true if up navigation successfully reached the activity indicated by upIntent and
6926 * upIntent was delivered to it. false if an instance of the indicated activity could
6927 * not be found and this activity was simply finished normally.
6928 */
6929 public boolean navigateUpTo(Intent upIntent) {
6930 if (mParent == null) {
6931 ComponentName destInfo = upIntent.getComponent();
6932 if (destInfo == null) {
6933 destInfo = upIntent.resolveActivity(getPackageManager());
6934 if (destInfo == null) {
6935 return false;
6936 }
6937 upIntent = new Intent(upIntent);
6938 upIntent.setComponent(destInfo);
6939 }
6940 int resultCode;
6941 Intent resultData;
6942 synchronized (this) {
6943 resultCode = mResultCode;
6944 resultData = mResultData;
6945 }
6946 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006947 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006948 }
6949 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006950 upIntent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006951 return ActivityManager.getService().navigateUpTo(mToken, upIntent,
Adam Powelldd8fab22012-03-22 17:47:27 -07006952 resultCode, resultData);
6953 } catch (RemoteException e) {
6954 return false;
6955 }
6956 } else {
6957 return mParent.navigateUpToFromChild(this, upIntent);
6958 }
6959 }
6960
6961 /**
6962 * This is called when a child activity of this one calls its
6963 * {@link #navigateUpTo} method. The default implementation simply calls
6964 * navigateUpTo(upIntent) on this activity (the parent).
6965 *
6966 * @param child The activity making the call.
6967 * @param upIntent An intent representing the target destination for up navigation
6968 *
6969 * @return true if up navigation successfully reached the activity indicated by upIntent and
6970 * upIntent was delivered to it. false if an instance of the indicated activity could
6971 * not be found and this activity was simply finished normally.
6972 */
6973 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6974 return navigateUpTo(upIntent);
6975 }
6976
6977 /**
6978 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6979 * this activity's logical parent. The logical parent is named in the application's manifest
6980 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006981 * Activity subclasses may override this method to modify the Intent returned by
6982 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6983 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006984 *
Adam Powell04d58112012-04-09 10:22:12 -07006985 * @return a new Intent targeting the defined parent of this activity or null if
6986 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006987 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006988 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006989 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006990 final String parentName = mActivityInfo.parentActivityName;
6991 if (TextUtils.isEmpty(parentName)) {
6992 return null;
6993 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006994
6995 // If the parent itself has no parent, generate a main activity intent.
6996 final ComponentName target = new ComponentName(this, parentName);
6997 try {
6998 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6999 final String parentActivity = parentInfo.parentActivityName;
7000 final Intent parentIntent = parentActivity == null
7001 ? Intent.makeMainActivity(target)
7002 : new Intent().setComponent(target);
7003 return parentIntent;
7004 } catch (NameNotFoundException e) {
7005 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
7006 "' in manifest");
7007 return null;
7008 }
Adam Powelldd8fab22012-03-22 17:47:27 -07007009 }
7010
George Mount31a21722014-03-24 17:44:36 -07007011 /**
George Mount62ab9b72014-05-02 13:51:17 -07007012 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007013 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007014 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07007015 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07007016 *
George Mount65580562014-08-29 08:15:48 -07007017 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07007018 */
George Mount65580562014-08-29 08:15:48 -07007019 public void setEnterSharedElementCallback(SharedElementCallback callback) {
7020 if (callback == null) {
7021 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07007022 }
George Mount65580562014-08-29 08:15:48 -07007023 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07007024 }
7025
7026 /**
7027 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07007028 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07007029 * will be called to handle shared elements on the <i>launching</i> Activity. Most
7030 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07007031 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07007032 *
George Mount65580562014-08-29 08:15:48 -07007033 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07007034 */
George Mount65580562014-08-29 08:15:48 -07007035 public void setExitSharedElementCallback(SharedElementCallback callback) {
7036 if (callback == null) {
7037 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07007038 }
George Mount65580562014-08-29 08:15:48 -07007039 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07007040 }
7041
George Mount8c2614c2014-06-10 11:12:01 -07007042 /**
7043 * Postpone the entering activity transition when Activity was started with
7044 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7045 * android.util.Pair[])}.
7046 * <p>This method gives the Activity the ability to delay starting the entering and
7047 * shared element transitions until all data is loaded. Until then, the Activity won't
7048 * draw into its window, leaving the window transparent. This may also cause the
7049 * returning animation to be delayed until data is ready. This method should be
7050 * called in {@link #onCreate(android.os.Bundle)} or in
7051 * {@link #onActivityReenter(int, android.content.Intent)}.
7052 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
7053 * start the transitions. If the Activity did not use
7054 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
7055 * android.util.Pair[])}, then this method does nothing.</p>
7056 */
7057 public void postponeEnterTransition() {
7058 mActivityTransitionState.postponeEnterTransition();
7059 }
7060
7061 /**
7062 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
7063 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
7064 * to have your Activity start drawing.
7065 */
7066 public void startPostponedEnterTransition() {
7067 mActivityTransitionState.startPostponedEnterTransition();
7068 }
7069
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007070 /**
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007071 * Create {@link DragAndDropPermissions} object bound to this activity and controlling the
7072 * access permissions for content URIs associated with the {@link DragEvent}.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007073 * @param event Drag event
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007074 * @return The {@link DragAndDropPermissions} object used to control access to the content URIs.
7075 * Null if no content URIs are associated with the event or if permissions could not be granted.
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007076 */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -07007077 public DragAndDropPermissions requestDragAndDropPermissions(DragEvent event) {
7078 DragAndDropPermissions dragAndDropPermissions = DragAndDropPermissions.obtain(event);
7079 if (dragAndDropPermissions != null && dragAndDropPermissions.take(getActivityToken())) {
7080 return dragAndDropPermissions;
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08007081 }
7082 return null;
7083 }
7084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07007086
Mathew Inwood45d2c252018-09-14 12:35:36 +01007087 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 final void setParent(Activity parent) {
7089 mParent = parent;
7090 }
7091
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007092 @UnsupportedAppUsage
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007093 final void attach(Context context, ActivityThread aThread,
7094 Instrumentation instr, IBinder token, int ident,
7095 Application application, Intent intent, ActivityInfo info,
7096 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007097 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07007098 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Andrii Kulian51c1b672017-04-07 18:39:32 -07007099 Window window, ActivityConfigCallback activityConfigCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 attachBaseContext(context);
7101
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007102 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07007103
Andrii Kulian51c1b672017-04-07 18:39:32 -07007104 mWindow = new PhoneWindow(this, window, activityConfigCallback);
Skuhnece2faa52015-08-11 10:36:38 -07007105 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07007107 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08007108 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
7110 mWindow.setSoftInputMode(info.softInputMode);
7111 }
Adam Powell269248d2011-08-02 10:26:54 -07007112 if (info.uiOptions != 0) {
7113 mWindow.setUiOptions(info.uiOptions);
7114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08007116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 mMainThread = aThread;
7118 mInstrumentation = instr;
7119 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007120 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 mApplication = application;
7122 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08007123 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 mComponent = intent.getComponent();
7125 mActivityInfo = info;
7126 mTitle = title;
7127 mParent = parent;
7128 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07007129 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07007130 if (voiceInteractor != null) {
7131 if (lastNonConfigurationInstances != null) {
7132 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
7133 } else {
7134 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
7135 Looper.myLooper());
7136 }
7137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138
Jeff Brown98365d72012-08-19 20:30:52 -07007139 mWindow.setWindowManager(
7140 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
7141 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07007142 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 if (mParent != null) {
7144 mWindow.setContainer(mParent.getWindow());
7145 }
7146 mWindowManager = mWindow.getWindowManager();
7147 mCurrentConfig = config;
Romain Guy48327452017-01-23 17:03:35 -08007148
7149 mWindow.setColorMode(info.colorMode);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007150
7151 setAutofillCompatibilityEnabled(application.isAutofillCompatibilityEnabled());
Svetoslav Ganov24c90452017-12-27 15:17:14 -08007152 }
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 /**
koprivaa1a78482018-10-09 10:09:23 -07007851 * Specifies whether an {@link Activity} should be shown on top of the lock screen whenever
chaviw59b98852017-06-13 12:05:44 -07007852 * 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}